ITK  5.3.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  * 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 #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  itkTypeMacroNoParent(Neighborhood);
68 
70  using PixelType = TPixel;
71 
77 
81 
84 
87 
90 
92  using DimensionValueType = unsigned int;
93 
97 
100  {
101  m_Radius.Fill(0);
102  m_Size.Fill(0);
103  for (DimensionValueType i = 0; i < VDimension; ++i)
104  {
105  m_StrideTable[i] = 0;
106  }
107  }
109 
111  virtual ~Neighborhood() = default;
112 
114  Neighborhood(const Self & other);
115 
117  Neighborhood(Self &&) = default;
118 
120  Self &
121  operator=(const Self & other);
122 
124  Self &
125  operator=(Self &&) = default;
126 
128  bool
129  operator==(const Self & other) const
130  {
131  return (m_Radius == other.m_Radius && m_Size == other.m_Size && m_DataBuffer == other.m_DataBuffer);
132  }
133 
134  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
135 
137  const SizeType
138  GetRadius() const
139  {
140  return m_Radius;
141  }
142 
147  {
148  return m_Radius[n];
149  }
150 
155  {
156  return m_Size[n];
157  }
158 
160  SizeType
161  GetSize() const
162  {
163  return m_Size;
164  }
165 
171  {
172  return (axis < VDimension) ? m_StrideTable[axis] : 0;
173  }
174 
176  Iterator
177  End()
178  {
179  return m_DataBuffer.end();
180  }
181  Iterator
182  Begin()
183  {
184  return m_DataBuffer.begin();
185  }
186  ConstIterator
187  End() const
188  {
189  return m_DataBuffer.end();
190  }
191  ConstIterator
192  Begin() const
193  {
194  return m_DataBuffer.begin();
195  }
197 
199  NeighborIndexType
200  Size() const
201  {
202  return m_DataBuffer.size();
203  }
204 
206  TPixel & operator[](NeighborIndexType i) { return m_DataBuffer[i]; }
207  const TPixel & operator[](NeighborIndexType i) const { return m_DataBuffer[i]; }
208  TPixel &
210  {
211  return m_DataBuffer[i];
212  }
214 
216  TPixel
218  {
219  return (this->operator[]((this->Size()) >> 1));
220  }
221 
224  void
225  SetRadius(const SizeType &);
226 
229  void
231  {
232  SizeType s;
233  std::copy_n(rad, VDimension, s.m_InternalArray);
234  this->SetRadius(s);
235  }
237 
241  void
242  SetRadius(const SizeValueType);
243 
245  void
246  Print(std::ostream & os) const
247  {
248  this->PrintSelf(os, Indent(0));
249  }
250 
252  AllocatorType &
254  {
255  return m_DataBuffer;
256  }
257  const AllocatorType &
259  {
260  return m_DataBuffer;
261  }
263 
265  TPixel & operator[](const OffsetType & o) { return this->operator[](this->GetNeighborhoodIndex(o)); }
266  const TPixel & operator[](const OffsetType & o) const { return this->operator[](this->GetNeighborhoodIndex(o)); }
268 
271  OffsetType
273  {
274  return m_OffsetTable[i];
275  }
276 
277  virtual NeighborIndexType
278  GetNeighborhoodIndex(const OffsetType &) const;
279 
280  NeighborIndexType
282  {
283  return static_cast<NeighborIndexType>(this->Size() / 2);
284  }
285 
286  std::slice
287  GetSlice(unsigned int) const;
288 
289 protected:
291  void
293  {
294  for (DimensionValueType i = 0; i < VDimension; ++i)
295  {
296  m_Size[i] = m_Radius[i] * 2 + 1;
297  }
298  }
300 
302  virtual void
304  {
305  m_DataBuffer.set_size(i);
306  }
307 
309  virtual void
310  PrintSelf(std::ostream &, Indent) const;
311 
313  virtual void
314  ComputeNeighborhoodStrideTable();
315 
318  virtual void
319  ComputeNeighborhoodOffsetTable();
320 
321 private:
325 
329 
332 
335  OffsetValueType m_StrideTable[VDimension];
336 
338  std::vector<OffsetType> m_OffsetTable;
339 };
340 
341 template <typename TPixel, unsigned int VDimension, typename TContainer>
342 std::ostream &
343 operator<<(std::ostream & os, const Neighborhood<TPixel, VDimension, TContainer> & neighborhood)
344 {
345  os << "Neighborhood:" << std::endl;
346  os << " Radius:" << neighborhood.GetRadius() << std::endl;
347  os << " Size:" << neighborhood.GetSize() << std::endl;
348  os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
349 
350  return os;
351 }
352 } // namespace itk
353 
354 #ifndef ITK_MANUAL_INSTANTIATION
355 # include "itkNeighborhood.hxx"
356 #endif
357 
358 #endif
itk::Neighborhood::SetSize
void SetSize()
Definition: itkNeighborhood.h:292
itk::Neighborhood::GetBufferReference
const AllocatorType & GetBufferReference() const
Definition: itkNeighborhood.h:258
itk::Neighborhood::operator[]
const TPixel & operator[](NeighborIndexType i) const
Definition: itkNeighborhood.h:207
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::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::Neighborhood::Size
NeighborIndexType Size() const
Definition: itkNeighborhood.h:200
itk::Neighborhood::GetStride
OffsetValueType GetStride(DimensionValueType axis) const
Definition: itkNeighborhood.h:170
itkOffset.h
itk::Neighborhood::SetRadius
void SetRadius(const SizeValueType *rad)
Definition: itkNeighborhood.h:230
itk::Neighborhood::GetRadius
SizeValueType GetRadius(DimensionValueType n) const
Definition: itkNeighborhood.h:146
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::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:331
itk::Neighborhood::GetSize
SizeValueType GetSize(DimensionValueType n) const
Definition: itkNeighborhood.h:154
itk::Neighborhood::m_OffsetTable
std::vector< OffsetType > m_OffsetTable
Definition: itkNeighborhood.h:338
itk::Neighborhood::GetBufferReference
AllocatorType & GetBufferReference()
Definition: itkNeighborhood.h:253
itk::Neighborhood::operator[]
TPixel & operator[](NeighborIndexType i)
Definition: itkNeighborhood.h:206
itk::Neighborhood< char, TImage::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkNeighborhood.h:80
itk::Neighborhood::m_Radius
SizeType m_Radius
Definition: itkNeighborhood.h:324
itk::Neighborhood::GetRadius
const SizeType GetRadius() const
Definition: itkNeighborhood.h:138
itkIndent.h
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:129
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::const_iterator
const ImageType ::InternalPixelType * * const_iterator
Definition: itkNeighborhoodAllocator.h:54
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:67
itk::Neighborhood::m_Size
SizeType m_Size
Definition: itkNeighborhood.h:328
itk::Neighborhood::GetOffset
OffsetType GetOffset(NeighborIndexType i) const
Definition: itkNeighborhood.h:272
itk::Neighborhood::GetSize
SizeType GetSize() const
Definition: itkNeighborhood.h:161
itkNeighborhoodAllocator.h
itk::Neighborhood::Print
void Print(std::ostream &os) const
Definition: itkNeighborhood.h:246
itk::Size::m_InternalArray
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:230
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkSliceIterator.h
itk::Neighborhood::GetCenterValue
TPixel GetCenterValue() const
Definition: itkNeighborhood.h:217
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::Neighborhood::GetElement
TPixel & GetElement(NeighborIndexType i)
Definition: itkNeighborhood.h:209
itk::Neighborhood::Allocate
virtual void Allocate(NeighborIndexType i)
Definition: itkNeighborhood.h:303
itk::Neighborhood::Neighborhood
Neighborhood()
Definition: itkNeighborhood.h:99
itk::Neighborhood< char, TImage::ImageDimension >::NeighborIndexType
SizeValueType NeighborIndexType
Definition: itkNeighborhood.h:96
itk::Neighborhood::GetCenterNeighborhoodIndex
NeighborIndexType GetCenterNeighborhoodIndex() const
Definition: itkNeighborhood.h:281
itk::Neighborhood::operator[]
TPixel & operator[](const OffsetType &o)
Definition: itkNeighborhood.h:265
itk::Neighborhood::operator[]
const TPixel & operator[](const OffsetType &o) const
Definition: itkNeighborhood.h:266
itk::ConstNeighborhoodIterator< ImageType, ZeroFluxNeumannBoundaryCondition< ImageType > >::Iterator
typename AllocatorType::iterator Iterator
Definition: itkNeighborhood.h:75
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::NeighborhoodAllocator< ImageType ::InternalPixelType * >::iterator
ImageType ::InternalPixelType * * iterator
Definition: itkNeighborhoodAllocator.h:53
itk::NeighborhoodAllocator< char >