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: 2007/01/30 20:56:08 $
00007   Version:   $Revision: 1.22 $
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   typedef typename TImage::AccessorFunctorType     AccessorFunctorType;
00131 
00134   ImageConstIterator()
00135     : m_Region(),
00136       m_PixelAccessor(),
00137       m_PixelAccessorFunctor()
00138   {
00139     m_Image = 0;
00140     m_Buffer = 0;
00141     m_Offset = 0;
00142     m_BeginOffset = 0;
00143     m_EndOffset = 0;
00144     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00145   }
00147 
00149   virtual ~ImageConstIterator() {};
00150 
00153   ImageConstIterator(const Self& it)
00154   {
00155     m_Image = it.m_Image;     // copy the smart pointer
00156 
00157     m_Region = it.m_Region;
00158     
00159     m_Buffer = it.m_Buffer;
00160     m_Offset = it.m_Offset;
00161     m_BeginOffset = it.m_BeginOffset;
00162     m_EndOffset = it.m_EndOffset;
00163     m_PixelAccessor = it.m_PixelAccessor;
00164     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00165     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00166   }
00167 
00170   ImageConstIterator( const ImageType *ptr,
00171                       const RegionType &region )
00172   {
00173     m_Image = ptr;
00174     m_Buffer = m_Image->GetBufferPointer();
00175     m_Region = region;
00177 
00178     // Compute the start offset
00179     m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00180     m_BeginOffset = m_Offset;
00181     
00182     // Compute the end offset. If any component of m_Region.GetSize()
00183     // is zero, the region is not valid and we set the EndOffset
00184     // to be same as BeginOffset so that iterator end condition is met
00185     // immediately.
00186     if (m_Region.GetNumberOfPixels() == 0)
00187       {
00188       // region is empty, probably has a size of 0 along one dimension
00189       m_EndOffset = m_BeginOffset;
00190       }
00191     else
00192       {
00193       IndexType ind(m_Region.GetIndex());
00194       SizeType size(m_Region.GetSize());
00195       for (unsigned int i=0; i < ImageIteratorDimension; ++i)
00196         {
00197         ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
00198         }
00199       m_EndOffset = m_Image->ComputeOffset( ind );
00200       m_EndOffset++;
00201       }
00202 
00203     m_PixelAccessor = ptr->GetPixelAccessor();
00204     m_PixelAccessorFunctor.SetPixelAccessor( m_PixelAccessor );
00205     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00206   }
00207   
00210   Self &operator=(const Self& it)
00211   {
00212     m_Image = it.m_Image;     // copy the smart pointer
00213     m_Region = it.m_Region;
00214 
00215     m_Buffer = it.m_Buffer;
00216     m_Offset = it.m_Offset;
00217     m_BeginOffset = it.m_BeginOffset;
00218     m_EndOffset = it.m_EndOffset;
00219     m_PixelAccessor = it.m_PixelAccessor;
00220     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00221     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00222 
00223     return *this;
00224   }
00225   
00227   static unsigned int GetImageIteratorDimension() 
00228     {return ImageIteratorDimension;}
00229 
00232   bool
00233   operator!=(const Self &it) const
00234     {
00235     // two iterators are the same if they "point to" the same memory location
00236     return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
00237     };
00239 
00242   bool
00243   operator==(const Self &it) const
00244     {
00245     // two iterators are the same if they "point to" the same memory location
00246     return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00247     };
00249 
00252   bool
00253   operator<=(const Self &it) const
00254     {
00255     // an iterator is "less than" another if it "points to" a lower
00256     // memory location
00257     return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00258     };
00260 
00263   bool
00264   operator<(const Self &it) const
00265     {
00266     // an iterator is "less than" another if it "points to" a lower
00267     // memory location
00268     return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00269     };
00271 
00274   bool
00275   operator>=(const Self &it) const
00276     {
00277     // an iterator is "greater than" another if it "points to" a higher
00278     // memory location
00279     return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00280     };
00282 
00285   bool
00286   operator>(const Self &it) const
00287     {
00288     // an iterator is "greater than" another if it "points to" a higher
00289     // memory location
00290     return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
00291     };
00293 
00298   const IndexType GetIndex() const
00299     { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) );  }
00300 
00303   virtual void SetIndex(const IndexType &ind)
00304     { m_Offset = m_Image->ComputeOffset( ind ); }
00305 
00308   const RegionType& GetRegion() const
00309     { return m_Region; };
00310 
00312   const ImageType * GetImage() const
00313     { return m_Image.GetPointer(); };
00314 
00316   PixelType Get(void) const  
00317     { return m_PixelAccessorFunctor.Get(*(m_Buffer+m_Offset)); }
00318 
00322   const PixelType & Value(void) const  
00323     { return *(m_Buffer + m_Offset); }
00324 
00329   Self Begin(void) const;
00330 
00333   void GoToBegin()
00334     {
00335     m_Offset = m_BeginOffset;
00336     };
00337 
00342   Self End(void) const;
00343 
00346   void GoToEnd()
00347     {
00348     m_Offset = m_EndOffset;
00349     };
00350 
00353   bool IsAtBegin(void) const
00354     {
00355     return (m_Offset == m_BeginOffset);
00356     }
00357 
00360   bool IsAtEnd(void) const
00361     {
00362     return (m_Offset == m_EndOffset);
00363     }
00364 
00365 
00366 protected: //made protected so other iterators can access 
00367   typename TImage::ConstWeakPointer  m_Image;
00368   RegionType                     m_Region;      // region to iterate over
00369   
00370   unsigned long  m_Offset;
00371   unsigned long  m_BeginOffset; // offset to first pixel in region
00372   unsigned long  m_EndOffset;  // offset to one pixel past last pixel in region
00373 
00374   const InternalPixelType      * m_Buffer;
00375 
00376   AccessorType                   m_PixelAccessor;
00377   AccessorFunctorType            m_PixelAccessorFunctor;
00378 };
00379 
00380 } // end namespace itk
00381 
00382 // Define instantiation macro for this template.
00383 #define ITK_TEMPLATE_ImageConstIterator(_, EXPORT, x, y) namespace itk { \
00384   _(1(class EXPORT ImageConstIterator< ITK_TEMPLATE_1 x >)) \
00385   namespace Templates { typedef ImageConstIterator< ITK_TEMPLATE_1 x > ImageConstIterator##y; } \
00386   }
00387 
00388 
00389 #if ITK_TEMPLATE_EXPLICIT
00390 # include "Templates/itkImageConstIterator+-.h"
00391 #endif
00392 
00393 #if ITK_TEMPLATE_TXX
00394 # include "itkImageConstIterator.txx"
00395 #endif
00396 
00397 #endif 
00398 

Generated at Tue Jul 29 20:29:23 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000