]> Creatis software - clitk.git/blob - registration/clitkMatrixTransformToVFGenericFilter.txx
Fixed MatrixTransformToVF's option "--like" with ITK > 4.6
[clitk.git] / registration / clitkMatrixTransformToVFGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkMatrixTransformToVFGenericFilter_txx
19 #define clitkMatrixTransformToVFGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkMatrixTransformToVFGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-------------------------------------------------------------------
35   // Update with the number of dimensions
36   //-------------------------------------------------------------------
37   template<unsigned int Dimension>
38   void 
39   MatrixTransformToVFGenericFilter::UpdateWithDim()
40   {
41  //    if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
42
43 //     if(PixelType == "short"){  
44 //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
45 //       UpdateWithDimAndPixelType<Dimension, signed short>(); 
46 //     }
47 //     //    else if(PixelType == "unsigned_short"){  
48 //     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
49 //     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
50 //     //     }
51     
52 //     else if (PixelType == "unsigned_char"){ 
53 //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
54 //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55 //     }
56     
57 //     //     else if (PixelType == "char"){ 
58 //     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
59 //     //       UpdateWithDimAndPixelType<Dimension, signed char>();
60 //     //     }
61 //     else {
62 //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63 //       UpdateWithDimAndPixelType<Dimension, float>();
64 //     }
65 //   }
66
67
68 //   //-------------------------------------------------------------------
69 //   // Update with the number of dimensions and the pixeltype
70 //   //-------------------------------------------------------------------
71 //   template <unsigned int Dimension, class  PixelType> 
72 //   void 
73 //   MatrixTransformToVFGenericFilter::UpdateWithDimAndPixelType()
74 //   {
75
76     // ImageTypes
77     typedef itk::Vector<float,Dimension> Displacement;
78     typedef itk::Image<Displacement, Dimension> OutputImageType;
79     
80     // Filter
81 #if ITK_VERSION_MAJOR >= 4
82 #  if ITK_VERSION_MINOR < 6
83     typedef itk::TransformToDisplacementFieldSource<OutputImageType, double> ConvertorType;
84 #  else
85     typedef itk::TransformToDisplacementFieldFilter<OutputImageType, double> ConvertorType;
86 #  endif
87 #else
88     typedef itk::TransformToDeformationFieldSource<OutputImageType, double> ConvertorType;
89 #endif
90
91     typename   ConvertorType::Pointer filter= ConvertorType::New();
92
93     // Output image info
94     if (m_ArgsInfo.like_given)
95       {
96         typedef itk::ImageFileReader<OutputImageType> ReaderType;
97         typename ReaderType::Pointer reader2=ReaderType::New();
98         reader2->SetFileName(m_ArgsInfo.like_arg);
99         reader2->Update();
100
101         typename OutputImageType::Pointer image=reader2->GetOutput();
102 #if ITK_VERSION_MAJOR > 4 || (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR >= 6)
103     filter->SetReferenceImage(image);
104     filter->UseReferenceImageOn();
105 #else
106     filter->SetOutputParametersFromImage(image);
107 #endif
108       }
109     else
110       {
111         unsigned int i=0;
112         if(m_ArgsInfo.origin_given)
113           {
114             typename OutputImageType::PointType origin;
115             for(i=0;i<Dimension;i++)
116               origin[i]=m_ArgsInfo.origin_arg[i];
117             filter->SetOutputOrigin(origin);
118           }
119         if (m_ArgsInfo.spacing_given)
120           {
121             typename OutputImageType::SpacingType spacing;
122             for(i=0;i<Dimension;i++)
123               spacing[i]=m_ArgsInfo.spacing_arg[i];
124             filter->SetOutputSpacing(spacing);
125           }
126         if (m_ArgsInfo.size_given)
127           {
128             typename OutputImageType::SizeType size;
129             for(i=0;i<Dimension;i++)
130               size[i]=m_ArgsInfo.size_arg[i];
131 #if ITK_VERSION_MAJOR > 4 || (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR >= 6)
132         filter->SetSize(size);
133 #else
134         filter->SetOutputSize(size);
135 #endif
136       }
137       }
138     
139     // Transform
140     typedef itk::AffineTransform<double, Dimension> TransformType;
141     typename TransformType::Pointer transform =TransformType::New();
142     itk::Matrix<double, Dimension+1, Dimension+1> homMatrix= ReadMatrix<Dimension>( m_ArgsInfo.matrix_arg);
143     itk::Matrix<double, Dimension, Dimension> matrix =GetRotationalPartMatrix( homMatrix);
144     itk::Vector<double, Dimension> offset= GetTranslationPartMatrix( homMatrix);
145     transform->SetMatrix(matrix);
146     transform->SetOffset(offset);
147     filter->SetTransform(transform);
148     filter->Update();
149     typename OutputImageType::Pointer output=filter->GetOutput();
150
151     // Output
152     typedef itk::ImageFileWriter<OutputImageType> WriterType;
153     typename WriterType::Pointer writer = WriterType::New();
154     writer->SetFileName(m_ArgsInfo.output_arg);
155     writer->SetInput(output);
156     writer->Update();
157
158   }
159
160
161 }//end clitk
162  
163 #endif //#define clitkMatrixTransformToVFGenericFilter_txx