From 017e4bb738e60225ef91e0856ca634141e634fb3 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Wed, 24 Jul 2013 13:26:03 +0200 Subject: [PATCH] Moved Elastix stuff to a separate file --- common/clitkElastix.h | 156 ++++++++++++++++++++ tools/clitkAffineTransformGenericFilter.h | 9 -- tools/clitkAffineTransformGenericFilter.txx | 137 +---------------- tools/clitkElastixTransformToMatrix.cxx | 5 +- 4 files changed, 160 insertions(+), 147 deletions(-) create mode 100644 common/clitkElastix.h diff --git a/common/clitkElastix.h b/common/clitkElastix.h new file mode 100644 index 0000000..ae5dc54 --- /dev/null +++ b/common/clitkElastix.h @@ -0,0 +1,156 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +#ifndef clitkElastix_h +#define clitkElastix_h + +//-------------------------------------------------------------------- +namespace clitk { + +//------------------------------------------------------------------- +bool +GetElastixValueFromTag(std::ifstream & is, + std::string tag, + std::string & value) +{ + std::string line; + is.seekg (0, is.beg); + while(std::getline(is, line)) { + unsigned pos = line.find(tag); + if (pos & values) +{ + std::stringstream strstr(s); + std::istream_iterator it(strstr); + std::istream_iterator end; + std::vector results(it, end); + values.clear(); + values.resize(results.size()); + for(uint i=0; i +typename itk::Matrix +createMatrixFromElastixFile(std::vector & filename, bool verbose=true) { + if (Dimension != 3) { + FATAL("Only 3D yet" << std::endl); + } + typename itk::Matrix matrix; + + itk::CenteredEuler3DTransform::Pointer mat = itk::CenteredEuler3DTransform::New(); + itk::CenteredEuler3DTransform::Pointer previous; + for(uint j=0; j cor; + GetValuesFromValue(s, cor); + + // Get Transformparameters + GetElastixValueFromTag(is, "TransformParameters ", s); // space is needed + if (!b) { + FATAL("Error must read 'TransformParameters' in " << filename[j] << std::endl); + } + std::vector results; + GetValuesFromValue(s, results); + + // construct a stream from the string + itk::CenteredEuler3DTransform::ParametersType p; + p.SetSize(9); + for(uint i=0; i<3; i++) + p[i] = atof(results[i].c_str()); // Rotation + for(uint i=0; i<3; i++) + p[i+3] = atof(cor[i].c_str()); // Centre of rotation + for(uint i=0; i<3; i++) + p[i+6] = atof(results[i+3].c_str()); // Translation + mat->SetParameters(p); + + if (verbose) { + std::cout << "Rotation (deg) : " << rad2deg(p[0]) << " " << rad2deg(p[1]) << " " << rad2deg(p[2]) << std::endl; + std::cout << "Center of rot (phy) : " << p[3] << " " << p[4] << " " << p[5] << std::endl; + std::cout << "Translation (phy) : " << p[6] << " " << p[7] << " " << p[8] << std::endl; + } + + // Compose with previous if needed + if (j!=0) { + mat->Compose(previous); + if (verbose) { + std::cout << "Composed rotation (deg) : " << rad2deg(mat->GetAngleX()) << " " << rad2deg(mat->GetAngleY()) << " " << rad2deg(mat->GetAngleZ()) << std::endl; + std::cout << "Composed center of rot (phy) : " << mat->GetCenter() << std::endl; + std::cout << "Compsoed translation (phy) : " << mat->GetTranslation() << std::endl; + } + } + // previous = mat->Clone(); // ITK4 + previous = itk::CenteredEuler3DTransform::New(); + previous->SetParameters(mat->GetParameters()); + } + + mat = previous; + for(uint i=0; i<3; i++) + for(uint j=0; j<3; j++) + matrix[i][j] = mat->GetMatrix()[i][j]; + // Offset is -Rc + t + c + matrix[0][3] = mat->GetOffset()[0]; + matrix[1][3] = mat->GetOffset()[1]; + matrix[2][3] = mat->GetOffset()[2]; + matrix[3][3] = 1; + + return matrix; +} +} +//------------------------------------------------------------------- + +#endif diff --git a/tools/clitkAffineTransformGenericFilter.h b/tools/clitkAffineTransformGenericFilter.h index 3950632..613e1cc 100644 --- a/tools/clitkAffineTransformGenericFilter.h +++ b/tools/clitkAffineTransformGenericFilter.h @@ -87,11 +87,6 @@ namespace clitk //---------------------------------------- void Update(); - template - static - typename itk::Matrix - createMatrixFromElastixFile(std::vector & filename, bool verbose=true); - protected: //---------------------------------------- @@ -108,10 +103,6 @@ namespace clitk template void UpdateWithDimAndPixelType(); template void UpdateWithDimAndVectorType(); - static bool GetElastixValueFromTag(std::ifstream & is, std::string tag, std::string & value); - static void GetValuesFromValue(const std::string & s, - std::vector & values); - //---------------------------------------- // Data members //---------------------------------------- diff --git a/tools/clitkAffineTransformGenericFilter.txx b/tools/clitkAffineTransformGenericFilter.txx index 03cf862..150c8c1 100644 --- a/tools/clitkAffineTransformGenericFilter.txx +++ b/tools/clitkAffineTransformGenericFilter.txx @@ -22,6 +22,7 @@ #include #include #include +#include "clitkElastix.h" namespace clitk { @@ -183,7 +184,7 @@ namespace clitk if (m_ArgsInfo.elastix_given) { std::vector s; for(uint i=0; i(s, m_Verbose); + matrix = createMatrixFromElastixFile(s, m_Verbose); } else matrix.SetIdentity(); @@ -491,140 +492,6 @@ namespace clitk } //------------------------------------------------------------------- - - - //------------------------------------------------------------------- - template - template - typename itk::Matrix - AffineTransformGenericFilter::createMatrixFromElastixFile(std::vector & filename, bool verbose) - { - if (Dimension != 3) { - FATAL("Only 3D yet" << std::endl); - } - typename itk::Matrix matrix; - - itk::CenteredEuler3DTransform::Pointer mat = itk::CenteredEuler3DTransform::New(); - itk::CenteredEuler3DTransform::Pointer previous; - for(uint j=0; j cor; - GetValuesFromValue(s, cor); - - // Get Transformparameters - GetElastixValueFromTag(is, "TransformParameters ", s); // space is needed - if (!b) { - FATAL("Error must read 'TransformParameters' in " << filename[j] << std::endl); - } - std::vector results; - GetValuesFromValue(s, results); - - // construct a stream from the string - itk::CenteredEuler3DTransform::ParametersType p; - p.SetSize(9); - for(uint i=0; i<3; i++) - p[i] = atof(results[i].c_str()); // Rotation - for(uint i=0; i<3; i++) - p[i+3] = atof(cor[i].c_str()); // Centre of rotation - for(uint i=0; i<3; i++) - p[i+6] = atof(results[i+3].c_str()); // Translation - mat->SetParameters(p); - - if (verbose) { - std::cout << "Rotation (deg) : " << rad2deg(p[0]) << " " << rad2deg(p[1]) << " " << rad2deg(p[2]) << std::endl; - std::cout << "Center of rot (phy) : " << p[3] << " " << p[4] << " " << p[5] << std::endl; - std::cout << "Translation (phy) : " << p[6] << " " << p[7] << " " << p[8] << std::endl; - } - - // Compose with previous if needed - if (j!=0) { - mat->Compose(previous); - if (verbose) { - std::cout << "Composed rotation (deg) : " << rad2deg(mat->GetAngleX()) << " " << rad2deg(mat->GetAngleY()) << " " << rad2deg(mat->GetAngleZ()) << std::endl; - std::cout << "Composed center of rot (phy) : " << mat->GetCenter() << std::endl; - std::cout << "Compsoed translation (phy) : " << mat->GetTranslation() << std::endl; - } - } - // previous = mat->Clone(); // ITK4 - previous = itk::CenteredEuler3DTransform::New(); - previous->SetParameters(mat->GetParameters()); - } - - mat = previous; - for(uint i=0; i<3; i++) - for(uint j=0; j<3; j++) - matrix[i][j] = mat->GetMatrix()[i][j]; - // Offset is -Rc + t + c - matrix[0][3] = mat->GetOffset()[0]; - matrix[1][3] = mat->GetOffset()[1]; - matrix[2][3] = mat->GetOffset()[2]; - matrix[3][3] = 1; - - return matrix; - } - - //------------------------------------------------------------------- - template - bool - AffineTransformGenericFilter::GetElastixValueFromTag(std::ifstream & is, - std::string tag, - std::string & value) - { - std::string line; - is.seekg (0, is.beg); - while(std::getline(is, line)) { - unsigned pos = line.find(tag); - if (pos - void - AffineTransformGenericFilter::GetValuesFromValue(const std::string & s, - std::vector & values) - { - std::stringstream strstr(s); - std::istream_iterator it(strstr); - std::istream_iterator end; - std::vector results(it, end); - values.clear(); - values.resize(results.size()); - for(uint i=0; i FilterType; std::vector l; l.push_back(args_info.input_arg); - itk::Matrix m = - FilterType::createMatrixFromElastixFile<3, int>(l, args_info.verbose_flag); + itk::Matrix m = clitk::createMatrixFromElastixFile<3>(l, args_info.verbose_flag); // Print matrix std::ofstream os; -- 2.47.1