ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkConstShapedNeighborhoodIterator.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 __itkConstShapedNeighborhoodIterator_h
00019 #define __itkConstShapedNeighborhoodIterator_h
00020 
00021 #include <vector>
00022 #include <list>
00023 #include "itkNeighborhoodIterator.h"
00024 
00025 namespace itk
00026 {
00070 template< class TImage,  class TBoundaryCondition =
00071             ZeroFluxNeumannBoundaryCondition< TImage > >
00072 class ITK_EXPORT ConstShapedNeighborhoodIterator:
00073   private NeighborhoodIterator< TImage, TBoundaryCondition >
00074 {
00075 public:
00076 
00078   typedef typename TImage::InternalPixelType InternalPixelType;
00079   typedef typename TImage::PixelType         PixelType;
00080 
00082   itkStaticConstMacro(Dimension, unsigned int, TImage::ImageDimension);
00083 
00085   typedef ConstShapedNeighborhoodIterator                    Self;
00086   typedef NeighborhoodIterator< TImage, TBoundaryCondition > Superclass;
00087 
00089   typedef typename Superclass::OffsetType      OffsetType;
00090   typedef typename OffsetType::OffsetValueType OffsetValueType;
00091   typedef typename Superclass::RadiusType      RadiusType;
00092   typedef typename Superclass::SizeType        SizeType;
00093   typedef typename SizeType::SizeValueType     SizeValueType;
00094 
00096   typedef TImage                                     ImageType;
00097   typedef typename TImage::RegionType                RegionType;
00098   typedef Index< itkGetStaticConstMacro(Dimension) > IndexType;
00099   typedef typename IndexType::IndexValueType         IndexValueType;
00100   typedef Neighborhood< PixelType, itkGetStaticConstMacro(Dimension) >
00101   NeighborhoodType;
00103 
00104   typedef typename NeighborhoodType::NeighborIndexType  NeighborIndexType;
00105 
00108   typedef std::list< NeighborIndexType >             IndexListType;
00109 
00110   typedef typename IndexListType::iterator        IndexListIterator;
00111   typedef typename IndexListType::const_iterator  IndexListConstIterator;
00112 
00114   typedef TBoundaryCondition BoundaryConditionType;
00115 
00117   typedef ImageBoundaryCondition< ImageType > *ImageBoundaryConditionPointerType;
00118 
00120   struct ConstIterator {
00121     ConstIterator() { m_NeighborhoodIterator = 0; }
00122     ConstIterator(Self *s)
00123     {
00124       m_NeighborhoodIterator = s;
00125       this->GoToBegin();
00126     }
00128 
00129     virtual ~ConstIterator() {}
00130     const ConstIterator & operator=(const ConstIterator & o)
00131     {
00132       m_NeighborhoodIterator = o.m_NeighborhoodIterator;
00133       m_ListIterator = o.m_ListIterator;
00134       return *this;
00135     }
00136 
00137     ConstIterator(const ConstIterator & o)
00138     {
00139       m_NeighborhoodIterator = o.m_NeighborhoodIterator;
00140       m_ListIterator = o.m_ListIterator;
00141     }
00142 
00143     void operator++(int)
00144     { m_ListIterator++; }
00145 
00146     void operator--(int)
00147     { m_ListIterator--; }
00148 
00149     const ConstIterator & operator++()
00150     {
00151       m_ListIterator++;
00152       return *this;
00153     }
00154 
00155     const ConstIterator & operator--()
00156     {
00157       m_ListIterator--;
00158       return *this;
00159     }
00160 
00161     bool operator!=(const ConstIterator & o) const
00162     { return m_ListIterator != o.m_ListIterator; }
00163     bool operator==(const ConstIterator & o) const
00164     { return m_ListIterator == o.m_ListIterator; }
00165 
00166     bool IsAtEnd() const
00167     {
00168       if ( m_ListIterator == m_NeighborhoodIterator->GetActiveIndexList().end() )
00169         {
00170         return true;
00171         }
00172       else
00173         {
00174         return false;
00175         }
00176     }
00177 
00178     void GoToBegin()
00179     {
00180       m_ListIterator = m_NeighborhoodIterator->GetActiveIndexList().begin();
00181     }
00182 
00183     void GoToEnd()
00184     {
00185       m_ListIterator = m_NeighborhoodIterator->GetActiveIndexList().end();
00186     }
00187 
00188     PixelType Get() const
00189     { return m_NeighborhoodIterator->GetPixel(*m_ListIterator); }
00190 
00191     OffsetType GetNeighborhoodOffset() const
00192     { return m_NeighborhoodIterator->GetOffset(*m_ListIterator); }
00193 
00194     typename IndexListType::value_type GetNeighborhoodIndex() const
00195     { return *m_ListIterator; }
00196 protected:
00197 
00198     Self *m_NeighborhoodIterator;
00199 
00200     typename IndexListType::const_iterator m_ListIterator;
00201 
00202     void ProtectedSet(const PixelType & v) const
00203     { m_NeighborhoodIterator->SetPixel(*m_ListIterator, v); }
00204   };
00205 
00208   const ConstIterator & Begin() const
00209   { return m_ConstBeginIterator; }
00210 
00213   const ConstIterator & End() const
00214   { return m_ConstEndIterator; }
00215 
00217   ConstShapedNeighborhoodIterator()
00218   {
00219     m_ConstBeginIterator = ConstIterator(this);
00220     m_ConstEndIterator = ConstIterator(this);
00221     m_ConstEndIterator.GoToEnd();
00222     m_CenterIsActive = false;
00223   }
00225 
00227   virtual ~ConstShapedNeighborhoodIterator()  {}
00228 
00231   ConstShapedNeighborhoodIterator(const SizeType & radius,
00232                                   const ImageType *ptr,
00233                                   const RegionType & region):
00234     Superclass (radius, const_cast< ImageType * >( ptr ), region)
00235   {
00236     m_ConstBeginIterator = ConstIterator(this);
00237     m_ConstEndIterator = ConstIterator(this);
00238     m_ConstEndIterator.GoToEnd();
00239     m_CenterIsActive = false;
00240   }
00242 
00243   // Expose the following methods from the superclass.  This is a
00244   // restricted subset of the methods available for
00245   // ConstNeighborhoodIterator.
00246   using Superclass::GetImagePointer;
00247   using Superclass::GetRadius;
00248   using Superclass::GetIndex;
00249   using Superclass::GetNeighborhoodIndex;
00250   using Superclass::GetCenterNeighborhoodIndex;
00251   using Superclass::GetRegion;
00252   using Superclass::GetBeginIndex;
00253   using Superclass::GoToBegin;
00254   using Superclass::GoToEnd;
00255   using Superclass::IsAtBegin;
00256   using Superclass::IsAtEnd;
00257   using Superclass::GetOffset;
00258   using Superclass::operator==;
00259   using Superclass::operator!=;
00260   using Superclass::operator<;
00261   using Superclass::operator>;
00262   using Superclass::operator>=;
00263   using Superclass::operator<=;
00264   using Superclass::operator[];
00265   using Superclass::GetElement;
00266   using Superclass::SetLocation;
00267   using Superclass::GetCenterPointer;
00268   using Superclass::GetCenterPixel;
00269   using Superclass::OverrideBoundaryCondition;
00270   using Superclass::ResetBoundaryCondition;
00271   using Superclass::GetBoundaryCondition;
00272   using Superclass::GetNeedToUseBoundaryCondition;
00273   using Superclass::SetNeedToUseBoundaryCondition;
00274   using Superclass::NeedToUseBoundaryConditionOn;
00275   using Superclass::NeedToUseBoundaryConditionOff;
00276   using Superclass::Print;
00277   using Superclass::operator-;
00278   using Superclass::GetPixel;
00279 
00281   Self & operator=(const Self & orig)
00282   {
00283     Superclass::operator=(orig);
00284     m_ActiveIndexList = orig.m_ActiveIndexList;
00285     m_CenterIsActive = orig.m_CenterIsActive;
00287 
00288     // Reset begin and end pointers
00289     m_ConstBeginIterator.GoToBegin();
00290     m_ConstEndIterator.GoToBegin();
00291     return *this;
00292   }
00293 
00295   virtual void PrintSelf(std::ostream &, Indent) const;
00296 
00300   virtual void ActivateOffset(const OffsetType & off)
00301   { this->ActivateIndex( Superclass::GetNeighborhoodIndex(off) ); }
00302   virtual void DeactivateOffset(const OffsetType & off)
00303   { this->DeactivateIndex( Superclass::GetNeighborhoodIndex(off) ); }
00305 
00307   virtual void ClearActiveList()
00308   {
00309     m_ActiveIndexList.clear();
00310     m_ConstBeginIterator.GoToBegin();
00311     m_ConstEndIterator.GoToEnd();
00312     m_CenterIsActive = false;
00313   }
00315 
00317   const IndexListType & GetActiveIndexList() const
00318   { return m_ActiveIndexList; }
00319 
00321   typename IndexListType::size_type GetActiveIndexListSize() const
00322   { return m_ActiveIndexList.size(); }
00323 
00327   void CreateActiveListFromNeighborhood(const NeighborhoodType &);
00328 
00331   Self & operator++();
00332 
00335   Self & operator--();
00336 
00340   Self & operator+=(const OffsetType &);
00341 
00345   Self & operator-=(const OffsetType &);
00346 
00347 protected:
00348   using Superclass::SetPixel;
00349   using Superclass::SetCenterPixel;
00351   ConstShapedNeighborhoodIterator(const ConstShapedNeighborhoodIterator &);
00352   // purposely not implemented
00353 
00354   friend struct ConstIterator;
00355 
00358   //  Superclass::SetPixel;
00359   //  Superclass::SetCenterPixel;
00360 
00366   virtual void ActivateIndex( NeighborIndexType );
00367 
00368   virtual void DeactivateIndex( NeighborIndexType );
00369 
00370   bool          m_CenterIsActive;
00371   IndexListType m_ActiveIndexList;
00372   ConstIterator m_ConstEndIterator;
00373   ConstIterator m_ConstBeginIterator;
00374 };
00375 } // namespace itk
00376 
00377 #ifndef ITK_MANUAL_INSTANTIATION
00378 #include "itkConstShapedNeighborhoodIterator.hxx"
00379 #endif
00380 
00381 #endif
00382