ITK  6.0.0
Insight Toolkit
itkConstantBoundaryImageNeighborhoodPixelAccessPolicy.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 itkConstantBoundaryImageNeighborhoodPixelAccessPolicy_h
20#define itkConstantBoundaryImageNeighborhoodPixelAccessPolicy_h
21
22#include "itkIndex.h"
23#include "itkOffset.h"
24#include "itkSize.h"
25
26namespace itk
27{
28
41template <typename TImage>
43{
44private:
45 using NeighborhoodAccessorFunctorType = typename TImage::NeighborhoodAccessorFunctorType;
46 using PixelType = typename TImage::PixelType;
47 using InternalPixelType = typename TImage::InternalPixelType;
48
49 using ImageDimensionType = typename TImage::ImageDimensionType;
50 static constexpr ImageDimensionType ImageDimension = TImage::ImageDimension;
51
56
57 // Index value to the image buffer, indexing the current pixel. -1 is used to indicate out-of-bounds.
59
60 // A reference to the accessor of the image.
62
63 // The constant whose value is returned a pixel value outside the image is queried.
65
66
67 // Private helper function. Tells whether the pixel at 'pixelIndex' is inside the image.
68 static bool
69 IsInside(const IndexType & pixelIndex, const ImageSizeType & imageSize) noexcept
70 {
71 bool result = true;
72
73 for (ImageDimensionType i = 0; i < ImageDimension; ++i)
74 {
75 const IndexValueType indexValue = pixelIndex[i];
76
77 // Note: Do not 'quickly' break or return out of the for-loop when the
78 // result is false! For performance reasons (loop unrolling, etc.) it
79 // appears preferable to complete the for-loop iteration in this case!
80 result = result && (indexValue >= 0) && (static_cast<ImageSizeValueType>(indexValue) < imageSize[i]);
81 }
82 return result;
83 }
84
85
86 // Private helper function. Calculates and returns the index value of the
87 // current pixel within the image buffer.
88 static IndexValueType
89 CalculatePixelIndexValue(const OffsetType & offsetTable, const IndexType & pixelIndex) noexcept
90 {
91 IndexValueType result = 0;
92
93 for (ImageDimensionType i = 0; i < ImageDimension; ++i)
94 {
95 result += pixelIndex[i] * offsetTable[i];
96 }
97 return result;
98 }
99
100public:
104
105 // Deleted member functions:
109
110 // Explicitly-defaulted functions:
113 default;
114
118 const OffsetType & offsetTable,
119 const NeighborhoodAccessorFunctorType & neighborhoodAccessor,
120 const IndexType & pixelIndex,
121 const PixelType constant = {}) noexcept
122 : m_PixelIndexValue{ IsInside(pixelIndex, imageSize) ? CalculatePixelIndexValue(offsetTable, pixelIndex) : -1 }
123 , m_NeighborhoodAccessor(neighborhoodAccessor)
124 , m_Constant{ constant }
125 {}
133 GetPixelValue(const InternalPixelType * const imageBufferPointer) const noexcept
134 {
135 return (m_PixelIndexValue < 0) ? m_Constant : m_NeighborhoodAccessor.Get(imageBufferPointer + m_PixelIndexValue);
136 }
137
140 void
141 SetPixelValue(InternalPixelType * const imageBufferPointer, const PixelType & pixelValue) const noexcept
142 {
143 if (m_PixelIndexValue >= 0)
144 {
145 m_NeighborhoodAccessor.Set(imageBufferPointer + m_PixelIndexValue, pixelValue);
146 }
147 }
148};
152} // namespace itk
153
154#endif
ConstantBoundaryImageNeighborhoodPixelAccessPolicy(const ConstantBoundaryImageNeighborhoodPixelAccessPolicy &)=default
ConstantBoundaryImageNeighborhoodPixelAccessPolicy & operator=(const ConstantBoundaryImageNeighborhoodPixelAccessPolicy &)=delete
void SetPixelValue(InternalPixelType *const imageBufferPointer, const PixelType &pixelValue) const noexcept
static bool IsInside(const IndexType &pixelIndex, const ImageSizeType &imageSize) noexcept
PixelType GetPixelValue(const InternalPixelType *const imageBufferPointer) const noexcept
ConstantBoundaryImageNeighborhoodPixelAccessPolicy(const ImageSizeType &imageSize, const OffsetType &offsetTable, const NeighborhoodAccessorFunctorType &neighborhoodAccessor, const IndexType &pixelIndex, const PixelType constant={}) noexcept
static IndexValueType CalculatePixelIndexValue(const OffsetType &offsetTable, const IndexType &pixelIndex) noexcept
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
long IndexValueType
Definition: itkIntTypes.h:93
unsigned long SizeValueType
Definition: itkIntTypes.h:86
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:69