]> Creatis software - clitk.git/blob - filters/clitkImageFillRegionGenericFilter.cxx
- correct bug: string pixeltype is different in ITK and VTK
[clitk.git] / filters / clitkImageFillRegionGenericFilter.cxx
1 /*=========================================================================
2
3   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
4   l'Image). All rights reserved. See Doc/License.txt or
5   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
6                                                                                 
7      This software is distributed WITHOUT ANY WARRANTY; without even
8      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9      PURPOSE.  See the above copyright notices for more information.
10                                                                              
11 =========================================================================*/
12
13 #ifndef CLITKIMAGEFILLREGIONGENERICFILTER_CXX
14 #define CLITKIMAGEFILLREGIONGENERICFILTER_CXX
15
16 /**
17  -------------------------------------------------------------------
18  * @file   clitkImageFillRegionGenericFilter.cxx
19  * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
20  * @date   23 Feb 2008
21
22  * @brief  
23  -------------------------------------------------------------------*/
24
25 #include "clitkImageFillRegionGenericFilter.h"
26
27 //--------------------------------------------------------------------
28 clitk::ImageFillRegionGenericFilter::ImageFillRegionGenericFilter():
29   clitk::ImageToImageGenericFilter<Self>("ImageFillRegion") {
30   InitializeImageType<2>();
31   InitializeImageType<3>();   
32   mPixelValue = 0;
33   m_IsCentered=false;
34   mSphericRegion=false;  
35 }
36 //--------------------------------------------------------------------
37
38
39 //--------------------------------------------------------------------
40 template<unsigned int Dim>
41 void clitk::ImageFillRegionGenericFilter::InitializeImageType() {      
42   // ADD_IMAGE_TYPE(Dim, char);
43   ADD_IMAGE_TYPE(Dim, short);
44   // ADD_IMAGE_TYPE(Dim, unsigned short);
45 //   ADD_IMAGE_TYPE(Dim, int);
46   ADD_IMAGE_TYPE(Dim, float);
47   // ADD_IMAGE_TYPE(Dim, double);
48 }
49 //--------------------------------------------------------------------
50
51
52
53 //--------------------------------------------------------------------
54 template<class ImageType>
55 void clitk::ImageFillRegionGenericFilter::UpdateWithInputImageType() {
56
57   // Typedef
58   typedef typename ImageType::PixelType PixelType;
59   static unsigned int Dim = ImageType::ImageDimension;
60
61   // Spheric region
62   if (mSphericRegion) return Update_WithDimAndPixelType_SphericRegion<ImageType::ImageDimension,PixelType>();
63
64   // Read input
65   typename ImageType::Pointer input = GetInput<ImageType>(0);
66
67   // Get pixel value in correct type
68   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
69
70   // Get region
71   typedef typename ImageType::RegionType RegionType;
72   typedef typename ImageType::SizeType SizeType;
73   typedef typename ImageType::IndexType IndexType;
74   RegionType region;
75   SizeType size;
76   IndexType start;
77   for(unsigned int i=0; i<Dim; i++) {
78     size[i] = mSize[i];
79     start[i] = mStart[i];
80   }
81   region.SetSize(size);
82   region.SetIndex(start);
83
84   // Build iterator
85   typedef itk::ImageRegionIterator<ImageType> IteratorType;
86   IteratorType it(input, region);
87   it.GoToBegin();
88   while (!it.IsAtEnd()) {
89     it.Set(value);
90     ++it;
91   }
92
93   // Write results
94   SetNextOutput<ImageType>(input);
95 }
96 //--------------------------------------------------------------------
97
98 //--------------------------------------------------------------------
99 template<unsigned int Dim, class PixelType>
100 void clitk::ImageFillRegionGenericFilter::Update_WithDimAndPixelType_SphericRegion() {
101
102   // Read input
103   typedef itk::Image<PixelType,Dim> ImageType;
104   //typename ImageType::Pointer input = clitk::readImage<ImageType>(mInputFilenames[0], mIOVerbose);
105   typename ImageType::Pointer input = GetInput<ImageType>(0);
106
107   // Get pixel value in correct type
108   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
109
110   // Centered?
111   if(m_IsCentered)
112     {
113       typename ImageType::SizeType size= input->GetLargestPossibleRegion().GetSize();
114       typename ImageType::SpacingType spacing= input->GetSpacing();
115       typename ImageType::PointType origin= input->GetOrigin();
116       mCenter.resize(Dim);
117       for (unsigned int i=0; i<Dim; i++)
118         mCenter[i]=origin[i]+(double)size[i]/2*spacing[i];
119     }
120
121   // Build iterator
122   typedef itk::ImageRegionIteratorWithIndex<ImageType> IteratorType;
123   IteratorType it(input, input->GetLargestPossibleRegion());
124   it.GoToBegin();
125
126   typename ImageType::PointType point; 
127   //typename itk::Vector<double, Dim> distance; 
128   typename ImageType::IndexType index;
129   //bool inside;
130   double distance;
131   
132   while (!it.IsAtEnd())
133     {    
134       //      inside=true;
135       index=it.GetIndex();
136       input->TransformIndexToPhysicalPoint(index, point);
137       distance=0.0;      
138       for(unsigned int i=0; i<Dim; i++)
139         distance+=powf( ( (mCenter[i]-point[i])/mRadius[i] ), 2);
140         
141       //  inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
142       //          distance[i]=mCenter[i]-point[i];
143       //          inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
144       //        }
145       
146       if (distance<1)
147         it.Set(value);
148       ++it;
149     }
150
151   // Write results
152   SetNextOutput<ImageType>(input);
153 }
154
155 //--------------------------------------------------------------------
156
157
158
159 //--------------------------------------------------------------------
160 void clitk::ImageFillRegionGenericFilter::SetSphericRegion(std::vector<double> &  radius, 
161                                                            std::vector<double> & center) 
162 {
163   mRadius.clear(); 
164   mRadius.resize(radius.size());
165   std::copy(radius.begin(), radius.end(), mRadius.begin());
166   mCenter.clear();
167   mCenter.resize(center.size());
168   std::copy(center.begin(), center.end(), mCenter.begin());
169   mSphericRegion = true;
170   m_IsCentered=false;
171 }
172
173 void clitk::ImageFillRegionGenericFilter::SetSphericRegion(std::vector<double> & radius) {
174   mRadius.clear(); 
175   mRadius.resize(radius.size());
176   std::copy(radius.begin(), radius.end(), mRadius.begin());
177   m_IsCentered=true;
178   mSphericRegion = true;
179 }
180 //--------------------------------------------------------------------
181
182
183 #endif //define CLITKIMAGEFILLREGIONGENERICFILTER_CXX