ITK  5.2.0
Insight Toolkit
itkIndexRange.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 
19 #ifndef itkIndexRange_h
20 #define itkIndexRange_h
21 
22 #include <cassert>
23 #include <cstddef> // For ptrdiff_t.
24 #include <iterator> // For bidirectional_iterator_tag and reverse_iterator.
25 #include <type_traits> // For conditional and enable_if.
26 
27 #include "itkImageRegion.h"
28 #include "itkIndex.h"
29 #include "itkSize.h"
30 
31 namespace itk
32 {
33 
77 template <unsigned VDimension, bool VBeginAtZero>
78 class IndexRange final
79 {
80 public:
81  constexpr static unsigned Dimension = VDimension;
84 
85  class const_iterator final
86  {
87  public:
88  // Types conforming the iterator requirements of the C++ standard library:
89  using difference_type = std::ptrdiff_t;
91  using reference = const IndexType &;
92  using pointer = const IndexType *;
93  using iterator_category = std::bidirectional_iterator_tag;
94 
100  const_iterator() = default;
101 
102 
104  reference operator*() const ITK_NOEXCEPT { return m_Index; }
105 
106 
108  pointer operator->() const ITK_NOEXCEPT { return &(**this); }
109 
110 
113  operator++() ITK_NOEXCEPT
114  {
115  for (unsigned i = 0; i < (VDimension - 1); ++i)
116  {
117  auto & indexValue = m_Index[i];
119 
120  ++indexValue;
121 
122  if (indexValue <= m_MaxIndex[i])
123  {
124  return *this;
125  }
126  indexValue = m_MinIndex[i];
127  }
128  ++m_Index.back();
129 
130  return *this;
131  }
132 
133 
137  operator++(int) ITK_NOEXCEPT
138  {
139  auto result = *this;
140  ++(*this);
141  return result;
142  }
144 
145 
148  operator--() ITK_NOEXCEPT
149  {
150  for (unsigned i = 0; i < (VDimension - 1); ++i)
151  {
152  auto & indexValue = m_Index[i];
154 
155  --indexValue;
156 
157  if (indexValue >= m_MinIndex[i])
158  {
159  return *this;
160  }
161  indexValue = m_MaxIndex[i];
162  }
163  --m_Index.back();
164  return *this;
165  }
166 
167 
171  operator--(int) ITK_NOEXCEPT
172  {
173  auto result = *this;
174  --(*this);
175  return result;
176  }
178 
179 
183  friend bool
184  operator==(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
185  {
186  assert(lhs.m_MaxIndex == rhs.m_MaxIndex);
187 
188  return lhs.m_Index == rhs.m_Index;
189  }
190 
191 
193  friend bool
194  operator!=(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
195  {
196  // Implemented just like the corresponding std::rel_ops operator.
197  return !(lhs == rhs);
198  }
199 
200 
202  friend bool
203  operator<(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
204  {
205  for (unsigned i = VDimension; i > 0; --i)
206  {
207  const auto difference = lhs.m_Index[i - 1] - rhs.m_Index[i - 1];
209 
210  if (difference < 0)
211  {
212  return true;
213  }
214  if (difference > 0)
215  {
216  break;
217  }
218  }
219  return false;
220  }
221 
222 
224  friend bool
225  operator>(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
226  {
227  // Implemented just like the corresponding std::rel_ops operator.
228  return rhs < lhs;
229  }
230 
231 
233  friend bool
234  operator<=(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
235  {
236  // Implemented just like the corresponding std::rel_ops operator.
237  return !(rhs < lhs);
238  }
239 
240 
242  friend bool
243  operator>=(const const_iterator & lhs, const const_iterator & rhs) ITK_NOEXCEPT
244  {
245  // Implemented just like the corresponding std::rel_ops operator.
246  return !(lhs < rhs);
247  }
248 
249 
250  private:
251  friend class IndexRange;
252 
253  // Represents an N-dimensional index that is always zero
254  // Aims towards zero runtime overhead.
255  struct ZeroIndex
256  {
257  // The "index" operator.
258  constexpr IndexValueType operator[](unsigned) const { return 0; }
259 
260  // Implicitly converts to a default-initialized itk::Index<N>.
261  constexpr operator IndexType() const { return IndexType(); }
262  };
263 
264 
265  // When BeginAtZero is true, use zero as minimum index, otherwise use itk::Index<N>.
266  using MinIndexType = typename std::conditional<VBeginAtZero, ZeroIndex, IndexType>::type;
267 
268  // Private constructor, only used by friend class IndexRange.
269  const_iterator(const IndexType & index, const MinIndexType & minIndex, const IndexType & maxIndex) ITK_NOEXCEPT
270  :
271  // Note: Use parentheses instead of curly braces to initialize data members,
272  // to avoid AppleClang 6.0.0.6000056 compilation error, "no viable conversion..."
273  m_Index(index)
274  , m_MinIndex(minIndex)
275  , m_MaxIndex(maxIndex)
276  {}
277 
278 
279  // IndexRange::const_iterator data members:
280 
281  // Current (N-dimensional) index.
283 
284  // Minimum (N-dimensional) index.
286 
287  // Maximum (N-dimensional) index.
289  };
290 
292  using reverse_iterator = std::reverse_iterator<iterator>;
293  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
294 
300  IndexRange() = default;
301 
302 
305  explicit IndexRange(const SizeType & gridSize)
306  : // Note: Use parentheses instead of curly braces to initialize data members,
307  // to avoid AppleClang 6.0.0.6000056 compile errors, "no viable conversion..."
308  m_MinIndex()
309  , m_MaxIndex(CalculateMaxIndex(typename iterator::MinIndexType(), gridSize))
310  {}
311 
312 
317  template <bool VIsSubstitutionFailure = VBeginAtZero,
318  typename TVoid = typename std::enable_if<!VIsSubstitutionFailure>::type>
319  explicit IndexRange(const ImageRegion<VDimension> & imageRegion)
320  : // Note: Use parentheses instead of curly braces to initialize data members,
321  // to avoid AppleClang 6.0.0.6000056 compile errors, "no viable conversion..."
322  m_MinIndex(imageRegion.GetIndex())
323  , m_MaxIndex(CalculateMaxIndex(imageRegion.GetIndex(), imageRegion.GetSize()))
324  {
325  // Three compile-time asserts, just to check if SFINAE worked properly:
326  static_assert(!VIsSubstitutionFailure,
327  "This template should (of course) be instantiated without substitution failure.");
328  static_assert(std::is_same<TVoid, void>::value,
329  "std::enable_if<!VIsSubstitutionFailure> should yield void, by definition.");
330  static_assert(!VBeginAtZero, "This constructor should only be is available when VBeginAtZero is false.");
331  }
333 
334 
336  iterator
337  begin() const ITK_NOEXCEPT
338  {
340  }
341 
343  iterator
344  end() const ITK_NOEXCEPT
345  {
346  IndexType index = m_MinIndex;
347  index.back() = m_MaxIndex.back() + 1;
348  return iterator(index, m_MinIndex, m_MaxIndex);
349  }
351 
354  const_iterator
355  cbegin() const ITK_NOEXCEPT
356  {
357  return this->begin();
358  }
359 
361  const_iterator
362  cend() const ITK_NOEXCEPT
363  {
364  return this->end();
365  }
366 
369  rbegin() const ITK_NOEXCEPT
370  {
371  return reverse_iterator(this->end());
372  }
373 
376  rend() const ITK_NOEXCEPT
377  {
378  return reverse_iterator(this->begin());
379  }
380 
383  crbegin() const ITK_NOEXCEPT
384  {
385  return this->rbegin();
386  }
387 
390  crend() const ITK_NOEXCEPT
391  {
392  return this->rend();
393  }
394 
395 
397  std::size_t
398  size() const ITK_NOEXCEPT
399  {
400  std::size_t result = 1;
401 
402  for (unsigned i = 0; i < VDimension; ++i)
403  {
404  result *= ((m_MaxIndex[i] + 1) - m_MinIndex[i]);
405  }
406  return result;
407  }
408 
409 
411  bool
412  empty() const ITK_NOEXCEPT
413  {
414  // When an IndexRange is empty, each index value of m_MaxIndex is less than the corresponding
415  // index value of m_MinIndex. And vise versa: when an IndexRange is non-empty, each index value
416  // of m_MaxIndex is greater than or equal to the corresponding index value of m_MinIndex.
417  // Note that the range contains one element when m_MaxIndex == m_MinIndex.
418  return m_MaxIndex[0] < m_MinIndex[0];
419  }
420 
421 
422 private:
424 
425  static IndexType
426  CalculateMaxIndex(const MinIndexType & minIndex, const SizeType & size)
427  {
428  const bool sizeHasZeroValue = [&size] {
429  for (const auto sizeValue : size)
430  {
431  if (sizeValue == 0)
432  {
433  return true;
434  }
435  }
436  return false;
437  }();
438 
439  // Treat any size that has a zero value equally.
440  const SizeType normalizedSize = sizeHasZeroValue ? SizeType{ { 0 } } : size;
441 
442  IndexType index;
443 
444  for (unsigned i = 0; i < VDimension; ++i)
445  {
446  index[i] = minIndex[i] + static_cast<IndexValueType>(normalizedSize[i]) - 1;
447  }
448 
449  return index;
450  }
451 
452  // IndexRange data members:
453 
454  // Minimum (N-dimensional) index.
456 
457  // Maximum (N-dimensional) index.
459 };
460 
461 template <unsigned VDimension>
463 
464 template <unsigned VDimension>
466 
467 } // namespace itk
468 
469 #endif
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::IndexRange::rbegin
reverse_iterator rbegin() const noexcept
Definition: itkIndexRange.h:369
itk::IndexRange::const_iterator::ZeroIndex
Definition: itkIndexRange.h:255
itk::IndexRange::cend
const_iterator cend() const noexcept
Definition: itkIndexRange.h:362
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::IndexRange::const_iterator::ZeroIndex::operator[]
constexpr IndexValueType operator[](unsigned) const
Definition: itkIndexRange.h:258
itk::IndexRange::CalculateMaxIndex
static IndexType CalculateMaxIndex(const MinIndexType &minIndex, const SizeType &size)
Definition: itkIndexRange.h:426
itk::IndexRange::const_iterator
Definition: itkIndexRange.h:85
itk::ImageRegion< VDimension >
itk::IndexRange::empty
bool empty() const noexcept
Definition: itkIndexRange.h:412
itk::IndexRange::const_iterator::operator->
pointer operator->() const noexcept
Definition: itkIndexRange.h:108
itk::IndexRange::const_iterator::m_MinIndex
MinIndexType m_MinIndex
Definition: itkIndexRange.h:285
itk::IndexRange::const_iterator::operator>
friend bool operator>(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:225
itk::IndexRange::const_iterator::operator<=
friend bool operator<=(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:234
itk::IndexRange::Dimension
constexpr static unsigned Dimension
Definition: itkIndexRange.h:81
itk::IndexRange::const_iterator::operator--
const_iterator operator--(int) noexcept
Definition: itkIndexRange.h:171
itk::IndexRange::crend
const_reverse_iterator crend() const noexcept
Definition: itkIndexRange.h:390
itk::IndexRange::const_iterator::operator>=
friend bool operator>=(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:243
itk::IndexRange::IndexType
Index< VDimension > IndexType
Definition: itkIndexRange.h:83
itk::IndexRange::m_MaxIndex
IndexType m_MaxIndex
Definition: itkIndexRange.h:458
itk::IndexRange::end
iterator end() const noexcept
Definition: itkIndexRange.h:344
itkImageRegion.h
itk::IndexRange::cbegin
const_iterator cbegin() const noexcept
Definition: itkIndexRange.h:355
itk::IndexRange::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkIndexRange.h:293
itk::IndexRange::crbegin
const_reverse_iterator crbegin() const noexcept
Definition: itkIndexRange.h:383
itk::IndexRange::size
std::vcl_size_t size() const noexcept
Definition: itkIndexRange.h:398
itk::IndexRange::IndexRange
IndexRange(const ImageRegion< VDimension > &imageRegion)
Definition: itkIndexRange.h:319
itk::IndexRange::const_iterator::difference_type
std::ptrdiff_t difference_type
Definition: itkIndexRange.h:89
itk::IndexRange::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkIndexRange.h:292
itk::IndexRange::const_iterator::const_iterator
const_iterator()=default
itk::IndexRange::const_iterator::operator*
reference operator*() const noexcept
Definition: itkIndexRange.h:104
itk::Index::back
reference back()
Definition: itkIndex.h:453
itk::IndexRange::const_iterator::operator!=
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:194
itk::IndexRange::const_iterator::const_iterator
const_iterator(const IndexType &index, const MinIndexType &minIndex, const IndexType &maxIndex) noexcept
Definition: itkIndexRange.h:269
itk::IndexRange::const_iterator::operator++
const_iterator & operator++() noexcept
Definition: itkIndexRange.h:113
itkIndex.h
itk::IndexRange::IndexRange
IndexRange()=default
itk::IndexRange::const_iterator::operator++
const_iterator operator++(int) noexcept
Definition: itkIndexRange.h:137
itk::IndexRange::const_iterator::m_MaxIndex
IndexType m_MaxIndex
Definition: itkIndexRange.h:288
itk::IndexRange::rend
reverse_iterator rend() const noexcept
Definition: itkIndexRange.h:376
itk::Index::Filled
static Self Filled(const IndexValueType value)
Definition: itkIndex.h:480
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::IndexRange::const_iterator::MinIndexType
typename std::conditional< VBeginAtZero, ZeroIndex, IndexType >::type MinIndexType
Definition: itkIndexRange.h:266
itk::IndexRange::const_iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: itkIndexRange.h:93
itk::IndexRange::const_iterator::operator==
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:184
itk::IndexRange::m_MinIndex
MinIndexType m_MinIndex
Definition: itkIndexRange.h:455
itk::IndexRange::MinIndexType
typename iterator::MinIndexType MinIndexType
Definition: itkIndexRange.h:423
itk::IndexRange::iterator
const_iterator iterator
Definition: itkIndexRange.h:291
itk::IndexValueType
signed long IndexValueType
Definition: itkIntTypes.h:90
itk::IndexRange
Definition: itkIndexRange.h:78
itk::IndexRange::begin
iterator begin() const noexcept
Definition: itkIndexRange.h:337
itk::IndexRange::IndexRange
IndexRange(const SizeType &gridSize)
Definition: itkIndexRange.h:305
itk::IndexRange::const_iterator::m_Index
IndexType m_Index
Definition: itkIndexRange.h:282
itk::IndexRange::const_iterator::operator--
const_iterator & operator--() noexcept
Definition: itkIndexRange.h:148
itkSize.h
itk::IndexRange::const_iterator::operator<
friend bool operator<(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: itkIndexRange.h:203