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