ITK  5.3.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 
304  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
305 
306 
311  const IndexType
313  {
314  return m_Image->ComputeIndex(m_Offset);
315  }
316 
319  virtual void
320  SetIndex(const IndexType & ind)
321  {
322  m_Offset = m_Image->ComputeOffset(ind);
323  }
324 
327  const RegionType &
328  GetRegion() const
329  {
330  return m_Region;
331  }
332 
334  const PixelType
335  Get() const
336  {
337  return m_PixelAccessorFunctor.Get(*(m_Buffer + m_Offset));
338  }
339 
341  void
342  Set(const PixelType & value) const
343  {
344  this->m_PixelAccessorFunctor.Set(*(const_cast<InternalPixelType *>(this->m_Buffer + this->m_Offset)), value);
345  }
346 
350  const PixelType &
351  Value() const
352  {
353  return *(m_Buffer + m_Offset);
354  }
355 
359  const PixelType &
361  {
362  return *(m_Buffer + m_Offset);
363  }
364 
367  void
369  {
370  m_Offset = m_BeginOffset;
371  }
372 
375  void
377  {
378  m_Offset = m_EndOffset;
379  }
380 
383  bool
384  IsAtBegin() const
385  {
386  return (m_Offset == m_BeginOffset);
387  }
388 
391  bool
392  IsAtEnd() const
393  {
394  return (m_Offset == m_EndOffset);
395  }
396 
397 protected: // made protected so other iterators can access
398  typename ImageType::ConstWeakPointer m_Image;
399 
400  RegionType m_Region; // region to iterate over
401 
403  SizeValueType m_BeginOffset; // offset to last pixel in region
404  SizeValueType m_EndOffset; // offset to one pixel before first pixel
405 
407 
410 };
411 } // end namespace itk
412 
413 #endif
itk::ImageReverseConstIterator::PixelType
typename TImage::PixelType PixelType
Definition: itkImageReverseConstIterator.h:127
itkImageConstIterator.h
itk::ImageReverseConstIterator::GetIndex
const IndexType GetIndex()
Definition: itkImageReverseConstIterator.h:312
itk::ImageReverseConstIterator
Multi-dimensional image iterator.
Definition: itkImageReverseConstIterator.h:87
itk::ImageConstIterator::GetImage
const ImageType * GetImage() const
Definition: itkImageConstIterator.h:329
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:404
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:406
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator(const ImageType *ptr, const RegionType &region)
Definition: itkImageReverseConstIterator.h:172
itk::ImageReverseConstIterator::Get
const PixelType Get() const
Definition: itkImageReverseConstIterator.h:335
itk::ImageReverseConstIterator::OffsetType
typename TImage::OffsetType OffsetType
Definition: itkImageReverseConstIterator.h:109
itk::ImageReverseConstIterator::Set
void Set(const PixelType &value) const
Definition: itkImageReverseConstIterator.h:342
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageConstIterator::GetIndex
const IndexType GetIndex() const
Definition: itkImageConstIterator.h:306
itk::ImageReverseConstIterator::SizeType
typename TImage::SizeType SizeType
Definition: itkImageReverseConstIterator.h:106
itk::ImageReverseConstIterator::m_PixelAccessorFunctor
AccessorFunctorType m_PixelAccessorFunctor
Definition: itkImageReverseConstIterator.h:409
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::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:376
itk::ImageReverseConstIterator::IsAtEnd
bool IsAtEnd() const
Definition: itkImageReverseConstIterator.h:392
itk::ImageReverseConstIterator::GetRegion
const RegionType & GetRegion() const
Definition: itkImageReverseConstIterator.h:328
itk::ImageReverseConstIterator::AccessorType
typename TImage::AccessorType AccessorType
Definition: itkImageReverseConstIterator.h:131
itk::ImageReverseConstIterator::IsAtBegin
bool IsAtBegin() const
Definition: itkImageReverseConstIterator.h:384
itk::ImageReverseConstIterator::operator==
bool operator==(const Self &it) const
Definition: itkImageReverseConstIterator.h:298
itk::ImageReverseConstIterator::Value
const PixelType & Value()
Definition: itkImageReverseConstIterator.h:360
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:351
itk::ImageReverseConstIterator::m_Image
ImageType::ConstWeakPointer m_Image
Definition: itkImageReverseConstIterator.h:398
itk::ImageConstIterator::GetRegion
const RegionType & GetRegion() const
Definition: itkImageConstIterator.h:322
itk::ImageReverseConstIterator::ImageReverseConstIterator
ImageReverseConstIterator(const Self &it)
Definition: itkImageReverseConstIterator.h:155
itk::ImageReverseConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageReverseConstIterator.h:368
itk::ImageReverseConstIterator::m_BeginOffset
SizeValueType m_BeginOffset
Definition: itkImageReverseConstIterator.h:403
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:320
itk::ImageReverseConstIterator::m_Region
RegionType m_Region
Definition: itkImageReverseConstIterator.h:400
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkSize.h
itk::ImageReverseConstIterator::m_PixelAccessor
AccessorType m_PixelAccessor
Definition: itkImageReverseConstIterator.h:408
itk::ImageReverseConstIterator::m_Offset
SizeValueType m_Offset
Definition: itkImageReverseConstIterator.h:402