00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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;
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 ®ion )
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 )
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
00189 m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00190 m_BeginOffset = m_Offset;
00191
00192
00193
00194
00195
00196 if (m_Region.GetNumberOfPixels() == 0)
00197 {
00198
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;
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
00246 return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
00247 };
00249
00252 bool
00253 operator==(const Self &it) const
00254 {
00255
00256 return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00257 };
00259
00262 bool
00263 operator<=(const Self &it) const
00264 {
00265
00266
00267 return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00268 };
00270
00273 bool
00274 operator<(const Self &it) const
00275 {
00276
00277
00278 return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00279 };
00281
00284 bool
00285 operator>=(const Self &it) const
00286 {
00287
00288
00289 return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00290 };
00292
00295 bool
00296 operator>(const Self &it) const
00297 {
00298
00299
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:
00377 typename TImage::ConstWeakPointer m_Image;
00378 RegionType m_Region;
00379
00380 unsigned long m_Offset;
00381 unsigned long m_BeginOffset;
00382 unsigned long m_EndOffset;
00383
00384 const InternalPixelType * m_Buffer;
00385
00386 AccessorType m_PixelAccessor;
00387 AccessorFunctorType m_PixelAccessorFunctor;
00388 };
00389
00390 }
00391
00392
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