ITK  5.2.0
Insight Toolkit
itkImageRegionReverseConstIterator.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 itkImageRegionReverseConstIterator_h
19 #define itkImageRegionReverseConstIterator_h
20 
22 #include "itkImageRegionIterator.h"
23 
24 namespace itk
25 {
102 template <typename TImage>
103 class ITK_TEMPLATE_EXPORT ImageRegionReverseConstIterator : public ImageReverseConstIterator<TImage>
104 {
105 public:
109 
113 
116  using SizeType = typename Superclass::SizeType;
117 
121 
124 
128 
133  using PixelContainerPointer = typename PixelContainer::Pointer;
134 
137 
140 
144 
147 
150  : Superclass()
151  {
152  m_SpanBeginOffset = 0;
153  m_SpanEndOffset = 0;
154  }
156 
160  : Superclass(ptr, region)
161  {
162  m_SpanBeginOffset = this->m_BeginOffset;
163  m_SpanEndOffset = this->m_BeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
164  }
166 
175  : Superclass(it)
176  {
177  IndexType ind = this->GetIndex();
178 
179  m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
180  (ind[0] - this->m_Region.GetIndex()[0]);
181  m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
182  }
183 
187  : Superclass(it)
188  {
189  IndexType ind = this->GetIndex();
190 
191  m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
192  (ind[0] - this->m_Region.GetIndex()[0]);
193  m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
194  }
195 
199  : Superclass(it)
200  {
201  IndexType ind = this->GetIndex();
202 
203  m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
204  (ind[0] - this->m_Region.GetIndex()[0]);
205  m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
206  }
207 
210  void
212  {
213  Superclass::GoToBegin();
214 
215  // reset the span offsets
216  m_SpanBeginOffset = this->m_BeginOffset;
217  m_SpanEndOffset = this->m_BeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
218  }
219 
222  void
224  {
225  Superclass::GoToEnd();
226 
227  // reset the span offsets
228  m_SpanEndOffset = this->m_EndOffset;
229  m_SpanBeginOffset = m_SpanEndOffset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
230  }
231 
235  void
236  SetIndex(const IndexType & ind) override
237  {
238  Superclass::SetIndex(ind);
239  m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
240  (ind[0] - this->m_Region.GetIndex()[0]);
241  m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
242  }
244 
253  Self &
255  {
256  if (--this->m_Offset <= m_SpanEndOffset)
257  {
258  // We have past the beginning of the span (row), need to wrap around.
259 
260  // First move forward one pixel, because we are going to use a different
261  // algorithm to compute the next pixel
262  this->m_Offset++;
263 
264  // Get the index of the first pixel on the span (row)
266  this->m_Image->ComputeIndex(static_cast<OffsetValueType>(this->m_Offset));
267 
268  const typename ImageConstIterator<TImage>::IndexType & startIndex = this->m_Region.GetIndex();
269  const typename ImageConstIterator<TImage>::SizeType & size = this->m_Region.GetSize();
270 
271  // Deccrement along a row, then wrap at the beginning of the region row.
272  bool done;
273  unsigned int dim;
274 
275  // Check to see if we are past the first pixel in the region
276  // Note that --ind[0] moves to the previous pixel along the row.
277  done = (--ind[0] == startIndex[0] - 1);
278  for (unsigned int i = 1; done && i < this->ImageIteratorDimension; i++)
279  {
280  done = (ind[i] == startIndex[i]);
281  }
282 
283  // if the iterator is outside the region (but not past region begin) then
284  // we need to wrap around the region
285  dim = 0;
286  if (!done)
287  {
288  while ((dim < this->ImageIteratorDimension - 1) && (ind[dim] < startIndex[dim]))
289  {
290  ind[dim] = startIndex[dim] + static_cast<OffsetValueType>(size[dim]) - 1;
291  ind[++dim]--;
292  }
293  }
294  this->m_Offset = this->m_Image->ComputeOffset(ind);
295  m_SpanBeginOffset = this->m_Offset;
296  m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(size[0]);
297  }
298  return *this;
299  }
300 
309  Self &
311  {
312  if (++this->m_Offset >= m_SpanBeginOffset)
313  {
314  // We have reached the end of the span (row), need to wrap around.
315 
316  // First back up one pixel, because we are going to use a different
317  // algorithm to compute the next pixel
318  --this->m_Offset;
319 
320  // Get the index of the last pixel on the span (row)
322  this->m_Image->ComputeIndex(static_cast<OffsetValueType>(this->m_Offset));
323 
324  const typename ImageIterator<TImage>::IndexType & startIndex = this->m_Region.GetIndex();
325  const typename ImageIterator<TImage>::SizeType & size = this->m_Region.GetSize();
326 
327  // Increment along a row, then wrap at the end of the region row.
328  bool done;
329  unsigned int dim;
330 
331  // Check to see if we are past the last pixel in the region
332  // Note that ++ind[0] moves to the next pixel along the row.
333  done = (++ind[0] == startIndex[0] + static_cast<OffsetValueType>(size[0]));
334  for (unsigned int i = 1; done && i < this->ImageIteratorDimension; i++)
335  {
336  done = (ind[i] == startIndex[i] + static_cast<OffsetValueType>(size[i]) - 1);
337  }
338 
339  // if the iterator is outside the region (but not past region end) then
340  // we need to wrap around the region
341  dim = 0;
342  if (!done)
343  {
344  while ((dim < this->ImageIteratorDimension - 1) &&
345  (ind[dim] > startIndex[dim] + static_cast<OffsetValueType>(size[dim]) - 1))
346  {
347  ind[dim] = startIndex[dim];
348  ind[++dim]++;
349  }
350  }
351  this->m_Offset = this->m_Image->ComputeOffset(ind);
352  m_SpanBeginOffset = this->m_Offset;
353  m_SpanEndOffset = this->m_Offset - static_cast<OffsetValueType>(size[0]);
354  }
355  return *this;
356  }
357 
358 protected:
359  SizeValueType m_SpanBeginOffset; // offset to last pixel in the row
360  SizeValueType m_SpanEndOffset; // offset to one pixel before the row
361 };
362 } // end namespace itk
363 
364 #endif
itk::ImageRegionReverseConstIterator::ImageRegionReverseConstIterator
ImageRegionReverseConstIterator(const ImageConstIterator< TImage > &it)
Definition: itkImageRegionReverseConstIterator.h:174
itk::ImageRegionReverseConstIterator::SetIndex
void SetIndex(const IndexType &ind) override
Definition: itkImageRegionReverseConstIterator.h:236
itk::ImageReverseConstIterator::PixelType
typename TImage::PixelType PixelType
Definition: itkImageReverseConstIterator.h:127
itk::ImageConstIterator::SizeType
typename TImage::SizeType SizeType
Definition: itkImageConstIterator.h:103
itk::ImageRegionReverseConstIterator::OffsetType
typename Superclass::OffsetType OffsetType
Definition: itkImageRegionReverseConstIterator.h:120
itk::ImageReverseConstIterator::GetIndex
const IndexType GetIndex()
Definition: itkImageReverseConstIterator.h:318
itk::ImageReverseConstIterator
Multi-dimensional image iterator.
Definition: itkImageReverseConstIterator.h:87
itk::ImageIterator::SizeType
typename Superclass::SizeType SizeType
Definition: itkImageIterator.h:83
itk::ImageRegionReverseConstIterator::PixelContainer
typename Superclass::PixelContainer PixelContainer
Definition: itkImageRegionReverseConstIterator.h:132
itk::ImageRegionReverseConstIterator::GoToEnd
void GoToEnd()
Definition: itkImageRegionReverseConstIterator.h:223
itk::ImageIterator::IndexType
typename Superclass::IndexType IndexType
Definition: itkImageIterator.h:82
itk::ImageRegionReverseConstIterator::AccessorType
typename Superclass::AccessorType AccessorType
Definition: itkImageRegionReverseConstIterator.h:143
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ImageRegionReverseConstIterator::RegionType
typename Superclass::RegionType RegionType
Definition: itkImageRegionReverseConstIterator.h:123
itk::ImageRegionReverseConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageRegionReverseConstIterator.h:211
itk::ImageRegionReverseConstIterator::InternalPixelType
typename Superclass::InternalPixelType InternalPixelType
Definition: itkImageRegionReverseConstIterator.h:136
itk::ImageRegionReverseConstIterator::PixelType
typename Superclass::PixelType PixelType
Definition: itkImageRegionReverseConstIterator.h:139
itk::ImageRegionReverseConstIterator::ImageRegionReverseConstIterator
ImageRegionReverseConstIterator()
Definition: itkImageRegionReverseConstIterator.h:149
itkImageRegionIterator.h
itk::ImageReverseConstIterator::PixelContainer
typename TImage::PixelContainer PixelContainer
Definition: itkImageReverseConstIterator.h:120
itk::ImageReverseConstIterator::OffsetType
typename TImage::OffsetType OffsetType
Definition: itkImageReverseConstIterator.h:109
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:78
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageRegionReverseConstIterator::operator--
Self & operator--()
Definition: itkImageRegionReverseConstIterator.h:310
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::ImageRegionReverseConstIterator::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImageRegionReverseConstIterator.h:133
itk::ImageReverseConstIterator::ImageType
TImage ImageType
Definition: itkImageReverseConstIterator.h:115
itk::ImageRegionReverseConstIterator::operator++
Self & operator++()
Definition: itkImageRegionReverseConstIterator.h:254
itk::ImageReverseConstIterator::AccessorType
typename TImage::AccessorType AccessorType
Definition: itkImageReverseConstIterator.h:131
itk::ImageRegionReverseConstIterator::SizeType
typename Superclass::SizeType SizeType
Definition: itkImageRegionReverseConstIterator.h:116
itk::ImageRegionReverseConstIterator::m_SpanEndOffset
SizeValueType m_SpanEndOffset
Definition: itkImageRegionReverseConstIterator.h:360
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::ImageRegionReverseConstIterator
A multi-dimensional image iterator designed to walk a specified image region in reverse.
Definition: itkImageRegionReverseConstIterator.h:103
itk::ImageConstIterator
A multi-dimensional image iterator templated over image type.
Definition: itkImageConstIterator.h:84
itk::ImageRegionReverseConstIterator::m_SpanBeginOffset
SizeValueType m_SpanBeginOffset
Definition: itkImageRegionReverseConstIterator.h:359
itk::ImageRegionReverseConstIterator::ImageType
typename Superclass::ImageType ImageType
Definition: itkImageRegionReverseConstIterator.h:127
itkImageReverseConstIterator.h
itk::ImageConstIterator::IndexType
typename TImage::IndexType IndexType
Definition: itkImageConstIterator.h:100
itk::ImageRegionReverseConstIterator::ImageRegionReverseConstIterator
ImageRegionReverseConstIterator(const ImageType *ptr, const RegionType &region)
Definition: itkImageRegionReverseConstIterator.h:159
itk::ImageRegionReverseConstIterator::ImageRegionReverseConstIterator
ImageRegionReverseConstIterator(const ImageReverseConstIterator< TImage > &it)
Definition: itkImageRegionReverseConstIterator.h:186
itk::ImageRegionReverseConstIterator::IndexType
typename Superclass::IndexType IndexType
Definition: itkImageRegionReverseConstIterator.h:112
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::ImageRegionReverseConstIterator::ImageRegionReverseConstIterator
ImageRegionReverseConstIterator(const ImageRegionIterator< TImage > &it)
Definition: itkImageRegionReverseConstIterator.h:198