ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkImageRegionReverseConstIterator.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 __itkImageRegionReverseConstIterator_h
00019 #define __itkImageRegionReverseConstIterator_h
00020 
00021 #include "itkImageReverseConstIterator.h"
00022 #include "itkImageRegionIterator.h"
00023 
00024 namespace itk
00025 {
00102 template< typename TImage >
00103 class ITK_EXPORT ImageRegionReverseConstIterator:public ImageReverseConstIterator< TImage >
00104 {
00105 public:
00106 
00108   typedef ImageRegionReverseConstIterator     Self;
00109   typedef ImageReverseConstIterator< TImage > Superclass;
00110 
00115   enum { ImageIteratorDimension = Superclass::ImageIteratorDimension };
00116 
00119   typedef typename Superclass::IndexType      IndexType;
00120 
00123   typedef typename Superclass::SizeType      SizeType;
00124 
00127   typedef typename Superclass::OffsetType      OffsetType;
00128 
00130   typedef typename Superclass::RegionType RegionType;
00131 
00134   typedef typename Superclass::ImageType ImageType;
00135 
00139   typedef typename Superclass::PixelContainer PixelContainer;
00140   typedef typename PixelContainer::Pointer    PixelContainerPointer;
00141 
00143   typedef typename Superclass::InternalPixelType InternalPixelType;
00144 
00146   typedef typename Superclass::PixelType PixelType;
00147 
00150   typedef typename Superclass::AccessorType AccessorType;
00151 
00153   itkTypeMacro(ImageRegionReverseConstIterator, ImageReverseConstIterator);
00154 
00156   ImageRegionReverseConstIterator():Superclass()
00157   {
00158     m_SpanBeginOffset = 0;
00159     m_SpanEndOffset = 0;
00160   }
00161 
00164   ImageRegionReverseConstIterator(const ImageType *ptr, const RegionType & region):
00165     Superclass(ptr, region)
00166   {
00167     m_SpanBeginOffset = this->m_BeginOffset;
00168     m_SpanEndOffset = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00169   }
00171 
00179   ImageRegionReverseConstIterator(const ImageConstIterator< TImage > & it):Superclass(it)
00180   {
00181     IndexType ind = this->GetIndex();
00182 
00183     m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
00184                         - ( ind[0] - this->m_Region.GetIndex()[0] );
00185     m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00186   }
00187 
00190   ImageRegionReverseConstIterator(const ImageReverseConstIterator< TImage > & it):Superclass(it)
00191   {
00192     IndexType ind = this->GetIndex();
00193 
00194     m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
00195                         - ( ind[0] - this->m_Region.GetIndex()[0] );
00196     m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00197   }
00198 
00201   ImageRegionReverseConstIterator(const ImageRegionIterator< TImage > & it):Superclass(it)
00202   {
00203     IndexType ind = this->GetIndex();
00204 
00205     m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
00206                         - ( ind[0] - this->m_Region.GetIndex()[0] );
00207     m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00208   }
00209 
00212   void GoToBegin()
00213   {
00214     Superclass::GoToBegin();
00215 
00216     // reset the span offsets
00217     m_SpanBeginOffset = this->m_BeginOffset;
00218     m_SpanEndOffset   = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00219   }
00220 
00223   void GoToEnd()
00224   {
00225     Superclass::GoToEnd();
00226 
00227     // reset the span offsets
00228     m_SpanEndOffset = this->m_EndOffset;
00229     m_SpanBeginOffset = m_SpanEndOffset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00230   }
00231 
00235   Self Begin(void) const;
00236 
00241   Self End(void) const;
00242 
00246   void SetIndex(const IndexType & ind)
00247   {
00248     Superclass::SetIndex(ind);
00249     m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
00250                         - ( ind[0] - this->m_Region.GetIndex()[0] );
00251     m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
00252   }
00254 
00263   Self &
00264   operator++()
00265   {
00266     if ( --this->m_Offset <= m_SpanEndOffset )
00267       {
00268       // We have past the beginning of the span (row), need to wrap around.
00269 
00270       // First move forward one pixel, because we are going to use a different
00271       // algorithm to compute the next pixel
00272       this->m_Offset++;
00273 
00274       // Get the index of the first pixel on the span (row)
00275       typename ImageConstIterator< TImage >::IndexType
00276       ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
00277 
00278       const typename ImageConstIterator< TImage >::IndexType &
00279       startIndex = this->m_Region.GetIndex();
00280       const typename ImageConstIterator< TImage >::SizeType &
00281       size = this->m_Region.GetSize();
00282 
00283       // Deccrement along a row, then wrap at the beginning of the region row.
00284       bool         done;
00285       unsigned int dim;
00286 
00287       // Check to see if we are past the first pixel in the region
00288       // Note that --ind[0] moves to the previous pixel along the row.
00289       done = ( --ind[0] == startIndex[0] - 1 );
00290       for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
00291         {
00292         done = ( ind[i] == startIndex[i] );
00293         }
00294 
00295       // if the iterator is outside the region (but not past region begin) then
00296       // we need to wrap around the region
00297       dim = 0;
00298       if ( !done )
00299         {
00300         while ( ( dim < ImageIteratorDimension - 1 )
00301                 && ( ind[dim] < startIndex[dim] ) )
00302           {
00303           ind[dim] = startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1;
00304           ind[++dim]--;
00305           }
00306         }
00307       this->m_Offset = this->m_Image->ComputeOffset(ind);
00308       m_SpanBeginOffset = this->m_Offset;
00309       m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( size[0] );
00310       }
00311     return *this;
00312   }
00313 
00322   Self & operator--()
00323   {
00324     if ( ++this->m_Offset >= m_SpanBeginOffset )
00325       {
00326       // We have reached the end of the span (row), need to wrap around.
00327 
00328       // First back up one pixel, because we are going to use a different
00329       // algorithm to compute the next pixel
00330       --this->m_Offset;
00331 
00332       // Get the index of the last pixel on the span (row)
00333       typename ImageConstIterator< TImage >::IndexType
00334       ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
00335 
00336       const typename ImageIterator< TImage >::IndexType &
00337       startIndex = this->m_Region.GetIndex();
00338       const typename ImageIterator< TImage >::SizeType &
00339       size = this->m_Region.GetSize();
00340 
00341       // Increment along a row, then wrap at the end of the region row.
00342       bool         done;
00343       unsigned int dim;
00344 
00345       // Check to see if we are past the last pixel in the region
00346       // Note that ++ind[0] moves to the next pixel along the row.
00347       done = ( ++ind[0] == startIndex[0] + static_cast< OffsetValueType >( size[0] ) );
00348       for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
00349         {
00350         done = ( ind[i] == startIndex[i] + static_cast< OffsetValueType >( size[i] ) - 1 );
00351         }
00352 
00353       // if the iterator is outside the region (but not past region end) then
00354       // we need to wrap around the region
00355       dim = 0;
00356       if ( !done )
00357         {
00358         while ( ( dim < ImageIteratorDimension - 1 )
00359                 && ( ind[dim] > startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1 ) )
00360           {
00361           ind[dim] = startIndex[dim];
00362           ind[++dim]++;
00363           }
00364         }
00365       this->m_Offset = this->m_Image->ComputeOffset(ind);
00366       m_SpanBeginOffset = this->m_Offset;
00367       m_SpanEndOffset = this->m_Offset - static_cast< OffsetValueType >( size[0] );
00368       }
00369     return *this;
00370   }
00371 
00372 protected:
00373   SizeValueType m_SpanBeginOffset; // offset to last pixel in the row
00374   SizeValueType m_SpanEndOffset;   // offset to one pixel before the row
00375 };
00376 } // end namespace itk
00377 
00378 // Define instantiation macro for this template.
00379 #define ITK_TEMPLATE_ImageRegionReverseConstIterator(_, EXPORT, TypeX, TypeY)                               \
00380   namespace itk                                                                                             \
00381   {                                                                                                         \
00382   _( 1 ( class EXPORT ImageRegionReverseConstIterator< ITK_TEMPLATE_1 TypeX > ) )                           \
00383   namespace Templates                                                                                       \
00384   {                                                                                                         \
00385   typedef ImageRegionReverseConstIterator< ITK_TEMPLATE_1 TypeX > ImageRegionReverseConstIterator##TypeY; \
00386   }                                                                                                         \
00387   }
00388 
00389 #if ITK_TEMPLATE_EXPLICIT
00390 #include "Templates/itkImageRegionReverseConstIterator+-.h"
00391 #endif
00392 
00393 #if ITK_TEMPLATE_TXX
00394 #include "itkImageRegionReverseConstIterator.hxx"
00395 #endif
00396 
00397 #endif
00398