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

itkImageRegionReverseConstIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkImageRegionReverseConstIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2004/12/11 20:29:18 $
00007   Version:   $Revision: 1.6 $
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 __itkImageRegionReverseConstIterator_h
00018 #define __itkImageRegionReverseConstIterator_h
00019 
00020 #include "itkImageReverseConstIterator.h"
00021 
00022 namespace itk
00023 {
00024 
00099 template<typename TImage>
00100 class ITK_EXPORT ImageRegionReverseConstIterator : public ImageReverseConstIterator<TImage>
00101 {
00102 public:
00104   typedef ImageRegionReverseConstIterator Self;
00105   typedef ImageReverseConstIterator<TImage>  Superclass;
00106   
00111   enum { ImageIteratorDimension = Superclass::ImageIteratorDimension };
00112 
00115   typedef typename Superclass::IndexType IndexType;
00116   typedef typename Superclass::IndexValueType IndexValueType;
00117   
00120   typedef typename Superclass::SizeType SizeType;
00121   typedef typename Superclass::SizeValueType SizeValueType;
00122   
00125   typedef typename Superclass::OffsetType OffsetType;
00126   typedef typename Superclass::OffsetValueType OffsetValueType;
00127   
00129   typedef typename Superclass::RegionType   RegionType;
00130 
00133   typedef typename Superclass::ImageType ImageType;
00134 
00138   typedef typename Superclass::PixelContainer PixelContainer;
00139   typedef typename PixelContainer::Pointer PixelContainerPointer;
00140   
00142   typedef typename Superclass::InternalPixelType   InternalPixelType;
00143 
00145   typedef typename Superclass::PixelType   PixelType;
00146 
00149   typedef typename Superclass::AccessorType     AccessorType;
00150 
00152   itkTypeMacro(ImageRegionReverseConstIterator, ImageReverseConstIterator);
00153 
00155   ImageRegionReverseConstIterator() : Superclass()
00156   {
00157     m_SpanBeginOffset = 0;
00158     m_SpanEndOffset = 0;
00159   }
00160   
00163   ImageRegionReverseConstIterator(ImageType *ptr,
00164                       const RegionType &region)
00165     : Superclass(ptr, region)
00166   {
00167     m_SpanBeginOffset = this->m_BeginOffset;
00168     m_SpanEndOffset = this->m_BeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
00169   }
00170 
00178   ImageRegionReverseConstIterator( const ImageConstIterator<TImage> &it):Superclass(it)
00179   {
00180     IndexType ind = this->GetIndex();
00181     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
00182       - (ind[0] - this->m_Region.GetIndex()[0]);
00183     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
00184   }
00185 
00188   ImageRegionReverseConstIterator( const ImageReverseConstIterator<TImage> &it):Superclass(it)
00189   {
00190     IndexType ind = this->GetIndex();
00191     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
00192       - (ind[0] - this->m_Region.GetIndex()[0]);
00193     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
00194   }
00195 
00198   ImageRegionReverseConstIterator( const ImageRegionIterator<TImage> &it):Superclass(it)
00199   {
00200     IndexType ind = this->GetIndex();
00201     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
00202       - (ind[0] - this->m_Region.GetIndex()[0]);
00203     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
00204   }
00205 
00208   void GoToBegin()
00209   {
00210     Superclass::GoToBegin();
00211 
00212     // reset the span offsets
00213     m_SpanBeginOffset = this->m_BeginOffset;
00214     m_SpanEndOffset   = this->m_BeginOffset
00215       - static_cast<long>(this->m_Region.GetSize()[0]);
00216   };
00217   
00220   void GoToEnd()
00221   {
00222     Superclass::GoToEnd();
00223     
00224     // reset the span offsets
00225     m_SpanEndOffset = this->m_EndOffset;
00226     m_SpanBeginOffset = m_SpanEndOffset
00227       + static_cast<long>(this->m_Region.GetSize()[0]);
00228   };
00229 
00233   Self Begin(void) const;
00234 
00239   Self End(void) const;
00240 
00241 
00245   void SetIndex(const IndexType &ind)
00246   { Superclass::SetIndex(ind);
00247     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
00248       - (ind[0] - this->m_Region.GetIndex()[0]);
00249     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
00250   }
00251   
00260   Self &
00261   operator++()
00262   {
00263     if (--this->m_Offset <= m_SpanEndOffset)
00264       {
00265       // We have past the beginning of the span (row), need to wrap around.
00266 
00267       // First move forward one pixel, because we are going to use a different
00268       // algorithm to compute the next pixel
00269       this->m_Offset++;
00270       
00271       // Get the index of the first pixel on the span (row)
00272       typename ImageConstIterator<TImage>::IndexType
00273         ind = this->m_Image->ComputeIndex( static_cast<OffsetValueType>(this->m_Offset) );
00274 
00275       const typename ImageConstIterator<TImage>::IndexType&
00276         startIndex = this->m_Region.GetIndex();
00277       const typename ImageConstIterator<TImage>::SizeType&
00278         size = this->m_Region.GetSize();
00279 
00280       // Deccrement along a row, then wrap at the beginning of the region row.
00281       bool done;
00282       unsigned int dim;
00283 
00284       // Check to see if we are past the first pixel in the region
00285       // Note that --ind[0] moves to the previous pixel along the row.
00286       done = (--ind[0] == startIndex[0] - 1);
00287       for (unsigned int i=1; done && i < ImageIteratorDimension; i++)
00288         {
00289         done = (ind[i] == startIndex[i]);
00290         }
00291       
00292       // if the iterator is outside the region (but not past region begin) then
00293       // we need to wrap around the region
00294       dim = 0;
00295       if (!done)
00296         {
00297         while ( (dim < ImageIteratorDimension - 1)
00298                 && (ind[dim] < startIndex[dim]) )
00299           {
00300           ind[dim] = startIndex[dim] + static_cast<long>(size[dim]) - 1;
00301           ind[++dim]--;
00302           }
00303         }
00304       this->m_Offset = this->m_Image->ComputeOffset( ind );
00305       m_SpanBeginOffset = this->m_Offset;
00306       m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(size[0]);
00307       }
00308     return *this;
00309   }
00310 
00319   Self & operator--()
00320   {
00321     if (++this->m_Offset >= m_SpanBeginOffset)
00322       {
00323       // We have reached the end of the span (row), need to wrap around.
00324 
00325       // First back up one pixel, because we are going to use a different
00326       // algorithm to compute the next pixel
00327       --this->m_Offset;
00328       
00329       // Get the index of the last pixel on the span (row)
00330       typename ImageConstIterator<TImage>::IndexType
00331         ind = this->m_Image->ComputeIndex( static_cast<OffsetValueType>(this->m_Offset) );
00332 
00333       const typename ImageIterator<TImage>::IndexType&
00334         startIndex = this->m_Region.GetIndex();
00335       const typename ImageIterator<TImage>::SizeType&
00336         size = this->m_Region.GetSize();
00337 
00338       // Increment along a row, then wrap at the end of the region row.
00339       bool done;
00340       unsigned int dim;
00341 
00342       // Check to see if we are past the last pixel in the region
00343       // Note that ++ind[0] moves to the next pixel along the row.
00344       done = (++ind[0] == startIndex[0] + static_cast<long>(size[0]));
00345       for (unsigned int i=1; done && i < ImageIteratorDimension; i++)
00346         {
00347         done = (ind[i] == startIndex[i] + static_cast<long>(size[i]) - 1);
00348         }
00349       
00350       // if the iterator is outside the region (but not past region end) then
00351       // we need to wrap around the region
00352       dim = 0;
00353       if (!done)
00354         {
00355         while ( (dim < ImageIteratorDimension - 1)
00356                 && (ind[dim] > startIndex[dim] + static_cast<long>(size[dim]) - 1) )
00357           {
00358           ind[dim] = startIndex[dim];
00359           ind[++dim]++;
00360           }
00361         }
00362       this->m_Offset = this->m_Image->ComputeOffset( ind );
00363       m_SpanBeginOffset = this->m_Offset;
00364       m_SpanEndOffset = this->m_Offset - static_cast<long>(size[0]);
00365       }
00366     return *this;
00367   }
00368 
00369 protected:
00370   unsigned long m_SpanBeginOffset;  // offset to last pixel in the row
00371   unsigned long m_SpanEndOffset;  // offset to one pixel before the row
00372        
00373 };
00374 
00375 } // end namespace itk
00376 
00377 #ifndef ITK_MANUAL_INSTANTIATION
00378 #include "itkImageRegionReverseConstIterator.txx"
00379 #endif
00380 
00381 #endif 

Generated at Wed May 24 23:24:52 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000