From: Romulo Pinho Date: Thu, 19 Jan 2012 16:51:00 +0000 (+0100) Subject: refactoring... X-Git-Tag: v1.3.0~117 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=5be83a695bafa815cca85e180c0070e30020fa4c;p=clitk.git refactoring... - Put coeff to dvf function in separate file, as a global function - Changed affected files/classes --- diff --git a/common/clitkCoeffsToDVF.h b/common/clitkCoeffsToDVF.h new file mode 100644 index 0000000..0fe30bb --- /dev/null +++ b/common/clitkCoeffsToDVF.h @@ -0,0 +1,86 @@ +/*========================================================================= + 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 clitkCoeffsToDVF_h +#define clitkCoeffsToDVF_h + +#include "clitkBSplineDeformableTransform.h" +#if ITK_VERSION_MAJOR >= 4 +#include "itkTransformToDisplacementFieldSource.h" +#else +#include "itkTransformToDeformationFieldSource.h" +#endif + +//------------------------------------------------------------------- +// Convert Coefficient image to DVF +//------------------------------------------------------------------- +template +typename DisplacementFieldType::Pointer +CoeffsToDVF(std::string fileName, std::string likeFileName, bool verbose = false) +{ + typedef clitk::BSplineDeformableTransform TransformType; + typedef typename TransformType::CoefficientImageType CoefficientImageType; + + typedef itk::ImageFileReader CoeffReaderType; + typename CoeffReaderType::Pointer reader = CoeffReaderType::New(); + reader->SetFileName(fileName); + reader->Update(); + + typename TransformType::Pointer transform = TransformType::New(); + transform->SetCoefficientImage(reader->GetOutput()); + +#if ITK_VERSION_MAJOR >= 4 + typedef itk::TransformToDisplacementFieldSource ConvertorType; +#else + typedef itk::TransformToDeformationFieldSource ConvertorType; +#endif + + typedef itk::ImageIOBase ImageIOType; + typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode); + imageIO->SetFileName(likeFileName); + imageIO->ReadImageInformation(); + + typename ConvertorType::Pointer convertor= ConvertorType::New(); + typename ConvertorType::SizeType output_size; + typename ConvertorType::SpacingType output_spacing; + typename ConvertorType::OriginType output_origin; + typename ConvertorType::DirectionType output_direction; + for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) { + output_size[i] = imageIO->GetDimensions(i); + output_spacing[i] = imageIO->GetSpacing(i); + output_origin[i] = imageIO->GetOrigin(i); + for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++) + output_direction[i][j] = imageIO->GetDirection(i)[j]; + } + + if (verbose) { + std::cout << "Interpolating coefficients with grid:" << std::endl; + std::cout << output_size << output_spacing << std::endl; + } + + convertor->SetNumberOfThreads(1); + convertor->SetTransform(transform); + convertor->SetOutputOrigin(output_origin); + convertor->SetOutputSpacing(output_spacing); + convertor->SetOutputSize(output_size); + convertor->SetOutputDirection(output_direction); + convertor->Update(); + + return convertor->GetOutput(); +} + +#endif diff --git a/tools/clitkComposeVFGenericFilter.h b/tools/clitkComposeVFGenericFilter.h index b86f22b..4d9d9f5 100644 --- a/tools/clitkComposeVFGenericFilter.h +++ b/tools/clitkComposeVFGenericFilter.h @@ -67,7 +67,6 @@ namespace clitk //Templated members template void UpdateWithDim(std::string PixelType); template void UpdateWithDimAndPixelType(); - template typename DisplacementFieldType::Pointer CoeffsToDVF(std::string fileName, std::string likeFileName); std::string m_InputName1; std::string m_InputName2; diff --git a/tools/clitkComposeVFGenericFilter.txx b/tools/clitkComposeVFGenericFilter.txx index 07df988..8b0ecdf 100644 --- a/tools/clitkComposeVFGenericFilter.txx +++ b/tools/clitkComposeVFGenericFilter.txx @@ -19,13 +19,7 @@ #define __clitkComposeVFGenericFilter_txx #include "clitkComposeVFGenericFilter.h" -#include "clitkBSplineDeformableTransform.h" -#include "clitkBSplineDeformableTransformInitializer.h" -#if ITK_VERSION_MAJOR >= 4 -#include "itkTransformToDisplacementFieldSource.h" -#else -#include "itkTransformToDeformationFieldSource.h" -#endif +#include "clitkCoeffsToDVF.h" namespace clitk { @@ -43,59 +37,6 @@ namespace clitk } } - template - typename DisplacementFieldType::Pointer ComposeVFGenericFilter::CoeffsToDVF(std::string fileName, std::string likeFileName) - { - typedef clitk::BSplineDeformableTransform TransformType; - typedef typename TransformType::CoefficientImageType CoefficientImageType; - - typedef itk::ImageFileReader CoeffReaderType; - typename CoeffReaderType::Pointer reader = CoeffReaderType::New(); - reader->SetFileName(fileName); - reader->Update(); - - typename TransformType::Pointer transform = TransformType::New(); - transform->SetCoefficientImage(reader->GetOutput()); - -#if ITK_VERSION_MAJOR >= 4 - typedef itk::TransformToDisplacementFieldSource ConvertorType; -#else - typedef itk::TransformToDeformationFieldSource ConvertorType; -#endif - - typedef itk::ImageIOBase ImageIOType; - typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode); - imageIO->SetFileName(likeFileName); - imageIO->ReadImageInformation(); - - typename ConvertorType::Pointer convertor= ConvertorType::New(); - typename ConvertorType::SizeType output_size; - typename ConvertorType::SpacingType output_spacing; - typename ConvertorType::OriginType output_origin; - typename ConvertorType::DirectionType output_direction; - for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) { - output_size[i] = imageIO->GetDimensions(i); - output_spacing[i] = imageIO->GetSpacing(i); - output_origin[i] = imageIO->GetOrigin(i); - for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++) - output_direction[i][j] = imageIO->GetDirection(i)[j]; - } - - if (m_Verbose) { - std::cout << "Interpolating coefficients with grid:" << std::endl; - std::cout << output_size << output_spacing << std::endl; - } - - convertor->SetNumberOfThreads(1); - convertor->SetTransform(transform); - convertor->SetOutputOrigin(output_origin); - convertor->SetOutputSpacing(output_spacing); - convertor->SetOutputSize(output_size); - convertor->SetOutputDirection(output_direction); - convertor->Update(); - - return convertor->GetOutput(); - } template void ComposeVFGenericFilter::UpdateWithDimAndPixelType() @@ -106,8 +47,8 @@ namespace clitk //Define the image type if (m_Type == 1) { - input1 = this->CoeffsToDVF(m_InputName1, m_LikeImage); - input2 = this->CoeffsToDVF(m_InputName2, m_LikeImage); + input1 = CoeffsToDVF(m_InputName1, m_LikeImage, m_Verbose); + input2 = CoeffsToDVF(m_InputName2, m_LikeImage, m_Verbose); } else { //Read the input1 diff --git a/tools/clitkInvertVFGenericFilter.h b/tools/clitkInvertVFGenericFilter.h index cdaf86f..012524c 100644 --- a/tools/clitkInvertVFGenericFilter.h +++ b/tools/clitkInvertVFGenericFilter.h @@ -99,7 +99,6 @@ namespace clitk //---------------------------------------- template void UpdateWithDim(std::string PixelType); template void UpdateWithDimAndPixelType(); - template typename DisplacementFieldType::Pointer CoeffsToDVF(std::string fileName, std::string likeFileName); //---------------------------------------- diff --git a/tools/clitkInvertVFGenericFilter.txx b/tools/clitkInvertVFGenericFilter.txx index 5cd12c4..e4b590e 100644 --- a/tools/clitkInvertVFGenericFilter.txx +++ b/tools/clitkInvertVFGenericFilter.txx @@ -19,12 +19,7 @@ #define clitkInvertVFGenericFilter_txx #include "itkVectorResampleImageFilter.h" -#include "clitkBSplineDeformableTransform.h" -#if ITK_VERSION_MAJOR >= 4 -#include "itkTransformToDisplacementFieldSource.h" -#else -#include "itkTransformToDeformationFieldSource.h" -#endif +#include "clitkCoeffsToDVF.h" /* ================================================= * @file clitkInvertVFGenericFilter.txx @@ -106,65 +101,6 @@ InvertVFGenericFilter::UpdateWithDim(std::string PixelType) // } } -//------------------------------------------------------------------- -// Convert Coefficient image to DVF -//------------------------------------------------------------------- -template -template -typename DisplacementFieldType::Pointer -InvertVFGenericFilter::CoeffsToDVF(std::string fileName, std::string likeFileName) -{ - typedef clitk::BSplineDeformableTransform TransformType; - typedef typename TransformType::CoefficientImageType CoefficientImageType; - - typedef itk::ImageFileReader CoeffReaderType; - typename CoeffReaderType::Pointer reader = CoeffReaderType::New(); - reader->SetFileName(fileName); - reader->Update(); - - typename TransformType::Pointer transform = TransformType::New(); - transform->SetCoefficientImage(reader->GetOutput()); - -#if ITK_VERSION_MAJOR >= 4 - typedef itk::TransformToDisplacementFieldSource ConvertorType; -#else - typedef itk::TransformToDeformationFieldSource ConvertorType; -#endif - - typedef itk::ImageIOBase ImageIOType; - typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode); - imageIO->SetFileName(likeFileName); - imageIO->ReadImageInformation(); - - typename ConvertorType::Pointer convertor= ConvertorType::New(); - typename ConvertorType::SizeType output_size; - typename ConvertorType::SpacingType output_spacing; - typename ConvertorType::OriginType output_origin; - typename ConvertorType::DirectionType output_direction; - for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) { - output_size[i] = imageIO->GetDimensions(i); - output_spacing[i] = imageIO->GetSpacing(i); - output_origin[i] = imageIO->GetOrigin(i); - for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++) - output_direction[i][j] = imageIO->GetDirection(i)[j]; - } - - if (m_Verbose) { - std::cout << "Interpolating coefficients with grid:" << std::endl; - std::cout << output_size << output_spacing << std::endl; - } - - convertor->SetNumberOfThreads(1); - convertor->SetTransform(transform); - convertor->SetOutputOrigin(output_origin); - convertor->SetOutputSpacing(output_spacing); - convertor->SetOutputSize(output_size); - convertor->SetOutputDirection(output_direction); - convertor->Update(); - - return convertor->GetOutput(); -} - //------------------------------------------------------------------- // Update with the number of dimensions and the pixeltype diff --git a/tools/clitkWarpImageGenericFilter.h b/tools/clitkWarpImageGenericFilter.h index 9394d8d..0ce874d 100644 --- a/tools/clitkWarpImageGenericFilter.h +++ b/tools/clitkWarpImageGenericFilter.h @@ -98,7 +98,6 @@ namespace clitk //---------------------------------------- template void UpdateWithDim(std::string PixelType); template void UpdateWithDimAndPixelType(); - template typename DisplacementFieldType::Pointer CoeffsToDVF(std::string fileName, std::string likeFileName); //---------------------------------------- // Data members diff --git a/tools/clitkWarpImageGenericFilter.txx b/tools/clitkWarpImageGenericFilter.txx index ce979aa..c64080e 100644 --- a/tools/clitkWarpImageGenericFilter.txx +++ b/tools/clitkWarpImageGenericFilter.txx @@ -28,12 +28,7 @@ ===================================================*/ #include "itkVectorResampleImageFilter.h" -#include "clitkBSplineDeformableTransform.h" -#if ITK_VERSION_MAJOR >= 4 -#include "itkTransformToDisplacementFieldSource.h" -#else -#include "itkTransformToDeformationFieldSource.h" -#endif +#include "clitkCoeffsToDVF.h" namespace clitk { @@ -71,63 +66,6 @@ WarpImageGenericFilter::UpdateWithDim(std::string PixelType) } } -//------------------------------------------------------------------- -// Convert Coefficient image to DVF -//------------------------------------------------------------------- -template -typename DisplacementFieldType::Pointer -WarpImageGenericFilter::CoeffsToDVF(std::string fileName, std::string likeFileName) -{ - typedef clitk::BSplineDeformableTransform TransformType; - typedef typename TransformType::CoefficientImageType CoefficientImageType; - - typedef itk::ImageFileReader CoeffReaderType; - typename CoeffReaderType::Pointer reader = CoeffReaderType::New(); - reader->SetFileName(fileName); - reader->Update(); - - typename TransformType::Pointer transform = TransformType::New(); - transform->SetCoefficientImage(reader->GetOutput()); - -#if ITK_VERSION_MAJOR >= 4 - typedef itk::TransformToDisplacementFieldSource ConvertorType; -#else - typedef itk::TransformToDeformationFieldSource ConvertorType; -#endif - - typedef itk::ImageIOBase ImageIOType; - typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode); - imageIO->SetFileName(likeFileName); - imageIO->ReadImageInformation(); - - typename ConvertorType::Pointer convertor= ConvertorType::New(); - typename ConvertorType::SizeType output_size; - typename ConvertorType::SpacingType output_spacing; - typename ConvertorType::OriginType output_origin; - typename ConvertorType::DirectionType output_direction; - for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) { - output_size[i] = imageIO->GetDimensions(i); - output_spacing[i] = imageIO->GetSpacing(i); - output_origin[i] = imageIO->GetOrigin(i); - for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++) - output_direction[i][j] = imageIO->GetDirection(i)[j]; - } - - if (m_Verbose) { - std::cout << "Interpolating coefficients with grid:" << std::endl; - std::cout << output_size << output_spacing << std::endl; - } - - convertor->SetNumberOfThreads(1); - convertor->SetTransform(transform); - convertor->SetOutputOrigin(output_origin); - convertor->SetOutputSpacing(output_spacing); - convertor->SetOutputSize(output_size); - convertor->SetOutputDirection(output_direction); - convertor->Update(); - - return convertor->GetOutput(); -} //------------------------------------------------------------------- // Update with the number of dimensions and the pixeltype @@ -152,7 +90,7 @@ WarpImageGenericFilter::UpdateWithDimAndPixelType() typename DeformationFieldType::Pointer deformationField; if (m_ArgsInfo.coeff_given) { - deformationField = CoeffsToDVF(m_ArgsInfo.coeff_arg, m_InputFileName); + deformationField = CoeffsToDVF(m_ArgsInfo.coeff_arg, m_InputFileName, m_Verbose); } else { //Read the deformation field