ITK  6.0.0
Insight Toolkit
itkRectangularImageNeighborhoodShape.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
19#ifndef itkRectangularImageNeighborhoodShape_h
20#define itkRectangularImageNeighborhoodShape_h
21
22#include <algorithm> // For transform.
23#include <cassert>
24
25#include "itkOffset.h"
26#include "itkSize.h"
27
28namespace itk
29{
30
52template <unsigned int VImageDimension>
54{
55public:
56 static constexpr unsigned int ImageDimension = VImageDimension;
60 constexpr explicit RectangularImageNeighborhoodShape(const Size<ImageDimension> & radius) noexcept
61 : m_Radius(radius)
63 {}
64
65
67 constexpr size_t
68 GetNumberOfOffsets() const noexcept
69 {
70 return m_NumberOfOffsets;
71 }
72
73
75 void
76 FillOffsets(Offset<ImageDimension> * const offsets) const noexcept
77 {
78 if (m_NumberOfOffsets > 0)
79 {
80 assert(offsets != nullptr);
84 std::transform(m_Radius.begin(), m_Radius.end(), offset.begin(), [](const SizeValueType radiusValue) {
85 return -static_cast<OffsetValueType>(radiusValue);
86 });
87
88 for (size_t i = 0; i < m_NumberOfOffsets; ++i)
89 {
90 offsets[i] = offset;
91
92 for (unsigned int dimensionIndex = 0; dimensionIndex < ImageDimension; ++dimensionIndex)
93 {
94 OffsetValueType & offsetValue = offset[dimensionIndex];
95
96 ++offsetValue;
97
98 if (offsetValue <= static_cast<OffsetValueType>(m_Radius[dimensionIndex]))
99 {
100 break;
101 }
102 offsetValue = -static_cast<OffsetValueType>(m_Radius[dimensionIndex]);
103 }
104 }
105 }
106 }
107
108private:
109 // The radius of the neighborhood along each direction.
111
112 // The number of offsets needed to represent this shape.
114
115
116 // Private helper function to calculate the number of Offsets by a recursive
117 // function call. Recursion is necessary for C++11 constexpr.
118 constexpr size_t
119 CalculateNumberOfOffsets(const unsigned int dimension) const noexcept
120 {
121 return (dimension == 0)
122 ? 1
123 : (2 * m_Radius.m_InternalArray[dimension - 1] + 1) * CalculateNumberOfOffsets(dimension - 1);
124 }
125};
126
127} // namespace itk
128
129#endif
constexpr RectangularImageNeighborhoodShape(const Size< ImageDimension > &radius) noexcept
constexpr vcl_size_t GetNumberOfOffsets() const noexcept
constexpr vcl_size_t CalculateNumberOfOffsets(const unsigned int dimension) const noexcept
void FillOffsets(Offset< ImageDimension > *const offsets) const noexcept
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86
long OffsetValueType
Definition: itkIntTypes.h:97
constexpr iterator begin()
Definition: itkOffset.h:316
constexpr iterator begin()
Definition: itkSize.h:286
constexpr iterator end()
Definition: itkSize.h:304
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:243