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 
29 namespace itk
30 {
53 template <typename TPixel, unsigned int VDimension = 2, typename TAllocator = NeighborhoodAllocator<TPixel>>
54 class ITK_TEMPLATE_EXPORT Neighborhood
55 {
56 public:
58  using Self = Neighborhood;
59 
61  using AllocatorType = TAllocator;
62 
64  static constexpr unsigned int NeighborhoodDimension = VDimension;
65 
67  itkVirtualGetNameOfClassMacro(Neighborhood);
68 
70  using PixelType = TPixel;
71 
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 
125  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
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 
151  SizeType
152  GetSize() const
153  {
154  return m_Size;
155  }
156 
162  {
163  return (axis < VDimension) ? m_StrideTable[axis] : 0;
164  }
165 
167  Iterator
168  End()
169  {
170  return m_DataBuffer.end();
171  }
172  Iterator
173  Begin()
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
216  SetRadius(const SizeType &);
217 
220  void
222  {
223  SizeType s;
224  std::copy_n(rad, VDimension, s.m_InternalArray);
225  this->SetRadius(s);
226  }
232  void
233  SetRadius(const SizeValueType);
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
269  GetNeighborhoodIndex(const OffsetType &) const;
270 
271  NeighborIndexType
273  {
274  return static_cast<NeighborIndexType>(this->Size() / 2);
275  }
276 
277  std::slice
278  GetSlice(unsigned int) const;
279 
280 protected:
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
305  ComputeNeighborhoodStrideTable();
306 
309  virtual void
310  ComputeNeighborhoodOffsetTable();
311 
312 private:
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 
332 template <typename TPixel, unsigned int VDimension, typename TContainer>
333 std::ostream &
334 operator<<(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
itk::Neighborhood::SetSize
void SetSize()
Definition: itkNeighborhood.h:283
itk::Neighborhood::GetBufferReference
const AllocatorType & GetBufferReference() const
Definition: itkNeighborhood.h:249
itk::Neighborhood::operator[]
const TPixel & operator[](NeighborIndexType i) const
Definition: itkNeighborhood.h:198
itk::ConstNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::NeighborIndexType
typename NeighborhoodType::NeighborIndexType NeighborIndexType
Definition: itkConstNeighborhoodIterator.h:89
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::ConstNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::PixelType
typename ImageType ::PixelType PixelType
Definition: itkConstNeighborhoodIterator.h:57
itk::SliceIterator
A flexible iterator for itk containers(i.e. itk::Neighborhood) that support pixel access through oper...
Definition: itkSliceIterator.h:48
itk::Neighborhood::Size
NeighborIndexType Size() const
Definition: itkNeighborhood.h:191
itk::Neighborhood::GetStride
OffsetValueType GetStride(DimensionValueType axis) const
Definition: itkNeighborhood.h:161
itkOffset.h
itk::Neighborhood::SetRadius
void SetRadius(const SizeValueType *rad)
Definition: itkNeighborhood.h:221
itk::Neighborhood::GetRadius
SizeValueType GetRadius(DimensionValueType n) const
Definition: itkNeighborhood.h:137
itk::Neighborhood
A light-weight container object for storing an N-dimensional neighborhood of values.
Definition: itkNeighborhood.h:54
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ConstNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::ConstIterator
typename AllocatorType::const_iterator ConstIterator
Definition: itkNeighborhood.h:76
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::ConstShapedNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkConstShapedNeighborhoodIterator.h:91
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Neighborhood::m_DataBuffer
AllocatorType m_DataBuffer
Definition: itkNeighborhood.h:322
itk::Neighborhood::GetSize
SizeValueType GetSize(DimensionValueType n) const
Definition: itkNeighborhood.h:145
itk::Neighborhood::GetBufferReference
AllocatorType & GetBufferReference()
Definition: itkNeighborhood.h:244
itk::Neighborhood::operator[]
TPixel & operator[](NeighborIndexType i)
Definition: itkNeighborhood.h:197
itk::Neighborhood< char, TImage::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkNeighborhood.h:80
itk::Neighborhood::m_Radius
SizeType m_Radius
Definition: itkNeighborhood.h:315
itk::Neighborhood::GetRadius
const SizeType GetRadius() const
Definition: itkNeighborhood.h:129
itkIndent.h
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:97
itk::Neighborhood< char, TImage::ImageDimension >::DimensionValueType
unsigned int DimensionValueType
Definition: itkNeighborhood.h:92
itk::Neighborhood::operator==
bool operator==(const Self &other) const
Definition: itkNeighborhood.h:120
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::const_iterator
const ImageType ::InternalPixelType * * const_iterator
Definition: itkNeighborhoodAllocator.h:55
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:66
itk::Neighborhood::m_Size
SizeType m_Size
Definition: itkNeighborhood.h:319
itk::Neighborhood::GetOffset
OffsetType GetOffset(NeighborIndexType i) const
Definition: itkNeighborhood.h:263
itk::Neighborhood::GetSize
SizeType GetSize() const
Definition: itkNeighborhood.h:152
itkNeighborhoodAllocator.h
itk::Neighborhood::Print
void Print(std::ostream &os) const
Definition: itkNeighborhood.h:237
itk::Size::m_InternalArray
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:243
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkSliceIterator.h
itk::Neighborhood::GetCenterValue
TPixel GetCenterValue() const
Definition: itkNeighborhood.h:208
itk::ConstNeighborhoodIterator
Const version of NeighborhoodIterator, defining iteration of a local N-dimensional neighborhood of pi...
Definition: itkConstNeighborhoodIterator.h:51
itk::Neighborhood::GetElement
TPixel & GetElement(NeighborIndexType i)
Definition: itkNeighborhood.h:200
itk::Neighborhood::Allocate
virtual void Allocate(NeighborIndexType i)
Definition: itkNeighborhood.h:294
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::Neighborhood< char, TImage::ImageDimension >::NeighborIndexType
SizeValueType NeighborIndexType
Definition: itkNeighborhood.h:96
itk::Neighborhood::GetCenterNeighborhoodIndex
NeighborIndexType GetCenterNeighborhoodIndex() const
Definition: itkNeighborhood.h:272
itk::Neighborhood::operator[]
TPixel & operator[](const OffsetType &o)
Definition: itkNeighborhood.h:256
itk::Neighborhood::operator[]
const TPixel & operator[](const OffsetType &o) const
Definition: itkNeighborhood.h:257
itk::ConstNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::Iterator
typename AllocatorType::iterator Iterator
Definition: itkNeighborhood.h:75
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::iterator
ImageType ::InternalPixelType * * iterator
Definition: itkNeighborhoodAllocator.h:54
itk::NeighborhoodAllocator< char >