ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkShapedFloodFilledFunctionConditionalConstIterator.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 __itkShapedFloodFilledFunctionConditionalConstIterator_h
00019 #define __itkShapedFloodFilledFunctionConditionalConstIterator_h
00020 
00021 #include <queue>
00022 #include <vector>
00023 
00024 #include "itkSize.h"
00025 #include "itkConditionalConstIterator.h"
00026 #include "itkConnectedComponentAlgorithm.h"
00027 
00028 namespace itk
00029 {
00042 template< class TImage, class TFunction >
00043 class ITK_EXPORT ShapedFloodFilledFunctionConditionalConstIterator:
00044   public ConditionalConstIterator< TImage >
00045 {
00046 public:
00047 
00049   typedef ShapedFloodFilledFunctionConditionalConstIterator Self;
00050 
00052   typedef TFunction FunctionType;
00053 
00055   typedef typename TFunction::InputType FunctionInputType;
00056 
00058   typedef typename TImage::IndexType IndexType;
00059 
00061   typedef typename std::vector< IndexType > SeedsContainerType;
00062 
00064   typedef typename TImage::OffsetType OffsetType;
00065 
00067   typedef typename TImage::SizeType SizeType;
00068 
00070   typedef typename TImage::RegionType RegionType;
00071 
00073   typedef TImage ImageType;
00074 
00076   typedef typename TImage::InternalPixelType InternalPixelType;
00077 
00079   typedef typename TImage::PixelType PixelType;
00080 
00082   typedef typename itk::ShapedNeighborhoodIterator< ImageType > NeighborhoodIteratorType;
00083 
00088   itkStaticConstMacro(NDimensions, unsigned int, TImage::ImageDimension);
00089 
00093   ShapedFloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00094                                                     FunctionType *fnPtr,
00095                                                     IndexType startIndex);
00096 
00100   ShapedFloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00101                                                     FunctionType *fnPtr,
00102                                                     std::vector< IndexType > & startIndices);
00103 
00107   ShapedFloodFilledFunctionConditionalConstIterator(const ImageType *imagePtr,
00108                                                     FunctionType *fnPtr);
00109 
00114   void FindSeedPixel();
00115 
00117   void FindSeedPixels();
00118 
00120   void InitializeIterator();
00121 
00123   virtual ~ShapedFloodFilledFunctionConditionalConstIterator() {}
00124 
00126   virtual bool IsPixelIncluded(const IndexType & index) const = 0;
00127 
00130   Self & operator=(const Self & it)
00131   {
00132     this->m_Image = it.m_Image;     // copy the smart pointer
00133     this->m_Region = it.m_Region;
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   void ClearSeeds()
00164   {
00165     m_Seeds.clear();
00166   }
00167 
00170   void GoToBegin()
00171   {
00172     // Clear the queue
00173     while ( !m_IndexStack.empty() )
00174       {
00175       m_IndexStack.pop();
00176       }
00177 
00178     this->m_IsAtEnd = true;
00179     // Initialize the temporary image
00180     m_TempPtr->FillBuffer(
00181       NumericTraits< typename TTempImage::PixelType >::Zero
00182       );
00183 
00184     for ( unsigned int i = 0; i < m_Seeds.size(); i++ )
00185       {
00186       if ( this->m_Image->GetBufferedRegion().IsInside (m_Seeds[i])
00187            && this->IsPixelIncluded(m_Seeds[i]) )
00188         {
00189         // Push the seed onto the queue
00190         m_IndexStack.push(m_Seeds[i]);
00191 
00192         // Obviously, we're at the beginning
00193         this->m_IsAtEnd = false;
00194 
00195         // Mark the start index in the temp image as inside the
00196         // function, neighbor check incomplete
00197         m_TempPtr->SetPixel(m_Seeds[i], 2);
00198         }
00199       }
00200   }
00201 
00203   void operator++()
00204   { this->DoFloodStep(); }
00205 
00206   void DoFloodStep();
00207 
00208   virtual SmartPointer< FunctionType > GetFunction() const
00209   {
00210     return m_Function;
00211   }
00212 
00217   void SetFullyConnected(const bool _arg);
00218 
00219   bool GetFullyConnected() const;
00220 
00221   itkBooleanMacro(FullyConnected);
00222 
00223   virtual const SeedsContainerType &GetSeeds() const
00224   {
00225     return m_Seeds;
00226   }
00227 protected: //made protected so other iterators can access
00229   SmartPointer< FunctionType > m_Function;
00230 
00236   typedef Image< unsigned char, itkGetStaticConstMacro(NDimensions) > TTempImage;
00237 
00238   typename TTempImage::Pointer m_TempPtr;
00239 
00241   SeedsContainerType m_Seeds;
00242 
00244   typename ImageType::PointType m_ImageOrigin;
00245 
00247   typename ImageType::SpacingType m_ImageSpacing;
00248 
00250   NeighborhoodIteratorType m_NeighborhoodIterator;
00251 
00253   RegionType m_ImageRegion;
00254 
00256   std::queue< IndexType > m_IndexStack;
00257 
00259   FunctionInputType m_LocationVector;
00260 
00263   bool m_FoundUncheckedNeighbor;
00264 
00266   bool m_IsValidIndex;
00267 
00273   bool m_FullyConnected;
00274 };
00275 } // end namespace itk
00277 
00278 #ifndef ITK_MANUAL_INSTANTIATION
00279 #include "itkShapedFloodFilledFunctionConditionalConstIterator.hxx"
00280 #endif
00281 
00282 #endif
00283