Skip to content

Commit

Permalink
ENH: Transform binary VTK input file to binary output file
Browse files Browse the repository at this point in the history
If the VTK input file is a binary file, the output file should also be a binary file.

Otherwise, the output file is still ASCII, as before.

Inspired by pull request #943 "ENH: Support for binary point file (.bin) reading and writing", ChristophKirst, Aug 2, 2023.
  • Loading branch information
N-Dekker committed Oct 16, 2024
1 parent e285c98 commit 90c64c5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Core/ComponentBaseClasses/elxTransformBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,16 @@ TransformBase<TElastix>::TransformPointsSomePointsVTK(const std::string & filena

try
{
itk::WriteMesh(outputMesh, outputPointsFileName);
const auto writer = itk::MeshFileWriter<MeshType>::New();
writer->SetInput(outputMesh);
writer->SetFileName(outputPointsFileName);

if (itk::Deref(meshReader->GetModifiableMeshIO()).GetFileType() == itk::IOFileEnum::Binary)
{
writer->SetFileTypeAsBINARY();
}

writer->Update();
}
catch (const itk::ExceptionObject & err)
{
Expand Down
44 changes: 44 additions & 0 deletions Testing/PythonTests/transformix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,50 @@ def test_zero_translation_of_vtk_2d_points(self) -> None:
output_mesh = reader.GetOutput()
self.assert_equal_mesh(output_mesh, input_mesh)

def test_zero_translation_of_vtk_2d_points_binary(self) -> None:
"""Tests zero-translation of VTK points in 2D"""

source_directory_path = pathlib.Path(__file__).resolve().parent
output_directory_path = self.create_test_function_output_directory()

parameter_directory_path = source_directory_path / "TransformParameters"

input_mesh = itk.Mesh[itk.D, 2].New()
for i in range(4):
input_mesh.SetPoint(
i, (self.random_finite_float32(), self.random_finite_float32())
)

writer = itk.MeshFileWriter[itk.Mesh[itk.D, 2]].New()
writer.SetInput(input_mesh)
writer.SetFileTypeAsBINARY()
writer.SetFileName(str(output_directory_path / "inputpoints.vtk"))
writer.Write()

subprocess.run(
[
str(self.transformix_exe_file_path),
"-def",
str(output_directory_path / "inputpoints.vtk"),
"-tp",
str(parameter_directory_path / "Translation(0,0).txt"),
"-out",
str(output_directory_path),
],
capture_output=True,
check=True,
)

# Note that itk.meshread does not work, as the following produces a 3D mesh, instead of a 2D
# mesh.
#
# output_mesh = itk.meshread(str(output_directory_path / "outputpoints.vtk"))
reader = itk.MeshFileReader[itk.Mesh[itk.D, 2]].New()
reader.SetFileName(str(output_directory_path / "outputpoints.vtk"))
reader.Update()
output_mesh = reader.GetOutput()
self.assert_equal_mesh(output_mesh, input_mesh)

def test_translation_deformation_field(self) -> None:
"""Tests zero-translation of VTK points in 2D"""

Expand Down

0 comments on commit 90c64c5

Please sign in to comment.