]> Creatis software - clitk.git/blob - filters/clitkImageFillRegionGenericFilter.cxx
added the new headers
[clitk.git] / filters / clitkImageFillRegionGenericFilter.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://oncora1.lyon.fnclcc.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 CLITKIMAGEFILLREGIONGENERICFILTER_CXX
19 #define CLITKIMAGEFILLREGIONGENERICFILTER_CXX
20 /**
21  -------------------------------------------------------------------
22  * @file   clitkImageFillRegionGenericFilter.cxx
23  * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24  * @date   23 Feb 2008
25
26  * @brief  
27  -------------------------------------------------------------------*/
28
29 #include "clitkImageFillRegionGenericFilter.h"
30
31 //--------------------------------------------------------------------
32 clitk::ImageFillRegionGenericFilter::ImageFillRegionGenericFilter():
33   clitk::ImageToImageGenericFilter<Self>("ImageFillRegion") {
34   InitializeImageType<2>();
35   InitializeImageType<3>();   
36   mPixelValue = 0;
37   m_IsCentered=false;
38   mSphericRegion=false;  
39 }
40 //--------------------------------------------------------------------
41
42
43 //--------------------------------------------------------------------
44 template<unsigned int Dim>
45 void clitk::ImageFillRegionGenericFilter::InitializeImageType() {      
46   // ADD_IMAGE_TYPE(Dim, char);
47   ADD_IMAGE_TYPE(Dim, short);
48   // ADD_IMAGE_TYPE(Dim, unsigned short);
49 //   ADD_IMAGE_TYPE(Dim, int);
50   ADD_IMAGE_TYPE(Dim, float);
51   // ADD_IMAGE_TYPE(Dim, double);
52 }
53 //--------------------------------------------------------------------
54
55
56
57 //--------------------------------------------------------------------
58 template<class ImageType>
59 void clitk::ImageFillRegionGenericFilter::UpdateWithInputImageType() {
60
61   // Typedef
62   typedef typename ImageType::PixelType PixelType;
63   static unsigned int Dim = ImageType::ImageDimension;
64
65   // Spheric region
66   if (mSphericRegion) return Update_WithDimAndPixelType_SphericRegion<ImageType::ImageDimension,PixelType>();
67
68   // Read input
69   typename ImageType::Pointer input = GetInput<ImageType>(0);
70
71   // Get pixel value in correct type
72   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
73
74   // Get region
75   typedef typename ImageType::RegionType RegionType;
76   typedef typename ImageType::SizeType SizeType;
77   typedef typename ImageType::IndexType IndexType;
78   RegionType region;
79   SizeType size;
80   IndexType start;
81   for(unsigned int i=0; i<Dim; i++) {
82     size[i] = mSize[i];
83     start[i] = mStart[i];
84   }
85   region.SetSize(size);
86   region.SetIndex(start);
87
88   // Build iterator
89   typedef itk::ImageRegionIterator<ImageType> IteratorType;
90   IteratorType it(input, region);
91   it.GoToBegin();
92   while (!it.IsAtEnd()) {
93     it.Set(value);
94     ++it;
95   }
96
97   // Write results
98   SetNextOutput<ImageType>(input);
99 }
100 //--------------------------------------------------------------------
101
102 //--------------------------------------------------------------------
103 template<unsigned int Dim, class PixelType>
104 void clitk::ImageFillRegionGenericFilter::Update_WithDimAndPixelType_SphericRegion() {
105
106   // Read input
107   typedef itk::Image<PixelType,Dim> ImageType;
108   //typename ImageType::Pointer input = clitk::readImage<ImageType>(mInputFilenames[0], mIOVerbose);
109   typename ImageType::Pointer input = GetInput<ImageType>(0);
110
111   // Get pixel value in correct type
112   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
113
114   // Centered?
115   if(m_IsCentered)
116     {
117       typename ImageType::SizeType size= input->GetLargestPossibleRegion().GetSize();
118       typename ImageType::SpacingType spacing= input->GetSpacing();
119       typename ImageType::PointType origin= input->GetOrigin();
120       mCenter.resize(Dim);
121       for (unsigned int i=0; i<Dim; i++)
122         mCenter[i]=origin[i]+(double)size[i]/2*spacing[i];
123     }
124
125   // Build iterator
126   typedef itk::ImageRegionIteratorWithIndex<ImageType> IteratorType;
127   IteratorType it(input, input->GetLargestPossibleRegion());
128   it.GoToBegin();
129
130   typename ImageType::PointType point; 
131   //typename itk::Vector<double, Dim> distance; 
132   typename ImageType::IndexType index;
133   //bool inside;
134   double distance;
135   
136   while (!it.IsAtEnd())
137     {    
138       //      inside=true;
139       index=it.GetIndex();
140       input->TransformIndexToPhysicalPoint(index, point);
141       distance=0.0;      
142       for(unsigned int i=0; i<Dim; i++)
143         distance+=powf( ( (mCenter[i]-point[i])/mRadius[i] ), 2);
144         
145       //  inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
146       //          distance[i]=mCenter[i]-point[i];
147       //          inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
148       //        }
149       
150       if (distance<1)
151         it.Set(value);
152       ++it;
153     }
154
155   // Write results
156   SetNextOutput<ImageType>(input);
157 }
158
159 //--------------------------------------------------------------------
160
161
162
163 //--------------------------------------------------------------------
164 void clitk::ImageFillRegionGenericFilter::SetSphericRegion(std::vector<double> &  radius, 
165                                                            std::vector<double> & center) 
166 {
167   mRadius.clear(); 
168   mRadius.resize(radius.size());
169   std::copy(radius.begin(), radius.end(), mRadius.begin());
170   mCenter.clear();
171   mCenter.resize(center.size());
172   std::copy(center.begin(), center.end(), mCenter.begin());
173   mSphericRegion = true;
174   m_IsCentered=false;
175 }
176
177 void clitk::ImageFillRegionGenericFilter::SetSphericRegion(std::vector<double> & radius) {
178   mRadius.clear(); 
179   mRadius.resize(radius.size());
180   std::copy(radius.begin(), radius.end(), mRadius.begin());
181   m_IsCentered=true;
182   mSphericRegion = true;
183 }
184 //--------------------------------------------------------------------
185
186
187 #endif //define CLITKIMAGEFILLREGIONGENERICFILTER_CXX