]> Creatis software - clitk.git/blob - filters/clitkImageFillRegionGenericFilter.txx
upport
[clitk.git] / filters / clitkImageFillRegionGenericFilter.txx
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_TXX
14 #define CLITKIMAGEFILLREGIONGENERICFILTER_TXX
15
16 /*-------------------------------------------------
17  * @file   clitkImageFillRegionGenericFilter.txx
18  * @author Cristina Gimenez <cristina.gs@gmail.com>
19  * @date   9 August 2006
20  * 
21  -------------------------------------------------*/
22
23 //--------------------------------------------------------------------
24 template<unsigned int Dim>
25 void ImageFillRegionGenericFilter::Update_WithDim() { 
26 #define TRY_TYPE(TYPE)                                                  \
27   if (IsSameType<TYPE>(mPixelTypeName)) { Update_WithDimAndPixelType<Dim, TYPE>(); return; } 
28   // TRY_TYPE(signed char);
29   // TRY_TYPE(uchar);
30   TRY_TYPE(short);
31   //TRY_TYPE(ushort);
32   // TRY_TYPE(int);
33   //   TRY_TYPE(unsigned int); 
34   TRY_TYPE(float);
35   // TRY_TYPE(double);
36 #undef TRY_TYPE
37
38   std::string list = CreateListOfTypes<uchar, short, ushort, int, uint, float, double>();
39   std::cerr << "Error, I don't know the type '" << mPixelTypeName << "' for the input image '"
40             << mInputFilenames[0] << "'." << std::endl << "Known types are " << list << std::endl;
41   exit(0);
42 }
43 //--------------------------------------------------------------------
44
45 //--------------------------------------------------------------------
46 template<unsigned int Dim, class PixelType>
47 void ImageFillRegionGenericFilter::Update_WithDimAndPixelType() {
48
49   // Spheric region
50   if (mSphericRegion) return Update_WithDimAndPixelType_SphericRegion<Dim,PixelType>();
51
52   // Read input
53   typedef itk::Image<PixelType,Dim> ImageType;
54   typename ImageType::Pointer input = GetInput<ImageType>(0);
55
56   // Get pixel value in correct type
57   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
58
59   // Get region
60   typedef typename ImageType::RegionType RegionType;
61   typedef typename ImageType::SizeType SizeType;
62   typedef typename ImageType::IndexType IndexType;
63   RegionType region;
64   SizeType size;
65   IndexType start;
66   for(unsigned int i=0; i<Dim; i++) {
67     size[i] = mSize[i];
68     start[i] = mStart[i];
69   }
70   region.SetSize(size);
71   region.SetIndex(start);
72
73   // Build iterator
74   typedef itk::ImageRegionIterator<ImageType> IteratorType;
75   IteratorType it(input, region);
76   it.GoToBegin();
77   while (!it.IsAtEnd()) {
78     it.Set(value);
79     ++it;
80   }
81
82   // Write results
83   SetNextOutput<ImageType>(input);
84 }
85 //--------------------------------------------------------------------
86
87 //--------------------------------------------------------------------
88 template<unsigned int Dim, class PixelType>
89 void ImageFillRegionGenericFilter::Update_WithDimAndPixelType_SphericRegion() {
90
91   // Read input
92   typedef itk::Image<PixelType,Dim> ImageType;
93   //typename ImageType::Pointer input = clitk::readImage<ImageType>(mInputFilenames[0], mIOVerbose);
94   typename ImageType::Pointer input = GetInput<ImageType>(0);
95
96   // Get pixel value in correct type
97   PixelType value = PixelTypeDownCast<double, PixelType>(mPixelValue); 
98
99   // Centered?
100   if(m_IsCentered)
101     {
102       typename ImageType::SizeType size= input->GetLargestPossibleRegion().GetSize();
103       typename ImageType::SpacingType spacing= input->GetSpacing();
104       typename ImageType::PointType origin= input->GetOrigin();
105       mCenter.resize(Dim);
106       for (unsigned int i=0; i<Dim; i++)
107         mCenter[i]=origin[i]+(double)size[i]/2*spacing[i];
108     }
109
110   // Build iterator
111   typedef itk::ImageRegionIteratorWithIndex<ImageType> IteratorType;
112   IteratorType it(input, input->GetLargestPossibleRegion());
113   it.GoToBegin();
114
115   typename ImageType::PointType point; 
116   //typename itk::Vector<double, Dim> distance; 
117   typename ImageType::IndexType index;
118   //bool inside;
119   double distance;
120   
121   while (!it.IsAtEnd())
122     {    
123       //      inside=true;
124       index=it.GetIndex();
125       input->TransformIndexToPhysicalPoint(index, point);
126       distance=0.0;      
127       for(unsigned int i=0; i<Dim; i++)
128         distance+=powf( ( (mCenter[i]-point[i])/mRadius[i] ), 2);
129         
130       //  inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
131       //          distance[i]=mCenter[i]-point[i];
132       //          inside= ( (fabs(distance[i])<fabs(mRadius[i])) && inside );
133       //        }
134       
135       if (distance<1)
136         it.Set(value);
137       ++it;
138     }
139
140   // Write results
141   SetNextOutput<ImageType>(input);
142 }
143
144 //--------------------------------------------------------------------
145
146 #endif  //#define CLITKIMAGEFILLREGIONGENERICFILTER_TXX