ITK  4.4.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< class TPixel, unsigned int VDimension = 2,
54  class TAllocator = NeighborhoodAllocator< TPixel > >
55 class ITK_EXPORT Neighborhood
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 
149  SizeType GetSize() const
150  { return m_Size; }
151 
155  OffsetValueType GetStride(DimensionValueType axis) const
156  { return ( axis < VDimension ) ? m_StrideTable[axis] : 0; }
157 
159  Iterator End()
160  { return m_DataBuffer.end(); }
161  Iterator Begin()
162  { return m_DataBuffer.begin(); }
163  ConstIterator End() const
164  { return m_DataBuffer.end(); }
165  ConstIterator Begin() const
166  { return m_DataBuffer.begin(); }
168 
171  { return m_DataBuffer.size(); }
172 
174  TPixel & operator[](NeighborIndexType i)
175  { return m_DataBuffer[i]; }
176  const TPixel & operator[](NeighborIndexType i) const
177  { return m_DataBuffer[i]; }
178  TPixel & GetElement(NeighborIndexType 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 
212  AllocatorType & GetBufferReference()
213  { return m_DataBuffer; }
214  const AllocatorType & GetBufferReference() const
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 
227  OffsetType GetOffset(NeighborIndexType i) const
228  { return m_OffsetTable[i]; }
229 
230  virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const;
231 
232  NeighborIndexType GetCenterNeighborhoodIndex() const
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:
267  SizeType m_Radius;
268 
271  SizeType m_Size;
272 
275 
278  OffsetValueType m_StrideTable[VDimension];
279 
281  std::vector< OffsetType > m_OffsetTable;
282 };
283 
284 template< class TPixel, unsigned int VDimension, class 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
307