ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkNeighborhood.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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,
54  typename TAllocator = NeighborhoodAllocator< TPixel > >
55 class ITK_TEMPLATE_EXPORT Neighborhood
56 {
57 public:
59  using Self = Neighborhood;
60 
62  using AllocatorType = TAllocator;
63 
65  static constexpr unsigned int NeighborhoodDimension = VDimension;
66 
68  itkTypeMacroNoParent(Neighborhood);
69 
71  using PixelType = TPixel;
72 
78 
82 
85 
88 
91 
93  using DimensionValueType = unsigned int;
94 
98 
101  {
102  m_Radius.Fill(0);
103  m_Size.Fill(0);
104  for ( DimensionValueType i = 0; i < VDimension; i++ )
105  {
106  m_StrideTable[i] = 0;
107  }
108  }
110 
112  virtual ~Neighborhood() = default;
113 
115  Neighborhood(const Self & other);
116 
118  Neighborhood(Self &&) = default;
119 
121  Self & operator=(const Self & other);
122 
124  Self & operator=(Self&&) = default;
125 
127  bool
128  operator==(const Self & other) const
129  {
130  return ( m_Radius == other.m_Radius
131  && m_Size == other.m_Size
132  && m_DataBuffer == other.m_DataBuffer );
133  }
134 
136  bool operator!=(const Self & other) const
137  {
138  return ( m_Radius != other.m_Radius
139  || m_Size != other.m_Size
140  || m_DataBuffer != other.m_DataBuffer );
141  }
142 
144  const SizeType GetRadius() const
145  { return m_Radius; }
146 
150  { return m_Radius[n]; }
151 
155  { return m_Size[n]; }
156 
159  { return m_Size; }
160 
165  { return ( axis < VDimension ) ? m_StrideTable[axis] : 0; }
166 
169  { return m_DataBuffer.end(); }
171  { return m_DataBuffer.begin(); }
173  { return m_DataBuffer.end(); }
175  { return m_DataBuffer.begin(); }
177 
180  { return m_DataBuffer.size(); }
181 
184  { return m_DataBuffer[i]; }
185  const TPixel & operator[](NeighborIndexType i) const
186  { return m_DataBuffer[i]; }
188  { return m_DataBuffer[i]; }
190 
192  TPixel GetCenterValue() const
193  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
194 
197  void SetRadius(const SizeType &);
198 
201  void SetRadius(const SizeValueType *rad)
202  {
203  SizeType s;
204  std::copy(rad,
205  rad+VDimension,
206  s.m_InternalArray);
207  this->SetRadius(s);
208  }
210 
214  void SetRadius(const SizeValueType);
215 
217  void Print(std::ostream & os) const
218  { this->PrintSelf( os, Indent(0) ); }
219 
222  { return m_DataBuffer; }
224  { return m_DataBuffer; }
226 
228  TPixel & operator[](const OffsetType & o)
229  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
230  const TPixel & operator[](const OffsetType & o) const
231  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
233 
237  { return m_OffsetTable[i]; }
238 
239  virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const;
240 
242  {
243  return static_cast< NeighborIndexType >( this->Size() / 2 );
244  }
245 
246  std::slice GetSlice(unsigned int) const;
247 
248 protected:
250  void SetSize()
251  {
252  for ( DimensionValueType i = 0; i < VDimension; ++i )
253  {
254  m_Size[i] = m_Radius[i] * 2 + 1;
255  }
256  }
258 
260  virtual void Allocate(NeighborIndexType i)
261  { m_DataBuffer.set_size(i); }
262 
264  virtual void PrintSelf(std::ostream &, Indent) const;
265 
267  virtual void ComputeNeighborhoodStrideTable();
268 
271  virtual void ComputeNeighborhoodOffsetTable();
272 
273 private:
277 
281 
284 
287  OffsetValueType m_StrideTable[VDimension];
288 
290  std::vector< OffsetType > m_OffsetTable;
291 };
292 
293 template< typename TPixel, unsigned int VDimension, typename TContainer >
294 std::ostream & operator<<(std::ostream & os, const Neighborhood< TPixel, VDimension, TContainer > & neighborhood)
295 {
296  os << "Neighborhood:" << std::endl;
297  os << " Radius:" << neighborhood.GetRadius() << std::endl;
298  os << " Size:" << neighborhood.GetSize() << std::endl;
299  os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
300 
301  return os;
302 }
303 } // namespace itk
304 
305 #ifndef ITK_MANUAL_INSTANTIATION
306 #include "itkNeighborhood.hxx"
307 #endif
308 
309 /*
310 #ifndef ITK_MANUAL_INSTANTIATION
311 #include "itkNeighborhood.hxx"
312 #endif
313 */
314 
315 #endif
typename SizeType::SizeValueType SizeValueType
ConstIterator Begin() const
unsigned long SizeValueType
Definition: itkIntTypes.h:83
A light-weight container object for storing an N-dimensional neighborhood of values.
SizeValueType m_InternalArray[VDimension]
Definition: itkSize.h:220
TPixel & operator[](NeighborIndexType i)
typename AllocatorType::iterator Iterator
OffsetValueType GetStride(DimensionValueType axis) const
SizeValueType GetSize(DimensionValueType n) const
const AllocatorType & GetBufferReference() const
virtual void Allocate(NeighborIndexType i)
typename AllocatorType::const_iterator ConstIterator
NeighborIndexType Size() const
OffsetType GetOffset(NeighborIndexType i) const
TPixel & GetElement(NeighborIndexType i)
const SizeType GetRadius() const
SizeType GetSize() const
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image...
Definition: itkOffset.h:67
bool operator==(const Self &other) const
A flexible iterator for itk containers(i.e. itk::Neighborhood) that support pixel access through oper...
const TPixel & operator[](const OffsetType &o) const
TPixel GetCenterValue() const
const TPixel & operator[](NeighborIndexType i) const
AllocatorType m_DataBuffer
void SetRadius(const SizeValueType *rad)
NeighborIndexType GetCenterNeighborhoodIndex() const
Control indentation during Print() invocation.
Definition: itkIndent.h:49
bool operator!=(const Self &other) const
std::vector< OffsetType > m_OffsetTable
SizeValueType GetRadius(DimensionValueType n) const
void Print(std::ostream &os) const
signed long OffsetValueType
Definition: itkIntTypes.h:94
ConstIterator End() const
TPixel & operator[](const OffsetType &o)
AllocatorType & GetBufferReference()