ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkFloodFilledFunctionConditionalConstIterator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkFloodFilledFunctionConditionalConstIterator_h
00019 #define __itkFloodFilledFunctionConditionalConstIterator_h
00020 
00021 #include <queue>
00022 #include <vector>
00023 
00024 #include "itkSize.h"
00025 #include "itkConditionalConstIterator.h"
00026 #include "itkImage.h"
00027 
00028 namespace itk
00029 {
00038 template< class TImage, class TFunction >
00039 class ITK_EXPORT FloodFilledFunctionConditionalConstIterator:
00040   public ConditionalConstIterator< TImage >
00041 {
00042 public:
00043 
00045   typedef FloodFilledFunctionConditionalConstIterator Self;
00046 
00048   typedef TFunction FunctionType;
00049 
00051   typedef typename TFunction::InputType FunctionInputType;
00052 
00054   typedef typename TImage::IndexType IndexType;
00055 
00057   typedef typename std::vector<IndexType> SeedsContainerType;
00058 
00060   typedef typename TImage::SizeType SizeType;
00061 
00063   typedef typename TImage::RegionType RegionType;
00064 
00066   typedef TImage ImageType;
00067 
00069   typedef typename TImage::InternalPixelType InternalPixelType;
00070 
00072   typedef typename TImage::PixelType PixelType;
00073 
00078   itkStaticConstMacro(NDimensions, unsigned int, TImage::ImageDimension);
00079 
00083   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00084                                               FunctionType *fnPtr,
00085                                               IndexType startIndex);
00086 
00090   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00091                                               FunctionType *fnPtr,
00092                                               std::vector< IndexType > & startIndices);
00093 
00097   FloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00098                                               FunctionType *fnPtr);
00099 
00104   void FindSeedPixel();
00105 
00107   void FindSeedPixels();
00108 
00110   void InitializeIterator();
00111 
00113   virtual ~FloodFilledFunctionConditionalConstIterator() {}
00114 
00116   virtual bool IsPixelIncluded(const IndexType & index) const = 0;
00117 
00120   Self & operator=(const Self & it)
00121   {
00122     this->m_Image = it.m_Image;     // copy the smart pointer
00123     this->m_Region = it.m_Region;
00124     this->m_Function = it.m_Function;
00125     this->m_TemporaryPointer = it.m_TemporaryPointer;
00126     this->m_Seeds = it.m_Seeds;
00127     this->m_ImageOrigin = it.m_ImageOrigin;
00128     this->m_ImageSpacing = it.m_ImageSpacing;
00129     this->m_ImageRegion = it.m_ImageRegion;
00130     this->m_IndexStack = it.m_IndexStack;
00131     this->m_LocationVector = it.m_LocationVector;
00132     this->m_FoundUncheckedNeighbor = it.m_FoundUncheckedNeighbor;
00133     this->m_IsValidIndex = it.m_IsValidIndex;
00134     return *this;
00135   }
00136 
00138   static unsigned int GetIteratorDimension()
00139   { return TImage::ImageDimension; }
00140 
00145   const IndexType GetIndex()
00146   { return m_IndexStack.front(); }
00147 
00149   const PixelType & Get(void) const
00150   { return this->m_Image->GetPixel( m_IndexStack.front() ); }
00151 
00153   bool IsAtEnd()
00154   { return this->m_IsAtEnd; }
00155 
00157   void AddSeed(const IndexType &seed)
00158   {
00159     m_Seeds.push_back (seed);
00160   }
00161 
00163   virtual const SeedsContainerType &GetSeeds() const
00164   {
00165     return m_Seeds;
00166   }
00167 
00169   void ClearSeeds()
00170   {
00171     m_Seeds.clear();
00172   }
00173 
00176   void GoToBegin()
00177   {
00178     // Clear the queue
00179     while ( !m_IndexStack.empty() )
00180       {
00181       m_IndexStack.pop();
00182       }
00183 
00184     this->m_IsAtEnd = true;
00185     // Initialize the temporary image
00186     m_TemporaryPointer->FillBuffer(
00187       NumericTraits< typename TTempImage::PixelType >::Zero
00188       );
00189 
00190     for ( unsigned int i = 0; i < m_Seeds.size(); i++ )
00191       {
00192       if ( this->m_Image->GetBufferedRegion().IsInside (m_Seeds[i])
00193            && this->IsPixelIncluded(m_Seeds[i]) )
00194         {
00195         // Push the seed onto the queue
00196         m_IndexStack.push(m_Seeds[i]);
00197 
00198         // Obviously, we're at the beginning
00199         this->m_IsAtEnd = false;
00200 
00201         // Mark the start index in the temp image as inside the
00202         // function, neighbor check incomplete
00203         m_TemporaryPointer->SetPixel(m_Seeds[i], 2);
00204         }
00205       }
00206   }
00207 
00209   void operator++()
00210   { this->DoFloodStep(); }
00211 
00212   void DoFloodStep();
00213 
00214   virtual SmartPointer< FunctionType > GetFunction() const
00215   {
00216     return m_Function;
00217   }
00218 
00219 protected: //made protected so other iterators can access
00221   SmartPointer< FunctionType > m_Function;
00222 
00228   typedef Image< unsigned char, itkGetStaticConstMacro(NDimensions) > TTempImage;
00229   typename TTempImage::Pointer m_TemporaryPointer;
00230 
00232   SeedsContainerType m_Seeds;
00233 
00235   typename ImageType::PointType m_ImageOrigin;
00236 
00238   typename ImageType::SpacingType m_ImageSpacing;
00239 
00241   RegionType m_ImageRegion;
00242 
00244   std::queue< IndexType > m_IndexStack;
00245 
00247   FunctionInputType m_LocationVector;
00248 
00251   bool m_FoundUncheckedNeighbor;
00252 
00254   bool m_IsValidIndex;
00255 };
00256 } // end namespace itk
00257 
00258 // Define instantiation macro for this template.
00259 #define ITK_TEMPLATE_FloodFilledFunctionConditionalConstIterator(_, EXPORT, TypeX, TypeY)     \
00260   namespace itk                                                                               \
00261   {                                                                                           \
00262   _( 2 ( class EXPORT FloodFilledFunctionConditionalConstIterator< ITK_TEMPLATE_2 TypeX > ) ) \
00263   namespace Templates                                                                         \
00264   {                                                                                           \
00265   typedef FloodFilledFunctionConditionalConstIterator< ITK_TEMPLATE_2 TypeX >                 \
00266   FloodFilledFunctionConditionalConstIterator##TypeY;                                       \
00267   }                                                                                           \
00268   }
00269 
00270 #if ITK_TEMPLATE_EXPLICIT
00271 #include "Templates/itkFloodFilledFunctionConditionalConstIterator+-.h"
00272 #endif
00273 
00274 #if ITK_TEMPLATE_TXX
00275 #include "itkFloodFilledFunctionConditionalConstIterator.hxx"
00276 #endif
00277 
00278 #endif
00279