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 &
199 {
200 return m_DataBuffer[i];
201 }
202 const TPixel &
204 {
205 return m_DataBuffer[i];
206 }
207 TPixel &
209 {
210 return m_DataBuffer[i];
211 }
215 TPixel
217 {
218 return (this->operator[]((this->Size()) >> 1));
219 }
220
223 void
225
228 void
230 {
231 SizeType s;
232 std::copy_n(rad, VDimension, s.m_InternalArray);
233 this->SetRadius(s);
234 }
240 void
242
244 void
245 Print(std::ostream & os) const
246 {
247 this->PrintSelf(os, Indent(0));
248 }
249
251 AllocatorType &
253 {
254 return m_DataBuffer;
255 }
256 const AllocatorType &
258 {
259 return m_DataBuffer;
260 }
264 TPixel &
266 {
267 return this->operator[](this->GetNeighborhoodIndex(o));
268 }
269 const TPixel &
270 operator[](const OffsetType & o) const
271 {
272 return this->operator[](this->GetNeighborhoodIndex(o));
273 }
278 OffsetType
280 {
281 return m_OffsetTable[i];
282 }
283
284 virtual NeighborIndexType
286
289 {
290 return static_cast<NeighborIndexType>(this->Size() / 2);
291 }
292
293 std::slice
294 GetSlice(unsigned int) const;
295
296protected:
298 void
300 {
301 for (DimensionValueType i = 0; i < VDimension; ++i)
302 {
303 m_Size[i] = m_Radius[i] * 2 + 1;
304 }
305 }
309 virtual void
311 {
312 m_DataBuffer.set_size(i);
313 }
314
316 virtual void
317 PrintSelf(std::ostream &, Indent) const;
318
320 virtual void
322
325 virtual void
327
328private:
331 SizeType m_Radius{ { 0 } };
332
335 SizeType m_Size{ { 0 } };
336
338 AllocatorType m_DataBuffer{};
339
342 OffsetValueType m_StrideTable[VDimension]{ 0 };
343
345 std::vector<OffsetType> m_OffsetTable{};
346};
347
348template <typename TPixel, unsigned int VDimension, typename TContainer>
349std::ostream &
350operator<<(std::ostream & os, const Neighborhood<TPixel, VDimension, TContainer> & neighborhood)
351{
352 os << "Neighborhood: " << std::endl;
353 os << " Radius: " << neighborhood.GetRadius() << std::endl;
354 os << " Size: " << neighborhood.GetSize() << std::endl;
355 os << " DataBuffer: " << neighborhood.GetBufferReference() << std::endl;
356
357 return os;
358}
359} // namespace itk
360
361#ifndef ITK_MANUAL_INSTANTIATION
362# include "itkNeighborhood.hxx"
363#endif
364
365#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:244