Skip to content

Commit

Permalink
ENH: Support reading a mesh of non-3D points from a .vtk PolyData file
Browse files Browse the repository at this point in the history
`itk::MeshFileWriter` does allow writing non-3D points to a .vtk file, so it
appears both reasonable and useful for `itk::MeshFileReader` to allow reading
those points back.

Implemented by setting the PointDimension within MeshFileReader::GenerateData(),
and tested by GTest VTKPolyDataMeshIO.SupportsPointDimensionsGreaterThanOne.

Related to issue slicersalt/ITKShape#8 "Add 2D mesh support"
by Matt McCormick.
  • Loading branch information
N-Dekker authored and dzenanz committed Aug 17, 2022
1 parent b963ba8 commit 236f378
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Modules/IO/MeshBase/include/itkMeshFileReader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ template <typename TOutputMesh, typename ConvertPointPixelTraits, typename Conve
void
MeshFileReader<TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits>::GenerateData()
{
m_MeshIO->SetPointDimension(OutputPointDimension);

typename TOutputMesh::Pointer output = this->GetOutput();

// Test if the file exists and if it can be opened.
Expand Down
1 change: 0 additions & 1 deletion Modules/IO/MeshVTK/src/itkVTKPolyDataMeshIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ VTKPolyDataMeshIO ::ReadMeshInformation()

// Get number of Points
ss >> this->m_NumberOfPoints;
this->m_PointDimension = 3; // vtk only support 3 dimensional points

// Get point component type
StringType pointType;
Expand Down
24 changes: 24 additions & 0 deletions Modules/IO/MeshVTK/test/itkVTKPolyDataMeshIOGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@

namespace
{
template <unsigned int VDimension>
auto
MakePointOfIncreasingCoordValues()
{
itk::Point<float, VDimension> point;
std::iota(point.begin(), point.end(), 1.0f);
return point;
}


// Expects that VTKPolyDataMeshIO supports writing points, and then reading them back losslessly. (When a NaN coordinate
// value is written, a NaN coordinate value is expected to be read back.)
Expand Down Expand Up @@ -162,3 +171,18 @@ TEST(VTKPolyDataMeshIO, SupportWriteAndReadOfNaNCoordValues)
writeAsBinary);
}
}


// Tests that a mesh of any `PointDimension` > 1 is properly written and read back, not just 3D (the default).
TEST(VTKPolyDataMeshIO, SupportsPointDimensionsGreaterThanOne)
{
for (const bool writeAsBinary : { false, true })
{
Expect_lossless_writing_and_reading_of_points<itk::Mesh<int, 2>>(
"VTKPolyDataMeshIOGTest_Supports2D.vtk", { MakePointOfIncreasingCoordValues<2>() }, writeAsBinary);
Expect_lossless_writing_and_reading_of_points<itk::Mesh<int, 3>>(
"VTKPolyDataMeshIOGTest_Supports3D.vtk", { MakePointOfIncreasingCoordValues<3>() }, writeAsBinary);
Expect_lossless_writing_and_reading_of_points<itk::Mesh<int, 4>>(
"VTKPolyDataMeshIOGTest_Supports4D.vtk", { MakePointOfIncreasingCoordValues<4>() }, writeAsBinary);
}
}

0 comments on commit 236f378

Please sign in to comment.