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: 2004/11/01 21:16:31 $
00007   Version:   $Revision: 1.19 $
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 __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 
00083 template<typename TImage>
00084 class ITK_EXPORT ImageConstIterator
00085 {
00086 public:
00088   typedef ImageConstIterator Self;
00089   
00094   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
00095                       TImage::ImageDimension);
00096 
00098   typedef typename TImage::IndexType  IndexType;
00099   typedef typename TImage::IndexValueType  IndexValueType;
00100   
00102   typedef typename TImage::SizeType    SizeType;
00103   typedef typename TImage::SizeValueType  SizeValueType;
00104     
00106   typedef typename TImage::OffsetType    OffsetType;
00107   typedef typename TImage::OffsetValueType  OffsetValueType;
00108     
00110   typedef typename TImage::RegionType   RegionType;
00111 
00113   typedef TImage   ImageType;
00114 
00118   typedef typename TImage::PixelContainer PixelContainer;
00119   typedef typename PixelContainer::Pointer PixelContainerPointer;
00120   
00122   typedef typename TImage::InternalPixelType   InternalPixelType;
00123 
00125   typedef typename TImage::PixelType   PixelType;
00126 
00129   typedef typename TImage::AccessorType     AccessorType;
00130 
00133   ImageConstIterator()
00134     : m_Region(),
00135       m_PixelAccessor()
00136   {
00137     m_Image = 0;
00138     m_Buffer = 0;
00139     m_Offset = 0;
00140     m_BeginOffset = 0;
00141     m_EndOffset = 0;
00142   }
00143 
00145   virtual ~ImageConstIterator() {};
00146 
00149   ImageConstIterator(const Self& it)
00150   {
00151     m_Image = it.m_Image;     // copy the smart pointer
00152 
00153     m_Region = it.m_Region;
00154     
00155     m_Buffer = it.m_Buffer;
00156     m_Offset = it.m_Offset;
00157     m_BeginOffset = it.m_BeginOffset;
00158     m_EndOffset = it.m_EndOffset;
00159     m_PixelAccessor = it.m_PixelAccessor;
00160   }
00161 
00164   ImageConstIterator( const ImageType *ptr,
00165                       const RegionType &region )
00166   {
00167     m_Image = ptr;
00168     m_Buffer = m_Image->GetBufferPointer();
00169     m_Region = region;
00170 
00171     // Compute the start offset
00172     m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00173     m_BeginOffset = m_Offset;
00174     
00175     // Compute the end offset. If any component of m_Region.GetSize()
00176     // is zero, the region is not valid and we set the EndOffset
00177     // to be same as BeginOffset so that iterator end condition is met
00178     // immediately.
00179     if (m_Region.GetNumberOfPixels() == 0)
00180       {
00181       // region is empty, probably has a size of 0 along one dimension
00182       m_EndOffset = m_BeginOffset;
00183       }
00184     else
00185       {
00186       IndexType ind(m_Region.GetIndex());
00187       SizeType size(m_Region.GetSize());
00188       for (unsigned int i=0; i < ImageIteratorDimension; ++i)
00189         {
00190         ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
00191         }
00192       m_EndOffset = m_Image->ComputeOffset( ind );
00193       m_EndOffset++;
00194       }
00195 
00196     m_PixelAccessor = ptr->GetPixelAccessor();
00197   }
00198   
00201   Self &operator=(const Self& it)
00202   {
00203     m_Image = it.m_Image;     // copy the smart pointer
00204     m_Region = it.m_Region;
00205     
00206     m_Buffer = it.m_Buffer;
00207     m_Offset = it.m_Offset;
00208     m_BeginOffset = it.m_BeginOffset;
00209     m_EndOffset = it.m_EndOffset;
00210     m_PixelAccessor = it.m_PixelAccessor;
00211 
00212     return *this;
00213   }
00214   
00216   static unsigned int GetImageIteratorDimension() 
00217     {return ImageIteratorDimension;}
00218 
00221   bool
00222   operator!=(const Self &it) const
00223     {
00224     // two iterators are the same if they "point to" the same memory location
00225     return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
00226     };
00227 
00230   bool
00231   operator==(const Self &it) const
00232     {
00233     // two iterators are the same if they "point to" the same memory location
00234     return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00235     };
00236   
00239   bool
00240   operator<=(const Self &it) const
00241     {
00242     // an iterator is "less than" another if it "points to" a lower
00243     // memory location
00244     return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00245     };
00246 
00249   bool
00250   operator<(const Self &it) const
00251     {
00252     // an iterator is "less than" another if it "points to" a lower
00253     // memory location
00254     return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00255     };
00256 
00259   bool
00260   operator>=(const Self &it) const
00261     {
00262     // an iterator is "greater than" another if it "points to" a higher
00263     // memory location
00264     return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00265     };
00266 
00269   bool
00270   operator>(const Self &it) const
00271     {
00272     // an iterator is "greater than" another if it "points to" a higher
00273     // memory location
00274     return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
00275     };
00276 
00281   const IndexType GetIndex() const
00282     { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) );  }
00283 
00286   virtual void SetIndex(const IndexType &ind)
00287     { m_Offset = m_Image->ComputeOffset( ind ); }
00288 
00291   const RegionType& GetRegion() const
00292     { return m_Region; };
00293 
00295   const ImageType * GetImage() const
00296     { return m_Image.GetPointer(); };
00297 
00299   PixelType Get(void) const  
00300     { return m_PixelAccessor.Get(*(m_Buffer+m_Offset)); }
00301   
00305   const PixelType & Value(void) const  
00306     { return *(m_Buffer+m_Offset); }
00307  
00312   Self Begin(void) const;
00313 
00316   void GoToBegin()
00317     {
00318     m_Offset = m_BeginOffset;
00319     };
00320 
00325   Self End(void) const;
00326 
00329   void GoToEnd()
00330     {
00331     m_Offset = m_EndOffset;
00332     };
00333 
00336   bool IsAtBegin(void) const
00337     {
00338     return (m_Offset == m_BeginOffset);
00339     }
00340 
00343   bool IsAtEnd(void) const
00344     {
00345     return (m_Offset == m_EndOffset);
00346     }
00347   
00348 
00349 protected: //made protected so other iterators can access 
00350   typename TImage::ConstWeakPointer  m_Image;
00351   RegionType                     m_Region;      // region to iterate over
00352   
00353   unsigned long  m_Offset;
00354   unsigned long  m_BeginOffset; // offset to first pixel in region
00355   unsigned long  m_EndOffset;  // offset to one pixel past last pixel in region
00356 
00357   const InternalPixelType      * m_Buffer;
00358 
00359   AccessorType                   m_PixelAccessor;
00360 };
00361 
00362 } // end namespace itk
00363 
00364 #ifndef ITK_MANUAL_INSTANTIATION
00365 #include "itkImageConstIterator.txx"
00366 #endif
00367 
00368 #endif 

Generated at Tue Mar 29 23:54:33 2005 for ITK by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2000