]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
405540c41f4494b3e4c3c0749dbaab07f6f5673e
[clitk.git] / tools / clitkInvertVFGenericFilter.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 clitkInvertVFGenericFilter_txx
19 #define clitkInvertVFGenericFilter_txx
20
21 #include "itkVectorResampleImageFilter.h"
22 #include "clitkConvertBLUTCoeffsToVFFilter.h"
23
24 /* =================================================
25  * @file   clitkInvertVFGenericFilter.txx
26  * @author
27  * @date
28  *
29  * @brief
30  *
31  ===================================================*/
32
33
34 namespace clitk
35 {
36
37 //-----------------------------------------------------------
38 // Constructor
39 //-----------------------------------------------------------
40 template<class args_info_type>
41 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
42 {
43   m_Verbose=false;
44   m_InputFileName="";
45 }
46
47
48 //-----------------------------------------------------------
49 // Update
50 //-----------------------------------------------------------
51 template<class args_info_type>
52 void InvertVFGenericFilter<args_info_type>::Update()
53 {
54   // Read the Dimension and PixelType
55   int Dimension;
56   std::string PixelType;
57   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
58
59
60   // Call UpdateWithDim
61   if(Dimension==2) UpdateWithDim<2>(PixelType);
62   else if(Dimension==3) UpdateWithDim<3>(PixelType);
63   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
64   else {
65     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
66     return;
67   }
68 }
69
70 //-------------------------------------------------------------------
71 // Update with the number of dimensions
72 //-------------------------------------------------------------------
73 template<class args_info_type>
74 template<unsigned int Dimension>
75 void
76 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
77 {
78   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
79
80   //    if(PixelType == "short"){
81   //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
82   //       UpdateWithDimAndPixelType<Dimension, signed short>();
83   //     }
84   //    else if(PixelType == "unsigned_short"){
85   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
86   //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
87   //     }
88
89   //     else if (PixelType == "unsigned_char"){
90   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
91   //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
92   //     }
93
94   //     else if (PixelType == "char"){
95   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
96   //       UpdateWithDimAndPixelType<Dimension, signed char>();
97   //     }
98   //  else {
99   if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
100   UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
101   // }
102 }
103
104
105 //-------------------------------------------------------------------
106 // Update with the number of dimensions and the pixeltype
107 //-------------------------------------------------------------------
108 template<class args_info_type>
109 template <unsigned int Dimension, class  PixelType>
110 void
111 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
112 {
113
114   // ImageTypes
115   typedef itk::Image<PixelType, Dimension> InputImageType;
116   typedef itk::Image<PixelType, Dimension> OutputImageType;
117
118   // Read the input
119   typedef itk::ImageFileReader<InputImageType> InputReaderType;
120   typename InputReaderType::Pointer reader = InputReaderType::New();
121   reader->SetFileName( m_InputFileName);
122   reader->Update();
123   typename InputImageType::Pointer input = reader->GetOutput();
124
125   // Filter
126   typename OutputImageType::Pointer output;
127   switch (m_ArgsInfo.type_arg) {
128
129     // clitk filter
130   case 0: {
131     // Create the InvertVFFilter
132     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
133     typename FilterType::Pointer filter =FilterType::New();
134     if (m_ArgsInfo.like_given) {
135       typename FilterType::SpacingType spacing;
136       typename FilterType::SizeType size;
137       itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg);
138       for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
139         size[i] = header->GetDimensions(i);
140         spacing[i] = header->GetSpacing(i);
141       }
142
143       typedef itk::VectorResampleImageFilter<InputImageType, OutputImageType> ResampleFilterType;
144       typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
145       resampler->SetInput(input);
146       resampler->SetOutputOrigin(input->GetOrigin());
147       resampler->SetOutputDirection(input->GetDirection());
148       resampler->SetOutputSpacing(spacing);
149       resampler->SetSize(size);
150       resampler->Update();
151       spacing = resampler->GetOutput()->GetSpacing();
152       size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize();
153       filter->SetInput(resampler->GetOutput());
154     }
155     else
156       filter->SetInput(input);
157
158     filter->SetVerbose(m_Verbose);
159     if (m_ArgsInfo.threads_given) {
160 #if ITK_VERSION_MAJOR <= 4
161       filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
162 #else
163       filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
164 #endif
165     }
166     if (m_ArgsInfo.pad_given) {
167       PixelType pad;
168       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
169         pad.Fill(m_ArgsInfo.pad_arg[0]);
170       else
171         for(unsigned int i=0; i<Dimension; i++)
172           pad[i]=m_ArgsInfo.pad_arg[i];
173     }
174     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
175     filter->Update();
176     output=filter->GetOutput();
177
178     break;
179   }
180
181   case 1: {
182     // Create the InvertVFFilter
183     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
184     typename FilterType::Pointer filter =FilterType::New();
185     if (m_ArgsInfo.like_given) {
186       typedef ConvertBLUTCoeffsToVFFilter<InputImageType> VFFilterType;
187       typename VFFilterType::Pointer vf_filter = VFFilterType::New();
188       vf_filter->SetInputFileName(m_InputFileName);
189       vf_filter->SetLikeFileName(m_ArgsInfo.like_arg);
190       vf_filter->SetVerbose(m_Verbose);
191       vf_filter->Update();
192       filter->SetInput(vf_filter->GetOutput());
193     }
194
195     filter->SetVerbose(m_Verbose);
196     if (m_ArgsInfo.threads_given) {
197 #if ITK_VERSION_MAJOR <= 4
198       filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
199 #else
200       filter->SetNumberOfWorkUnits(m_ArgsInfo.threads_arg);
201 #endif
202     }
203     if (m_ArgsInfo.pad_given) {
204       PixelType pad;
205       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
206         pad.Fill(m_ArgsInfo.pad_arg[0]);
207       else
208         for(unsigned int i=0; i<Dimension; i++)
209           pad[i]=m_ArgsInfo.pad_arg[i];
210     }
211     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
212     filter->Update();
213     output=filter->GetOutput();
214
215     break;
216   }
217
218   case 2: {
219     // Create the InverseDeformationFieldFilter
220     typedef itk::InverseDisplacementFieldImageFilter<InputImageType,OutputImageType> FilterType;
221     typename FilterType::Pointer filter =FilterType::New();
222     filter->SetInput(input);
223     filter->SetOutputOrigin(input->GetOrigin());
224     filter->SetOutputSpacing(input->GetSpacing());
225     filter->SetSize(input->GetLargestPossibleRegion().GetSize());
226     filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
227     filter->Update();
228     output=filter->GetOutput();
229
230     break;
231   }
232
233     
234   }
235
236   // Output
237   typedef itk::ImageFileWriter<OutputImageType> WriterType;
238   typename WriterType::Pointer writer = WriterType::New();
239   writer->SetFileName(m_ArgsInfo.output_arg);
240   writer->SetInput(output);
241   writer->Update();
242
243 }
244
245
246 }//end clitk
247
248 #endif //#define clitkInvertVFGenericFilter_txx