]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
2ddd46c5b4d6cc51422efd9fb17e5f6d97a4a0a7
[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 /* =================================================
22  * @file   clitkInvertVFGenericFilter.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 InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
39 {
40   m_Verbose=false;
41   m_InputFileName="";
42 }
43
44
45 //-----------------------------------------------------------
46 // Update
47 //-----------------------------------------------------------
48 template<class args_info_type>
49 void InvertVFGenericFilter<args_info_type>::Update()
50 {
51   // Read the Dimension and PixelType
52   int Dimension;
53   std::string PixelType;
54   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
55
56
57   // Call UpdateWithDim
58   if(Dimension==2) UpdateWithDim<2>(PixelType);
59   else if(Dimension==3) UpdateWithDim<3>(PixelType);
60   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
61   else {
62     std::cout<<"Error, Only for 2 or 3  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 InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
74 {
75   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
76
77   //    if(PixelType == "short"){
78   //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
79   //       UpdateWithDimAndPixelType<Dimension, signed short>();
80   //     }
81   //    else if(PixelType == "unsigned_short"){
82   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
83   //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
84   //     }
85
86   //     else if (PixelType == "unsigned_char"){
87   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
88   //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
89   //     }
90
91   //     else if (PixelType == "char"){
92   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
93   //       UpdateWithDimAndPixelType<Dimension, signed char>();
94   //     }
95   //  else {
96   if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
97   UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
98   // }
99 }
100
101
102 //-------------------------------------------------------------------
103 // Update with the number of dimensions and the pixeltype
104 //-------------------------------------------------------------------
105 template<class args_info_type>
106 template <unsigned int Dimension, class  PixelType>
107 void
108 InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
109 {
110
111   // ImageTypes
112   typedef itk::Image<PixelType, Dimension> InputImageType;
113   typedef itk::Image<PixelType, Dimension> OutputImageType;
114
115   // Read the input
116   typedef itk::ImageFileReader<InputImageType> InputReaderType;
117   typename InputReaderType::Pointer reader = InputReaderType::New();
118   reader->SetFileName( m_InputFileName);
119   reader->Update();
120   typename InputImageType::Pointer input= reader->GetOutput();
121
122   // Filter
123   typename OutputImageType::Pointer output;
124   switch (m_ArgsInfo.type_arg) {
125
126     // clitk filter
127   case 0: {
128     // Create the InvertVFFilter
129     typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
130     typename FilterType::Pointer filter =FilterType::New();
131     filter->SetInput(input);
132     typename FilterType::SpacingType spacing = input->GetSpacing();
133     typename FilterType::SizeType size = input->GetLargestPossibleRegion().GetSize();
134     if (m_ArgsInfo.like_given) {
135       itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg);
136       for(unsigned int i=0; i<InputImageType::ImageDimension; i++) {
137         size[i] = header->GetDimensions(i);
138         spacing[i] = header->GetSpacing(i);
139       }
140     }
141     std::cout << spacing << size << std::endl;
142     filter->SetOutputSpacing(spacing);
143     filter->SetOutputSize(size);
144
145     filter->SetVerbose(m_Verbose);
146     if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
147     if (m_ArgsInfo.pad_given) {
148       PixelType pad;
149       if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
150         pad.Fill(m_ArgsInfo.pad_arg[0]);
151       else
152         for(unsigned int i=0; i<Dimension; i++)
153           pad[i]=m_ArgsInfo.pad_arg[i];
154     }
155     filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
156     filter->Update();
157     output=filter->GetOutput();
158
159     break;
160   }
161
162   case 1: {
163     // Create the InverseDeformationFieldFilter
164 #if ITK_VERSION_MAJOR >= 4
165     typedef itk::InverseDisplacementFieldImageFilter<InputImageType,OutputImageType> FilterType;
166 #else
167     typedef itk::InverseDeformationFieldImageFilter<InputImageType,OutputImageType> FilterType;
168 #endif
169     typename FilterType::Pointer filter =FilterType::New();
170     filter->SetInput(input);
171     filter->SetOutputOrigin(input->GetOrigin());
172     filter->SetOutputSpacing(input->GetSpacing());
173     filter->SetSize(input->GetLargestPossibleRegion().GetSize());
174     filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
175     filter->Update();
176     output=filter->GetOutput();
177
178     break;
179   }
180
181   }
182
183   // Output
184   typedef itk::ImageFileWriter<OutputImageType> WriterType;
185   typename WriterType::Pointer writer = WriterType::New();
186   writer->SetFileName(m_ArgsInfo.output_arg);
187   writer->SetInput(output);
188   writer->Update();
189
190 }
191
192
193 }//end clitk
194
195 #endif //#define clitkInvertVFGenericFilter_txx