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
26namespace itk
27{
28
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 }
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 TCoordRep>
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 TCoordRep>
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
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
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
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 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{
530}
531
532} // end namespace itk
533
534#endif
Pixel-wise addition of two images.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
bool operator!=(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:491
*par Constraints *The filter 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
bool operator==(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:484
bool operator>(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:505
bool operator>=(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:519
std::ostream & operator<<(std::ostream &os, const Offset< VDimension > &ind)
Definition: itkOffset.h:464
void swap(Offset< VDimension > &one, Offset< VDimension > &two)
Definition: itkOffset.h:527
long OffsetValueType
Definition: itkIntTypes.h:97
bool operator<=(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:512
bool operator<(const Offset< VDimension > &one, const Offset< VDimension > &two)
Definition: itkOffset.h:498
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:67
unsigned int size_type
Definition: itkOffset.h:288
ptrdiff_t difference_type
Definition: itkOffset.h:289
constexpr reference back()
Definition: itkOffset.h:418
const Self operator-(const Self &vec) const
Definition: itkOffset.h:142
constexpr const_reference back() const
Definition: itkOffset.h:424
void CopyWithRound(const FixedArray< TCoordRep, VDimension > &point)
Definition: itkOffset.h:249
const value_type & const_reference
Definition: itkOffset.h:285
reverse_iterator rbegin()
Definition: itkOffset.h:346
constexpr bool empty() const
Definition: itkOffset.h:382
constexpr iterator begin()
Definition: itkOffset.h:316
static constexpr unsigned int GetOffsetDimension()
Definition: itkOffset.h:84
const Self & operator+=(const Self &vec)
Definition: itkOffset.h:155
value_type * iterator
Definition: itkOffset.h:286
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkOffset.h:291
constexpr reference front()
Definition: itkOffset.h:406
void SetElement(unsigned long element, OffsetValueType val)
Definition: itkOffset.h:203
const_reference operator[](size_type pos) const
Definition: itkOffset.h:389
const Self operator+(const Size< VDimension > &sz) const
Definition: itkOffset.h:105
const_reverse_iterator rbegin() const
Definition: itkOffset.h:352
static Self GetBasisOffset(unsigned int dim)
Definition: itkOffset.h:455
void SetOffset(const OffsetValueType val[VDimension])
Definition: itkOffset.h:191
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:77
constexpr iterator end()
Definition: itkOffset.h:334
const_reference at(size_type pos) const
Definition: itkOffset.h:399
const Self & operator-=(const Self &vec)
Definition: itkOffset.h:167
const value_type * const_iterator
Definition: itkOffset.h:287
OffsetValueType * data()
Definition: itkOffset.h:430
constexpr const_reference front() const
Definition: itkOffset.h:412
constexpr const_iterator end() const
Definition: itkOffset.h:340
value_type & reference
Definition: itkOffset.h:284
constexpr size_type max_size() const
Definition: itkOffset.h:376
void assign(const value_type &newValue)
Definition: itkOffset.h:298
reverse_iterator rend()
Definition: itkOffset.h:358
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:310
const OffsetValueType * data() const
Definition: itkOffset.h:436
const Self & operator-=(const Size< VDimension > &sz)
Definition: itkOffset.h:130
void Fill(OffsetValueType value)
Definition: itkOffset.h:223
itk::OffsetValueType value_type
Definition: itkOffset.h:283
void CopyWithCast(const FixedArray< TCoordRep, VDimension > &point)
Definition: itkOffset.h:261
const_reverse_iterator rend() const
Definition: itkOffset.h:364
reference operator[](size_type pos)
Definition: itkOffset.h:387
reference at(size_type pos)
Definition: itkOffset.h:392
void swap(Offset &other)
Definition: itkOffset.h:304
constexpr size_type size() const
Definition: itkOffset.h:370
OffsetValueType m_InternalArray[VDimension]
Definition: itkOffset.h:242
const OffsetValueType * GetOffset() const
Definition: itkOffset.h:181
constexpr const_iterator begin() const
Definition: itkOffset.h:322
constexpr const_iterator cend() const
Definition: itkOffset.h:328
OffsetValueType GetElement(unsigned long element) const
Definition: itkOffset.h:215
std::reverse_iterator< iterator > reverse_iterator
Definition: itkOffset.h:290
const Self & operator+=(const Size< VDimension > &sz)
Definition: itkOffset.h:118