ITK  4.2.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 #include <stdlib.h>
29 #include <string.h>
30 
31 namespace itk
32 {
55 template< class TPixel, unsigned int VDimension = 2,
56  class TAllocator = NeighborhoodAllocator< TPixel > >
57 class ITK_EXPORT Neighborhood
58 {
59 public:
61  typedef Neighborhood Self;
62 
64  typedef TAllocator AllocatorType;
65 
67  itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
68 
70  typedef TPixel PixelType;
71 
77 
79  typedef::itk::Size< VDimension > SizeType;
81 
83  typedef::itk::Size< VDimension > RadiusType;
84 
87 
90 
92  typedef unsigned int DimensionValueType;
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() {}
112 
114  Neighborhood(const Self & other);
115 
117  Self & operator=(const Self & other);
118 
120  bool
121  operator==(const Self & other) const
122  {
123  return ( m_Radius == other.m_Radius
124  && m_Size == other.m_Size
125  && m_DataBuffer == other.m_DataBuffer );
126  }
127 
129  bool operator!=(const Self & other) const
130  {
131  return ( m_Radius != other.m_Radius
132  || m_Size != other.m_Size
133  || m_DataBuffer != other.m_DataBuffer );
134  }
135 
137  const SizeType GetRadius() const
138  { return m_Radius; }
139 
143  { return m_Radius[n]; }
144 
148  { return m_Size[n]; }
149 
151  SizeType GetSize() const
152  { return m_Size; }
153 
157  OffsetValueType GetStride(DimensionValueType axis) const
158  { return ( axis < VDimension ) ? m_StrideTable[axis] : 0; }
159 
161  Iterator End()
162  { return m_DataBuffer.end(); }
163  Iterator Begin()
164  { return m_DataBuffer.begin(); }
165  ConstIterator End() const
166  { return m_DataBuffer.end(); }
167  ConstIterator Begin() const
168  { return m_DataBuffer.begin(); }
170 
173  { return m_DataBuffer.size(); }
174 
176  TPixel & operator[](NeighborIndexType i)
177  { return m_DataBuffer[i]; }
178  const TPixel & operator[](NeighborIndexType i) const
179  { return m_DataBuffer[i]; }
180  TPixel & GetElement(NeighborIndexType i)
181  { return m_DataBuffer[i]; }
183 
185  TPixel GetCenterValue() const
186  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
187 
190  void SetRadius(const SizeType &);
191 
194  void SetRadius(const SizeValueType *rad)
195  {
196  SizeType s;
197 
198  memcpy(s.m_Size, rad, sizeof( SizeValueType ) * VDimension);
199  this->SetRadius(s);
200  }
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 // Define instantiation macro for this template.
297 #define ITK_TEMPLATE_Neighborhood(_, EXPORT, TypeX, TypeY) \
298  namespace itk \
299  { \
300  _( 2 ( class EXPORT Neighborhood< ITK_TEMPLATE_2 TypeX > ) ) \
301  namespace Templates \
302  { \
303  typedef Neighborhood< ITK_TEMPLATE_2 TypeX > \
304  Neighborhood##TypeY; \
305  } \
306  }
307 
308 #if ITK_TEMPLATE_EXPLICIT
309 #include "Templates/itkNeighborhood+-.h"
310 #endif
311 
312 #if ITK_TEMPLATE_TXX
313 #include "itkNeighborhood.hxx"
314 #endif
315 
316 /*
317 #ifndef ITK_MANUAL_INSTANTIATION
318 #include "itkNeighborhood.hxx"
319 #endif
320 */
321 
322 #endif
323