ITK  5.1.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 namespace Experimental
31 {
32 
54 template <unsigned int VImageDimension>
56 {
57 public:
58  static constexpr unsigned int ImageDimension = VImageDimension;
60 
62  constexpr explicit RectangularImageNeighborhoodShape(const Size<ImageDimension> & radius) ITK_NOEXCEPT
63  : m_Radius(radius)
65  {}
66 
67 
69  constexpr std::size_t
70  GetNumberOfOffsets() const ITK_NOEXCEPT
71  {
72  return m_NumberOfOffsets;
73  }
74 
75 
77  void
78  FillOffsets(Offset<ImageDimension> * const offsets) const ITK_NOEXCEPT
79  {
80  if (m_NumberOfOffsets > 0)
81  {
82  assert(offsets != nullptr);
85 
86  std::transform(m_Radius.begin(), m_Radius.end(), offset.begin(), [](const SizeValueType radiusValue) {
87  return -static_cast<OffsetValueType>(radiusValue);
88  });
89 
90  for (std::size_t i = 0; i < m_NumberOfOffsets; ++i)
91  {
92  offsets[i] = offset;
93 
94  for (unsigned dimensionIndex = 0; dimensionIndex < ImageDimension; ++dimensionIndex)
95  {
96  OffsetValueType & offsetValue = offset[dimensionIndex];
97 
98  ++offsetValue;
99 
100  if (offsetValue <= static_cast<OffsetValueType>(m_Radius[dimensionIndex]))
101  {
102  break;
103  }
104  offsetValue = -static_cast<OffsetValueType>(m_Radius[dimensionIndex]);
105  }
106  }
107  }
108  }
109 
110 private:
111  // The radius of the neighborhood along each direction.
113 
114  // The number of offsets needed to represent this shape.
115  std::size_t m_NumberOfOffsets;
116 
117 
118  // Private helper function to calculate the number of Offsets by a recursive
119  // function call. Recursion is necessary for C++11 constexpr.
120  constexpr std::size_t
121  CalculateNumberOfOffsets(const unsigned dimension) const ITK_NOEXCEPT
122  {
123  return (dimension == 0)
124  ? 1
125  : (2 * m_Radius.m_InternalArray[dimension - 1] + 1) * CalculateNumberOfOffsets(dimension - 1);
126  }
127 };
128 
129 } // namespace Experimental
130 } // namespace itk
131 
132 #endif
itk::Size::end
iterator end()
Definition: itkSize.h:279
itk::Size::begin
iterator begin()
Definition: itkSize.h:267
itk::Experimental::RectangularImageNeighborhoodShape::m_NumberOfOffsets
std::vcl_size_t m_NumberOfOffsets
Definition: itkRectangularImageNeighborhoodShape.h:115
itk::Size< ImageDimension >
itkOffset.h
itk::Experimental::RectangularImageNeighborhoodShape::RectangularImageNeighborhoodShape
constexpr RectangularImageNeighborhoodShape(const Size< ImageDimension > &radius) noexcept
Definition: itkRectangularImageNeighborhoodShape.h:62
itk::Experimental::RectangularImageNeighborhoodShape
Definition: itkRectangularImageNeighborhoodShape.h:55
itk::Experimental::RectangularImageNeighborhoodShape::ImageDimension
static constexpr unsigned int ImageDimension
Definition: itkRectangularImageNeighborhoodShape.h:58
itk::Experimental::RectangularImageNeighborhoodShape::m_Radius
Size< ImageDimension > m_Radius
Definition: itkRectangularImageNeighborhoodShape.h:112
itk::Offset< ImageDimension >
itk::Experimental::RectangularImageNeighborhoodShape::GetNumberOfOffsets
constexpr std::vcl_size_t GetNumberOfOffsets() const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:70
itk::Experimental::RectangularImageNeighborhoodShape::FillOffsets
void FillOffsets(Offset< ImageDimension > *const offsets) const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:78
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: itkArray.h:26
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::Offset::begin
iterator begin()
Definition: itkOffset.h:311
itk::Experimental::RectangularImageNeighborhoodShape::CalculateNumberOfOffsets
constexpr std::vcl_size_t CalculateNumberOfOffsets(const unsigned dimension) const noexcept
Definition: itkRectangularImageNeighborhoodShape.h:121
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itkSize.h