1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef CLITKVFMERGE_CXX
19 #define CLITKVFMERGE_CXX
22 * @file clitkVFMerge.cxx
23 * @author Jef Vandemeulebroucke <jefvdmb@gmail.com>
24 * @date June 15 10:14:53 2007
26 * @brief Read in one VF (ex mhd, vf) and write to another. Transforming from mm to voxels needed for the vf format is implemented in clitkVfImageIO.cxx .
32 #include "clitkVFMerge_ggo.h"
34 #include "clitkImageCommon.h"
37 //#include "itkReadTransform.h"
38 #include "itkImageFileWriter.h"
40 #include "itkImageFileReader.h"
41 //#include "itkRawImageIO.h"
44 int main( int argc, char *argv[] )
48 GGO(clitkVFMerge, args_info);
51 const unsigned int SpaceDimension = 3;
52 const unsigned int ModelDimension = 4;
53 typedef itk::Vector< float, SpaceDimension > Displacement;
54 typedef itk::Image< Displacement, SpaceDimension > DeformationFieldType;
55 typedef itk::Image< Displacement, ModelDimension > DynamicDeformationFieldType;
56 typedef itk::ImageFileReader< DeformationFieldType > DeformationFieldReaderType;
57 typedef itk::ImageFileWriter< DynamicDeformationFieldType > DynamicDeformationFieldWriterType;
61 DynamicDeformationFieldType::Pointer dynamicDeformationField=DynamicDeformationFieldType::New();
64 //declare their iterators
65 typedef itk::ImageRegionIterator< DynamicDeformationFieldType> DynamicDeformationFieldIteratorType;
66 DynamicDeformationFieldIteratorType *dynamicIteratorPointer= new DynamicDeformationFieldIteratorType;
68 for (unsigned int i=0 ; i< args_info.inputs_num ; i ++) {
69 //read in the deformation field i
70 DeformationFieldReaderType::Pointer deformationFieldReader = DeformationFieldReaderType::New();
71 deformationFieldReader->SetFileName( args_info.inputs[i]);
72 if (args_info.verbose_flag) std::cout<<"Reading VF number "<< i+1 << std::endl;
73 deformationFieldReader->Update();
74 DeformationFieldType::Pointer currentDeformationField = deformationFieldReader->GetOutput();
76 //create an iterator for the current deformation field
77 typedef itk::ImageRegionIterator<DeformationFieldType> FieldIteratorType;
78 FieldIteratorType fieldIterator(currentDeformationField, currentDeformationField->GetLargestPossibleRegion());
80 //Allocate memory for the dynamic components
82 DynamicDeformationFieldType::RegionType dynamicDeformationFieldRegion;
83 DynamicDeformationFieldType::RegionType::SizeType dynamicDeformationFieldSize;
84 DeformationFieldType::RegionType::SizeType deformationFieldSize;
85 deformationFieldSize= currentDeformationField->GetLargestPossibleRegion().GetSize();
86 dynamicDeformationFieldSize[0]=deformationFieldSize[0];
87 dynamicDeformationFieldSize[1]=deformationFieldSize[1];
88 dynamicDeformationFieldSize[2]=deformationFieldSize[2];
89 dynamicDeformationFieldSize[3]=args_info.inputs_num;
90 dynamicDeformationFieldRegion.SetSize(dynamicDeformationFieldSize);
91 DynamicDeformationFieldType::IndexType start;
93 dynamicDeformationFieldRegion.SetIndex(start);
94 dynamicDeformationField->SetRegions(dynamicDeformationFieldRegion);
95 dynamicDeformationField->Allocate();
99 DeformationFieldType::SpacingType spacing= currentDeformationField->GetSpacing();
100 DynamicDeformationFieldType::SpacingType dynamicSpacing;
101 dynamicSpacing[0]=spacing[0];
102 dynamicSpacing[1]=spacing[1];
103 dynamicSpacing[2]=spacing[2];
104 dynamicSpacing[3]=args_info.spacing_arg; //JV par exemple
105 dynamicDeformationField->SetSpacing(dynamicSpacing);
106 DynamicDeformationFieldType::PointType origin;
107 origin[0]=args_info.xorigin_arg;
108 origin[1]=args_info.yorigin_arg;
109 origin[2]=args_info.zorigin_arg;
110 origin[3]=0; //temporal origin is always 0
111 dynamicDeformationField->SetOrigin(origin);
113 // Creating iterators for the currentDeformationField and the DynamicDeformationField
114 DynamicDeformationFieldIteratorType *dynamicIterator= new DynamicDeformationFieldIteratorType(dynamicDeformationField, dynamicDeformationField->GetLargestPossibleRegion());
115 dynamicIteratorPointer=dynamicIterator;
116 dynamicIteratorPointer->GoToBegin();
118 if (args_info.verbose_flag) std::cout<<"Merging VF number "<< i+1 << std::endl;
119 //Copy the current component of the input into dynamicDeformationFieldComponent
120 fieldIterator.GoToBegin();
121 while(!fieldIterator.IsAtEnd()) {
122 dynamicIteratorPointer->Set(fieldIterator.Get());
124 ++(*dynamicIteratorPointer);
129 //Write the vector field
130 DynamicDeformationFieldWriterType::Pointer writer = DynamicDeformationFieldWriterType::New();
131 writer->SetInput( dynamicDeformationField );
132 writer->SetFileName( args_info.output_arg );
133 if (args_info.verbose_flag) std::cout<<"Writing the dynamic VF"<< std::endl;
139 } catch( itk::ExceptionObject & excp ) {
140 std::cerr << "Problem writing the output file" << std::endl;
141 std::cerr << excp << std::endl;