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
00025 namespace itk
00026 {
00027
00083 template<typename TImage>
00084 class ITK_EXPORT ImageConstIterator
00085 {
00086 public:
00088 typedef ImageConstIterator Self;
00089
00094 itkStaticConstMacro(ImageIteratorDimension, unsigned int,
00095 TImage::ImageDimension);
00096
00098 typedef typename TImage::IndexType IndexType;
00099 typedef typename TImage::IndexValueType IndexValueType;
00100
00102 typedef typename TImage::SizeType SizeType;
00103 typedef typename TImage::SizeValueType SizeValueType;
00104
00106 typedef typename TImage::OffsetType OffsetType;
00107 typedef typename TImage::OffsetValueType OffsetValueType;
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 typedef typename TImage::AccessorFunctorType AccessorFunctorType;
00131
00134 ImageConstIterator()
00135 : m_Region(),
00136 m_PixelAccessor(),
00137 m_PixelAccessorFunctor()
00138 {
00139 m_Image = 0;
00140 m_Buffer = 0;
00141 m_Offset = 0;
00142 m_BeginOffset = 0;
00143 m_EndOffset = 0;
00144 m_PixelAccessorFunctor.SetBegin( m_Buffer );
00145 }
00146
00148 virtual ~ImageConstIterator() {};
00149
00152 ImageConstIterator(const Self& it)
00153 {
00154 m_Image = it.m_Image;
00155
00156 m_Region = it.m_Region;
00157
00158 m_Buffer = it.m_Buffer;
00159 m_Offset = it.m_Offset;
00160 m_BeginOffset = it.m_BeginOffset;
00161 m_EndOffset = it.m_EndOffset;
00162 m_PixelAccessor = it.m_PixelAccessor;
00163 m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00164 m_PixelAccessorFunctor.SetBegin( m_Buffer );
00165 }
00166
00169 ImageConstIterator( const ImageType *ptr,
00170 const RegionType ®ion )
00171 {
00172 m_Image = ptr;
00173 m_Buffer = m_Image->GetBufferPointer();
00174 m_Region = region;
00175
00176
00177 m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
00178 m_BeginOffset = m_Offset;
00179
00180
00181
00182
00183
00184 if (m_Region.GetNumberOfPixels() == 0)
00185 {
00186
00187 m_EndOffset = m_BeginOffset;
00188 }
00189 else
00190 {
00191 IndexType ind(m_Region.GetIndex());
00192 SizeType size(m_Region.GetSize());
00193 for (unsigned int i=0; i < ImageIteratorDimension; ++i)
00194 {
00195 ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
00196 }
00197 m_EndOffset = m_Image->ComputeOffset( ind );
00198 m_EndOffset++;
00199 }
00200
00201 m_PixelAccessor = ptr->GetPixelAccessor();
00202 m_PixelAccessorFunctor.SetPixelAccessor( m_PixelAccessor );
00203 m_PixelAccessorFunctor.SetBegin( m_Buffer );
00204 }
00205
00208 Self &operator=(const Self& it)
00209 {
00210 m_Image = it.m_Image;
00211 m_Region = it.m_Region;
00212
00213 m_Buffer = it.m_Buffer;
00214 m_Offset = it.m_Offset;
00215 m_BeginOffset = it.m_BeginOffset;
00216 m_EndOffset = it.m_EndOffset;
00217 m_PixelAccessor = it.m_PixelAccessor;
00218 m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
00219 m_PixelAccessorFunctor.SetBegin( m_Buffer );
00220
00221 return *this;
00222 }
00223
00225 static unsigned int GetImageIteratorDimension()
00226 {return ImageIteratorDimension;}
00227
00230 bool
00231 operator!=(const Self &it) const
00232 {
00233
00234 return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
00235 };
00236
00239 bool
00240 operator==(const Self &it) const
00241 {
00242
00243 return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
00244 };
00245
00248 bool
00249 operator<=(const Self &it) const
00250 {
00251
00252
00253 return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
00254 };
00255
00258 bool
00259 operator<(const Self &it) const
00260 {
00261
00262
00263 return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
00264 };
00265
00268 bool
00269 operator>=(const Self &it) const
00270 {
00271
00272
00273 return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
00274 };
00275
00278 bool
00279 operator>(const Self &it) const
00280 {
00281
00282
00283 return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
00284 };
00285
00290 const IndexType GetIndex() const
00291 { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) ); }
00292
00295 virtual void SetIndex(const IndexType &ind)
00296 { m_Offset = m_Image->ComputeOffset( ind ); }
00297
00300 const RegionType& GetRegion() const
00301 { return m_Region; };
00302
00304 const ImageType * GetImage() const
00305 { return m_Image.GetPointer(); };
00306
00308 PixelType Get(void) const
00309 { return m_PixelAccessorFunctor.Get(*(m_Buffer+m_Offset)); }
00310
00314 const PixelType & Value(void) const
00315 { return *(m_Buffer + m_Offset); }
00316
00321 Self Begin(void) const;
00322
00325 void GoToBegin()
00326 {
00327 m_Offset = m_BeginOffset;
00328 };
00329
00334 Self End(void) const;
00335
00338 void GoToEnd()
00339 {
00340 m_Offset = m_EndOffset;
00341 };
00342
00345 bool IsAtBegin(void) const
00346 {
00347 return (m_Offset == m_BeginOffset);
00348 }
00349
00352 bool IsAtEnd(void) const
00353 {
00354 return (m_Offset == m_EndOffset);
00355 }
00356
00357
00358 protected:
00359 typename TImage::ConstWeakPointer m_Image;
00360 RegionType m_Region;
00361
00362 unsigned long m_Offset;
00363 unsigned long m_BeginOffset;
00364 unsigned long m_EndOffset;
00365
00366 const InternalPixelType * m_Buffer;
00367
00368 AccessorType m_PixelAccessor;
00369 AccessorFunctorType m_PixelAccessorFunctor;
00370 };
00371
00372 }
00373
00374 #ifndef ITK_MANUAL_INSTANTIATION
00375 #include "itkImageConstIterator.txx"
00376 #endif
00377
00378 #endif