From cedcf4f412f2a3e6635d546c2b9c678022497535 Mon Sep 17 00:00:00 2001 From: rblanc Date: Wed, 15 May 2013 17:01:10 +0200 Subject: [PATCH] added an option to transform vtk meshes with tool clitkTransformLandmarks --- common/clitkTransformUtilities.h | 26 ++++++++++++++++++++++- tools/clitkTransformLandmarks.cxx | 34 +++++++++++++++++++++++++++++++ tools/clitkTransformLandmarks.ggo | 2 +- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/common/clitkTransformUtilities.h b/common/clitkTransformUtilities.h index 3bdf220..cbc30b9 100644 --- a/common/clitkTransformUtilities.h +++ b/common/clitkTransformUtilities.h @@ -23,7 +23,8 @@ #include "itkPoint.h" #include "clitkImageCommon.h" #include "clitkCommon.h" - +#include +#include namespace clitk { @@ -278,6 +279,29 @@ namespace clitk return matrix; } + inline vtkMatrix4x4* ReadVTKMatrix3D(std::string fileName) { + // read input matrix + std::ifstream is; + openFileForReading(is, fileName); + std::vector nb; + double x; + skipComment(is); + is >> x; + while (!is.eof()) { + nb.push_back(x); + skipComment(is); + is >> x; + } + + vtkSmartPointer matrix = vtkSmartPointer::New(); + unsigned int index=0; + for (unsigned int i=0;i<4;i++) + for (unsigned int j=0;j<4;j++) + matrix->SetElement(i,j, nb[index++]); + + return matrix; + } + inline itk::Matrix ReadMatrix2D(std::string fileName) { // read input matrix diff --git a/tools/clitkTransformLandmarks.cxx b/tools/clitkTransformLandmarks.cxx index 86745a4..df5dbe8 100644 --- a/tools/clitkTransformLandmarks.cxx +++ b/tools/clitkTransformLandmarks.cxx @@ -23,6 +23,14 @@ #include #include +#include +#include +#include +#include "vtkPolyDataReader.h" +#include "vtkPolyDataWriter.h" +#include +#include + typedef itk::Matrix MatrixType; typedef itk::Point PointType; typedef std::vector PointArrayType; @@ -52,6 +60,32 @@ int main(int argc, char** argv) if (strcmp(args_info.type_arg, "txt") == 0) { read_points_txt(args_info.input_arg, inputPoints, data); } + else if (strcmp(args_info.type_arg, "vtk") == 0) { + vtkSmartPointer reader = vtkSmartPointer::New(); + reader->SetFileName(args_info.input_arg); + reader->Update(); + vtkSmartPointer writer = vtkSmartPointer::New(); + writer->SetFileName( args_info.output_arg ); + + if (args_info.matrix_given) { + vtkSmartPointer transformFilter = vtkSmartPointer::New(); + vtkSmartPointer transform = vtkSmartPointer::New(); + vtkMatrix4x4* matrix = clitk::ReadVTKMatrix3D(args_info.matrix_arg); + vtkSmartPointer matrixT = vtkSmartPointer::New(); + vtkMatrix4x4::Invert(matrix, matrixT); //not sure why, but this seems necessary for using the same .mat as when loading file with vv (probably due to the inversion trick performed in the vv reader...) + transform->SetMatrix(matrixT); + transformFilter->SetInputConnection(reader->GetOutputPort()); + transformFilter->SetTransform(transform); + writer->SetInputConnection(transformFilter->GetOutputPort()); + + } + else { //just write the output + writer->SetInputConnection( reader->GetOutputPort() ); + } + + writer->Write(); + return 0; + } else { read_points_pts(args_info.input_arg, inputPoints); } diff --git a/tools/clitkTransformLandmarks.ggo b/tools/clitkTransformLandmarks.ggo index c731c23..4772224 100644 --- a/tools/clitkTransformLandmarks.ggo +++ b/tools/clitkTransformLandmarks.ggo @@ -10,6 +10,6 @@ option "input" i "Input landmarks filename" string yes option "matrix" m "Input 4x4 matrix filename ('.mat' file)" string no option "spacing" s "If given, applies the given spacing (x,y,z) to the input points." double no multiple default="1" option "output" o "Output landmarks filename" string yes -option "type" t "Landmarks type ('pts' for Jef; 'txt' for VV)" string no default="txt" +option "type" t "Landmarks type ('pts' for Jef; 'txt' for VV ; 'vtk' for vtk meshes)" string no default="txt" -- 2.47.1