]> Creatis software - clitk.git/blob - tools/clitkAffineTransformGenericFilter.txx
Reformatted using new coding style
[clitk.git] / tools / clitkAffineTransformGenericFilter.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://oncora1.lyon.fnclcc.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 clitkAffineTransformGenericFilter_txx
19 #define clitkAffineTransformGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkAffineTransformGenericFilter.txx
23  * @author
24  * @date
25  *
26  * @brief
27  *
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34 //-----------------------------------------------------------
35 // Constructor
36 //-----------------------------------------------------------
37 template<class args_info_type>
38 AffineTransformGenericFilter<args_info_type>::AffineTransformGenericFilter()
39 {
40   m_Verbose=false;
41   m_InputFileName="";
42 }
43
44
45 //-----------------------------------------------------------
46 // Update
47 //-----------------------------------------------------------
48 template<class args_info_type>
49 void AffineTransformGenericFilter<args_info_type>::Update()
50 {
51   // Read the Dimension and PixelType
52   int Dimension, Components;
53   std::string PixelType;
54   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components);
55
56
57   // Call UpdateWithDim
58   if(Dimension==2) UpdateWithDim<2>(PixelType, Components);
59   else if(Dimension==3) UpdateWithDim<3>(PixelType, Components);
60   else if (Dimension==4)UpdateWithDim<4>(PixelType, Components);
61   else {
62     std::cout<<"Error, Only for 2, 3 or 4  Dimensions!!!"<<std::endl ;
63     return;
64   }
65 }
66
67 //-------------------------------------------------------------------
68 // Update with the number of dimensions
69 //-------------------------------------------------------------------
70 template<class args_info_type>
71 template<unsigned int Dimension>
72 void
73 AffineTransformGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType, int Components)
74 {
75   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<<Components<<" component(s) of "<<  PixelType<<"..."<<std::endl;
76
77   if (Components==1) {
78     if(PixelType == "short") {
79       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
80       UpdateWithDimAndPixelType<Dimension, signed short>();
81     }
82     //    else if(PixelType == "unsigned_short"){
83     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
84     //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
85     //     }
86
87     else if (PixelType == "unsigned_char") {
88       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
89       UpdateWithDimAndPixelType<Dimension, unsigned char>();
90     }
91
92     //     else if (PixelType == "char"){
93     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
94     //       UpdateWithDimAndPixelType<Dimension, signed char>();
95     //     }
96     else {
97       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
98       UpdateWithDimAndPixelType<Dimension, float>();
99     }
100   }
101
102   else if (Components==3) {
103     if (m_Verbose) std::cout  << "Launching transform in "<< Dimension <<"D and 3D float (DVF)" << std::endl;
104     UpdateWithDimAndVectorType<Dimension, itk::Vector<float, Dimension> >();
105   }
106
107   else std::cerr<<"Number of components is "<<Components<<", not supported!"<<std::endl;
108
109 }
110
111
112 //-------------------------------------------------------------------
113 // Update with the number of dimensions and the pixeltype
114 //-------------------------------------------------------------------
115 template<class args_info_type>
116 template <unsigned int Dimension, class  PixelType>
117 void
118 AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
119 {
120
121   // ImageTypes
122   typedef itk::Image<PixelType, Dimension> InputImageType;
123   typedef itk::Image<PixelType, Dimension> OutputImageType;
124
125   // Read the input
126   typedef itk::ImageFileReader<InputImageType> InputReaderType;
127   typename InputReaderType::Pointer reader = InputReaderType::New();
128   reader->SetFileName( m_InputFileName);
129   reader->Update();
130   typename InputImageType::Pointer input= reader->GetOutput();
131
132   //Filter
133   typedef  itk::ResampleImageFilter< InputImageType,OutputImageType >  ResampleFilterType;
134   typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
135
136   // Matrix
137   typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
138   if (m_ArgsInfo.matrix_given) {
139     matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
140     if (m_Verbose) std::cout<<"Reading the matrix..."<<std::endl;
141   } else {
142     matrix.SetIdentity();
143   }
144   if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
145   if (m_Verbose) std::cout<<matrix<<std::endl;
146   typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
147   typename itk::Vector<double,Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
148
149   // Transform
150   typedef itk::AffineTransform<double, Dimension> AffineTransformType;
151   typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
152   affineTransform->SetMatrix(rotationMatrix);
153   affineTransform->SetTranslation(translationPart);
154
155   // Interp
156   typedef clitk::GenericInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
157   typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
158   genericInterpolator->SetArgsInfo(m_ArgsInfo);
159
160   // Properties
161   if (m_ArgsInfo.like_given) {
162     typename InputReaderType::Pointer likeReader=InputReaderType::New();
163     likeReader->SetFileName(m_ArgsInfo.like_arg);
164     likeReader->Update();
165     resampler->SetOutputParametersFromImage(likeReader->GetOutput());
166   } else {
167     //Size
168     typename OutputImageType::SizeType outputSize;
169     if (m_ArgsInfo.size_given) {
170       for(unsigned int i=0; i< Dimension; i++)
171         outputSize[i]=m_ArgsInfo.size_arg[i];
172     } else outputSize=input->GetLargestPossibleRegion().GetSize();
173     std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
174
175     //Spacing
176     typename OutputImageType::SpacingType outputSpacing;
177     if (m_ArgsInfo.spacing_given) {
178       for(unsigned int i=0; i< Dimension; i++)
179         outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
180     } else outputSpacing=input->GetSpacing();
181     std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
182
183     //Origin
184     typename OutputImageType::PointType outputOrigin;
185     if (m_ArgsInfo.origin_given) {
186       for(unsigned int i=0; i< Dimension; i++)
187         outputOrigin[i]=m_ArgsInfo.origin_arg[i];
188     } else outputOrigin=input->GetOrigin();
189     std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
190
191     // Set
192     resampler->SetSize( outputSize );
193     resampler->SetOutputSpacing( outputSpacing );
194     resampler->SetOutputOrigin(  outputOrigin );
195
196   }
197
198   resampler->SetInput( input );
199   resampler->SetTransform( affineTransform );
200   resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
201   resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
202
203   try {
204     resampler->Update();
205   } catch(itk::ExceptionObject) {
206     std::cerr<<"Error resampling the image"<<std::endl;
207   }
208
209   typename OutputImageType::Pointer output = resampler->GetOutput();
210
211   // Output
212   typedef itk::ImageFileWriter<OutputImageType> WriterType;
213   typename WriterType::Pointer writer = WriterType::New();
214   writer->SetFileName(m_ArgsInfo.output_arg);
215   writer->SetInput(output);
216   writer->Update();
217
218 }
219
220 //-------------------------------------------------------------------
221 // Update with the number of dimensions and the pixeltype (components)
222 //-------------------------------------------------------------------
223 template<class args_info_type>
224 template<unsigned int Dimension, class PixelType>
225 void AffineTransformGenericFilter<args_info_type>::UpdateWithDimAndVectorType()
226 {
227   // ImageTypes
228   typedef itk::Image<PixelType, Dimension> InputImageType;
229   typedef itk::Image<PixelType, Dimension> OutputImageType;
230
231   // Read the input
232   typedef itk::ImageFileReader<InputImageType> InputReaderType;
233   typename InputReaderType::Pointer reader = InputReaderType::New();
234   reader->SetFileName( m_InputFileName);
235   reader->Update();
236   typename InputImageType::Pointer input= reader->GetOutput();
237
238   //Filter
239   typedef  itk::VectorResampleImageFilter< InputImageType,OutputImageType, double >  ResampleFilterType;
240   typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
241
242   // Matrix
243   typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
244   if (m_ArgsInfo.matrix_given)
245     matrix= clitk::ReadMatrix<Dimension>(m_ArgsInfo.matrix_arg);
246   else
247     matrix.SetIdentity();
248   if (m_Verbose) std::cout<<"Using the following matrix:"<<std::endl;
249   if (m_Verbose) std::cout<<matrix<<std::endl;
250   typename itk::Matrix<double, Dimension, Dimension> rotationMatrix=clitk::GetRotationalPartMatrix(matrix);
251   typename itk::Vector<double, Dimension> translationPart= clitk::GetTranslationPartMatrix(matrix);
252
253   // Transform
254   typedef itk::AffineTransform<double, Dimension> AffineTransformType;
255   typename AffineTransformType::Pointer affineTransform=AffineTransformType::New();
256   affineTransform->SetMatrix(rotationMatrix);
257   affineTransform->SetTranslation(translationPart);
258
259   // Interp
260   typedef clitk::GenericVectorInterpolator<args_info_type, InputImageType, double> GenericInterpolatorType;
261   typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New();
262   genericInterpolator->SetArgsInfo(m_ArgsInfo);
263
264   // Properties
265   if (m_ArgsInfo.like_given) {
266     typename InputReaderType::Pointer likeReader=InputReaderType::New();
267     likeReader->SetFileName(m_ArgsInfo.like_arg);
268     likeReader->Update();
269     resampler->SetSize( likeReader->GetOutput()->GetLargestPossibleRegion().GetSize() );
270     resampler->SetOutputSpacing( likeReader->GetOutput()->GetSpacing() );
271     resampler->SetOutputOrigin(  likeReader->GetOutput()->GetOrigin() );
272   } else {
273     //Size
274     typename OutputImageType::SizeType outputSize;
275     if (m_ArgsInfo.size_given) {
276       for(unsigned int i=0; i< Dimension; i++)
277         outputSize[i]=m_ArgsInfo.size_arg[i];
278     } else outputSize=input->GetLargestPossibleRegion().GetSize();
279     std::cout<<"Setting the size to "<<outputSize<<"..."<<std::endl;
280
281     //Spacing
282     typename OutputImageType::SpacingType outputSpacing;
283     if (m_ArgsInfo.spacing_given) {
284       for(unsigned int i=0; i< Dimension; i++)
285         outputSpacing[i]=m_ArgsInfo.spacing_arg[i];
286     } else outputSpacing=input->GetSpacing();
287     std::cout<<"Setting the spacing to "<<outputSpacing<<"..."<<std::endl;
288
289     //Origin
290     typename OutputImageType::PointType outputOrigin;
291     if (m_ArgsInfo.origin_given) {
292       for(unsigned int i=0; i< Dimension; i++)
293         outputOrigin[i]=m_ArgsInfo.origin_arg[i];
294     } else outputOrigin=input->GetOrigin();
295     std::cout<<"Setting the origin to "<<outputOrigin<<"..."<<std::endl;
296
297     // Set
298     resampler->SetSize( outputSize );
299     resampler->SetOutputSpacing( outputSpacing );
300     resampler->SetOutputOrigin(  outputOrigin );
301
302   }
303
304   resampler->SetInput( input );
305   resampler->SetTransform( affineTransform );
306   resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer());
307   resampler->SetDefaultPixelValue( static_cast<PixelType>(m_ArgsInfo.pad_arg) );
308
309   try {
310     resampler->Update();
311   } catch(itk::ExceptionObject) {
312     std::cerr<<"Error resampling the image"<<std::endl;
313   }
314
315   typename OutputImageType::Pointer output = resampler->GetOutput();
316
317   // Output
318   typedef itk::ImageFileWriter<OutputImageType> WriterType;
319   typename WriterType::Pointer writer = WriterType::New();
320   writer->SetFileName(m_ArgsInfo.output_arg);
321   writer->SetInput(output);
322   writer->Update();
323
324 }
325
326
327 } //end clitk
328
329 #endif //#define clitkAffineTransformGenericFilter_txx