Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkConstNeighborhoodIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkConstNeighborhoodIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/07/13 13:31:38 $
00007   Version:   $Revision: 1.33 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkConstNeighborhoodIterator_h
00018 #define __itkConstNeighborhoodIterator_h
00019 
00020 #include <vector>
00021 #include <string.h>
00022 #include <iostream>
00023 #include "itkImage.h"
00024 #include "itkIndex.h"
00025 #include "itkOffset.h"
00026 #include "itkSize.h"
00027 #include "itkImageRegion.h"
00028 #include "itkMacro.h"
00029 #include "itkNeighborhood.h"
00030 #include "itkImageBoundaryCondition.h"
00031 #include "itkExceptionObject.h"
00032 #include "itkZeroFluxNeumannBoundaryCondition.h"
00033 
00034 namespace itk {
00035 
00050 template<class TImage,  class TBoundaryCondition
00051                        = ZeroFluxNeumannBoundaryCondition<TImage> >
00052 class ITK_EXPORT ConstNeighborhoodIterator
00053   :  public Neighborhood<ITK_TYPENAME TImage::InternalPixelType *,
00054                          ::itk::GetImageDimension<TImage>::ImageDimension>
00055 {
00056 public:
00058   typedef typename TImage::InternalPixelType InternalPixelType;
00059   typedef typename TImage::PixelType PixelType;
00060     
00062   itkStaticConstMacro(Dimension, unsigned int, TImage::ImageDimension);
00063   
00065   typedef ConstNeighborhoodIterator Self;
00066   typedef Neighborhood<InternalPixelType *, itkGetStaticConstMacro(Dimension)> Superclass;
00067 
00069   typedef typename Superclass::OffsetType OffsetType;
00070   typedef typename OffsetType::OffsetValueType OffsetValueType;
00071   typedef typename Superclass::RadiusType RadiusType;  
00072   typedef typename Superclass::SizeType SizeType;
00073   typedef typename Superclass::SizeValueType SizeValueType;
00074   typedef typename Superclass::Iterator Iterator;
00075   typedef typename Superclass::ConstIterator ConstIterator;
00076   
00078   typedef TImage ImageType;
00079   typedef typename TImage::RegionType RegionType;
00080   typedef Index<itkGetStaticConstMacro(Dimension)> IndexType;
00081   typedef typename IndexType::IndexValueType IndexValueType;
00082   typedef Neighborhood<PixelType, itkGetStaticConstMacro(Dimension)> NeighborhoodType;
00083 
00085   typedef TBoundaryCondition BoundaryConditionType;
00086   
00088   typedef ImageBoundaryCondition<ImageType> *ImageBoundaryConditionPointerType;
00089   typedef ImageBoundaryCondition<ImageType> const *ImageBoundaryConditionConstPointerType;
00090 
00092   ConstNeighborhoodIterator();
00093 
00095   virtual ~ConstNeighborhoodIterator() {}
00096 
00098   ConstNeighborhoodIterator( const ConstNeighborhoodIterator & );
00099 
00102   ConstNeighborhoodIterator(const SizeType &radius,
00103                        const ImageType * ptr,
00104                        const RegionType &region
00105                        )
00106   {
00107     this->Initialize(radius, ptr, region);
00108     for (unsigned int i=0; i < Dimension; i++)
00109       { m_InBounds[i] = false; }
00110     this->ResetBoundaryCondition();
00111   }
00112 
00114   Self &operator=(const Self& orig);
00115 
00117   virtual void PrintSelf(std::ostream &, Indent) const;
00118 
00121   OffsetType ComputeInternalIndex(unsigned int n) const;
00122 
00124   IndexType GetBound() const
00125     {    return m_Bound;   }
00126 
00129   long GetBound(unsigned int n) const
00130     {    return m_Bound[n];  }
00131   
00133   const InternalPixelType *GetCenterPointer() const
00134     {    return (this->operator[]((this->Size())>>1));  }
00135   
00137   PixelType GetCenterPixel() const
00138     {    return *( this->GetCenterPointer() );  }
00139 
00141   const ImageType * GetImagePointer(void) const
00142     { return m_ConstImage; }
00143  
00146   virtual IndexType GetIndex(void) const
00147     { return m_Loop;  }
00148   
00151   virtual NeighborhoodType GetNeighborhood() const;
00152 
00154   virtual PixelType GetPixel(const unsigned i) const
00155     { 
00156     if( !m_NeedToUseBoundaryCondition )
00157       {
00158       return ( * ( this->operator[]( i ) ) );
00159       }
00160     bool inbounds; 
00161     return this->GetPixel( i, inbounds ); 
00162     }
00163 
00169   virtual PixelType GetPixel(const unsigned i, bool& IsInBounds) const;
00170 
00173   virtual PixelType GetPixel(const OffsetType &o) const
00174   { bool inbounds; return (this->GetPixel(this->GetNeighborhoodIndex(o), inbounds)); }
00175 
00181   virtual PixelType GetPixel(const OffsetType &o,
00182                              bool& IsInBounds) const
00183   { return (this->GetPixel(this->GetNeighborhoodIndex(o), IsInBounds)); }
00184   
00188   virtual PixelType GetNext(const unsigned axis, const unsigned i) const
00189   {    return (this->GetPixel(this->GetCenterNeighborhoodIndex()
00190                            + (i * this->GetStride(axis)))); }
00191 
00195   virtual PixelType GetNext(const unsigned axis) const
00196   {    return (this->GetPixel(this->GetCenterNeighborhoodIndex()
00197                            + this->GetStride(axis))); }
00198 
00202   virtual PixelType GetPrevious(const unsigned axis, const unsigned i) const
00203   { return (this->GetPixel(this->GetCenterNeighborhoodIndex()
00204                            - (i * this->GetStride(axis)))); }
00205   
00209   virtual PixelType GetPrevious(const unsigned axis) const
00210   { return (this->GetPixel(this->GetCenterNeighborhoodIndex()
00211                            - this->GetStride(axis))); } 
00212   
00215   virtual IndexType GetIndex(const OffsetType &o) const
00216   { return (this->GetIndex() + o); }
00217 
00220   virtual IndexType GetIndex(const unsigned i) const
00221   { return (this->GetIndex() + this->GetOffset(i)); }
00222   
00224   RegionType GetRegion() const
00225     { return m_Region; }
00226   
00229   IndexType GetBeginIndex() const
00230     { return m_BeginIndex; }
00231 
00234   RegionType GetBoundingBoxAsImageRegion() const;
00235   
00237   OffsetType GetWrapOffset() const
00238     {  return m_WrapOffset;  }
00239 
00245   OffsetValueType GetWrapOffset(unsigned int n) const
00246     {    return m_WrapOffset[n];   }
00247 
00251   virtual void GoToBegin();
00252   
00255   virtual void GoToEnd();
00256   
00259   virtual void Initialize(const SizeType &radius, const ImageType *ptr,
00260                           const RegionType &region);
00261 
00264   virtual bool IsAtBegin() const
00265     {    return ( this->GetCenterPointer() == m_Begin );   }
00266   
00269   virtual bool IsAtEnd() const
00270     {
00271       if ( this->GetCenterPointer() > m_End )
00272         {
00273         ExceptionObject e(__FILE__, __LINE__);
00274         OStringStream msg;
00275         msg << "In method IsAtEnd, CenterPointer = " << this->GetCenterPointer()
00276             << " is greater than End = " << m_End
00277             << std::endl
00278             << "  " << *this;
00279         e.SetDescription(msg.str().c_str());
00280         throw e;
00281         }
00282       return ( this->GetCenterPointer() == m_End );
00283     }
00284   
00289   Self &operator++();
00290   
00295   Self &operator--();  
00296  
00300   bool operator==(const Self &it) const 
00301     {   return  it.GetCenterPointer() == this->GetCenterPointer();   }
00302   
00306   bool operator!=(const Self &it) const
00307     {    return  it.GetCenterPointer() != this->GetCenterPointer();  }
00308   
00312   bool operator<(const Self &it) const
00313     {  return  this->GetCenterPointer() < it.GetCenterPointer();  }
00314 
00318   bool operator<=(const Self &it) const
00319     {    return  this->GetCenterPointer() <= it.GetCenterPointer();  }
00320   
00324   bool operator>(const Self &it) const
00325     {    return  this->GetCenterPointer() > it.GetCenterPointer();  }
00326 
00330   bool operator>=(const Self &it) const
00331     {    return  this->GetCenterPointer() >= it.GetCenterPointer();  }
00332 
00337   void SetLocation( const IndexType& position )
00338     {
00339       this->SetLoop(position);
00340       this->SetPixelPointers(position);
00341     }
00342   
00343 
00347   Self &operator+=(const OffsetType &);
00348 
00352   Self &operator-=(const OffsetType &);
00353 
00355   OffsetType operator-(const Self& b)
00356   {  return m_Loop - b.m_Loop;  }
00357 
00361   bool InBounds() const;
00362 
00368   virtual void OverrideBoundaryCondition(const ImageBoundaryConditionPointerType i)
00369     { m_BoundaryCondition = i; }
00370 
00373   virtual void ResetBoundaryCondition()
00374     { m_BoundaryCondition = &m_InternalBoundaryCondition;  }
00375 
00377   void SetBoundaryCondition( const TBoundaryCondition &c )
00378   { m_InternalBoundaryCondition = c; }
00379 
00381   const BoundaryConditionType *GetBoundaryCondition() const
00382   { return dynamic_cast<BoundaryConditionType *>(m_BoundaryCondition); }
00383 
00385   void NeedToUseBoundaryConditionOn()
00386   {
00387     this->SetNeedToUseBoundaryCondition(true);
00388   }
00389   void NeedToUseBoundaryConditionOff()
00390   {
00391     this->SetNeedToUseBoundaryCondition(false);
00392   }
00393   void SetNeedToUseBoundaryCondition(bool b)
00394   {
00395     m_NeedToUseBoundaryCondition = b;
00396   }
00397   bool GetNeedToUseBoundaryCondition() const
00398   {
00399     return m_NeedToUseBoundaryCondition;
00400   }
00401   
00402   
00403 protected:
00404   
00407   virtual void SetLoop( const IndexType& p )
00408     {  m_Loop = p; m_IsInBoundsValid = false;}
00409   
00413   virtual void SetBound(const SizeType &);
00414 
00419   virtual void SetPixelPointers(const IndexType &);
00420 
00423   virtual void SetBeginIndex( const IndexType& start)
00424     {  m_BeginIndex = start;  }
00425 
00428   virtual void SetEndIndex();
00429   
00432   IndexType m_BeginIndex;
00433 
00435   IndexType m_Bound;
00436 
00438   const InternalPixelType *m_Begin;
00439   
00441   typename ImageType::ConstWeakPointer m_ConstImage;
00442 
00443   /*
00444    * A pointer to one past the last pixel in the iteration region.
00445    */
00446   const InternalPixelType *m_End;
00447 
00450   IndexType m_EndIndex;
00451 
00453   IndexType m_Loop;
00454  
00456   RegionType m_Region;
00457 
00462   OffsetType m_WrapOffset;
00463 
00468   ImageBoundaryConditionPointerType m_BoundaryCondition;
00469 
00472   mutable bool m_InBounds[Dimension];
00473 
00475   mutable bool m_IsInBounds;
00476   
00480   mutable bool m_IsInBoundsValid;
00481   
00483   IndexType m_InnerBoundsLow;
00484   
00486   IndexType m_InnerBoundsHigh;
00487 
00489   TBoundaryCondition m_InternalBoundaryCondition;
00490 
00492   bool m_NeedToUseBoundaryCondition;
00493 
00494 };
00495 
00496 template<class TImage>
00497 inline ConstNeighborhoodIterator<TImage>
00498 operator+(const ConstNeighborhoodIterator<TImage> &it,
00499           const typename ConstNeighborhoodIterator<TImage>
00500           ::OffsetType &ind)
00501 {
00502   ConstNeighborhoodIterator<TImage> ret;
00503   ret = it;
00504   ret += ind;
00505   return ret;
00506 }
00507 
00508 template<class TImage>
00509 inline ConstNeighborhoodIterator<TImage>
00510 operator+(const typename ConstNeighborhoodIterator<TImage>
00511           ::OffsetType &ind,
00512           const ConstNeighborhoodIterator<TImage> &it)
00513 {  return (it + ind); }
00514 
00515 template<class TImage>
00516 inline ConstNeighborhoodIterator<TImage>
00517 operator-(const ConstNeighborhoodIterator<TImage> &it,
00518           const typename ConstNeighborhoodIterator<TImage>
00519           ::OffsetType &ind)
00520 {
00521   ConstNeighborhoodIterator<TImage> ret;
00522   ret = it;
00523   ret -= ind;
00524   return ret;
00525 }
00526 
00527 
00528 
00529 
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538   
00539 } // namespace itk
00540 
00541 
00542 #ifndef ITK_MANUAL_INSTANTIATION
00543 #include "itkConstNeighborhoodIterator.txx"
00544 #endif
00545 
00546 #endif

Generated at Tue Aug 30 16:43:26 2005 for ITK by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2000