ITK  5.2.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  * http://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 
28 namespace itk
29 {
30 
52 template <unsigned int VImageDimension>
54 {
55 public:
56  static constexpr unsigned int ImageDimension = VImageDimension;
58 
60  constexpr explicit RectangularImageNeighborhoodShape(const Size<ImageDimension> & radius) ITK_NOEXCEPT
61  : m_Radius(radius)
63  {}
64 
65 
67  constexpr std::size_t
68  GetNumberOfOffsets() const ITK_NOEXCEPT
69  {
70  return m_NumberOfOffsets;
71  }
72 
73 
75  void
76  FillOffsets(Offset<ImageDimension> * const offsets) const ITK_NOEXCEPT
77  {
78  if (m_NumberOfOffsets > 0)
79  {
80  assert(offsets != nullptr);
83 
84  std::transform(m_Radius.begin(), m_Radius.end(), offset.begin(), [](const SizeValueType radiusValue) {
85  return -static_cast<OffsetValueType>(radiusValue);
86  });
87 
88  for (std::size_t i = 0; i < m_NumberOfOffsets; ++i)
89  {
90  offsets[i] = offset;
91 
92  for (unsigned 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 
108 private:
109  // The radius of the neighborhood along each direction.
111 
112  // The number of offsets needed to represent this shape.
113  std::size_t m_NumberOfOffsets;
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 std::size_t
119  CalculateNumberOfOffsets(const unsigned dimension) const ITK_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
itk::Size::end
iterator end()
Definition: itkSize.h:279
itk::Size::begin
iterator begin()
Definition: itkSize.h:267
itk::RectangularImageNeighborhoodShape::m_Radius
Size< ImageDimension > m_Radius
Definition: itkRectangularImageNeighborhoodShape.h:110
itk::Size< ImageDimension >
itkOffset.h
itk::RectangularImageNeighborhoodShape::ImageDimension
static constexpr unsigned int ImageDimension
Definition: itkRectangularImageNeighborhoodShape.h:56
itk::RectangularImageNeighborhoodShape::CalculateNumberOfOffsets
constexpr std::vcl_size_t CalculateNumberOfOffsets(const unsigned dimension) const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:119
itk::Offset< ImageDimension >
itk::Size::m_InternalArray
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:230
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RectangularImageNeighborhoodShape::RectangularImageNeighborhoodShape
constexpr RectangularImageNeighborhoodShape(const Size< ImageDimension > &radius) noexcept
Definition: itkRectangularImageNeighborhoodShape.h:60
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::RectangularImageNeighborhoodShape::m_NumberOfOffsets
std::vcl_size_t m_NumberOfOffsets
Definition: itkRectangularImageNeighborhoodShape.h:113
itk::Offset::begin
iterator begin()
Definition: itkOffset.h:311
itk::RectangularImageNeighborhoodShape::GetNumberOfOffsets
constexpr std::vcl_size_t GetNumberOfOffsets() const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:68
itk::RectangularImageNeighborhoodShape::FillOffsets
void FillOffsets(Offset< ImageDimension > *const offsets) const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:76
itk::RectangularImageNeighborhoodShape
Definition: itkRectangularImageNeighborhoodShape.h:53
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkSize.h