ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
26namespace itk
27{
28
64
65template <unsigned int VDimension = 2>
66struct ITK_TEMPLATE_EXPORT Offset final
67{
68public:
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 }
126
128 const Self &
130 {
131 for (unsigned int i = 0; i < VDimension; ++i)
132 {
133 m_InternalArray[i] -= sz[i];
134 }
135 return *this;
136 }
137
139 const Self
140 operator-(const Self & vec) const
141 {
142 Self result;
143
144 for (unsigned int i = 0; i < VDimension; ++i)
145 {
146 result[i] = m_InternalArray[i] - vec.m_InternalArray[i];
147 }
148 return result;
149 }
150
152 const Self &
153 operator+=(const Self & vec)
154 {
155 for (unsigned int i = 0; i < VDimension; ++i)
156 {
157 m_InternalArray[i] += vec.m_InternalArray[i];
158 }
159 return *this;
160 }
161
163 const Self &
164 operator-=(const Self & vec)
165 {
166 for (unsigned int i = 0; i < VDimension; ++i)
167 {
168 m_InternalArray[i] -= vec.m_InternalArray[i];
169 }
170 return *this;
171 }
172
175 const OffsetValueType *
176 GetOffset() const
177 {
178 return m_InternalArray;
179 }
180
185 void
186 SetOffset(const OffsetValueType val[VDimension])
187 {
188 std::copy_n(val, VDimension, m_InternalArray);
189 }
190
197 void
198 SetElement(unsigned long element, OffsetValueType val)
199 {
200 m_InternalArray[element] = val;
201 }
202
210 GetElement(unsigned long element) const
211 {
212 return m_InternalArray[element];
213 }
214
217 void
219 {
220 std::fill_n(begin(), size(), value);
221 } // MATCH std::array assign, ITK Fill
222
227 /*
228 * Ask the compiler to align a type to the maximum useful alignment for the target
229 * machine you are compiling for. Whenever you leave out the alignment factor in an
230 * aligned attribute specification, the compiler automatically sets the alignment
231 * for the type to the largest alignment that is ever used for any data type on
232 * the target machine you are compiling for. Doing this can often make copy
233 * operations more efficient, because the compiler can use whatever instructions
234 * copy the biggest chunks of memory when performing copies to or from the variables
235 * that have types that you have aligned this way.
236 */
237 static_assert(VDimension > 0, "Error: Only positive value sized VDimension allowed");
239
241 template <typename TCoordinate>
242 inline void
244 {
245 for (unsigned int i = 0; i < VDimension; ++i)
246 {
248 }
249 }
250
252 template <typename TCoordinate>
253 inline void
255 {
256 for (unsigned int i = 0; i < VDimension; ++i)
257 {
258 m_InternalArray[i] = static_cast<OffsetValueType>(point[i]);
259 }
260 }
261
265 static Self
266 GetBasisOffset(unsigned int dim);
267
268
269 // ======================= Mirror the access pattern behavior of the std::array class
279 using const_iterator = const value_type *;
280 using size_type = unsigned int;
281 using difference_type = ptrdiff_t;
282 using reverse_iterator = std::reverse_iterator<iterator>;
283 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
284
289 void
290 assign(const value_type & newValue)
291 {
292 std::fill_n(begin(), size(), newValue);
293 }
294
295 void
296 swap(Offset & other) noexcept
297 {
298 std::swap(m_InternalArray, other.m_InternalArray);
299 }
300
301 constexpr const_iterator
302 cbegin() const
303 {
304 return &m_InternalArray[0];
305 }
306
307 constexpr iterator
309 {
310 return &m_InternalArray[0];
311 }
312
313 constexpr const_iterator
314 begin() const
315 {
316 return &m_InternalArray[0];
317 }
318
319 constexpr const_iterator
320 cend() const
321 {
322 return &m_InternalArray[VDimension];
323 }
324
325 constexpr iterator
327 {
328 return &m_InternalArray[VDimension];
329 }
330
331 constexpr const_iterator
332 end() const
333 {
334 return &m_InternalArray[VDimension];
335 }
336
337 reverse_iterator
339 {
340 return reverse_iterator(end());
341 }
342
343 const_reverse_iterator
344 rbegin() const
345 {
346 return const_reverse_iterator(end());
347 }
348
349 reverse_iterator
351 {
352 return reverse_iterator(begin());
353 }
354
355 const_reverse_iterator
356 rend() const
357 {
359 }
360
361 constexpr size_type
362 size() const
363 {
364 return VDimension;
365 }
366
367 constexpr size_type
368 max_size() const
369 {
370 return VDimension;
371 }
372
373 constexpr bool
374 empty() const
375 {
376 return false;
377 }
378
379 reference
381 {
382 return m_InternalArray[pos];
383 }
384
385 const_reference
387 {
388 return m_InternalArray[pos];
389 }
390
391 reference
393 {
395 return m_InternalArray[pos];
396 }
397
398 const_reference
399 at(size_type pos) const
400 {
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
441private:
442 void
444 {
445 if (pos >= VDimension)
446 {
447 throw std::out_of_range("array::ExceptionThrowingBoundsCheck");
448 }
449 }
450
451}; //------------ End struct Offset
452
453template <unsigned int VDimension>
454Offset<VDimension>
456{
457 Self ind{};
458 ind.m_InternalArray[dim] = 1;
459 return ind;
460}
461
462template <unsigned int VDimension>
463std::ostream &
464operator<<(std::ostream & os, const Offset<VDimension> & ind)
465{
466 os << '[';
467 const 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.
482template <unsigned int VDimension>
483inline bool
485{
486 return std::equal(one.begin(), one.end(), two.begin());
487}
488
489template <unsigned int VDimension>
490inline bool
492{
493 return !(one == two);
494}
495
496template <unsigned int VDimension>
497inline bool
498operator<(const Offset<VDimension> & one, const Offset<VDimension> & two)
499{
500 return std::lexicographical_compare(one.begin(), one.end(), two.begin(), two.end());
501}
502
503template <unsigned int VDimension>
504inline bool
506{
507 return two < one;
508}
509
510template <unsigned int VDimension>
511inline bool
512operator<=(const Offset<VDimension> & one, const Offset<VDimension> & two)
513{
514 return !(one > two);
515}
516
517template <unsigned int VDimension>
518inline bool
520{
521 return !(one < two);
522}
523
524// Specialized algorithms [6.2.2.2].
525template <unsigned int VDimension>
526inline void
528{
529 std::swap(one.m_InternalArray, two.m_InternalArray);
530}
531
532} // end namespace itk
533
534#endif
Simulate a standard C array with copy semantics.
TInput TInput TReturn Round(TInput x)
Definition itkMath.h:178
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:247
bool operator>(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:566
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
long OffsetValueType
Definition itkIntTypes.h:97
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:573
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:545
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:552
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:559
bool operator>=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition itkIndex.h:580
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition itkOffset.h:67
constexpr reference back()
Definition itkOffset.h:418
const Self operator-(const Self &vec) const
Definition itkOffset.h:140
constexpr const_reference back() const
Definition itkOffset.h:424
const value_type & const_reference
Definition itkOffset.h:277
void CopyWithCast(const FixedArray< TCoordinate, VDimension > &point)
Definition itkOffset.h:254
reverse_iterator rbegin()
Definition itkOffset.h:338
constexpr bool empty() const
Definition itkOffset.h:374
constexpr iterator begin()
Definition itkOffset.h:308
static constexpr unsigned int GetOffsetDimension()
Definition itkOffset.h:84
const Self & operator+=(const Self &vec)
Definition itkOffset.h:153
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition itkOffset.h:283
constexpr reference front()
Definition itkOffset.h:406
void SetElement(unsigned long element, OffsetValueType val)
Definition itkOffset.h:198
const_reference operator[](size_type pos) const
Definition itkOffset.h:386
const Self operator+(const Size< VDimension > &sz) const
Definition itkOffset.h:105
const_reverse_iterator rbegin() const
Definition itkOffset.h:344
static Self GetBasisOffset(unsigned int dim)
Definition itkOffset.h:455
void SetOffset(const OffsetValueType val[VDimension])
Definition itkOffset.h:186
itk::OffsetValueType OffsetValueType
Definition itkOffset.h:77
constexpr iterator end()
Definition itkOffset.h:326
const_reference at(size_type pos) const
Definition itkOffset.h:399
const Self & operator-=(const Self &vec)
Definition itkOffset.h:164
const value_type * const_iterator
Definition itkOffset.h:279
OffsetValueType * data()
Definition itkOffset.h:430
constexpr const_reference front() const
Definition itkOffset.h:412
constexpr const_iterator end() const
Definition itkOffset.h:332
constexpr size_type max_size() const
Definition itkOffset.h:368
void assign(const value_type &newValue)
Definition itkOffset.h:290
void swap(Offset &other) noexcept
Definition itkOffset.h:296
reverse_iterator rend()
Definition itkOffset.h:350
const Self operator+(const Self &vec) const
Definition itkOffset.h:92
void ExceptionThrowingBoundsCheck(size_type pos) const
Definition itkOffset.h:443
constexpr const_iterator cbegin() const
Definition itkOffset.h:302
const OffsetValueType * data() const
Definition itkOffset.h:436
Offset< VDimension > OffsetType
Definition itkOffset.h:76
const Self & operator-=(const Size< VDimension > &sz)
Definition itkOffset.h:129
void Fill(OffsetValueType value)
Definition itkOffset.h:218
itk::OffsetValueType value_type
Definition itkOffset.h:275
const_reverse_iterator rend() const
Definition itkOffset.h:356
reference operator[](size_type pos)
Definition itkOffset.h:380
reference at(size_type pos)
Definition itkOffset.h:392
constexpr size_type size() const
Definition itkOffset.h:362
OffsetValueType m_InternalArray[VDimension]
Definition itkOffset.h:238
const OffsetValueType * GetOffset() const
Definition itkOffset.h:176
constexpr const_iterator begin() const
Definition itkOffset.h:314
static constexpr unsigned int Dimension
Definition itkOffset.h:80
constexpr const_iterator cend() const
Definition itkOffset.h:320
OffsetValueType GetElement(unsigned long element) const
Definition itkOffset.h:210
std::reverse_iterator< iterator > reverse_iterator
Definition itkOffset.h:282
void CopyWithRound(const FixedArray< TCoordinate, VDimension > &point)
Definition itkOffset.h:243
const Self & operator+=(const Size< VDimension > &sz)
Definition itkOffset.h:118
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition itkSize.h:70