ITK  5.2.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 
135  bool
136  operator!=(const Self & other) const
137  {
138  return (m_Radius != other.m_Radius || m_Size != other.m_Size || m_DataBuffer != other.m_DataBuffer);
139  }
140 
142  const SizeType
143  GetRadius() const
144  {
145  return m_Radius;
146  }
147 
152  {
153  return m_Radius[n];
154  }
155 
160  {
161  return m_Size[n];
162  }
163 
165  SizeType
166  GetSize() const
167  {
168  return m_Size;
169  }
170 
176  {
177  return (axis < VDimension) ? m_StrideTable[axis] : 0;
178  }
179 
181  Iterator
182  End()
183  {
184  return m_DataBuffer.end();
185  }
186  Iterator
187  Begin()
188  {
189  return m_DataBuffer.begin();
190  }
191  ConstIterator
192  End() const
193  {
194  return m_DataBuffer.end();
195  }
196  ConstIterator
197  Begin() const
198  {
199  return m_DataBuffer.begin();
200  }
202 
204  NeighborIndexType
205  Size() const
206  {
207  return m_DataBuffer.size();
208  }
209 
211  TPixel & operator[](NeighborIndexType i) { return m_DataBuffer[i]; }
212  const TPixel & operator[](NeighborIndexType i) const { return m_DataBuffer[i]; }
213  TPixel &
215  {
216  return m_DataBuffer[i];
217  }
219 
221  TPixel
223  {
224  return (this->operator[]((this->Size()) >> 1));
225  }
226 
229  void
230  SetRadius(const SizeType &);
231 
234  void
236  {
237  SizeType s;
238  std::copy_n(rad, VDimension, s.m_InternalArray);
239  this->SetRadius(s);
240  }
242 
246  void
247  SetRadius(const SizeValueType);
248 
250  void
251  Print(std::ostream & os) const
252  {
253  this->PrintSelf(os, Indent(0));
254  }
255 
257  AllocatorType &
259  {
260  return m_DataBuffer;
261  }
262  const AllocatorType &
264  {
265  return m_DataBuffer;
266  }
268 
270  TPixel & operator[](const OffsetType & o) { return this->operator[](this->GetNeighborhoodIndex(o)); }
271  const TPixel & operator[](const OffsetType & o) const { return this->operator[](this->GetNeighborhoodIndex(o)); }
273 
276  OffsetType
278  {
279  return m_OffsetTable[i];
280  }
281 
282  virtual NeighborIndexType
283  GetNeighborhoodIndex(const OffsetType &) const;
284 
285  NeighborIndexType
287  {
288  return static_cast<NeighborIndexType>(this->Size() / 2);
289  }
290 
291  std::slice
292  GetSlice(unsigned int) const;
293 
294 protected:
296  void
298  {
299  for (DimensionValueType i = 0; i < VDimension; ++i)
300  {
301  m_Size[i] = m_Radius[i] * 2 + 1;
302  }
303  }
305 
307  virtual void
309  {
310  m_DataBuffer.set_size(i);
311  }
312 
314  virtual void
315  PrintSelf(std::ostream &, Indent) const;
316 
318  virtual void
319  ComputeNeighborhoodStrideTable();
320 
323  virtual void
324  ComputeNeighborhoodOffsetTable();
325 
326 private:
330 
334 
337 
340  OffsetValueType m_StrideTable[VDimension];
341 
343  std::vector<OffsetType> m_OffsetTable;
344 };
345 
346 template <typename TPixel, unsigned int VDimension, typename TContainer>
347 std::ostream &
348 operator<<(std::ostream & os, const Neighborhood<TPixel, VDimension, TContainer> & neighborhood)
349 {
350  os << "Neighborhood:" << std::endl;
351  os << " Radius:" << neighborhood.GetRadius() << std::endl;
352  os << " Size:" << neighborhood.GetSize() << std::endl;
353  os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
354 
355  return os;
356 }
357 } // namespace itk
358 
359 #ifndef ITK_MANUAL_INSTANTIATION
360 # include "itkNeighborhood.hxx"
361 #endif
362 
363 /*
364 #ifndef ITK_MANUAL_INSTANTIATION
365 #include "itkNeighborhood.hxx"
366 #endif
367 */
368 
369 #endif
itk::Neighborhood::SetSize
void SetSize()
Definition: itkNeighborhood.h:297
itk::Neighborhood::GetBufferReference
const AllocatorType & GetBufferReference() const
Definition: itkNeighborhood.h:263
itk::Neighborhood::operator[]
const TPixel & operator[](NeighborIndexType i) const
Definition: itkNeighborhood.h:212
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
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:218
itk::Neighborhood::Size
NeighborIndexType Size() const
Definition: itkNeighborhood.h:205
itk::Neighborhood::GetStride
OffsetValueType GetStride(DimensionValueType axis) const
Definition: itkNeighborhood.h:175
itkOffset.h
itk::Neighborhood::SetRadius
void SetRadius(const SizeValueType *rad)
Definition: itkNeighborhood.h:235
itk::Neighborhood::GetRadius
SizeValueType GetRadius(DimensionValueType n) const
Definition: itkNeighborhood.h:151
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::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Neighborhood::m_DataBuffer
AllocatorType m_DataBuffer
Definition: itkNeighborhood.h:336
itk::Neighborhood::GetSize
SizeValueType GetSize(DimensionValueType n) const
Definition: itkNeighborhood.h:159
itk::Neighborhood::m_OffsetTable
std::vector< OffsetType > m_OffsetTable
Definition: itkNeighborhood.h:343
itk::Neighborhood::GetBufferReference
AllocatorType & GetBufferReference()
Definition: itkNeighborhood.h:258
itk::Neighborhood::operator[]
TPixel & operator[](NeighborIndexType i)
Definition: itkNeighborhood.h:211
itk::Neighborhood< char, TImage::ImageDimension >::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkNeighborhood.h:80
itk::Neighborhood::m_Radius
SizeType m_Radius
Definition: itkNeighborhood.h:329
itk::Neighborhood::GetRadius
const SizeType GetRadius() const
Definition: itkNeighborhood.h:143
itkIndent.h
itk::Neighborhood< char, TImage::ImageDimension >::Iterator
typename AllocatorType::iterator Iterator
Definition: itkNeighborhood.h:75
itk::Neighborhood< char, TImage::ImageDimension >::PixelType
char PixelType
Definition: itkNeighborhood.h:70
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< char >::const_iterator
const char * const_iterator
Definition: itkNeighborhoodAllocator.h:54
itk::Neighborhood< char, TImage::ImageDimension >::ConstIterator
typename AllocatorType::const_iterator ConstIterator
Definition: itkNeighborhood.h:76
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:333
itk::Neighborhood::GetOffset
OffsetType GetOffset(NeighborIndexType i) const
Definition: itkNeighborhood.h:277
itk::Neighborhood::GetSize
SizeType GetSize() const
Definition: itkNeighborhood.h:166
itkNeighborhoodAllocator.h
itk::Neighborhood::Print
void Print(std::ostream &os) const
Definition: itkNeighborhood.h:251
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:222
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::Neighborhood::GetElement
TPixel & GetElement(NeighborIndexType i)
Definition: itkNeighborhood.h:214
itk::Neighborhood::Allocate
virtual void Allocate(NeighborIndexType i)
Definition: itkNeighborhood.h:308
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:286
itk::Neighborhood::operator[]
TPixel & operator[](const OffsetType &o)
Definition: itkNeighborhood.h:270
itk::Neighborhood::operator[]
const TPixel & operator[](const OffsetType &o) const
Definition: itkNeighborhood.h:271
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Neighborhood::operator!=
bool operator!=(const Self &other) const
Definition: itkNeighborhood.h:136
itk::NeighborhoodAllocator< char >::iterator
char * iterator
Definition: itkNeighborhoodAllocator.h:53
itk::NeighborhoodAllocator< char >