BUG: AppendFile saves references to objects outside the appended object under the wrong ID

Abierto
#1,960 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 6 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
52/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

Read src/simplnx/DataStructure/IO/HDF5/DataStructureWriter.cpp around AppendFile and the ID remapping in src/simplnx/Utilities/DataObjectUtilities.hpp; the issue also identifies test/H5Test.cpp and provides a reproducing test. Run the listed ctest command to confirm the failure. Done means appending either reports an error naming an outside reference or stores a valid reference, as the test permits; the appropriate behavior for callers that move arrays out of a geometry remains open.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug

Problem

DataStructureWriter::AppendFile (DREAM3D::AppendFile, Python append_to_dream3d_file) can save a geometry whose reference names the wrong object. When the appended object references an object outside itself, the saved reference keeps the old ID. After the appended object is renumbered, that ID can belong to another saved object.

Expected: the append returns an error that names the outside reference, or the saved reference names the referenced object. Actual: the append reports success, and the reloaded geometry's Vertex List ID names its Face list (Triangle Geometry/Faces).

Occurs on develop 32ceb10b9.

Steps to Reproduce

  1. Create a DataStructure with a top-level Triangle Geometry Triangle Geometry whose Vertices and Faces arrays are its children, and a top-level group Other.
  2. Move Triangle Geometry/Vertices into Other, as the Move Data filter does. The geometry still references the vertex array.
  3. Write an empty DataStructure to a new .dream3d file, then append Triangle Geometry to it with AppendFile.
  4. Read the file back. The geometry's Vertex List ID is 2, and object 2 in the file is Triangle Geometry/Faces.

From Python, the same sequence is a pipeline that ends with Move Data, followed by nx.append_to_dream3d_file(path, data_structure, nx.DataPath(["Triangle Geometry"])). The unit test below reproduces it directly; the Python route was not run.

Cause

AppendFile copies the DataStructure, removes every top-level object except the appended one, and renumbers the copy from the file's NextObjectId (DataStructureWriter.cpp#L103-L113):

  DataStructure dataStructureShallowCopy = dataStructure;

  for(DataObject* topLevelObject : dataStructureShallowCopy.getTopLevelData())
  {
    if(topLevelObject->getName() != targetName)
    {
      dataStructureShallowCopy.removeData(topLevelObject->getId());
    }
  }

  dataStructureShallowCopy.resetIds(nextObjectId);

The vertex array is a child of Other, so the pruning removes it. resetIds builds an old-to-new map only from the objects that remain, and each object then remaps its references through that map. A reference with no entry in the map keeps its old value (DataObjectUtilities.hpp#L47-L56):

  if(originalId == updatedId.first && !visited[visitedIndex])
  {
    visited[visitedIndex] = true;
    return updatedId.second;
  }
  return originalId;

In the test, the geometry is ID 1, the vertices 2, the faces 3 and the group 4. The file's NextObjectId is 1, so the geometry becomes 1 and the faces become 2. The vertex reference stays 2, which now names the faces.

Move Data (MoveDataFilter.cpp#L49) accepts any object, including a geometry's own vertex, edge or face array, so a pipeline can create such an outside reference.

Verification Unit Test

File: test/H5Test.cpp (append at the end; it also needs #include "simplnx/Filter/Actions/MoveDataAction.hpp").

TEST_CASE("DataStructureWriter: AppendFile does not store a reference to an object outside the appended object", "[simplnx][HDF5][AppendFile]")
{
  const fs::path filePath = GetDataDir() / "AppendFileOutsideReference.dream3d";
  // An empty file: renumbering the appended object starts at ID 1.
  DataStructure baseDataStructure;
  const auto baseWrite = HDF5::DataStructureWriter::WriteFile(baseDataStructure, filePath);
  SIMPLNX_RESULT_REQUIRE_VALID(baseWrite);

  // Geometry ID 1, vertices ID 2, faces ID 3, group ID 4.
  DataStructure dataStructure;
  dataStructure.setNextId(1);
  auto* geometryPtr = TriangleGeom::Create(dataStructure, "Triangle Geometry");
  REQUIRE(geometryPtr != nullptr);
  const DataPath verticesPath({"Triangle Geometry", "Vertices"});
  const DataPath facesPath({"Triangle Geometry", "Faces"});
  const auto verticesCreated = ArrayCreationUtilities::CreateArray<float32>(dataStructure, {3}, {3}, verticesPath, IDataAction::Mode::Execute);
  SIMPLNX_RESULT_REQUIRE_VALID(verticesCreated);
  const auto facesCreated = ArrayCreationUtilities::CreateArray<IGeometry::MeshIndexType>(dataStructure, {1}, {3}, facesPath, IDataAction::Mode::Execute);
  SIMPLNX_RESULT_REQUIRE_VALID(facesCreated);
  auto& vertices = dataStructure.getDataRefAs<Float32Array>(verticesPath);
  auto& faces = dataStructure.getDataRefAs<IGeometry::SharedFaceList>(facesPath);
  const std::vector<float32> vertexValues = {0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F};
  for(usize index = 0; index < vertexValues.size(); ++index)
  {
    vertices[index] = vertexValues[index];
  }
  faces[0] = 0;
  faces[1] = 1;
  faces[2] = 2;
  geometryPtr->setVertices(vertices);
  geometryPtr->setFaceList(faces);
  REQUIRE(DataGroup::Create(dataStructure, "Other") != nullptr);

  // What the Move Data filter does: the vertex array leaves the geometry, and the geometry still references it.
  const auto moved = MoveDataAction(verticesPath, DataPath({"Other"})).apply(dataStructure, IDataAction::Mode::Execute);
  SIMPLNX_RESULT_REQUIRE_VALID(moved);
  REQUIRE(geometryPtr->getVertexListId() == vertices.getId());

  const auto appendResult = HDF5::DataStructureWriter::AppendFile(filePath, dataStructure, DataPath({"Triangle Geometry"}));
  if(appendResult.invalid())
  {
    // Rejecting the outside reference is correct behavior.
    return;
  }
  auto readResult = HDF5::DataStructureReader::ReadFile(filePath);
  SIMPLNX_RESULT_REQUIRE_VALID(readResult);
  const DataStructure& reloaded = readResult.value();
  const auto& reloadedGeometry = reloaded.getDataRefAs<TriangleGeom>(DataPath({"Triangle Geometry"}));
  const auto vertexListId = reloadedGeometry.getVertexListId();
  if(!vertexListId.has_value())
  {
    return;
  }
  // A stored vertex reference must name the vertex array, not another object of the file.
  const DataObject* namedPtr = reloaded.getData(vertexListId.value());
  CAPTURE(vertexListId.value());
  CAPTURE(namedPtr == nullptr ? std::string("no object") : namedPtr->getDataPaths().front().toString());
  const auto* reloadedVerticesPtr = dynamic_cast<const Float32Array*>(namedPtr);
  REQUIRE(reloadedVerticesPtr != nullptr);
  for(usize index = 0; index < vertexValues.size(); ++index)
  {
    CHECK((*reloadedVerticesPtr)[index] == vertexValues[index]);
  }
}

Run: ctest --test-dir <build-dir> -R "AppendFile does not store a reference" -j 1 --output-on-failure

Output on develop 32ceb10b9 (Release, macOS):

test/H5Test.cpp:1564: FAILED:
  REQUIRE( reloadedVerticesPtr != nullptr )
with expansion:
  vertexListId.value() := 2
  namedPtr == nullptr ? std::string("no object") : namedPtr->getDataPaths().
  front().toString() := "Triangle Geometry/Faces"

Proposed Fix

In DataStructureWriter::AppendFile, before the pruning loop, collect every ID that an object of the appended subtree references (geometry vertex, edge, face, cell and attribute-matrix IDs, and any other checkUpdatedIds reference). For each referenced object outside the subtree, either:

  • return a new error that names the appended object, the reference and the outside object's path, or
  • clear that reference in the copy, with a warning, so the file stores no ID for it.

The test above accepts either result. Which one is right for callers that move arrays out of a geometry is still open.

Notes

  • PR #1919 handles one case of this in AppendFile: geometry connectivity lists (vertex incidence and element neighbors). The writer no longer saves them, and AppendFile removes them and their references from its copy before pruning. Other outside references are unchanged by that PR.
  • resetIds is called only by AppendFile. A full DREAM3D::WriteFile writes every object with its own ID, so it is not affected.
Lenguaje dominante
C++
Estrellas
18
Forks
15
Merge medio
4 d 23 h
PR fusionados (30 d)
42

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de BlueQuartzSoftware/simplnx

Todos los issues de BlueQuartzSoftware/simplnx

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.