]> Creatis software - clitk.git/blob - tools/clitkConeBeamProjectImageGenericFilter.cxx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[clitk.git] / tools / clitkConeBeamProjectImageGenericFilter.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 CLITKCONEBEAMPROJECTIMAGEGENERICFILTER_CXX
19 #define CLITKCONEBEAMPROJECTIMAGEGENERICFILTER_CXX
20
21 /**
22  * @file   clitkConeBeamProjectImageGenericFilter.cxx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   Wed April 30  13:47:57 2008
25  * 
26  * @brief  
27  * 
28  * 
29  */
30
31 #include "clitkConeBeamProjectImageGenericFilter.h"
32
33 namespace clitk
34 {
35
36   //====================================================================
37   // Constructor
38   ConeBeamProjectImageGenericFilter::ConeBeamProjectImageGenericFilter()
39   {
40     m_Verbose=false;
41     m_InputFileName="";
42   }
43
44   //====================================================================
45   // Update
46   void ConeBeamProjectImageGenericFilter::Update()
47   {
48     //Read the PixelType and dimension of the input image
49     int Dimension;
50     std::string PixelType;
51     clitk::ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
52     if (Dimension == 3)
53       {
54         if (m_Verbose) std::cout  << "Input was detected to be "<< Dimension << "D and " << PixelType << "..." << std::endl;
55   
56         if(PixelType == "short"){  
57           if (m_Verbose) std::cout  << "Launching Projection in signed short..." << std::endl;
58           UpdateWithPixelType<signed short>(); 
59         }
60         
61 //      else if(PixelType == "unsigned_short"){  
62 //        if (m_Verbose) std::cout  << "Launching Projection in unsigned_short..." << std::endl;
63 //        UpdateWithPixelType<unsigned short>(); 
64 //      }
65         
66         else if (PixelType == "unsigned_char"){ 
67           if (m_Verbose) std::cout  << "Launching Projection in unsigned_char..." << std::endl;
68           UpdateWithPixelType<unsigned char>();
69         }
70         
71 //      else if (PixelType == "char"){ 
72 //        if (m_Verbose) std::cout  << "Launching Projection in signed_char..." << std::endl;
73 //        UpdateWithPixelType<signed char>();
74 //      }
75         else {
76           if (m_Verbose) std::cout  << "Launching Projection in float..." << std::endl;
77           UpdateWithPixelType<float>();
78         }
79       }
80     
81     else {itkExceptionMacro(<< "Input Image dimension is " << Dimension 
82                             << " but I can only work on 3D images.");
83     }
84   }
85   
86   //================================================================================
87   template <class  PixelType> void ConeBeamProjectImageGenericFilter::UpdateWithPixelType()
88   {
89     
90     //===================================================================
91     // Read the input image 
92     const unsigned int InputImageDimension=3;
93     typedef itk::Image<PixelType, InputImageDimension> InputImageType;
94     typedef itk::ImageFileReader<InputImageType> ImageReaderType;
95     typename  ImageReaderType::Pointer reader = ImageReaderType::New();
96     reader->SetFileName(m_InputFileName);
97     reader->Update();
98     typename InputImageType::Pointer input = reader->GetOutput();
99
100     // Define the output type
101     //JV always float?
102     typedef float OutputPixelType; 
103     const unsigned int OutputImageDimension=2;
104     typedef itk::Image<OutputPixelType, OutputImageDimension> OutputImageType;
105
106     // Create the ConeBeamProjectImageFilter
107     typedef clitk::ConeBeamProjectImageFilter<InputImageType, OutputImageType> ConeBeamProjectImageFilterType;
108     typename ConeBeamProjectImageFilterType::Pointer filter=ConeBeamProjectImageFilterType::New();
109
110     // Pass all the necessary parameters
111     filter->SetInput(input);
112     filter->SetVerbose(m_Verbose);
113     if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
114     
115     // Projection parameters
116     typename InputImageType::PointType iso;
117     unsigned int i;
118     if (m_ArgsInfo.iso_given)
119       for(i=0;i<InputImageDimension;i++)
120         {
121           iso[i]=m_ArgsInfo.iso_arg[i];
122           filter->SetIsoCenter(iso);
123         }
124     filter->SetSourceToScreen(m_ArgsInfo.screen_arg);
125     filter->SetSourceToAxis(m_ArgsInfo.axis_arg); 
126     filter->SetProjectionAngle(m_ArgsInfo.angle_arg);
127     if (m_ArgsInfo.matrix_given)
128       {
129         itk::Matrix<double,4,4> rt =ReadMatrix3D(m_ArgsInfo.matrix_arg);
130         filter->SetRigidTransformMatrix(rt);
131       }
132     filter->SetEdgePaddingValue(static_cast<OutputPixelType>(m_ArgsInfo.pad_arg));
133
134     DD(m_ArgsInfo.panel_position_arg);
135     if (m_ArgsInfo.panel_shift_given) // one should read the specific values for each angle in Frame.dbf
136       filter->SetPanelShift(m_ArgsInfo.panel_shift_arg[0], m_ArgsInfo.panel_shift_arg[1]);
137     else { // approximate panel positions hard coded values for the elekta synergy
138       if (strcmp(m_ArgsInfo.panel_position_arg,"small") ==0)
139         filter->SetPanelShift(0., 0.);
140       else if (strcmp(m_ArgsInfo.panel_position_arg,"medium") ==0)
141         filter->SetPanelShift(114.84, 0.); // VD : 120 , 0 ?
142       else if (strcmp(m_ArgsInfo.panel_position_arg,"large") ==0)
143         filter->SetPanelShift(190., 0.);
144       else assert(false); //Unsupported panel position
145     }
146     // Output image info
147     if (m_ArgsInfo.like_given)
148       {
149         typedef itk::ImageFileReader<OutputImageType> ReaderType;
150         ReaderType::Pointer reader2=ReaderType::New();
151         reader2->SetFileName(m_ArgsInfo.like_arg);
152         reader2->Update();
153
154         OutputImageType::Pointer image=reader2->GetOutput();
155         filter->SetOutputParametersFromImage(image);
156       }
157     else
158       {
159         if (m_ArgsInfo.origin_given)
160           {
161             OutputImageType::PointType origin;
162             for(i=0;i<OutputImageDimension;i++)
163               origin[i]=m_ArgsInfo.origin_arg[i];
164             filter->SetOutputOrigin(origin);
165           }
166         if (m_ArgsInfo.spacing_given)
167           {
168             OutputImageType::SpacingType spacing;
169             for(i=0;i<OutputImageDimension;i++)
170               spacing[i]=m_ArgsInfo.spacing_arg[i];
171             filter->SetOutputSpacing(spacing);
172           }
173         if (m_ArgsInfo.size_given)
174           {
175             OutputImageType::SizeType size;
176             for(i=0;i<OutputImageDimension;i++)
177               size[i]=m_ArgsInfo.size_arg[i];
178             filter->SetOutputSize(size);
179           }
180       }
181
182     //Go
183     filter->Update();
184     
185     //Get the output
186     OutputImageType::Pointer output=filter->GetOutput();
187  
188     //Write the output
189     typedef itk::ImageFileWriter<OutputImageType> OutputWriterType;
190     OutputWriterType::Pointer outputWriter = OutputWriterType::New();
191     outputWriter->SetInput(output);
192     outputWriter->SetFileName(m_ArgsInfo.output_arg); 
193     if (m_Verbose)std::cout<<"Writing projected image..."<<std::endl;
194     outputWriter->Update(); 
195   }
196
197
198   //====================================================================
199
200 }
201 #endif  //#define CLITKCONEBEAMPROJECTIMAGEGENERICFILTER_CXX