ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageReverseConstIterator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageReverseConstIterator_h
00019 #define __itkImageReverseConstIterator_h
00020 
00021 #include "itkSize.h"
00022 #include "itkImageConstIterator.h"
00023 #include <memory>
00024 
00025 namespace itk
00026 {
00086 template< typename TImage >
00087 class ITK_EXPORT ImageReverseConstIterator
00088 {
00089 public:
00091   typedef ImageReverseConstIterator Self;
00092 
00097   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
00098                       TImage::ImageDimension);
00099 
00101   typedef typename TImage::IndexType      IndexType;
00102 
00104   typedef typename TImage::SizeType      SizeType;
00105 
00107   typedef typename TImage::OffsetType      OffsetType;
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 
00132   typedef typename TImage::AccessorFunctorType AccessorFunctorType;
00133 
00136   ImageReverseConstIterator():m_PixelAccessor(), m_PixelAccessorFunctor()
00137   {
00138     m_Buffer = 0;
00139     m_Offset = 0;
00140     m_BeginOffset = 0;
00141     m_EndOffset = 0;
00142     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00143   }
00145 
00147   virtual ~ImageReverseConstIterator() {}
00148 
00151   ImageReverseConstIterator(const Self & it)
00152   {
00153     m_Image = it.m_Image;     // copy the smart pointer
00154 
00155     m_Region = it.m_Region;
00156 
00157     m_Buffer = it.m_Buffer;
00158     m_Offset = it.m_Offset;
00159     m_BeginOffset = it.m_BeginOffset;
00160     m_EndOffset = it.m_EndOffset;
00161     m_PixelAccessor = it.m_PixelAccessor;
00162     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00163     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00164   }
00165 
00168   ImageReverseConstIterator(const ImageType *ptr, const RegionType & region)
00169   {
00170     SizeValueType offset;
00171 
00172     m_Image = ptr;
00173     m_Buffer = m_Image->GetBufferPointer();
00174     m_Region = region;
00175 
00176     // Compute the end offset, one pixel before the first pixel
00177     offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00178     m_EndOffset = offset - 1;
00179 
00180     // Compute the begin offset, the last pixel in the region
00181     IndexType ind( m_Region.GetIndex() );
00182     SizeType  size( m_Region.GetSize() );
00183     for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
00184       {
00185       ind[i] += ( size[i] - 1 );
00186       }
00187     m_BeginOffset = m_Image->ComputeOffset(ind);
00188     m_Offset = m_BeginOffset;
00189 
00190     m_PixelAccessor = ptr->GetPixelAccessor();
00191     m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
00192     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00193   }
00194 
00202   ImageReverseConstIterator(const ImageConstIterator< TImage > & it)
00203   {
00204     m_Image = it.GetImage();
00205     m_Region = it.GetRegion();
00206     m_Buffer = m_Image->GetBufferPointer();
00208 
00209     IndexType ind = it.GetIndex();
00210 
00211     m_Offset = m_Image->ComputeOffset(ind);
00212 
00213     // Compute the end offset, one pixel before the first pixel
00214     m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
00215 
00216     // Compute the begin offset, the last pixel in the region
00217     IndexType regInd( m_Region.GetIndex() );
00218     SizeType  regSize( m_Region.GetSize() );
00219     for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
00220       {
00221       regInd[i] += ( regSize[i] - 1 );
00222       }
00223     m_BeginOffset = m_Image->ComputeOffset(regInd);
00224 
00225     m_PixelAccessor = m_Image->GetPixelAccessor();
00226     m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
00227     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00228   }
00229 
00232   Self & operator=(const Self & it)
00233   {
00234     m_Image = it.m_Image;     // copy the smart pointer
00235     m_Region = it.m_Region;
00236 
00237     m_Buffer = it.m_Buffer;
00238     m_Offset = it.m_Offset;
00239     m_BeginOffset = it.m_BeginOffset;
00240     m_EndOffset = it.m_EndOffset;
00241     m_PixelAccessor = it.m_PixelAccessor;
00242     m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
00243     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00244     return *this;
00245   }
00246 
00249   Self & operator=(const ImageConstIterator< TImage > & it)
00250   {
00251     m_Image = it.GetImage();
00252     m_Region = it.GetRegion();
00253     m_Buffer = m_Image->GetBufferPointer();
00255 
00256     IndexType ind = it.GetIndex();
00257 
00258     m_Offset = m_Image->ComputeOffset(ind);
00259 
00260     // Compute the end offset, one pixel before the first pixel
00261     m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
00262 
00263     // Compute the begin offset, the last pixel in the region
00264     IndexType regInd( m_Region.GetIndex() );
00265     SizeType  regSize( m_Region.GetSize() );
00266     for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
00267       {
00268       regInd[i] += ( regSize[i] - 1 );
00269       }
00270     m_BeginOffset = m_Image->ComputeOffset(regInd);
00271 
00272     m_PixelAccessor = m_Image->GetPixelAccessor();
00273     m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
00274     m_PixelAccessorFunctor.SetBegin(m_Buffer);
00275 
00276     return *this;
00277   }
00278 
00280   static unsigned int GetImageIteratorDimension()
00281   { return TImage::ImageDimension; }
00282 
00285   bool
00286   operator!=(const Self & it) const
00287   {
00288     // two iterators are the same if they "point to" the same memory location
00289     return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
00290   }
00291 
00294   bool
00295   operator==(const Self & it) const
00296   {
00297     // two iterators are the same if they "point to" the same memory location
00298     return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
00299   }
00300 
00305   const IndexType GetIndex()
00306   { return m_Image->ComputeIndex(m_Offset);  }
00307 
00310   virtual void SetIndex(const IndexType & ind)
00311   { m_Offset = m_Image->ComputeOffset(ind); }
00312 
00315   const RegionType & GetRegion() const
00316   { return m_Region; }
00317 
00319   const PixelType Get(void) const
00320   { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
00321 
00323   void Set(const PixelType & value) const
00324   {
00325     this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
00326                                           this->m_Buffer + this->m_Offset ) ), value);
00327   }
00328 
00332   const PixelType & Value(void) const
00333   { return *( m_Buffer + m_Offset ); }
00334 
00338   const PixelType & Value(void)
00339   { return *( m_Buffer + m_Offset ); }
00340 
00344   Self Begin() const;
00345 
00348   void GoToBegin()
00349   {
00350     m_Offset = m_BeginOffset;
00351   }
00352 
00356   Self End() const;
00357 
00360   void GoToEnd()
00361   {
00362     m_Offset = m_EndOffset;
00363   }
00364 
00367   bool IsAtBegin()
00368   {
00369     return ( m_Offset == m_BeginOffset );
00370   }
00371 
00374   bool IsAtEnd()
00375   {
00376     return ( m_Offset == m_EndOffset );
00377   }
00378 
00379 protected: //made protected so other iterators can access
00380   typename ImageType::ConstWeakPointer m_Image;
00381 
00382   RegionType m_Region;                              // region to iterate over
00383 
00384   SizeValueType m_Offset;
00385   SizeValueType m_BeginOffset;  // offset to last pixel in region
00386   SizeValueType m_EndOffset;    // offset to one pixel before first pixel
00387 
00388   const InternalPixelType *m_Buffer;
00389 
00390   AccessorType        m_PixelAccessor;
00391   AccessorFunctorType m_PixelAccessorFunctor;
00392 };
00393 } // end namespace itk
00394 
00395 // Define instantiation macro for this template.
00396 #define ITK_TEMPLATE_ImageReverseConstIterator(_, EXPORT, TypeX, TypeY)                         \
00397   namespace itk                                                                                 \
00398   {                                                                                             \
00399   _( 1 ( class EXPORT ImageReverseConstIterator< ITK_TEMPLATE_1 TypeX > ) )                     \
00400   namespace Templates                                                                           \
00401   {                                                                                             \
00402   typedef ImageReverseConstIterator< ITK_TEMPLATE_1 TypeX > ImageReverseConstIterator##TypeY; \
00403   }                                                                                             \
00404   }
00405 
00406 #if ITK_TEMPLATE_EXPLICIT
00407 #include "Templates/itkImageReverseConstIterator+-.h"
00408 #endif
00409 
00410 #if ITK_TEMPLATE_TXX
00411 #include "itkImageReverseConstIterator.hxx"
00412 #endif
00413 
00414 #endif
00415