ITK  5.2.0
Insight Toolkit
itkImageConstIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageConstIterator_h
19 #define itkImageConstIterator_h
20 
21 #include "itkImage.h"
22 #include "itkIndex.h"
23 #include "itkNumericTraits.h"
24 
25 namespace itk
26 {
83 template <typename TImage>
84 class ITK_TEMPLATE_EXPORT ImageConstIterator
85 {
86 public:
89 
94  static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
95 
97  itkTypeMacroNoParent(ImageConstIterator);
98 
100  using IndexType = typename TImage::IndexType;
101 
103  using SizeType = typename TImage::SizeType;
104 
106  using OffsetType = typename TImage::OffsetType;
107 
109  using RegionType = typename TImage::RegionType;
110 
112  using ImageType = TImage;
113 
117  using PixelContainer = typename TImage::PixelContainer;
118  using PixelContainerPointer = typename PixelContainer::Pointer;
119 
121  using InternalPixelType = typename TImage::InternalPixelType;
122 
124  using PixelType = typename TImage::PixelType;
125 
128  using AccessorType = typename TImage::AccessorType;
129  using AccessorFunctorType = typename TImage::AccessorFunctorType;
130 
134  : m_Region()
135  , m_PixelAccessor()
136  , m_PixelAccessorFunctor()
137  {
138  m_Image = nullptr;
139  m_Buffer = nullptr;
140  m_Offset = 0;
141  m_BeginOffset = 0;
142  m_EndOffset = 0;
143  m_PixelAccessorFunctor.SetBegin(m_Buffer);
144  }
146 
148  virtual ~ImageConstIterator() = default;
149 
153  {
154  m_Image = it.m_Image; // copy the smart pointer
155 
156  m_Region = it.m_Region;
157 
158  m_Buffer = it.m_Buffer;
159  m_Offset = it.m_Offset;
160  m_BeginOffset = it.m_BeginOffset;
161  m_EndOffset = it.m_EndOffset;
162  m_PixelAccessor = it.m_PixelAccessor;
163  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
164  m_PixelAccessorFunctor.SetBegin(m_Buffer);
165  }
166 
169  ImageConstIterator(const ImageType * ptr, const RegionType & region)
170  {
171  m_Image = ptr;
172  m_Buffer = m_Image->GetBufferPointer();
174 
175  SetRegion(region);
176 
177  m_PixelAccessor = ptr->GetPixelAccessor();
178  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
179  m_PixelAccessorFunctor.SetBegin(m_Buffer);
180  }
181 
184  Self &
185  operator=(const Self & it)
186  {
187  if (this != &it)
188  {
189  m_Image = it.m_Image; // copy the smart pointer
190  m_Region = it.m_Region;
192 
193  m_Buffer = it.m_Buffer;
194  m_Offset = it.m_Offset;
195  m_BeginOffset = it.m_BeginOffset;
196  m_EndOffset = it.m_EndOffset;
197  m_PixelAccessor = it.m_PixelAccessor;
198  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
199  m_PixelAccessorFunctor.SetBegin(m_Buffer);
200  }
201  return *this;
202  }
203 
205  virtual void
206  SetRegion(const RegionType & region)
207  {
208  m_Region = region;
209 
210  if (region.GetNumberOfPixels() > 0) // If region is non-empty
211  {
212  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
213  itkAssertOrThrowMacro((bufferedRegion.IsInside(m_Region)),
214  "Region " << m_Region << " is outside of buffered region " << bufferedRegion);
215  }
216 
217  // Compute the start offset
218  m_Offset = m_Image->ComputeOffset(m_Region.GetIndex());
219  m_BeginOffset = m_Offset;
220 
221  // Compute the end offset. If any component of m_Region.GetSize()
222  // is zero, the region is not valid and we set the EndOffset
223  // to be same as BeginOffset so that iterator end condition is met
224  // immediately.
225  IndexType ind(m_Region.GetIndex());
226  SizeType size(m_Region.GetSize());
227  if (m_Region.GetNumberOfPixels() == 0)
228  {
229  // region is empty, probably has a size of 0 along one dimension
230  m_EndOffset = m_BeginOffset;
231  }
232  else
233  {
234  for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
235  {
236  ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
237  }
238  m_EndOffset = m_Image->ComputeOffset(ind);
239  m_EndOffset++;
240  }
241  }
242 
244  static unsigned int
246  {
247  return TImage::ImageDimension;
248  }
249 
252  bool
253  operator!=(const Self & it) const
254  {
255  // two iterators are the same if they "point to" the same memory location
256  return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
257  }
258 
261  bool
262  operator==(const Self & it) const
263  {
264  // two iterators are the same if they "point to" the same memory location
265  return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
266  }
267 
270  bool
271  operator<=(const Self & it) const
272  {
273  // an iterator is "less than" another if it "points to" a lower
274  // memory location
275  return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
276  }
277 
280  bool
281  operator<(const Self & it) const
282  {
283  // an iterator is "less than" another if it "points to" a lower
284  // memory location
285  return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
286  }
287 
290  bool
291  operator>=(const Self & it) const
292  {
293  // an iterator is "greater than" another if it "points to" a higher
294  // memory location
295  return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
296  }
297 
300  bool
301  operator>(const Self & it) const
302  {
303  // an iterator is "greater than" another if it "points to" a higher
304  // memory location
305  return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
306  }
307 
312  const IndexType
313  GetIndex() const
314  {
315  return m_Image->ComputeIndex(static_cast<OffsetValueType>(m_Offset));
316  }
317 
320  virtual void
321  SetIndex(const IndexType & ind)
322  {
323  m_Offset = m_Image->ComputeOffset(ind);
324  }
325 
328  const RegionType &
329  GetRegion() const
330  {
331  return m_Region;
332  }
333 
335  const ImageType *
336  GetImage() const
337  {
338  return m_Image.GetPointer();
339  }
340 
342  PixelType
343  Get() const
344  {
345  return m_PixelAccessorFunctor.Get(*(m_Buffer + m_Offset));
346  }
347 
351  const PixelType &
352  Value() const
353  {
354  return *(m_Buffer + m_Offset);
355  }
356 
359  void
361  {
362  m_Offset = m_BeginOffset;
363  }
364 
367  void
369  {
370  m_Offset = m_EndOffset;
371  }
372 
375  bool
376  IsAtBegin() const
377  {
378  return (m_Offset == m_BeginOffset);
379  }
380 
383  bool
384  IsAtEnd() const
385  {
386  return (m_Offset == m_EndOffset);
387  }
388 
389 protected: // made protected so other iterators can access
390  typename TImage::ConstWeakPointer m_Image;
391 
392  RegionType m_Region; // region to iterate over
393 
395  OffsetValueType m_BeginOffset; // offset to first pixel in region
396  OffsetValueType m_EndOffset; // offset to one pixel past last pixel in region
397 
398  const InternalPixelType * m_Buffer;
399 
400  AccessorType m_PixelAccessor;
401  AccessorFunctorType m_PixelAccessorFunctor;
402 };
403 } // end namespace itk
404 
405 #endif
itk::ImageConstIterator::ImageConstIterator
ImageConstIterator()
Definition: itkImageConstIterator.h:133
itk::ImageConstIterator::operator!=
bool operator!=(const Self &it) const
Definition: itkImageConstIterator.h:253
itk::ImageConstIterator< ImageType >::SizeType
typename ImageType ::SizeType SizeType
Definition: itkImageConstIterator.h:103
itk::ImageConstIterator< ImageType >::PixelType
typename ImageType ::PixelType PixelType
Definition: itkImageConstIterator.h:124
itk::ImageConstIterator::m_PixelAccessorFunctor
AccessorFunctorType m_PixelAccessorFunctor
Definition: itkImageConstIterator.h:401
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:546
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:560
itk::ImageConstIterator::m_BeginOffset
OffsetValueType m_BeginOffset
Definition: itkImageConstIterator.h:395
itk::ImageConstIterator::m_Buffer
const InternalPixelType * m_Buffer
Definition: itkImageConstIterator.h:398
itk::ImageConstIterator< ImageType >::OffsetType
typename ImageType ::OffsetType OffsetType
Definition: itkImageConstIterator.h:106
itk::ImageConstIterator::GetImage
const ImageType * GetImage() const
Definition: itkImageConstIterator.h:336
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::ImageConstIterator::SetRegion
virtual void SetRegion(const RegionType &region)
Definition: itkImageConstIterator.h:206
itk::ImageConstIterator::operator=
Self & operator=(const Self &it)
Definition: itkImageConstIterator.h:185
itk::ImageConstIterator::ImageConstIterator
ImageConstIterator(const Self &it)
Definition: itkImageConstIterator.h:152
itk::ImageConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageConstIterator.h:360
itk::ImageConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkImageConstIterator.h:384
itk::ImageConstIterator::m_Region
RegionType m_Region
Definition: itkImageConstIterator.h:392
itk::ImageConstIterator< ImageType >::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImageConstIterator.h:118
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageConstIterator::operator>=
bool operator>=(const Self &it) const
Definition: itkImageConstIterator.h:291
itk::ImageConstIterator::GetIndex
const IndexType GetIndex() const
Definition: itkImageConstIterator.h:313
itk::ImageConstIterator::GetImageIteratorDimension
static unsigned int GetImageIteratorDimension()
Definition: itkImageConstIterator.h:245
itk::ImageConstIterator::m_Image
TImage::ConstWeakPointer m_Image
Definition: itkImageConstIterator.h:390
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::ImageConstIterator::GoToEnd
void GoToEnd()
Definition: itkImageConstIterator.h:368
itk::ImageConstIterator::m_Offset
OffsetValueType m_Offset
Definition: itkImageConstIterator.h:394
itk::ImageConstIterator::m_PixelAccessor
AccessorType m_PixelAccessor
Definition: itkImageConstIterator.h:400
itkIndex.h
itk::ImageConstIterator::IsAtBegin
bool IsAtBegin() const
Definition: itkImageConstIterator.h:376
itk::ImageConstIterator::Value
const PixelType & Value() const
Definition: itkImageConstIterator.h:352
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ImageConstIterator::Get
PixelType Get() const
Definition: itkImageConstIterator.h:343
itk::ImageConstIterator
A multi-dimensional image iterator templated over image type.
Definition: itkImageConstIterator.h:84
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::ImageConstIterator< ImageType >::ImageType
ImageType ImageType
Definition: itkImageConstIterator.h:112
itk::ImageConstIterator< ImageType >::PixelContainer
typename ImageType ::PixelContainer PixelContainer
Definition: itkImageConstIterator.h:117
itk::ImageConstIterator::GetRegion
const RegionType & GetRegion() const
Definition: itkImageConstIterator.h:329
itk::ImageConstIterator::SetIndex
virtual void SetIndex(const IndexType &ind)
Definition: itkImageConstIterator.h:321
itkNumericTraits.h
itk::ImageConstIterator< ImageType >::IndexType
typename ImageType ::IndexType IndexType
Definition: itkImageConstIterator.h:100
itk::ImageConstIterator::ImageConstIterator
ImageConstIterator(const ImageType *ptr, const RegionType &region)
Definition: itkImageConstIterator.h:169
itk::ImageConstIterator::operator>
bool operator>(const Self &it) const
Definition: itkImageConstIterator.h:301
itk::ImageConstIterator::m_EndOffset
OffsetValueType m_EndOffset
Definition: itkImageConstIterator.h:396
itk::ImageConstIterator::operator==
bool operator==(const Self &it) const
Definition: itkImageConstIterator.h:262