ITK  6.0.0
Insight Toolkit
itkNeighborhood.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#ifndef itkNeighborhood_h
19#define itkNeighborhood_h
20
21#include <iostream>
23#include "itkIndent.h"
24#include "itkSliceIterator.h"
25#include "vnl/vnl_vector.h"
26#include "itkOffset.h"
27#include <vector>
28
29namespace itk
30{
53template <typename TPixel, unsigned int VDimension = 2, typename TAllocator = NeighborhoodAllocator<TPixel>>
54class ITK_TEMPLATE_EXPORT Neighborhood
55{
56public:
59
61 using AllocatorType = TAllocator;
62
64 static constexpr unsigned int NeighborhoodDimension = VDimension;
65
67 itkVirtualGetNameOfClassMacro(Neighborhood);
68
70 using PixelType = TPixel;
75 using Iterator = typename AllocatorType::iterator;
76 using ConstIterator = typename AllocatorType::const_iterator;
77
81
84
87
90
92 using DimensionValueType = unsigned int;
93
97
99 Neighborhood() = default;
100
102 virtual ~Neighborhood() = default;
103
105 Neighborhood(const Self &) = default;
106
108 Neighborhood(Self &&) = default;
109
111 Self &
112 operator=(const Self &) = default;
113
115 Self &
116 operator=(Self &&) = default;
117
119 bool
120 operator==(const Self & other) const
121 {
122 return (m_Radius == other.m_Radius && m_Size == other.m_Size && m_DataBuffer == other.m_DataBuffer);
123 }
124
126
128 const SizeType
129 GetRadius() const
130 {
131 return m_Radius;
132 }
133
138 {
139 return m_Radius.at(n);
140 }
141
146 {
147 return m_Size.at(n);
148 }
149
152 GetSize() const
153 {
154 return m_Size;
155 }
156
162 {
163 return (axis < VDimension) ? m_StrideTable[axis] : 0;
164 }
165
167 Iterator
169 {
170 return m_DataBuffer.end();
171 }
172 Iterator
174 {
175 return m_DataBuffer.begin();
176 }
177 ConstIterator
178 End() const
179 {
180 return m_DataBuffer.end();
181 }
182 ConstIterator
183 Begin() const
184 {
185 return m_DataBuffer.begin();
186 }
190 NeighborIndexType
191 Size() const
192 {
193 return m_DataBuffer.size();
194 }
195
197 TPixel & operator[](NeighborIndexType i) { return m_DataBuffer[i]; }
198 const TPixel & operator[](NeighborIndexType i) const { return m_DataBuffer[i]; }
199 TPixel &
201 {
202 return m_DataBuffer[i];
203 }
207 TPixel
209 {
210 return (this->operator[]((this->Size()) >> 1));
211 }
212
215 void
217
220 void
222 {
223 SizeType s;
224 std::copy_n(rad, VDimension, s.m_InternalArray);
225 this->SetRadius(s);
226 }
232 void
234
236 void
237 Print(std::ostream & os) const
238 {
239 this->PrintSelf(os, Indent(0));
240 }
241
243 AllocatorType &
245 {
246 return m_DataBuffer;
247 }
248 const AllocatorType &
250 {
251 return m_DataBuffer;
252 }
256 TPixel & operator[](const OffsetType & o) { return this->operator[](this->GetNeighborhoodIndex(o)); }
257 const TPixel & operator[](const OffsetType & o) const { return this->operator[](this->GetNeighborhoodIndex(o)); }
262 OffsetType
264 {
265 return m_OffsetTable[i];
266 }
267
268 virtual NeighborIndexType
270
273 {
274 return static_cast<NeighborIndexType>(this->Size() / 2);
275 }
276
277 std::slice
278 GetSlice(unsigned int) const;
279
280protected:
282 void
284 {
285 for (DimensionValueType i = 0; i < VDimension; ++i)
286 {
287 m_Size[i] = m_Radius[i] * 2 + 1;
288 }
289 }
293 virtual void
295 {
296 m_DataBuffer.set_size(i);
297 }
298
300 virtual void
301 PrintSelf(std::ostream &, Indent) const;
302
304 virtual void
306
309 virtual void
311
312private:
315 SizeType m_Radius{ { 0 } };
316
319 SizeType m_Size{ { 0 } };
320
322 AllocatorType m_DataBuffer{};
323
326 OffsetValueType m_StrideTable[VDimension]{ 0 };
327
329 std::vector<OffsetType> m_OffsetTable{};
330};
331
332template <typename TPixel, unsigned int VDimension, typename TContainer>
333std::ostream &
334operator<<(std::ostream & os, const Neighborhood<TPixel, VDimension, TContainer> & neighborhood)
335{
336 os << "Neighborhood: " << std::endl;
337 os << " Radius: " << neighborhood.GetRadius() << std::endl;
338 os << " Size: " << neighborhood.GetSize() << std::endl;
339 os << " DataBuffer: " << neighborhood.GetBufferReference() << std::endl;
340
341 return os;
342}
343} // namespace itk
344
345#ifndef ITK_MANUAL_INSTANTIATION
346# include "itkNeighborhood.hxx"
347#endif
348
349#endif
Control indentation during Print() invocation.
Definition: itkIndent.h:50
A light-weight container object for storing an N-dimensional neighborhood of values.
SizeType GetSize() const
typename AllocatorType::iterator Iterator
ConstIterator Begin() const
virtual void PrintSelf(std::ostream &, Indent) const
virtual void Allocate(NeighborIndexType i)
void SetRadius(const SizeValueType)
Neighborhood(Self &&)=default
virtual ~Neighborhood()=default
AllocatorType & GetBufferReference()
Neighborhood()=default
TPixel & operator[](const OffsetType &o)
Neighborhood(const Self &)=default
const TPixel & operator[](const OffsetType &o) const
TPixel GetCenterValue() const
TPixel & GetElement(NeighborIndexType i)
virtual void ComputeNeighborhoodOffsetTable()
OffsetType GetOffset(NeighborIndexType i) const
TAllocator AllocatorType
Self & operator=(Self &&)=default
virtual void ComputeNeighborhoodStrideTable()
void Print(std::ostream &os) const
SizeValueType GetSize(DimensionValueType n) const
virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const
unsigned int DimensionValueType
SizeValueType GetRadius(DimensionValueType n) const
const TPixel & operator[](NeighborIndexType i) const
NeighborIndexType GetCenterNeighborhoodIndex() const
const AllocatorType & GetBufferReference() const
void SetRadius(const SizeValueType *rad)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
void SetRadius(const SizeType &)
SizeValueType NeighborIndexType
typename AllocatorType::const_iterator ConstIterator
const SizeType GetRadius() const
TPixel & operator[](NeighborIndexType i)
NeighborIndexType Size() const
ConstIterator End() const
OffsetValueType GetStride(DimensionValueType axis) const
bool operator==(const Self &other) const
Self & operator=(const Self &)=default
typename SizeType::SizeValueType SizeValueType
AllocatorType m_DataBuffer
std::slice GetSlice(unsigned int) const
A flexible iterator for itk containers(i.e. itk::Neighborhood) that support pixel access through oper...
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
unsigned long SizeValueType
Definition: itkIntTypes.h:86
long OffsetValueType
Definition: itkIntTypes.h:97
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:243