]> Creatis software - clitk.git/blob - registration/clitkConvertBSplineDeformableTransformToVFGenericFilter.cxx
Removed typename outside template which prevents MSVC compilation
[clitk.git] / registration / clitkConvertBSplineDeformableTransformToVFGenericFilter.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 clitkConvertBSplineDeformableTransformToVFGenericFilter_cxx
19 #define clitkConvertBSplineDeformableTransformToVFGenericFilter_cxx
20
21 /* =================================================
22  * @file   clitkConvertBSplineDeformableTransformToVFGenericFilter.cxx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30 // clitk include
31 #include "clitkBSplineDeformableTransform.h"
32 #include "clitkTransformToDeformationFieldSource.h"
33 #include "clitkShapedBLUTSpatioTemporalDeformableTransform.h"
34 #include "clitkConvertBLUTCoeffsToVFFilter.h"
35 #include "clitkConvertBSplineDeformableTransformToVFGenericFilter.h"
36
37
38 namespace clitk
39 {
40
41
42   //-----------------------------------------------------------
43   // Constructor
44   //-----------------------------------------------------------
45   ConvertBSplineDeformableTransformToVFGenericFilter::ConvertBSplineDeformableTransformToVFGenericFilter()
46   {
47     m_Verbose=false;
48     m_InputFileName="";
49   }
50
51   //-------------------------------------------------------------------
52   // Update with the number of dimensions
53   //-------------------------------------------------------------------
54   template<>
55   void 
56   ConvertBSplineDeformableTransformToVFGenericFilter::UpdateWithDim<3>(std::string PixelType, int Components)
57 {
58     // Components
59     if (Components !=3)
60     {
61         std::cerr<<"Number of components is "<<Components<<"! Only 3 components is supported."<<std::endl;
62         return;
63     }
64     if (PixelType != "double")
65     {
66         std::cerr<<"PixelType is  "<<PixelType<<"! Only double coefficient images are supported."<<std::endl;
67         std::cerr<<"Reading image as double..."<<std::endl;
68     }
69
70     // ImageTypes
71     const unsigned int Dimension=3;
72     typedef itk::Vector<double, Dimension> InputPixelType;
73     typedef itk::Vector<float, Dimension> OutputPixelType;
74     typedef itk::Image<InputPixelType, Dimension> InputImageType;
75     typedef itk::Image<OutputPixelType, Dimension> OutputImageType;
76
77     typedef ConvertBLUTCoeffsToVFFilter<OutputImageType> FilterType;
78     FilterType::Pointer filter = FilterType::New();
79     filter->SetInputFileName(m_InputFileName);
80     filter->SetTransformType(m_ArgsInfo.type_arg);
81     filter->SetVerbose(m_ArgsInfo.verbose_flag);
82
83     if (m_ArgsInfo.like_given) {
84       filter->SetLikeFileName(m_ArgsInfo.like_arg);
85     }
86     else {
87       FilterType::OutputImagePointType origin;
88       FilterType::OutputImageSpacingType spacing;
89       FilterType::OutputImageSizeType size;
90       for (unsigned int i = 0; i < Dimension; i++) {
91         origin[i] = m_ArgsInfo.origin_arg[i];
92         spacing[i] = m_ArgsInfo.spacing_arg[i];
93         size[i] = m_ArgsInfo.size_arg[i];
94       }
95       filter->SetOutputOrigin(origin);
96       filter->SetOutputSpacing(spacing);
97       filter->SetOutputSize(size);
98     }
99     
100     if (m_ArgsInfo.order_given) {
101       FilterType::OutputImageSizeType order;
102       for (unsigned int i = 0; i < Dimension; i++) {
103         order[i] = m_ArgsInfo.order_arg[i];
104       }
105
106       filter->SetBLUTSplineOrders(order);
107     }
108     
109     if (m_ArgsInfo.mask_given)
110       filter->SetMaskFileName(m_ArgsInfo.mask_arg);
111     
112     filter->Update();
113     
114     OutputImageType::Pointer output=filter->GetOutput();
115
116     // -----------------------------------------------
117     // Output
118     // -----------------------------------------------
119     typedef itk::ImageFileWriter<OutputImageType> WriterType;
120     WriterType::Pointer writer = WriterType::New();
121     writer->SetFileName(m_ArgsInfo.output_arg);
122     writer->SetInput(output);
123     writer->Update();
124
125   }
126
127 /*
128   //-------------------------------------------------------------------
129   // Update with the number of dimensions
130   //-------------------------------------------------------------------
131   template<>
132 void
133 ConvertBSplineDeformableTransformToVFGenericFilter::UpdateWithDim<4>(std::string PixelType, int Components)
134 {
135     // Components
136     if (Components !=3)
137     {
138         std::cerr<<"Number of components is "<<Components<<"! Only 3 components is supported."<<std::endl;
139         return;
140     }
141     if (PixelType != "double")
142     {
143         std::cerr<<"PixelType is  "<<PixelType<<"! Only double coefficient images are supported."<<std::endl;
144         std::cerr<<"Reading image as double..."<<std::endl;
145     }
146
147     // ImageTypes
148     const unsigned int Dimension=4;
149     const unsigned int SpaceDimension=3;
150     typedef itk::Vector<double, SpaceDimension> InputPixelType;
151     typedef itk::Vector<float, SpaceDimension> OutputPixelType;
152     typedef itk::Image<InputPixelType, Dimension> InputImageType;
153     typedef itk::Image<OutputPixelType, Dimension> OutputImageType;
154
155     // Read the input
156     typedef itk::ImageFileReader<InputImageType> InputReaderType;
157     InputReaderType::Pointer reader = InputReaderType::New();
158     reader->SetFileName( m_InputFileName);
159     reader->Update();
160     InputImageType::Pointer input= reader->GetOutput();
161
162
163     // -----------------------------------------------
164     // Filter
165     // -----------------------------------------------
166     typedef clitk::TransformToDeformationFieldSource<OutputImageType, double> ConvertorType;
167     ConvertorType::Pointer filter= ConvertorType::New();
168
169     //Output image info
170     if (m_ArgsInfo.like_given)
171     {
172         typedef itk::ImageFileReader<OutputImageType> ReaderType;
173         ReaderType::Pointer reader2=ReaderType::New();
174         reader2->SetFileName(m_ArgsInfo.like_arg);
175         reader2->Update();
176
177         OutputImageType::Pointer image=reader2->GetOutput();
178         filter->SetOutputParametersFromImage(image);
179     }
180     else
181     {
182         unsigned int i=0;
183         if (m_ArgsInfo.origin_given)
184         {
185             OutputImageType::PointType origin;
186             for (i=0;i<Dimension;i++)
187                 origin[i]=m_ArgsInfo.origin_arg[i];
188             filter->SetOutputOrigin(origin);
189         }
190         if (m_ArgsInfo.spacing_given)
191         {
192             OutputImageType::SpacingType spacing;
193             for (i=0;i<Dimension;i++)
194                 spacing[i]=m_ArgsInfo.spacing_arg[i];
195             filter->SetOutputSpacing(spacing);
196         }
197         if (m_ArgsInfo.spacing_given)
198         {
199             OutputImageType::SizeType size;
200             for (i=0;i<Dimension;i++)
201                 size[i]=m_ArgsInfo.size_arg[i];
202             filter->SetOutputSize(size);
203         }
204     }
205     //Output image info
206     if (m_Verbose)
207     {
208         std::cout<< "Setting output origin to "<<filter->GetOutputOrigin()<<"..."<<std::endl;
209         std::cout<< "Setting output spacing to "<<filter->GetOutputSpacing()<<"..."<<std::endl;
210         std::cout<< "Setting output size to "<<filter->GetOutputSize()<<"..."<<std::endl;
211     }
212
213
214     // -----------------------------------------------
215     // Transform
216     // -----------------------------------------------
217     typedef clitk::ShapedBLUTSpatioTemporalDeformableTransform< double, Dimension, Dimension > TransformType;
218     TransformType::Pointer transform=TransformType::New();
219     transform->SetTransformShape(m_ArgsInfo.shape_arg);
220
221     // Spline orders:  Default is cubic splines
222     InputImageType::RegionType::SizeType splineOrders ;
223     splineOrders.Fill(3);
224     if (m_ArgsInfo.order_given)
225         for (unsigned int i=0; i<Dimension;i++)
226             splineOrders[i]=m_ArgsInfo.order_arg[i];
227     if (m_Verbose) std::cout<<"Setting the spline orders  to "<<splineOrders<<"..."<<std::endl;
228
229     // Mask
230     typedef itk::ImageMaskSpatialObject<  Dimension >   MaskType;
231     MaskType::Pointer  spatialObjectMask=NULL;
232     if (m_ArgsInfo.mask_given)
233     {
234         typedef itk::Image< unsigned char, Dimension >   ImageMaskType;
235         typedef itk::ImageFileReader< ImageMaskType >    MaskReaderType;
236         MaskReaderType::Pointer  maskReader = MaskReaderType::New();
237         maskReader->SetFileName(m_ArgsInfo.mask_arg);
238
239         try
240         {
241             maskReader->Update();
242         }
243         catch ( itk::ExceptionObject & err )
244         {
245             std::cerr << "ExceptionObject caught while reading mask !" << std::endl;
246             std::cerr << err << std::endl;
247             return;
248         }
249         if (m_Verbose)std::cout <<"Mask was read..." <<std::endl;
250
251         // Set the image to the spatialObject
252         spatialObjectMask = MaskType::New();
253         spatialObjectMask->SetImage( maskReader->GetOutput() );
254     }
255
256
257     // Samplingfactors
258     InputImageType::SizeType samplingFactors;
259     for (unsigned int i=0; i< Dimension; i++)
260     {
261         samplingFactors[i]= (int) ( input->GetSpacing()[i]/ filter->GetOutputSpacing()[i]);
262         if (m_Verbose) std::cout<<"Setting sampling factor "<<i<<" to "<<samplingFactors[i]<<"..."<<std::endl;
263     }
264     if ( !(m_ArgsInfo.shape_arg%2) )samplingFactors[Dimension-1]=5;
265
266     // Set
267     transform->SetSplineOrders(splineOrders);
268     transform->SetMask(spatialObjectMask);
269     transform->SetLUTSamplingFactors(samplingFactors);
270     transform->SetCoefficientImage(input);
271     filter->SetTransform(transform);
272
273
274     // -----------------------------------------------
275     // Update
276     // -----------------------------------------------
277     if (m_Verbose)std::cout<< "Converting the BSpline transform..."<<std::endl;
278     try
279     {
280         filter->Update();
281     }
282     catch (itk::ExceptionObject)
283     {
284         std::cerr<<"Error: Exception thrown during execution convertion filter!"<<std::endl;
285     }
286
287     OutputImageType::Pointer output=filter->GetOutput();
288
289
290     // -----------------------------------------------
291     // Output
292     // -----------------------------------------------
293     typedef itk::ImageFileWriter<OutputImageType> WriterType;
294     WriterType::Pointer writer = WriterType::New();
295     writer->SetFileName(m_ArgsInfo.output_arg);
296     writer->SetInput(output);
297     writer->Update();
298
299 }
300 */
301   //-----------------------------------------------------------
302   // Update
303   //-----------------------------------------------------------
304   void ConvertBSplineDeformableTransformToVFGenericFilter::Update()
305   {
306   
307     // Read the Dimension and PixelType
308     int Dimension, Components;
309     std::string PixelType;
310     ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components);
311
312     // Call UpdateWithDim
313     //if(Dimension==2) UpdateWithDim<2>(PixelType, Components);
314     if(Dimension==3) UpdateWithDim<3>(PixelType, Components);
315     //else if (Dimension==4) UpdateWithDim<4>(PixelType, Components); 
316     else 
317       {
318         std::cout<<"Error, Only for 3 Dimensions!!!"<<std::endl ;
319         return;
320       }
321   }
322
323 } //end clitk
324
325 #endif  //#define clitkConvertBSplineDeformableTransformToVFGenericFilter_cxx