00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkImageRegionReverseConstIterator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2002/09/30 12:41:11 $ 00007 Version: $Revision: 1.1 $ 00008 00009 Copyright (c) 2002 Insight 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 00071 template<typename TImage> 00072 class ImageRegionReverseConstIterator : public ImageReverseConstIterator<TImage> 00073 { 00074 public: 00076 typedef ImageRegionReverseConstIterator Self; 00077 typedef ImageReverseConstIterator<TImage> Superclass; 00078 00083 enum { ImageIteratorDimension = Superclass::ImageIteratorDimension }; 00084 00087 typedef typename Superclass::IndexType IndexType; 00088 typedef typename Superclass::IndexValueType IndexValueType; 00089 00092 typedef typename Superclass::SizeType SizeType; 00093 typedef typename Superclass::SizeValueType SizeValueType; 00094 00097 typedef typename Superclass::OffsetType OffsetType; 00098 typedef typename Superclass::OffsetValueType OffsetValueType; 00099 00101 typedef typename Superclass::RegionType RegionType; 00102 00105 typedef typename Superclass::ImageType ImageType; 00106 00110 typedef typename Superclass::PixelContainer PixelContainer; 00111 typedef typename PixelContainer::Pointer PixelContainerPointer; 00112 00114 typedef typename Superclass::InternalPixelType InternalPixelType; 00115 00117 typedef typename Superclass::PixelType PixelType; 00118 00121 typedef typename Superclass::AccessorType AccessorType; 00122 00124 itkTypeMacro(ImageRegionReverseConstIterator, ImageReverseConstIterator); 00125 00127 ImageRegionReverseConstIterator() : Superclass() 00128 { 00129 m_SpanBeginOffset = 0; 00130 m_SpanEndOffset = 0; 00131 } 00132 00135 ImageRegionReverseConstIterator(ImageType *ptr, 00136 const RegionType ®ion) 00137 : Superclass(ptr, region) 00138 { 00139 m_SpanBeginOffset = m_BeginOffset; 00140 m_SpanEndOffset = m_BeginOffset - static_cast<long>(m_Region.GetSize()[0]); 00141 } 00142 00150 ImageRegionReverseConstIterator( const ImageConstIterator<TImage> &it):Superclass(it) 00151 { 00152 IndexType ind = this->GetIndex(); 00153 m_SpanBeginOffset = m_Offset + static_cast<long>(m_Region.GetSize()[0]) 00154 - (ind[0] - m_Region.GetIndex()[0]); 00155 m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(m_Region.GetSize()[0]); 00156 } 00157 00160 ImageRegionReverseConstIterator( const ImageReverseConstIterator<TImage> &it):Superclass(it) 00161 { 00162 IndexType ind = this->GetIndex(); 00163 m_SpanBeginOffset = m_Offset + static_cast<long>(m_Region.GetSize()[0]) 00164 - (ind[0] - m_Region.GetIndex()[0]); 00165 m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(m_Region.GetSize()[0]); 00166 } 00167 00170 ImageRegionReverseConstIterator( const ImageRegionIterator<TImage> &it):Superclass(it) 00171 { 00172 IndexType ind = this->GetIndex(); 00173 m_SpanBeginOffset = m_Offset + static_cast<long>(m_Region.GetSize()[0]) 00174 - (ind[0] - m_Region.GetIndex()[0]); 00175 m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(m_Region.GetSize()[0]); 00176 } 00177 00181 Self Begin(void) const; 00182 00186 Self End(void) const; 00187 00188 00192 void SetIndex(const IndexType &ind) 00193 { Superclass::SetIndex(ind); 00194 m_SpanBeginOffset = m_Offset + static_cast<long>(m_Region.GetSize()[0]) 00195 - (ind[0] - m_Region.GetIndex()[0]); 00196 m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(m_Region.GetSize()[0]); 00197 } 00198 00207 Self & 00208 operator++() 00209 { 00210 if (--m_Offset <= m_SpanEndOffset) 00211 { 00212 // We have past the beginning of the span (row), need to wrap around. 00213 00214 // First move forward one pixel, because we are going to use a different 00215 // algorithm to compute the next pixel 00216 m_Offset++; 00217 00218 // Get the index of the first pixel on the span (row) 00219 typename ImageConstIterator<TImage>::IndexType 00220 ind = m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) ); 00221 00222 const typename ImageConstIterator<TImage>::IndexType& 00223 startIndex = m_Region.GetIndex(); 00224 const typename ImageConstIterator<TImage>::SizeType& 00225 size = m_Region.GetSize(); 00226 00227 // Deccrement along a row, then wrap at the beginning of the region row. 00228 bool done; 00229 unsigned int dim; 00230 00231 // Check to see if we are past the first pixel in the region 00232 // Note that --ind[0] moves to the previous pixel along the row. 00233 done = (--ind[0] == startIndex[0] - 1); 00234 for (unsigned int i=1; done && i < ImageIteratorDimension; i++) 00235 { 00236 done = (ind[i] == startIndex[i]); 00237 } 00238 00239 // if the iterator is outside the region (but not past region begin) then 00240 // we need to wrap around the region 00241 dim = 0; 00242 if (!done) 00243 { 00244 while ( (dim < ImageIteratorDimension - 1) 00245 && (ind[dim] < startIndex[dim]) ) 00246 { 00247 ind[dim] = startIndex[dim] + static_cast<long>(size[dim]) - 1; 00248 ind[++dim]--; 00249 } 00250 } 00251 m_Offset = m_Image->ComputeOffset( ind ); 00252 m_SpanBeginOffset = m_Offset; 00253 m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(size[0]); 00254 } 00255 return *this; 00256 } 00257 00266 Self & operator--() 00267 { 00268 if (++m_Offset >= m_SpanBeginOffset) 00269 { 00270 // We have reached the end of the span (row), need to wrap around. 00271 00272 // First back up one pixel, because we are going to use a different 00273 // algorithm to compute the next pixel 00274 --m_Offset; 00275 00276 // Get the index of the last pixel on the span (row) 00277 typename ImageConstIterator<TImage>::IndexType 00278 ind = m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) ); 00279 00280 const typename ImageIterator<TImage>::IndexType& 00281 startIndex = m_Region.GetIndex(); 00282 const typename ImageIterator<TImage>::SizeType& 00283 size = m_Region.GetSize(); 00284 00285 // Increment along a row, then wrap at the end of the region row. 00286 bool done; 00287 unsigned int dim; 00288 00289 // Check to see if we are past the last pixel in the region 00290 // Note that ++ind[0] moves to the next pixel along the row. 00291 done = (++ind[0] == startIndex[0] + static_cast<long>(size[0])); 00292 for (unsigned int i=1; done && i < ImageIteratorDimension; i++) 00293 { 00294 done = (ind[i] == startIndex[i] + static_cast<long>(size[i]) - 1); 00295 } 00296 00297 // if the iterator is outside the region (but not past region end) then 00298 // we need to wrap around the region 00299 dim = 0; 00300 if (!done) 00301 { 00302 while ( (dim < ImageIteratorDimension - 1) 00303 && (ind[dim] > startIndex[dim] + static_cast<long>(size[dim]) - 1) ) 00304 { 00305 ind[dim] = startIndex[dim]; 00306 ind[++dim]++; 00307 } 00308 } 00309 m_Offset = m_Image->ComputeOffset( ind ); 00310 m_SpanBeginOffset = m_Offset; 00311 m_SpanEndOffset = m_Offset - static_cast<long>(size[0]); 00312 } 00313 return *this; 00314 } 00315 00316 protected: 00317 unsigned long m_SpanBeginOffset; // offset to last pixel in the row 00318 unsigned long m_SpanEndOffset; // offset to one pixel before the row 00319 00320 }; 00321 00322 } // end namespace itk 00323 00324 #ifndef ITK_MANUAL_INSTANTIATION 00325 #include "itkImageRegionReverseConstIterator.txx" 00326 #endif 00327 00328 #endif