ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkRectangularImageNeighborhoodShape.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
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 
63  const Size<ImageDimension>& radius) ITK_NOEXCEPT
64  :
65  m_Radius(radius),
67  {
68  }
69 
70 
72  constexpr std::size_t GetNumberOfOffsets() const ITK_NOEXCEPT
73  {
74  return m_NumberOfOffsets;
75  }
76 
77 
79  void FillOffsets(Offset<ImageDimension>* const offsets) const ITK_NOEXCEPT
80  {
81  if (m_NumberOfOffsets > 0)
82  {
83  assert(offsets != nullptr);
86 
87  std::transform(m_Radius.begin(), m_Radius.end(), offset.begin(), [](const SizeValueType radiusValue)
88  {
89  return -static_cast<OffsetValueType>(radiusValue);
90  });
91 
92  for (std::size_t i = 0; i < m_NumberOfOffsets; ++i)
93  {
94  offsets[i] = offset;
95 
96  for (unsigned dimensionIndex = 0; dimensionIndex < ImageDimension; ++dimensionIndex)
97  {
98  OffsetValueType& offsetValue = offset[dimensionIndex];
99 
100  ++offsetValue;
101 
102  if (offsetValue <= static_cast<OffsetValueType>(m_Radius[dimensionIndex]))
103  {
104  break;
105  }
106  offsetValue = -static_cast<OffsetValueType>(m_Radius[dimensionIndex]);
107  }
108  }
109  }
110  }
111 
112 private:
113  // The radius of the neighborhood along each direction.
115 
116  // The number of offsets needed to represent this shape.
117  std::size_t m_NumberOfOffsets;
118 
119 
120  // Private helper function to calculate the number of Offsets by a recursive
121  // function call. Recursion is necessary for C++11 constexpr.
122  constexpr std::size_t CalculateNumberOfOffsets(const unsigned dimension) const ITK_NOEXCEPT
123  {
124  return (dimension == 0) ? 1 :
125  (2 * m_Radius.m_InternalArray[dimension - 1] + 1) * CalculateNumberOfOffsets(dimension - 1);
126  }
127 
128 };
129 
130 } // namespace Experimental
131 } // namespace itk
132 
133 #endif
constexpr std::vcl_size_t CalculateNumberOfOffsets(const unsigned dimension) const noexcept
unsigned long SizeValueType
Definition: itkIntTypes.h:83
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:220
iterator end()
Definition: itkSize.h:264
iterator begin()
Definition: itkOffset.h:294
iterator begin()
Definition: itkSize.h:254
constexpr RectangularImageNeighborhoodShape(const Size< ImageDimension > &radius) noexcept
signed long OffsetValueType
Definition: itkIntTypes.h:94
void FillOffsets(Offset< ImageDimension > *const offsets) const noexcept