ITK  5.2.0
Insight Toolkit
itkImageReverseConstIterator.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 itkImageReverseConstIterator_h
19 #define itkImageReverseConstIterator_h
20 
21 #include "itkSize.h"
22 #include "itkImageConstIterator.h"
23 #include <memory>
24 
25 namespace itk
26 {
86 template <typename TImage>
87 class ITK_TEMPLATE_EXPORT ImageReverseConstIterator
88 {
89 public:
92 
97  static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
98 
100  itkTypeMacroNoParent(ImageReverseConstIterator);
101 
103  using IndexType = typename TImage::IndexType;
104 
106  using SizeType = typename TImage::SizeType;
107 
109  using OffsetType = typename TImage::OffsetType;
110 
112  using RegionType = typename TImage::RegionType;
113 
115  using ImageType = TImage;
116 
120  using PixelContainer = typename TImage::PixelContainer;
121  using PixelContainerPointer = typename PixelContainer::Pointer;
122 
124  using InternalPixelType = typename TImage::InternalPixelType;
125 
127  using PixelType = typename TImage::PixelType;
128 
131  using AccessorType = typename TImage::AccessorType;
132 
134  using AccessorFunctorType = typename TImage::AccessorFunctorType;
135 
139  : m_PixelAccessor()
140  , m_PixelAccessorFunctor()
141  {
142  m_Buffer = 0;
143  m_Offset = 0;
144  m_BeginOffset = 0;
145  m_EndOffset = 0;
146  m_PixelAccessorFunctor.SetBegin(m_Buffer);
147  }
149 
151  virtual ~ImageReverseConstIterator() = default;
152 
156  {
157  m_Image = it.m_Image; // copy the smart pointer
158 
159  m_Region = it.m_Region;
160 
161  m_Buffer = it.m_Buffer;
162  m_Offset = it.m_Offset;
163  m_BeginOffset = it.m_BeginOffset;
164  m_EndOffset = it.m_EndOffset;
165  m_PixelAccessor = it.m_PixelAccessor;
166  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
167  m_PixelAccessorFunctor.SetBegin(m_Buffer);
168  }
169 
172  ImageReverseConstIterator(const ImageType * ptr, const RegionType & region)
173  {
174  SizeValueType offset;
175 
176  m_Image = ptr;
177  m_Buffer = m_Image->GetBufferPointer();
178  m_Region = region;
179 
180  // Compute the end offset, one pixel before the first pixel
181  offset = m_Image->ComputeOffset(m_Region.GetIndex());
182  m_EndOffset = offset - 1;
183 
184  // Compute the begin offset, the last pixel in the region
185  IndexType ind(m_Region.GetIndex());
186  SizeType size(m_Region.GetSize());
187  for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
188  {
189  ind[i] += (size[i] - 1);
190  }
191  m_BeginOffset = m_Image->ComputeOffset(ind);
192  m_Offset = m_BeginOffset;
193 
194  m_PixelAccessor = ptr->GetPixelAccessor();
195  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
196  m_PixelAccessorFunctor.SetBegin(m_Buffer);
197  }
198 
207  {
208  m_Image = it.GetImage();
209  m_Region = it.GetRegion();
210  m_Buffer = m_Image->GetBufferPointer();
212 
213  IndexType ind = it.GetIndex();
214 
215  m_Offset = m_Image->ComputeOffset(ind);
216 
217  // Compute the end offset, one pixel before the first pixel
218  m_EndOffset = m_Image->ComputeOffset(m_Region.GetIndex()) - 1;
219 
220  // Compute the begin offset, the last pixel in the region
221  IndexType regInd(m_Region.GetIndex());
222  SizeType regSize(m_Region.GetSize());
223  for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
224  {
225  regInd[i] += (regSize[i] - 1);
226  }
227  m_BeginOffset = m_Image->ComputeOffset(regInd);
228 
229  m_PixelAccessor = m_Image->GetPixelAccessor();
230  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
231  m_PixelAccessorFunctor.SetBegin(m_Buffer);
232  }
233 
236  Self &
237  operator=(const Self & it)
238  {
239  if (this != &it)
240  {
241  m_Image = it.m_Image; // copy the smart pointer
242  m_Region = it.m_Region;
244 
245  m_Buffer = it.m_Buffer;
246  m_Offset = it.m_Offset;
247  m_BeginOffset = it.m_BeginOffset;
248  m_EndOffset = it.m_EndOffset;
249  m_PixelAccessor = it.m_PixelAccessor;
250  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
251  m_PixelAccessorFunctor.SetBegin(m_Buffer);
252  }
253  return *this;
254  }
255 
258  Self &
260  {
261  m_Image = it.GetImage();
262  m_Region = it.GetRegion();
263  m_Buffer = m_Image->GetBufferPointer();
265 
266  IndexType ind = it.GetIndex();
267 
268  m_Offset = m_Image->ComputeOffset(ind);
269 
270  // Compute the end offset, one pixel before the first pixel
271  m_EndOffset = m_Image->ComputeOffset(m_Region.GetIndex()) - 1;
272 
273  // Compute the begin offset, the last pixel in the region
274  IndexType regInd(m_Region.GetIndex());
275  SizeType regSize(m_Region.GetSize());
276  for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
277  {
278  regInd[i] += (regSize[i] - 1);
279  }
280  m_BeginOffset = m_Image->ComputeOffset(regInd);
281 
282  m_PixelAccessor = m_Image->GetPixelAccessor();
283  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
284  m_PixelAccessorFunctor.SetBegin(m_Buffer);
285  return *this;
286  }
287 
289  static unsigned int
291  {
292  return TImage::ImageDimension;
293  }
294 
297  bool
298  operator!=(const Self & it) const
299  {
300  // two iterators are the same if they "point to" the same memory location
301  return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
302  }
303 
306  bool
307  operator==(const Self & it) const
308  {
309  // two iterators are the same if they "point to" the same memory location
310  return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
311  }
312 
317  const IndexType
319  {
320  return m_Image->ComputeIndex(m_Offset);
321  }
322 
325  virtual void
326  SetIndex(const IndexType & ind)
327  {
328  m_Offset = m_Image->ComputeOffset(ind);
329  }
330 
333  const RegionType &
334  GetRegion() const
335  {
336  return m_Region;
337  }
338 
340  const PixelType
341  Get() const
342  {
343  return m_PixelAccessorFunctor.Get(*(m_Buffer + m_Offset));
344  }
345 
347  void
348  Set(const PixelType & value) const
349  {
350  this->m_PixelAccessorFunctor.Set(*(const_cast<InternalPixelType *>(this->m_Buffer + this->m_Offset)), value);
351  }
352 
356  const PixelType &
357  Value() const
358  {
359  return *(m_Buffer + m_Offset);
360  }
361 
365  const PixelType &
367  {
368  return *(m_Buffer + m_Offset);
369  }
370 
373  void
375  {
376  m_Offset = m_BeginOffset;
377  }
378 
381  void
383  {
384  m_Offset = m_EndOffset;
385  }
386 
389  bool
390  IsAtBegin() const
391  {
392  return (m_Offset == m_BeginOffset);
393  }
394 
397  bool
398  IsAtEnd() const
399  {
400  return (m_Offset == m_EndOffset);
401  }
402 
403 protected: // made protected so other iterators can access
404  typename ImageType::ConstWeakPointer m_Image;
405 
406  RegionType m_Region; // region to iterate over
407 
409  SizeValueType m_BeginOffset; // offset to last pixel in region
410  SizeValueType m_EndOffset; // offset to one pixel before first pixel
411 
413 
416 };
417 } // end namespace itk
418 
419 #endif
itk::ImageReverseConstIterator::PixelType
typename TImage::PixelType PixelType
Definition: itkImageReverseConstIterator.h:127
itkImageConstIterator.h
itk::ImageReverseConstIterator::GetIndex
const IndexType GetIndex()
Definition: itkImageReverseConstIterator.h:318
itk::ImageReverseConstIterator
Multi-dimensional image iterator.
Definition: itkImageReverseConstIterator.h:87
itk::ImageConstIterator::GetImage
const ImageType * GetImage() const
Definition: itkImageConstIterator.h:336
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator(const ImageConstIterator< TImage > &it)
Definition: itkImageReverseConstIterator.h:206
itk::ImageReverseConstIterator::operator=
Self & operator=(const Self &it)
Definition: itkImageReverseConstIterator.h:237
itk::ImageReverseConstIterator::m_EndOffset
SizeValueType m_EndOffset
Definition: itkImageReverseConstIterator.h:410
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator()
Definition: itkImageReverseConstIterator.h:138
itk::ImageReverseConstIterator::PixelContainer
typename TImage::PixelContainer PixelContainer
Definition: itkImageReverseConstIterator.h:120
itk::ImageReverseConstIterator::AccessorFunctorType
typename TImage::AccessorFunctorType AccessorFunctorType
Definition: itkImageReverseConstIterator.h:134
itk::ImageReverseConstIterator::m_Buffer
const InternalPixelType * m_Buffer
Definition: itkImageReverseConstIterator.h:412
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator(const ImageType *ptr, const RegionType &region)
Definition: itkImageReverseConstIterator.h:172
itk::ImageReverseConstIterator::Get
const PixelType Get() const
Definition: itkImageReverseConstIterator.h:341
itk::ImageReverseConstIterator::OffsetType
typename TImage::OffsetType OffsetType
Definition: itkImageReverseConstIterator.h:109
itk::ImageReverseConstIterator::Set
void Set(const PixelType &value) const
Definition: itkImageReverseConstIterator.h:348
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageConstIterator::GetIndex
const IndexType GetIndex() const
Definition: itkImageConstIterator.h:313
itk::ImageReverseConstIterator::SizeType
typename TImage::SizeType SizeType
Definition: itkImageReverseConstIterator.h:106
itk::ImageReverseConstIterator::m_PixelAccessorFunctor
AccessorFunctorType m_PixelAccessorFunctor
Definition: itkImageReverseConstIterator.h:415
itk::ImageReverseConstIterator::IndexType
typename TImage::IndexType IndexType
Definition: itkImageReverseConstIterator.h:103
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::ImageReverseConstIterator::operator!=
bool operator!=(const Self &it) const
Definition: itkImageReverseConstIterator.h:298
itk::ImageReverseConstIterator::ImageType
TImage ImageType
Definition: itkImageReverseConstIterator.h:115
itk::ImageReverseConstIterator::RegionType
typename TImage::RegionType RegionType
Definition: itkImageReverseConstIterator.h:112
itk::ImageReverseConstIterator::GoToEnd
void GoToEnd()
Definition: itkImageReverseConstIterator.h:382
itk::ImageReverseConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkImageReverseConstIterator.h:398
itk::ImageReverseConstIterator::GetRegion
const RegionType & GetRegion() const
Definition: itkImageReverseConstIterator.h:334
itk::ImageReverseConstIterator::AccessorType
typename TImage::AccessorType AccessorType
Definition: itkImageReverseConstIterator.h:131
itk::ImageReverseConstIterator::IsAtBegin
bool IsAtBegin() const
Definition: itkImageReverseConstIterator.h:390
itk::ImageReverseConstIterator::operator==
bool operator==(const Self &it) const
Definition: itkImageReverseConstIterator.h:307
itk::ImageReverseConstIterator::Value
const PixelType & Value()
Definition: itkImageReverseConstIterator.h:366
itk::ImageReverseConstIterator::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImageReverseConstIterator.h:121
itk::ImageReverseConstIterator::InternalPixelType
typename TImage::InternalPixelType InternalPixelType
Definition: itkImageReverseConstIterator.h:124
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ImageConstIterator
A multi-dimensional image iterator templated over image type.
Definition: itkImageConstIterator.h:84
itk::ImageReverseConstIterator::Value
const PixelType & Value() const
Definition: itkImageReverseConstIterator.h:357
itk::ImageReverseConstIterator::m_Image
ImageType::ConstWeakPointer m_Image
Definition: itkImageReverseConstIterator.h:404
itk::ImageConstIterator::GetRegion
const RegionType & GetRegion() const
Definition: itkImageConstIterator.h:329
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator(const Self &it)
Definition: itkImageReverseConstIterator.h:155
itk::ImageReverseConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageReverseConstIterator.h:374
itk::ImageReverseConstIterator::m_BeginOffset
SizeValueType m_BeginOffset
Definition: itkImageReverseConstIterator.h:409
itk::ImageReverseConstIterator::GetImageIteratorDimension
static unsigned int GetImageIteratorDimension()
Definition: itkImageReverseConstIterator.h:290
itk::ImageReverseConstIterator::operator=
Self & operator=(const ImageConstIterator< TImage > &it)
Definition: itkImageReverseConstIterator.h:259
itk::ImageReverseConstIterator::SetIndex
virtual void SetIndex(const IndexType &ind)
Definition: itkImageReverseConstIterator.h:326
itk::ImageReverseConstIterator::m_Region
RegionType m_Region
Definition: itkImageReverseConstIterator.h:406
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkSize.h
itk::ImageReverseConstIterator::m_PixelAccessor
AccessorType m_PixelAccessor
Definition: itkImageReverseConstIterator.h:414
itk::ImageReverseConstIterator::m_Offset
SizeValueType m_Offset
Definition: itkImageReverseConstIterator.h:408