Skip to content

Commit 7af1e17

Browse files
thewtexhjmjohnson
authored andcommitted
BUG: Add VTKPolyDataMeshIO FIELD data support
Add reading of VTK legacy FIELD data entries in both POINT_DATA and CELL_DATA sections. When FIELD data is present and the number of tuples matches the point/cell count, the data is read as a VariableLengthVector pixel type. Only the first field data array per section is used; additional arrays are silently skipped. Add a test with a gourd.vtk file that exercises the FIELD data path. Co-authored-by: Matt McCormick <matt@mmmccormick.com>
1 parent 22f6d27 commit 7af1e17

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

Modules/IO/MeshVTK/src/itkVTKPolyDataMeshIO.cxx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,50 @@ VTKPolyDataMeshIO::ReadMeshInformation()
655655
this->m_NumberOfPointPixelComponents = this->m_PointDimension * (this->m_PointDimension + 1) / 2;
656656
this->m_UpdatePointData = true;
657657
}
658+
if (line.find("FIELD") != std::string::npos)
659+
{
660+
StringStreamType sss;
661+
sss << line;
662+
663+
sss >> item; // should be "FIELD"
664+
if (item == "FIELD")
665+
{
666+
sss >> item; // field data name (e.g. "FieldData")
667+
sss >> item; // number of field data arrays
668+
const int numArrays = std::stoi(item);
669+
if (numArrays >= 1)
670+
{
671+
// Read the first field data array header.
672+
// Additional arrays are silently skipped.
673+
std::getline(inputFile, line, '\n');
674+
675+
StringStreamType fieldStream;
676+
fieldStream << line;
677+
fieldStream >> item; // array name
678+
679+
fieldStream >> item; // numComponents
680+
const unsigned int numComponents = std::stoi(item);
681+
682+
fieldStream >> item; // numTuples
683+
const unsigned int numTuples = std::stoi(item);
684+
685+
fieldStream >> item; // data type
686+
const IOComponentEnum componentType = this->GetComponentTypeFromString(item);
687+
688+
if (this->m_NumberOfPoints == numTuples)
689+
{
690+
this->m_NumberOfPointPixelComponents = numComponents;
691+
this->m_PointPixelType = IOPixelEnum::VARIABLELENGTHVECTOR;
692+
this->m_PointPixelComponentType = componentType;
693+
if (this->m_PointPixelComponentType == IOComponentEnum::UNKNOWNCOMPONENTTYPE)
694+
{
695+
itkExceptionMacro("Unknown point pixel component type");
696+
}
697+
this->m_UpdatePointData = true;
698+
}
699+
}
700+
}
701+
}
658702
}
659703
else if (line.find("CELL_DATA") != std::string::npos)
660704
{
@@ -788,6 +832,50 @@ VTKPolyDataMeshIO::ReadMeshInformation()
788832
this->m_NumberOfCellPixelComponents = this->m_PointDimension * (this->m_PointDimension + 1) / 2;
789833
this->m_UpdateCellData = true;
790834
}
835+
if (line.find("FIELD") != std::string::npos)
836+
{
837+
StringStreamType sss;
838+
sss << line;
839+
840+
sss >> item; // should be "FIELD"
841+
if (item == "FIELD")
842+
{
843+
sss >> item; // field data name (e.g. "FieldData")
844+
sss >> item; // number of field data arrays
845+
const int numArrays = std::stoi(item);
846+
if (numArrays >= 1)
847+
{
848+
// Read the first field data array header.
849+
// Additional arrays are silently skipped.
850+
std::getline(inputFile, line, '\n');
851+
852+
StringStreamType fieldStream;
853+
fieldStream << line;
854+
fieldStream >> item; // array name
855+
856+
fieldStream >> item; // numComponents
857+
const unsigned int numComponents = std::stoi(item);
858+
859+
fieldStream >> item; // numTuples
860+
const unsigned int numTuples = std::stoi(item);
861+
862+
fieldStream >> item; // data type
863+
const IOComponentEnum componentType = this->GetComponentTypeFromString(item);
864+
865+
if (this->m_NumberOfCells == numTuples)
866+
{
867+
this->m_NumberOfCellPixelComponents = numComponents;
868+
this->m_CellPixelType = IOPixelEnum::VARIABLELENGTHVECTOR;
869+
this->m_CellPixelComponentType = componentType;
870+
if (this->m_CellPixelComponentType == IOComponentEnum::UNKNOWNCOMPONENTTYPE)
871+
{
872+
itkExceptionMacro("Unknown cell pixel component type");
873+
}
874+
this->m_UpdateCellData = true;
875+
}
876+
}
877+
}
878+
}
791879
}
792880
else if (line.find("OFFSETS") != std::string::npos)
793881
{

Modules/IO/MeshVTK/test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ itk_add_test(
176176
2
177177
1
178178
1)
179+
itk_add_test(
180+
NAME
181+
itkMeshFileReadWriteTestField
182+
COMMAND
183+
ITKIOMeshVTKTestDriver
184+
itkMeshFileReadWriteTest
185+
DATA{Input/gourd.vtk}
186+
${ITK_TEST_OUTPUT_DIR}/gourd.vtk)
179187

180188
set(ITKIOMeshVTKGTests itkVTKPolyDataMeshIOGTest.cxx)
181189

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bafkreidv66i2s5mleluikmvcpq32yziqt7qo3tvc5imncrx4auoimwtdua

0 commit comments

Comments
 (0)