ITK  4.13.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  typedef Neighborhood Self;
60 
62  typedef TAllocator AllocatorType;
63 
65  itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
66 
68  itkTypeMacroNoParent(Neighborhood);
69 
71  typedef TPixel PixelType;
72 
78 
80  typedef::itk::Size< VDimension > SizeType;
82 
84  typedef::itk::Size< VDimension > RadiusType;
85 
88 
91 
93  typedef unsigned int DimensionValueType;
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() {}
113 
115  Neighborhood(const Self & other);
116 
118  Self & operator=(const Self & other);
119 
121  bool
122  operator==(const Self & other) const
123  {
124  return ( m_Radius == other.m_Radius
125  && m_Size == other.m_Size
126  && m_DataBuffer == other.m_DataBuffer );
127  }
128 
130  bool operator!=(const Self & other) const
131  {
132  return ( m_Radius != other.m_Radius
133  || m_Size != other.m_Size
134  || m_DataBuffer != other.m_DataBuffer );
135  }
136 
138  const SizeType GetRadius() const
139  { return m_Radius; }
140 
144  { return m_Radius[n]; }
145 
149  { return m_Size[n]; }
150 
153  { return m_Size; }
154 
159  { return ( axis < VDimension ) ? m_StrideTable[axis] : 0; }
160 
163  { return m_DataBuffer.end(); }
165  { return m_DataBuffer.begin(); }
167  { return m_DataBuffer.end(); }
169  { return m_DataBuffer.begin(); }
171 
174  { return m_DataBuffer.size(); }
175 
178  { return m_DataBuffer[i]; }
179  const TPixel & operator[](NeighborIndexType i) const
180  { return m_DataBuffer[i]; }
182  { return m_DataBuffer[i]; }
184 
186  TPixel GetCenterValue() const
187  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
188 
191  void SetRadius(const SizeType &);
192 
195  void SetRadius(const SizeValueType *rad)
196  {
197  SizeType s;
198  std::copy(rad,
199  rad+VDimension,
200  s.m_Size);
201  this->SetRadius(s);
202  }
204 
208  void SetRadius(const SizeValueType);
209 
211  void Print(std::ostream & os) const
212  { this->PrintSelf( os, Indent(0) ); }
213 
216  { return m_DataBuffer; }
218  { return m_DataBuffer; }
220 
222  TPixel & operator[](const OffsetType & o)
223  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
224  const TPixel & operator[](const OffsetType & o) const
225  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
227 
231  { return m_OffsetTable[i]; }
232 
233  virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const;
234 
236  {
237  return static_cast< NeighborIndexType >( this->Size() / 2 );
238  }
239 
240  std::slice GetSlice(unsigned int) const;
241 
242 protected:
244  void SetSize()
245  {
246  for ( DimensionValueType i = 0; i < VDimension; ++i )
247  {
248  m_Size[i] = m_Radius[i] * 2 + 1;
249  }
250  }
252 
254  virtual void Allocate(NeighborIndexType i)
255  { m_DataBuffer.set_size(i); }
256 
258  virtual void PrintSelf(std::ostream &, Indent) const;
259 
261  virtual void ComputeNeighborhoodStrideTable();
262 
265  virtual void ComputeNeighborhoodOffsetTable();
266 
267 private:
271 
275 
278 
281  OffsetValueType m_StrideTable[VDimension];
282 
284  std::vector< OffsetType > m_OffsetTable;
285 };
286 
287 template< typename TPixel, unsigned int VDimension, typename TContainer >
288 std::ostream & operator<<(std::ostream & os, const Neighborhood< TPixel, VDimension, TContainer > & neighborhood)
289 {
290  os << "Neighborhood:" << std::endl;
291  os << " Radius:" << neighborhood.GetRadius() << std::endl;
292  os << " Size:" << neighborhood.GetSize() << std::endl;
293  os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
294 
295  return os;
296 }
297 } // namespace itk
298 
299 #ifndef ITK_MANUAL_INSTANTIATION
300 #include "itkNeighborhood.hxx"
301 #endif
302 
303 /*
304 #ifndef ITK_MANUAL_INSTANTIATION
305 #include "itkNeighborhood.hxx"
306 #endif
307 */
308 
309 #endif
typedef::itk::Size< VDimension > RadiusType
Neighborhood Self
ConstIterator Begin() const
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
signed long OffsetValueType
Definition: itkIntTypes.h:154
A light-weight container object for storing an N-dimensional neighborhood of values.
TPixel & operator[](NeighborIndexType i)
OffsetValueType GetStride(DimensionValueType axis) const
SizeValueType GetSize(DimensionValueType n) const
const AllocatorType & GetBufferReference() const
virtual void Allocate(NeighborIndexType i)
unsigned long SizeValueType
Definition: itkIntTypes.h:143
unsigned int DimensionValueType
NeighborIndexType Size() const
TAllocator AllocatorType
OffsetType GetOffset(NeighborIndexType i) const
Offset< VDimension > OffsetType
TPixel & GetElement(NeighborIndexType i)
typedef::itk::Size< VDimension > SizeType
const SizeType GetRadius() const
SizeType GetSize() const
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::iterator Iterator
AllocatorType m_DataBuffer
void SetRadius(const SizeValueType *rad)
NeighborIndexType GetCenterNeighborhoodIndex() const
Control indentation during Print() invocation.
Definition: itkIndent.h:49
AllocatorType::const_iterator ConstIterator
bool operator!=(const Self &other) const
SizeType::SizeValueType SizeValueType
std::vector< OffsetType > m_OffsetTable
SizeValueType GetRadius(DimensionValueType n) const
void Print(std::ostream &os) const
ConstIterator End() const
TPixel & operator[](const OffsetType &o)
AllocatorType & GetBufferReference()
SliceIterator< TPixel, Self > SliceIteratorType
SizeValueType NeighborIndexType