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: 2009-05-11 21:48:29 $
00007   Version:   $Revision: 1.27 $
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 #include "itkNumericTraits.h"
00025 
00026 namespace itk
00027 {
00028 
00084 template<typename TImage>
00085 class ITK_EXPORT ImageConstIterator
00086 {
00087 public:
00089   typedef ImageConstIterator Self;
00090 
00095   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
00096                       TImage::ImageDimension);
00097 
00099   typedef typename TImage::IndexType          IndexType;
00100   typedef typename TImage::IndexValueType     IndexValueType;
00101 
00103   typedef typename TImage::SizeType           SizeType;
00104   typedef typename TImage::SizeValueType      SizeValueType;
00105 
00107   typedef typename TImage::OffsetType         OffsetType;
00108   typedef typename TImage::OffsetValueType    OffsetValueType;
00109 
00111   typedef typename TImage::RegionType         RegionType;
00112 
00114   typedef TImage                              ImageType;
00115 
00119   typedef typename TImage::PixelContainer     PixelContainer;
00120   typedef typename PixelContainer::Pointer    PixelContainerPointer;
00121 
00123   typedef typename TImage::InternalPixelType   InternalPixelType;
00124 
00126   typedef typename TImage::PixelType   PixelType;
00127 
00130   typedef typename TImage::AccessorType            AccessorType;
00131   typedef typename TImage::AccessorFunctorType     AccessorFunctorType;
00132 
00135   ImageConstIterator()
00136     : m_Region(),
00137       m_PixelAccessor(),
00138       m_PixelAccessorFunctor()
00139     {
00140     m_Image = 0;
00141     m_Buffer = 0;
00142     m_Offset = 0;
00143     m_BeginOffset = 0;
00144     m_EndOffset = 0;
00145     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00146     }
00148 
00150   virtual ~ImageConstIterator() {};
00151 
00154   ImageConstIterator(const Self& it)
00155     {
00156     m_Image = it.m_Image;     // copy the smart pointer
00157 
00158     m_Region = it.m_Region;
00159 
00160     m_Buffer = it.m_Buffer;
00161     m_Offset = it.m_Offset;
00162     m_BeginOffset = it.m_BeginOffset;
00163     m_EndOffset = it.m_EndOffset;
00164     m_PixelAccessor = it.m_PixelAccessor;
00165     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00166     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00167     }
00168 
00171   ImageConstIterator( const ImageType *ptr,
00172                       const RegionType &region )
00173     {
00174     m_Image = ptr;
00175     m_Buffer = m_Image->GetBufferPointer();
00176     m_Region = region;
00178 
00179 #ifdef ITK_USE_REGION_VALIDATION_IN_ITERATORS
00180     if( region.GetNumberOfPixels() > 0 ) // If region is non-empty
00181       {
00182       const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
00183       itkAssertOrThrowMacro( (bufferedRegion.IsInside( m_Region )),
00184         "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
00185       }
00186 #endif
00187 
00188     // Compute the start offset
00189     m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00190     m_BeginOffset = m_Offset;
00191 
00192     // Compute the end offset. If any component of m_Region.GetSize()
00193     // is zero, the region is not valid and we set the EndOffset
00194     // to be same as BeginOffset so that iterator end condition is met
00195     // immediately.
00196     if (m_Region.GetNumberOfPixels() == 0)
00197       {
00198       // region is empty, probably has a size of 0 along one dimension
00199       m_EndOffset = m_BeginOffset;
00200       }
00201     else
00202       {
00203       IndexType ind(m_Region.GetIndex());
00204       SizeType size(m_Region.GetSize());
00205       for (unsigned int i=0; i < ImageIteratorDimension; ++i)
00206         {
00207         ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
00208         }
00209       m_EndOffset = m_Image->ComputeOffset( ind );
00210       m_EndOffset++;
00211       }
00212 
00213     m_PixelAccessor = ptr->GetPixelAccessor();
00214     m_PixelAccessorFunctor.SetPixelAccessor( m_PixelAccessor );
00215     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00216     }
00217 
00220   Self &operator=(const Self& it)
00221     {
00222     m_Image = it.m_Image;     // copy the smart pointer
00223     m_Region = it.m_Region;
00224 
00225     m_Buffer = it.m_Buffer;
00226     m_Offset = it.m_Offset;
00227     m_BeginOffset = it.m_BeginOffset;
00228     m_EndOffset = it.m_EndOffset;
00229     m_PixelAccessor = it.m_PixelAccessor;
00230     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00231     m_PixelAccessorFunctor.SetBegin( m_Buffer );
00232 
00233     return *this;
00234     }
00235 
00237   static unsigned int GetImageIteratorDimension()
00238     {return ImageIteratorDimension;}
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     // two iterators are the same if they "point to" the same memory location
00256     return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00257     };
00259 
00262   bool
00263   operator<=(const Self &it) const
00264     {
00265     // an iterator is "less than" another if it "points to" a lower
00266     // memory location
00267     return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00268     };
00270 
00273   bool
00274   operator<(const Self &it) const
00275     {
00276     // an iterator is "less than" another if it "points to" a lower
00277     // memory location
00278     return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00279     };
00281 
00284   bool
00285   operator>=(const Self &it) const
00286     {
00287     // an iterator is "greater than" another if it "points to" a higher
00288     // memory location
00289     return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00290     };
00292 
00295   bool
00296   operator>(const Self &it) const
00297     {
00298     // an iterator is "greater than" another if it "points to" a higher
00299     // memory location
00300     return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
00301     };
00303 
00308   const IndexType GetIndex() const
00309     { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) );  }
00310 
00313   virtual void SetIndex(const IndexType &ind)
00314     { m_Offset = m_Image->ComputeOffset( ind ); }
00315 
00318   const RegionType& GetRegion() const
00319     { return m_Region; }
00320 
00322   const ImageType * GetImage() const
00323     { return m_Image.GetPointer(); }
00324 
00326   PixelType Get(void) const
00327     { return m_PixelAccessorFunctor.Get(*(m_Buffer+m_Offset)); }
00328 
00332   const PixelType & Value(void) const
00333     { return *(m_Buffer + m_Offset); }
00334 
00339   Self Begin(void) const;
00340 
00343   void GoToBegin()
00344     {
00345     m_Offset = m_BeginOffset;
00346     }
00347 
00352   Self End(void) const;
00353 
00356   void GoToEnd()
00357     {
00358     m_Offset = m_EndOffset;
00359     }
00360 
00363   bool IsAtBegin(void) const
00364     {
00365     return (m_Offset == m_BeginOffset);
00366     }
00367 
00370   bool IsAtEnd(void) const
00371     {
00372     return (m_Offset == m_EndOffset);
00373     }
00374 
00375 
00376 protected: //made protected so other iterators can access
00377   typename TImage::ConstWeakPointer     m_Image;
00378   RegionType                            m_Region;  // region to iterate over
00379 
00380   unsigned long  m_Offset;
00381   unsigned long  m_BeginOffset; // offset to first pixel in region
00382   unsigned long  m_EndOffset;  // offset to one pixel past last pixel in region
00383 
00384   const InternalPixelType      * m_Buffer;
00385 
00386   AccessorType                   m_PixelAccessor;
00387   AccessorFunctorType            m_PixelAccessorFunctor;
00388 };
00389 
00390 } // end namespace itk
00391 
00392 // Define instantiation macro for this template.
00393 #define ITK_TEMPLATE_ImageConstIterator(_, EXPORT, x, y) namespace itk { \
00394   _(1(class EXPORT ImageConstIterator< ITK_TEMPLATE_1 x >)) \
00395   namespace Templates { typedef ImageConstIterator< ITK_TEMPLATE_1 x > ImageConstIterator##y; } \
00396   }
00397 
00398 
00399 #if ITK_TEMPLATE_EXPLICIT
00400 # include "Templates/itkImageConstIterator+-.h"
00401 #endif
00402 
00403 #if ITK_TEMPLATE_TXX
00404 # include "itkImageConstIterator.txx"
00405 #endif
00406 
00407 #endif
00408 

Generated at Thu May 28 10:15:56 2009 for ITK by doxygen 1.5.5 written by Dimitri van Heesch, © 1997-2000