]> Creatis software - clitk.git/blob - registration/clitkCalculateTREGenericFilter.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / registration / clitkCalculateTREGenericFilter.cxx
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 clitkCalculateTREGenericFilter_cxx
19 #define clitkCalculateTREGenericFilter_cxx
20
21 /* =================================================
22  * @file   clitkCalculateTREGenericFilter.cxx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 #include "clitkCalculateTREGenericFilter.h"
31
32
33 namespace clitk
34 {
35
36
37   //-----------------------------------------------------------
38   // Constructor
39   //-----------------------------------------------------------
40   CalculateTREGenericFilter::CalculateTREGenericFilter()
41   {
42     m_Verbose=false;
43     m_InputFileName="";
44     m_NumberOfFields=0;
45     m_NumberOfLists=0;
46     m_NumberOfPoints=0;
47   }
48
49
50   //-----------------------------------------------------------
51   // Update
52   //-----------------------------------------------------------
53   void CalculateTREGenericFilter::Update()
54   {
55     // Read the Dimension and PixelType
56     if (m_ArgsInfo.vf_given && m_ArgsInfo.coeff_given)
57       {
58         std::cout << "Error, You have to supply either vector fields or coefficient images. Supplying both is not allowed. See clitkCalculateTRE --help for details."<<std::endl;;
59         return;
60       }
61     else if (!m_ArgsInfo.vf_given && !m_ArgsInfo.coeff_given)
62       {
63         std::cout << "Error, You have to supply either vector fields or coefficient images. Supplying none is not allowed. See clitkCalculateTRE --help for details."<<std::endl;;
64         return;
65       }
66     else if (m_ArgsInfo.vf_given)
67       m_InputFileName=m_ArgsInfo.vf_arg[0];
68     else
69       {
70         m_InputFileName=m_ArgsInfo.coeff_arg[0];
71       }
72
73     int Dimension, Components;
74     std::string PixelType;
75     ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components);
76     if (Dimension < 2 || Dimension > 4)
77       {
78         std::cout<<"Error, Only for 2, 3 or 4  Dimensions!!!"<<std::endl ;
79         return;
80       }
81
82     // Call UpdateWithDim
83     if (m_ArgsInfo.vf_given)
84       {
85         if(Dimension==2) ReadVectorFields<2,2>();
86         else 
87         if(Dimension==3) ReadVectorFields<3,3>();
88         else if (Dimension==4)ReadVectorFields<4,3>(); 
89       }
90     else
91       {
92         if(Dimension==2) ReadCoefficientImages<2,2>();
93         else 
94         if(Dimension==3) ReadCoefficientImages<3,3>();
95         else if (Dimension==4)ReadCoefficientImages<4,3>(); 
96       }        
97   }
98
99   //-----------------------------
100   // Process DVF
101   //-----------------------------
102   template< >
103   void 
104   CalculateTREGenericFilter::ProcessVectorFields<4,3>(std::vector<itk::Image<itk::Vector<double, 3>, 4>::Pointer > dvfs,  char** filenames )
105   {
106     // Typedefs
107     typedef itk::Vector<double,3> PixelType;
108     typedef itk::Image<PixelType, 4> InputImageType;
109     typedef itk::Image<PixelType, 3> OutputImageType;
110
111     // IO
112     InputImageType::Pointer input=dvfs[0];
113     std::vector<itk::Image<itk::Vector<double, 3>, 3>::Pointer > new_dvfs;
114     
115     // Split vector field
116     typedef itk::ExtractImageFilter<InputImageType,OutputImageType> FilterType;
117     unsigned int splitDimension=3;
118
119     // Make new file names
120     std::vector<std::string> new_filenames;
121     std::string base = filenames[0];
122
123     // Set the extract region
124     InputImageType::SizeType size=input->GetLargestPossibleRegion().GetSize();
125     size[splitDimension]=0;
126     InputImageType::RegionType extracted_region;
127     extracted_region.SetSize(size);
128     InputImageType::IndexType index=input->GetLargestPossibleRegion().GetIndex();
129     
130   
131     // Loop
132     for (unsigned int i=0;i<input->GetLargestPossibleRegion().GetSize()[splitDimension];i++)
133       {
134         
135         // Skip?
136         if (m_ArgsInfo.skip_given && i==(unsigned int) m_ArgsInfo.skip_arg) continue;
137
138         // extract dvf
139          FilterType::Pointer filter= FilterType::New();
140         filter->SetInput(input);
141         index[splitDimension]=i;
142         extracted_region.SetIndex(index);
143         filter->SetExtractionRegion(extracted_region);
144         filter->Update();
145         new_dvfs.push_back(filter->GetOutput());
146         
147         // make name
148         std::ostringstream number_dvf;
149         number_dvf << i;
150         std::string number =  number_dvf.str();
151         new_filenames.push_back(base+"_"+number);
152       }
153
154     // Update
155     this->UpdateDVFWithDim<3>(new_dvfs, new_filenames); 
156
157   }
158
159   //-----------------------------
160   // Process Coefficient images
161   //-----------------------------
162   template< >
163   void 
164   CalculateTREGenericFilter::ProcessCoefficientImages<4,3>(std::vector<itk::Image<itk::Vector<double, 3>, 4>::Pointer > coeffs,  char** filenames )
165   {
166     // Typedefs
167     typedef itk::Vector<double,3> PixelType;
168     typedef itk::Image<PixelType, 4> InputImageType;
169     typedef itk::Image<PixelType, 3> OutputImageType;
170
171     // IO
172     InputImageType::Pointer input=coeffs[0];
173     std::vector<itk::Image<itk::Vector<double, 3>, 3>::Pointer > new_coeffs;
174     
175     // Split vector field
176     typedef itk::ExtractImageFilter<InputImageType,OutputImageType> FilterType;
177     unsigned int splitDimension=3;
178
179     // Make new file names
180     std::vector<std::string> new_filenames;
181     std::string base = filenames[0];
182
183     // Set the extract region
184     InputImageType::SizeType size=input->GetLargestPossibleRegion().GetSize();
185     size[splitDimension]=0;
186     InputImageType::RegionType extracted_region;
187     extracted_region.SetSize(size);
188     InputImageType::IndexType index=input->GetLargestPossibleRegion().GetIndex();
189     
190   
191     // Loop
192     for (unsigned int i=0;i<input->GetLargestPossibleRegion().GetSize()[splitDimension];i++)
193       {
194         
195         // Skip?
196         if (m_ArgsInfo.skip_given && i==(unsigned int) m_ArgsInfo.skip_arg) continue;
197
198         // extract dvf
199          FilterType::Pointer filter= FilterType::New();
200         filter->SetInput(input);
201         index[splitDimension]=i;
202         extracted_region.SetIndex(index);
203         filter->SetExtractionRegion(extracted_region);
204         filter->Update();
205         new_coeffs.push_back(filter->GetOutput());
206         
207         // make name
208         std::ostringstream number_dvf;
209         number_dvf << i;
210         std::string number =  number_dvf.str();
211         new_filenames.push_back(base+"_"+number);
212       }
213
214     // Update
215     this->UpdateCoeffsWithDim<3>(new_coeffs, new_filenames); 
216
217   }
218
219
220   
221 } //end clitk
222
223 #endif  //#define clitkCalculateTREGenericFilter_cxx