ITK  4.6.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 > >
56 {
57 public:
59  typedef Neighborhood Self;
60 
62  typedef TAllocator AllocatorType;
63 
65  itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
66 
68  typedef TPixel PixelType;
69 
75 
77  typedef::itk::Size< VDimension > SizeType;
79 
81  typedef::itk::Size< VDimension > RadiusType;
82 
85 
88 
90  typedef unsigned int DimensionValueType;
91 
95 
98  {
99  m_Radius.Fill(0);
100  m_Size.Fill(0);
101  for ( DimensionValueType i = 0; i < VDimension; i++ )
102  {
103  m_StrideTable[i] = 0;
104  }
105  }
107 
109  virtual ~Neighborhood() {}
110 
112  Neighborhood(const Self & other);
113 
115  Self & operator=(const Self & other);
116 
118  bool
119  operator==(const Self & other) const
120  {
121  return ( m_Radius == other.m_Radius
122  && m_Size == other.m_Size
123  && m_DataBuffer == other.m_DataBuffer );
124  }
125 
127  bool operator!=(const Self & other) const
128  {
129  return ( m_Radius != other.m_Radius
130  || m_Size != other.m_Size
131  || m_DataBuffer != other.m_DataBuffer );
132  }
133 
135  const SizeType GetRadius() const
136  { return m_Radius; }
137 
141  { return m_Radius[n]; }
142 
146  { return m_Size[n]; }
147 
150  { return m_Size; }
151 
156  { return ( axis < VDimension ) ? m_StrideTable[axis] : 0; }
157 
160  { return m_DataBuffer.end(); }
162  { return m_DataBuffer.begin(); }
164  { return m_DataBuffer.end(); }
166  { return m_DataBuffer.begin(); }
168 
171  { return m_DataBuffer.size(); }
172 
175  { return m_DataBuffer[i]; }
176  const TPixel & operator[](NeighborIndexType i) const
177  { return m_DataBuffer[i]; }
179  { return m_DataBuffer[i]; }
181 
183  TPixel GetCenterValue() const
184  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
185 
188  void SetRadius(const SizeType &);
189 
192  void SetRadius(const SizeValueType *rad)
193  {
194  SizeType s;
195  std::copy(rad,
196  rad+VDimension,
197  s.m_Size);
198  this->SetRadius(s);
199  }
201 
205  void SetRadius(const SizeValueType);
206 
208  void Print(std::ostream & os) const
209  { this->PrintSelf( os, Indent(0) ); }
210 
213  { return m_DataBuffer; }
215  { return m_DataBuffer; }
217 
219  TPixel & operator[](const OffsetType & o)
220  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
221  const TPixel & operator[](const OffsetType & o) const
222  { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
224 
228  { return m_OffsetTable[i]; }
229 
230  virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const;
231 
233  {
234  return static_cast< NeighborIndexType >( this->Size() / 2 );
235  }
236 
237  std::slice GetSlice(unsigned int) const;
238 
239 protected:
241  void SetSize()
242  {
243  for ( DimensionValueType i = 0; i < VDimension; ++i )
244  {
245  m_Size[i] = m_Radius[i] * 2 + 1;
246  }
247  }
249 
251  virtual void Allocate(NeighborIndexType i)
252  { m_DataBuffer.set_size(i); }
253 
255  virtual void PrintSelf(std::ostream &, Indent) const;
256 
258  virtual void ComputeNeighborhoodStrideTable();
259 
262  virtual void ComputeNeighborhoodOffsetTable();
263 
264 private:
268 
272 
275 
279 
281  std::vector< OffsetType > m_OffsetTable;
282 };
283 
284 template< typename TPixel, unsigned int VDimension, typename TContainer >
285 std::ostream & operator<<(std::ostream & os, const Neighborhood< TPixel, VDimension, TContainer > & neighborhood)
286 {
287  os << "Neighborhood:" << std::endl;
288  os << " Radius:" << neighborhood.GetRadius() << std::endl;
289  os << " Size:" << neighborhood.GetSize() << std::endl;
290  os << " DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
291 
292  return os;
293 }
294 } // namespace itk
295 
296 #ifndef ITK_MANUAL_INSTANTIATION
297 #include "itkNeighborhood.hxx"
298 #endif
299 
300 /*
301 #ifndef ITK_MANUAL_INSTANTIATION
302 #include "itkNeighborhood.hxx"
303 #endif
304 */
305 
306 #endif
virtual void ComputeNeighborhoodOffsetTable()
typedef::itk::Size< VDimension > RadiusType
Neighborhood Self
ConstIterator Begin() const
signed long OffsetValueType
Definition: itkIntTypes.h:154
A light-weight container object for storing an N-dimensional neighborhood of values.
Self & operator=(const Self &other)
TPixel & operator[](NeighborIndexType i)
virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const
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
virtual void PrintSelf(std::ostream &, Indent) const
TAllocator AllocatorType
OffsetType GetOffset(NeighborIndexType i) const
Offset< VDimension > OffsetType
TPixel & GetElement(NeighborIndexType i)
typedef::itk::Size< VDimension > SizeType
std::slice GetSlice(unsigned int) const
const SizeType GetRadius() const
SizeType GetSize() const
OffsetValueType m_StrideTable[VDimension]
bool operator==(const Self &other) const
A flexible iterator for itk containers(i.e. itk::Neighborhood) that support pixel access through oper...
static const unsigned int NeighborhoodDimension
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 SetRadius(const SizeType &)
void Print(std::ostream &os) const
ConstIterator End() const
TPixel & operator[](const OffsetType &o)
AllocatorType & GetBufferReference()
virtual void ComputeNeighborhoodStrideTable()
SliceIterator< TPixel, Self > SliceIteratorType
SizeValueType NeighborIndexType