ITK  6.0.0
Insight Toolkit
itkOffset.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  * https://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 itkOffset_h
19 #define itkOffset_h
20 
21 #include "itkSize.h"
22 #include "itkMath.h"
23 
24 #include <cstddef> // For ptrdiff_t.
25 
26 namespace itk
27 {
28 
65 template <unsigned int VDimension = 2>
66 struct ITK_TEMPLATE_EXPORT Offset final
67 {
68 public:
69  // Using the `rule of zero` to this aggregate type
70  // C++20 changes the definition of aggregate such that classes with any user-declared ctors are no longer aggregates.
71 
73  using Self = Offset;
74 
78 
80  static constexpr unsigned int Dimension = VDimension;
81 
83  static constexpr unsigned int
85  {
86  return VDimension;
87  }
88 
89 
91  const Self
92  operator+(const Self & vec) const
93  {
94  Self result;
95 
96  for (unsigned int i = 0; i < VDimension; ++i)
97  {
98  result[i] = m_InternalArray[i] + vec.m_InternalArray[i];
99  }
100  return result;
101  }
102 
104  const Self
105  operator+(const Size<VDimension> & sz) const
106  {
107  Self result;
108 
109  for (unsigned int i = 0; i < VDimension; ++i)
110  {
111  result[i] = m_InternalArray[i] + sz[i];
112  }
113  return result;
114  }
115 
117  const Self &
119  {
120  for (unsigned int i = 0; i < VDimension; ++i)
121  {
122  m_InternalArray[i] += sz[i];
123  }
124  return *this;
125  }
129  const Self &
131  {
132  for (unsigned int i = 0; i < VDimension; ++i)
133  {
134  m_InternalArray[i] -= sz[i];
135  }
136  return *this;
137  }
141  const Self
142  operator-(const Self & vec) const
143  {
144  Self result;
145 
146  for (unsigned int i = 0; i < VDimension; ++i)
147  {
148  result[i] = m_InternalArray[i] - vec.m_InternalArray[i];
149  }
150  return result;
151  }
152 
154  const Self &
155  operator+=(const Self & vec)
156  {
157  for (unsigned int i = 0; i < VDimension; ++i)
158  {
159  m_InternalArray[i] += vec.m_InternalArray[i];
160  }
161  return *this;
162  }
166  const Self &
167  operator-=(const Self & vec)
168  {
169  for (unsigned int i = 0; i < VDimension; ++i)
170  {
171  m_InternalArray[i] -= vec.m_InternalArray[i];
172  }
173  return *this;
174  }
180  const OffsetValueType *
181  GetOffset() const
182  {
183  return m_InternalArray;
184  }
185 
190  void
191  SetOffset(const OffsetValueType val[VDimension])
192  {
193  std::copy_n(val, VDimension, m_InternalArray);
194  }
195 
202  void
203  SetElement(unsigned long element, OffsetValueType val)
204  {
205  m_InternalArray[element] = val;
206  }
207 
215  GetElement(unsigned long element) const
216  {
217  return m_InternalArray[element];
218  }
219 
222  void
224  {
225  std::fill_n(begin(), size(), value);
226  } // MATCH std::array assign, ITK Fill
227 
232  /*
233  * Ask the compiler to align a type to the maximum useful alignment for the target
234  * machine you are compiling for. Whenever you leave out the alignment factor in an
235  * aligned attribute specification, the compiler automatically sets the alignment
236  * for the type to the largest alignment that is ever used for any data type on
237  * the target machine you are compiling for. Doing this can often make copy
238  * operations more efficient, because the compiler can use whatever instructions
239  * copy the biggest chunks of memory when performing copies to or from the variables
240  * that have types that you have aligned this way.
241  */
242  static_assert(VDimension > 0, "Error: Only positive value sized VDimension allowed");
243  alignas(OffsetValueType) OffsetValueType m_InternalArray[VDimension];
247  template <typename TCoordinate>
248  inline void
250  {
251  for (unsigned int i = 0; i < VDimension; ++i)
252  {
253  m_InternalArray[i] = Math::Round<OffsetValueType>(point[i]);
254  }
255  }
259  template <typename TCoordinate>
260  inline void
262  {
263  for (unsigned int i = 0; i < VDimension; ++i)
264  {
265  m_InternalArray[i] = static_cast<OffsetValueType>(point[i]);
266  }
267  }
273  static Self
274  GetBasisOffset(unsigned int dim);
275 
276 
277  // ======================= Mirror the access pattern behavior of the std::array class
285  using const_reference = const value_type &;
286  using iterator = value_type *;
287  using const_iterator = const value_type *;
288  using size_type = unsigned int;
289  using difference_type = ptrdiff_t;
290  using reverse_iterator = std::reverse_iterator<iterator>;
291  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
292 
297  void
298  assign(const value_type & newValue)
299  {
300  std::fill_n(begin(), size(), newValue);
301  }
302 
303  void
304  swap(Offset & other)
305  {
306  std::swap(m_InternalArray, other.m_InternalArray);
307  }
308 
309  constexpr const_iterator
310  cbegin() const
311  {
312  return &m_InternalArray[0];
313  }
314 
315  constexpr iterator
317  {
318  return &m_InternalArray[0];
319  }
320 
321  constexpr const_iterator
322  begin() const
323  {
324  return &m_InternalArray[0];
325  }
326 
327  constexpr const_iterator
328  cend() const
329  {
330  return &m_InternalArray[VDimension];
331  }
332 
333  constexpr iterator
334  end()
335  {
336  return &m_InternalArray[VDimension];
337  }
338 
339  constexpr const_iterator
340  end() const
341  {
342  return &m_InternalArray[VDimension];
343  }
344 
345  reverse_iterator
347  {
348  return reverse_iterator(end());
349  }
350 
351  const_reverse_iterator
352  rbegin() const
353  {
354  return const_reverse_iterator(end());
355  }
356 
357  reverse_iterator
359  {
360  return reverse_iterator(begin());
361  }
362 
363  const_reverse_iterator
364  rend() const
365  {
366  return const_reverse_iterator(begin());
367  }
368 
369  constexpr size_type
370  size() const
371  {
372  return VDimension;
373  }
374 
375  constexpr size_type
376  max_size() const
377  {
378  return VDimension;
379  }
380 
381  constexpr bool
382  empty() const
383  {
384  return false;
385  }
386 
387  reference operator[](size_type pos) { return m_InternalArray[pos]; }
388 
389  const_reference operator[](size_type pos) const { return m_InternalArray[pos]; }
390 
391  reference
393  {
394  ExceptionThrowingBoundsCheck(pos);
395  return m_InternalArray[pos];
396  }
397 
398  const_reference
399  at(size_type pos) const
400  {
401  ExceptionThrowingBoundsCheck(pos);
402  return m_InternalArray[pos];
403  }
404 
405  constexpr reference
407  {
408  return *begin();
409  }
410 
411  constexpr const_reference
412  front() const
413  {
414  return *begin();
415  }
416 
417  constexpr reference
419  {
420  return VDimension ? *(end() - 1) : *end();
421  }
422 
423  constexpr const_reference
424  back() const
425  {
426  return VDimension ? *(end() - 1) : *end();
427  }
428 
431  {
432  return &m_InternalArray[0];
433  }
434 
435  const OffsetValueType *
436  data() const
437  {
438  return &m_InternalArray[0];
439  }
440 
441 private:
442  void
444  {
445  if (pos >= VDimension)
446  {
447  throw std::out_of_range("array::ExceptionThrowingBoundsCheck");
448  }
449  }
450 
451 }; //------------ End struct Offset
452 
453 template <unsigned int VDimension>
454 Offset<VDimension>
456 {
457  Self ind{};
458  ind.m_InternalArray[dim] = 1;
459  return ind;
460 }
461 
462 template <unsigned int VDimension>
463 std::ostream &
464 operator<<(std::ostream & os, const Offset<VDimension> & ind)
465 {
466  os << '[';
467  unsigned int dimlim = VDimension - 1;
468  for (unsigned int i = 0; i < dimlim; ++i)
469  {
470  os << ind[i] << ", ";
471  }
472  if constexpr (VDimension >= 1)
473  {
474  os << ind[VDimension - 1];
475  }
476  os << ']';
477  return os;
478 }
479 
480 // ======================= Mirror the access pattern behavior of the std::array class
481 // Array comparisons.
482 template <unsigned int VDimension>
483 inline bool
485 {
486  return std::equal(one.begin(), one.end(), two.begin());
487 }
488 
489 template <unsigned int VDimension>
490 inline bool
492 {
493  return !(one == two);
494 }
495 
496 template <unsigned int VDimension>
497 inline bool
499 {
500  return std::lexicographical_compare(one.begin(), one.end(), two.begin(), two.end());
501 }
502 
503 template <unsigned int VDimension>
504 inline bool
506 {
507  return two < one;
508 }
509 
510 template <unsigned int VDimension>
511 inline bool
513 {
514  return !(one > two);
515 }
516 
517 template <unsigned int VDimension>
518 inline bool
520 {
521  return !(one < two);
522 }
523 
524 // Specialized algorithms [6.2.2.2].
525 template <unsigned int VDimension>
526 inline void
528 {
530 }
531 
532 } // end namespace itk
533 
534 #endif
itk::Offset::operator-=
const Self & operator-=(const Self &vec)
Definition: itkOffset.h:167
itk::Offset::GetOffset
const OffsetValueType * GetOffset() const
Definition: itkOffset.h:181
itk::Offset::Fill
void Fill(OffsetValueType value)
Definition: itkOffset.h:223
itk::Offset::end
constexpr iterator end()
Definition: itkOffset.h:334
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:557
itk::swap
void swap(Offset< VDimension > &one, Offset< VDimension > &two)
Definition: itkOffset.h:527
itk::Offset::back
constexpr const_reference back() const
Definition: itkOffset.h:424
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:571
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::Offset::begin
constexpr iterator begin()
Definition: itkOffset.h:316
itk::Offset::SetOffset
void SetOffset(const OffsetValueType val[VDimension])
Definition: itkOffset.h:191
itk::Offset::rbegin
reverse_iterator rbegin()
Definition: itkOffset.h:346
itk::Offset::operator[]
const_reference operator[](size_type pos) const
Definition: itkOffset.h:389
itk::Offset::rend
reverse_iterator rend()
Definition: itkOffset.h:358
itk::Offset< ImageDimension+1 >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkOffset.h:291
itk::Offset::GetBasisOffset
static Self GetBasisOffset(unsigned int dim)
Definition: itkOffset.h:455
itk::Offset::empty
constexpr bool empty() const
Definition: itkOffset.h:382
itk::Offset::front
constexpr reference front()
Definition: itkOffset.h:406
itk::Offset< ImageDimension+1 >::const_reference
const value_type & const_reference
Definition: itkOffset.h:285
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::Offset::rend
const_reverse_iterator rend() const
Definition: itkOffset.h:364
itk::Offset< ImageDimension+1 >::iterator
value_type * iterator
Definition: itkOffset.h:286
itk::Offset::CopyWithCast
void CopyWithCast(const FixedArray< TCoordinate, VDimension > &point)
Definition: itkOffset.h:261
itk::Offset::swap
void swap(Offset &other)
Definition: itkOffset.h:304
itk::Offset< ImageDimension+1 >::value_type
itk::OffsetValueType value_type
Definition: itkOffset.h:283
itk::Offset::operator[]
reference operator[](size_type pos)
Definition: itkOffset.h:387
itk::Offset::GetOffsetDimension
static constexpr unsigned int GetOffsetDimension()
Definition: itkOffset.h:84
itk::Offset::operator-=
const Self & operator-=(const Size< VDimension > &sz)
Definition: itkOffset.h:130
itk::Offset< ImageDimension+1 >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkOffset.h:290
itk::Offset::data
const OffsetValueType * data() const
Definition: itkOffset.h:436
itk::Offset::begin
constexpr const_iterator begin() const
Definition: itkOffset.h:322
itk::Offset::cbegin
constexpr const_iterator cbegin() const
Definition: itkOffset.h:310
itk::Offset< ImageDimension+1 >::difference_type
ptrdiff_t difference_type
Definition: itkOffset.h:289
itk::point
*par Constraints *The filter requires an image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
itk::Offset::size
constexpr size_type size() const
Definition: itkOffset.h:370
itk::Offset::operator+=
const Self & operator+=(const Self &vec)
Definition: itkOffset.h:155
itk::Offset::CopyWithRound
void CopyWithRound(const FixedArray< TCoordinate, VDimension > &point)
Definition: itkOffset.h:249
itk::Offset::operator+=
const Self & operator+=(const Size< VDimension > &sz)
Definition: itkOffset.h:118
itk::Offset::assign
void assign(const value_type &newValue)
Definition: itkOffset.h:298
itk::operator>=
bool operator>=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:578
itk::Offset::cend
constexpr const_iterator cend() const
Definition: itkOffset.h:328
itk::Offset::max_size
constexpr size_type max_size() const
Definition: itkOffset.h:376
itk::Offset::m_InternalArray
OffsetValueType m_InternalArray[VDimension]
Definition: itkOffset.h:242
itk::operator>
bool operator>(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:564
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:97
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::Offset::operator-
const Self operator-(const Self &vec) const
Definition: itkOffset.h:142
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:66
itk::Offset< ImageDimension+1 >::OffsetValueType
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:77
itk::Offset::operator+
const Self operator+(const Size< VDimension > &sz) const
Definition: itkOffset.h:105
itk::Offset::operator+
const Self operator+(const Self &vec) const
Definition: itkOffset.h:92
itk::operator!=
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:550
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Offset::rbegin
const_reverse_iterator rbegin() const
Definition: itkOffset.h:352
itk::Offset::data
OffsetValueType * data()
Definition: itkOffset.h:430
itk::Offset::at
reference at(size_type pos)
Definition: itkOffset.h:392
itk::Offset::back
constexpr reference back()
Definition: itkOffset.h:418
itk::Offset::GetElement
OffsetValueType GetElement(unsigned long element) const
Definition: itkOffset.h:215
itk::Offset< ImageDimension+1 >::reference
value_type & reference
Definition: itkOffset.h:284
itk::Offset::ExceptionThrowingBoundsCheck
void ExceptionThrowingBoundsCheck(size_type pos) const
Definition: itkOffset.h:443
itk::Offset::at
const_reference at(size_type pos) const
Definition: itkOffset.h:399
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::Offset< ImageDimension+1 >::size_type
unsigned int size_type
Definition: itkOffset.h:288
itk::Offset::front
constexpr const_reference front() const
Definition: itkOffset.h:412
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itkMath.h
itk::Offset::end
constexpr const_iterator end() const
Definition: itkOffset.h:340
itk::Offset< ImageDimension+1 >::const_iterator
const value_type * const_iterator
Definition: itkOffset.h:287
itk::Offset::SetElement
void SetElement(unsigned long element, OffsetValueType val)
Definition: itkOffset.h:203
itkSize.h