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

itkImageConstIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkImageConstIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2002/09/30 12:43:58 $
00007   Version:   $Revision: 1.11 $
00008 
00009   Copyright (c) 2002 Insight 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 __itkImageConstIterator_h
00018 #define __itkImageConstIterator_h
00019 
00020 #include "itkImage.h"
00021 #include "itkIndex.h"
00022 #include "itkSize.h"
00023 #include "itkOffset.h"
00024 
00025 namespace itk
00026 {
00027 
00055 template<typename TImage>
00056 class ImageConstIterator {
00057 public:
00059   typedef ImageConstIterator Self;
00060   
00065   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
00066                       TImage::ImageDimension);
00067 
00069   typedef typename TImage::IndexType  IndexType;
00070   typedef typename TImage::IndexValueType  IndexValueType;
00071   
00073   typedef typename TImage::SizeType    SizeType;
00074   typedef typename TImage::SizeValueType  SizeValueType;
00075     
00077   typedef typename TImage::OffsetType    OffsetType;
00078   typedef typename TImage::OffsetValueType  OffsetValueType;
00079     
00081   typedef typename TImage::RegionType   RegionType;
00082 
00084   typedef TImage   ImageType;
00085 
00089   typedef typename TImage::PixelContainer PixelContainer;
00090   typedef typename PixelContainer::Pointer PixelContainerPointer;
00091   
00093   typedef typename TImage::InternalPixelType   InternalPixelType;
00094 
00096   typedef typename TImage::PixelType   PixelType;
00097 
00100   typedef typename TImage::AccessorType     AccessorType;
00101 
00104   ImageConstIterator()
00105     : m_Region(),
00106       m_PixelAccessor()
00107   {
00108     m_Image = 0;
00109     m_Buffer = 0;
00110     m_Offset = 0;
00111     m_BeginOffset = 0;
00112     m_EndOffset = 0;
00113   }
00114 
00116   virtual ~ImageConstIterator() {};
00117 
00120   ImageConstIterator(const Self& it)
00121   {
00122     m_Image = it.m_Image;     // copy the smart pointer
00123 
00124     m_Region = it.m_Region;
00125     
00126     m_Buffer = it.m_Buffer;
00127     m_Offset = it.m_Offset;
00128     m_BeginOffset = it.m_BeginOffset;
00129     m_EndOffset = it.m_EndOffset;
00130     m_PixelAccessor = it.m_PixelAccessor;
00131   }
00132 
00135   ImageConstIterator( const ImageType *ptr,
00136                       const RegionType &region )
00137   {
00138     m_Image = ptr;
00139     m_Buffer = m_Image->GetBufferPointer();
00140     m_Region = region;
00141 
00142     // Compute the start offset
00143     m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00144     m_BeginOffset = m_Offset;
00145     
00146     // Compute the end offset
00147     IndexType ind(m_Region.GetIndex());
00148     SizeType size(m_Region.GetSize());
00149     for (unsigned int i=0; i < ImageIteratorDimension; ++i)
00150       {
00151       ind[i] += (size[i] - 1);
00152       }
00153     m_EndOffset = m_Image->ComputeOffset( ind );
00154     m_EndOffset++;
00155 
00156     m_PixelAccessor = ptr->GetPixelAccessor();
00157   }
00158   
00161   Self &operator=(const Self& it)
00162   {
00163     m_Image = it.m_Image;     // copy the smart pointer
00164     m_Region = it.m_Region;
00165     
00166     m_Buffer = it.m_Buffer;
00167     m_Offset = it.m_Offset;
00168     m_BeginOffset = it.m_BeginOffset;
00169     m_EndOffset = it.m_EndOffset;
00170     m_PixelAccessor = it.m_PixelAccessor;
00171 
00172     return *this;
00173   }
00174   
00176   static unsigned int GetImageIteratorDimension() 
00177     {return ImageIteratorDimension;}
00178 
00181   bool
00182   operator!=(const Self &it) const
00183     {
00184     // two iterators are the same if they "point to" the same memory location
00185     return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
00186     };
00187 
00190   bool
00191   operator==(const Self &it) const
00192     {
00193     // two iterators are the same if they "point to" the same memory location
00194     return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00195     };
00196   
00199   bool
00200   operator<=(const Self &it) const
00201     {
00202     // an iterator is "less than" another if it "points to" a lower
00203     // memory location
00204     return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00205     };
00206 
00209   bool
00210   operator<(const Self &it) const
00211     {
00212     // an iterator is "less than" another if it "points to" a lower
00213     // memory location
00214     return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00215     };
00216 
00219   bool
00220   operator>=(const Self &it) const
00221     {
00222     // an iterator is "greater than" another if it "points to" a higher
00223     // memory location
00224     return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00225     };
00226 
00229   bool
00230   operator>(const Self &it) const
00231     {
00232     // an iterator is "greater than" another if it "points to" a higher
00233     // memory location
00234     return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
00235     };
00236 
00241   const IndexType GetIndex() const
00242     { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) );  }
00243 
00246   virtual void SetIndex(const IndexType &ind)
00247     { m_Offset = m_Image->ComputeOffset( ind ); }
00248 
00251   const RegionType& GetRegion() const
00252     { return m_Region; };
00253 
00255   const ImageType * GetImage() const
00256     { return m_Image.GetPointer(); };
00257 
00259   PixelType Get(void) const  
00260     { return m_PixelAccessor.Get(*(m_Buffer+m_Offset)); }
00261   
00265   const PixelType & Value(void) const  
00266     { return *(m_Buffer+m_Offset); }
00267  
00272   Self Begin(void) const;
00273 
00276   void GoToBegin()
00277     {
00278     m_Offset = m_BeginOffset;
00279     };
00280 
00285   Self End(void) const;
00286 
00289   void GoToEnd()
00290     {
00291     m_Offset = m_EndOffset;
00292     };
00293 
00296   bool IsAtBegin(void) const
00297     {
00298     return (m_Offset == m_BeginOffset);
00299     }
00300 
00303   bool IsAtEnd(void) const
00304     {
00305     return (m_Offset == m_EndOffset);
00306     }
00307   
00308 
00309 protected: //made protected so other iterators can access 
00310   SmartPointer<const ImageType>  m_Image;
00311   RegionType                     m_Region;      // region to iterate over
00312   
00313   unsigned long  m_Offset;
00314   unsigned long  m_BeginOffset; // offset to first pixel in region
00315   unsigned long  m_EndOffset;  // offset to one pixel past last pixel in region
00316 
00317   const InternalPixelType      * m_Buffer;
00318 
00319   AccessorType                   m_PixelAccessor;
00320 };
00321 
00322 } // end namespace itk
00323 
00324 #ifndef ITK_MANUAL_INSTANTIATION
00325 #include "itkImageConstIterator.txx"
00326 #endif
00327 
00328 #endif 

Generated at Wed Mar 12 01:12:59 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000