KWStyle - itkFloodFilledFunctionConditionalConstIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkFloodFilledFunctionConditionalConstIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:35 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkFloodFilledFunctionConditionalConstIterator_h
18 #define __itkFloodFilledFunctionConditionalConstIterator_h
19
20 #include <queue>
21 #include <vector>
22
23 #include "itkIndex.h"
24 #include "itkSize.h"
25 #include "itkConditionalConstIterator.h"
26 #include "itkImage.h"
27
28 namespace itk
29 {
30
31 /**
32  * \class FloodFilledFunctionConditionalConstIterator
33  * \brief Iterates over a flood-filled spatial function. 
34  *
35  * \ingroup ImageIterators
36  *
37  */
38 template<class TImage, class TFunction>
39 LEN class ITK_EXPORT FloodFilledFunctionConditionalConstIterator: public ConditionalConstIterator<TImage>
40 {
41 public:
42   /** Standard class typedefs. */
43   typedef FloodFilledFunctionConditionalConstIterator Self;
44
45   /** Type of function */
46   typedef TFunction FunctionType;
47
48   /** Type of vector used to store location info in the spatial function */
49   typedef typename TFunction::InputType FunctionInputType;
50
51   /** Index typedef support. */
52   typedef typename TImage::IndexType  IndexType;
53
54   /** Size typedef support. */
55   typedef typename TImage::SizeType    SizeType;
56
57   /** Region typedef support */
58   typedef typename TImage::RegionType    RegionType;
59
60   /** Image typedef support. */
61   typedef TImage   ImageType;
62
63   /** Internal Pixel Type */
64   typedef typename TImage::InternalPixelType   InternalPixelType;
65
66   /** External Pixel Type */
67   typedef typename TImage::PixelType   PixelType;
68
69   /** Dimension of the image the iterator walks.  This constant is needed so
70    * that functions that are templated over image iterator type (as opposed to
71    * being templated over pixel type and dimension) can have compile time
72    * access to the dimension of the image that the iterator walks. */
73   itkStaticConstMacro(NDimensions, unsigned int, TImage::ImageDimension);
74
75   /** Constructor establishes an iterator to walk a particular image and a
76    * particular region of that image. This version of the constructor uses
77    * an explicit seed pixel for the flood fill, the "startIndex" */
78   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
79                                      FunctionType *fnPtr,
80                                      IndexType startIndex);
81
82   /** Constructor establishes an iterator to walk a particular image and a
83    * particular region of that image. This version of the constructor uses
84    * a list of seed pixels for the flood fill */
85   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
86                                      FunctionType *fnPtr,
87                                      std::vector<IndexType> & startIndices);
88
89   /** Constructor establishes an iterator to walk a particular image and a
90    * particular region of that image. This version of the constructor
91    * should be used when the seed pixel is unknown */
92   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
93                                               FunctionType *fnPtr);
94
95   /** Automatically find a seed pixel and set m_StartIndex. Does nothing
96    * if a seed pixel isn't found. A seed pixel is determined by
97 WCM    * traversing the input image's image's LargestPossibleRegion and
98    * applying the IsPixelIncluded() test.*/
99   void FindSeedPixel();
100
101   /** Automatically find all seed pixels. */
102   void FindSeedPixels();
103
104   /** Initializes the iterator, called from constructor */
105   void InitializeIterator();
106
107   /** Default Destructor. */
108   virtual ~FloodFilledFunctionConditionalConstIterator() {};
109
110   /** Compute whether the index of interest should be included in the flood */
111   virtual bool IsPixelIncluded(const IndexType & index) const = 0;
112   
113   /** operator= is provided to make sure the handle to the image is properly
114    * reference counted. */
115   Self &operator=(const Self& it)
116 IND **{
117     this->m_Image = it.m_Image;     // copy the smart pointer
118     this->m_Region = it.m_Region;
119     return *this;
120 IND **}
121   
122   /** Get the dimension (size) of the index. */
123   static unsigned int GetIteratorDimension() 
124     {return TImage::ImageDimension;}
125
126   /** Get the index. This provides a read only reference to the index.
127    * This causes the index to be calculated from pointer arithmetic and is
128    * therefore an expensive operation.
129    * \sa SetIndex */
130   const IndexType GetIndex()
131     { return m_IndexStack.front();}
132
133   /** Get the pixel value */
134   const PixelType & Get(void) const
135     { return this->m_Image->GetPixel(m_IndexStack.front() ); }
136  
137   /** Is the iterator at the end of the region? */
138   bool IsAtEnd()
139     { return this->m_IsAtEnd; };
140
141   /** Put more seeds on the list */
142   void AddSeed ( const IndexType seed )
143 IND **{
144     m_StartIndices.push_back ( seed );
145 IND **};
146
147   /** Clear all the seeds */
148   void ClearSeeds ()
149 IND **{
150     m_StartIndices.clear();
151 IND **};
152   
153 IND */** Move an iterator to the beginning of the region. "Begin" is
154 IND *** defined as the first pixel in the region. */
155   void GoToBegin()
156     {
157     // Clear the queue
158     while (!m_IndexStack.empty())
159       {
160       m_IndexStack.pop();
161       }
162
163     this->m_IsAtEnd = true;
164     // Initialize the temporary image
165 LEN     tempPtr->FillBuffer(NumericTraits<ITK_TYPENAME TTempImage::PixelType>::Zero);
166     
167     for ( unsigned int i = 0; i < m_StartIndices.size(); i++ )
168       {
169       if( this->m_Image->GetBufferedRegion().IsInside ( m_StartIndices[i] ) &&
170 IND **********this->IsPixelIncluded(m_StartIndices[i]) )
171         {
172         // Push the seed onto the queue
173         m_IndexStack.push(m_StartIndices[i]);
174         
175         // Obviously, we're at the beginning
176         this->m_IsAtEnd = false;
177         
178 LEN         // Mark the start index in the temp image as inside the function, neighbor check incomplete
179         tempPtr->SetPixel(m_StartIndices[i], 2);
180         }
181       }
182     };
183
184   /** Walk forward one index */
185   void operator++()
186     { this->DoFloodStep(); }
187
188   void DoFloodStep();
189   
190   virtual SmartPointer<FunctionType> GetFunction() const
191 IND **{
192     return m_Function;
193 IND **}
194
195
196 protected: //made protected so other iterators can access 
197   /** Smart pointer to the function we're evaluating */
198   SmartPointer<FunctionType> m_Function;
199
200   /** A temporary image used for storing info about indices
201    * 0 = pixel has not yet been processed
202    * 1 = pixel is not inside the function
203    * 2 = pixel is inside the function, neighbor check incomplete
204    * 3 = pixel is inside the function, neighbor check complete */
205   typedef Image<unsigned char, itkGetStaticConstMacro(NDimensions)> TTempImage;
206   typename TTempImage::Pointer tempPtr;
207   
208   /** A list of locations to start the recursive fill */
209   std::vector<IndexType> m_StartIndices;
210
211   /** The origin of the source image */
212   typename ImageType::PointType m_ImageOrigin;
213   
214   /** The spacing of the source image */
215   typename ImageType::SpacingType m_ImageSpacing;
216
217   /** Region of the source image */
218   RegionType   m_ImageRegion;
219
220   /** Stack used to hold the path of the iterator through the image */
221   std::queue<IndexType> m_IndexStack;
222
223   /** Location vector used in the flood algorithm */
224   FunctionInputType m_LocationVector;
225
226 LEN   /** Indicates whether or not we've found a neighbor that needs to be checked. */
227   bool m_FoundUncheckedNeighbor;
228
229   /** Indicates whether or not an index is valid (inside an image)/ */
230   bool m_IsValidIndex;
231 };
232
233 // end namespace itk
234
235 #ifndef ITK_MANUAL_INSTANTIATION
236 #include "itkFloodFilledFunctionConditionalConstIterator.txx"
237 #endif
238
239 #endif 
240

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:08PM
© Kitware Inc.