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) noexcept
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
389  {
390  return m_InternalArray[pos];
391  }
392 
393  const_reference
395  {
396  return m_InternalArray[pos];
397  }
398 
399  reference
401  {
402  ExceptionThrowingBoundsCheck(pos);
403  return m_InternalArray[pos];
404  }
405 
406  const_reference
407  at(size_type pos) const
408  {
409  ExceptionThrowingBoundsCheck(pos);
410  return m_InternalArray[pos];
411  }
412 
413  constexpr reference
415  {
416  return *begin();
417  }
418 
419  constexpr const_reference
420  front() const
421  {
422  return *begin();
423  }
424 
425  constexpr reference
427  {
428  return VDimension ? *(end() - 1) : *end();
429  }
430 
431  constexpr const_reference
432  back() const
433  {
434  return VDimension ? *(end() - 1) : *end();
435  }
436 
439  {
440  return &m_InternalArray[0];
441  }
442 
443  const OffsetValueType *
444  data() const
445  {
446  return &m_InternalArray[0];
447  }
448 
449 private:
450  void
452  {
453  if (pos >= VDimension)
454  {
455  throw std::out_of_range("array::ExceptionThrowingBoundsCheck");
456  }
457  }
458 
459 }; //------------ End struct Offset
460 
461 template <unsigned int VDimension>
462 Offset<VDimension>
464 {
465  Self ind{};
466  ind.m_InternalArray[dim] = 1;
467  return ind;
468 }
469 
470 template <unsigned int VDimension>
471 std::ostream &
472 operator<<(std::ostream & os, const Offset<VDimension> & ind)
473 {
474  os << '[';
475  const unsigned int dimlim = VDimension - 1;
476  for (unsigned int i = 0; i < dimlim; ++i)
477  {
478  os << ind[i] << ", ";
479  }
480  if constexpr (VDimension >= 1)
481  {
482  os << ind[VDimension - 1];
483  }
484  os << ']';
485  return os;
486 }
487 
488 // ======================= Mirror the access pattern behavior of the std::array class
489 // Array comparisons.
490 template <unsigned int VDimension>
491 inline bool
493 {
494  return std::equal(one.begin(), one.end(), two.begin());
495 }
496 
497 template <unsigned int VDimension>
498 inline bool
500 {
501  return !(one == two);
502 }
503 
504 template <unsigned int VDimension>
505 inline bool
507 {
508  return std::lexicographical_compare(one.begin(), one.end(), two.begin(), two.end());
509 }
510 
511 template <unsigned int VDimension>
512 inline bool
514 {
515  return two < one;
516 }
517 
518 template <unsigned int VDimension>
519 inline bool
521 {
522  return !(one > two);
523 }
524 
525 template <unsigned int VDimension>
526 inline bool
528 {
529  return !(one < two);
530 }
531 
532 // Specialized algorithms [6.2.2.2].
533 template <unsigned int VDimension>
534 inline void
536 {
537  std::swap(one.m_InternalArray, two.m_InternalArray);
538 }
539 
540 } // end namespace itk
541 
542 #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:566
itk::Offset::back
constexpr const_reference back() const
Definition: itkOffset.h:432
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:580
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:394
itk::Offset::rend
reverse_iterator rend()
Definition: itkOffset.h:358
itk::Offset::swap
void swap(Offset &other) noexcept
Definition: itkOffset.h:304
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:463
itk::Offset::empty
constexpr bool empty() const
Definition: itkOffset.h:382
itk::Offset::front
constexpr reference front()
Definition: itkOffset.h:414
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< ImageDimension+1 >::value_type
itk::OffsetValueType value_type
Definition: itkOffset.h:283
itk::Offset::operator[]
reference operator[](size_type pos)
Definition: itkOffset.h:388
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::swap
void swap(Offset< VDimension > &one, Offset< VDimension > &two) noexcept
Definition: itkOffset.h:535
itk::Offset::data
const OffsetValueType * data() const
Definition: itkOffset.h:444
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:587
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:573
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:97
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
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:559
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:438
itk::Offset::at
reference at(size_type pos)
Definition: itkOffset.h:400
itk::Offset::back
constexpr reference back()
Definition: itkOffset.h:426
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:451
itk::Offset::at
const_reference at(size_type pos) const
Definition: itkOffset.h:407
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:420
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