ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkNeighborhood.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkNeighborhood_h
00019 #define __itkNeighborhood_h
00020 
00021 #include <iostream>
00022 #include "itkNeighborhoodAllocator.h"
00023 #include "itkIndent.h"
00024 #include "itkSliceIterator.h"
00025 #include "vnl/vnl_vector.h"
00026 #include "itkOffset.h"
00027 #include <vector>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 namespace itk
00032 {
00055 template< class TPixel, unsigned int VDimension = 2,
00056           class TAllocator = NeighborhoodAllocator< TPixel > >
00057 class ITK_EXPORT Neighborhood
00058 {
00059 public:
00061   typedef Neighborhood Self;
00062 
00064   typedef TAllocator AllocatorType;
00065 
00067   itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
00068 
00070   typedef TPixel PixelType;
00071 
00075   typedef typename AllocatorType::iterator       Iterator;
00076   typedef typename AllocatorType::const_iterator ConstIterator;
00077 
00079   typedef::itk::Size< VDimension >         SizeType;
00080   typedef typename SizeType::SizeValueType SizeValueType;
00081 
00083   typedef::itk::Size< VDimension > RadiusType;
00084 
00086   typedef Offset< VDimension > OffsetType;
00087 
00089   typedef SliceIterator< TPixel, Self > SliceIteratorType;
00090 
00092   typedef unsigned int                  DimensionValueType;
00093 
00096   typedef unsigned int                  NeighborIndexType;
00097 
00099   Neighborhood()
00100   {
00101     m_Radius.Fill(0);
00102     m_Size.Fill(0);
00103     for ( DimensionValueType i = 0; i < VDimension; i++ )
00104       {
00105       m_StrideTable[i] = 0;
00106       }
00107   }
00109 
00111   virtual ~Neighborhood() {}
00112 
00114   Neighborhood(const Self & other);
00115 
00117   Self & operator=(const Self & other);
00118 
00120   bool
00121   operator==(const Self & other) const
00122   {
00123     return ( m_Radius == other.m_Radius
00124              && m_Size   == other.m_Size
00125              && m_DataBuffer == other.m_DataBuffer );
00126   }
00127 
00129   bool operator!=(const Self & other) const
00130   {
00131     return ( m_Radius != other.m_Radius
00132              || m_Size   != other.m_Size
00133              || m_DataBuffer != other.m_DataBuffer );
00134   }
00135 
00137   const SizeType GetRadius() const
00138   { return m_Radius; }
00139 
00142   SizeValueType GetRadius(DimensionValueType n) const
00143   { return m_Radius[n]; }
00144 
00147   SizeValueType GetSize(DimensionValueType n) const
00148   { return m_Size[n]; }
00149 
00151   SizeType GetSize() const
00152   { return m_Size; }
00153 
00157   OffsetValueType GetStride(DimensionValueType axis) const
00158   { return ( axis < VDimension ) ? m_StrideTable[axis] : 0;  }
00159 
00161   Iterator End()
00162   { return m_DataBuffer.end(); }
00163   Iterator Begin()
00164   { return m_DataBuffer.begin(); }
00165   ConstIterator End() const
00166   { return m_DataBuffer.end(); }
00167   ConstIterator Begin() const
00168   { return m_DataBuffer.begin(); }
00170 
00172   NeighborIndexType Size() const
00173   { return m_DataBuffer.size(); }
00174 
00176   TPixel & operator[](NeighborIndexType i)
00177   { return m_DataBuffer[i]; }
00178   const TPixel & operator[](NeighborIndexType i) const
00179   { return m_DataBuffer[i]; }
00180   TPixel & GetElement(NeighborIndexType i)
00181   { return m_DataBuffer[i]; }
00183 
00185   TPixel GetCenterValue() const
00186   { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
00187 
00190   void SetRadius(const SizeType &);
00191 
00194   void SetRadius(const SizeValueType *rad)
00195   {
00196     SizeType s;
00197 
00198     memcpy(s.m_Size, rad, sizeof( SizeValueType ) * VDimension);
00199     this->SetRadius(s);
00200   }
00201 
00205   void SetRadius(const SizeValueType);
00206 
00208   void Print(std::ostream & os) const
00209   { this->PrintSelf( os, Indent(0) );  }
00210 
00212   AllocatorType & GetBufferReference()
00213   { return m_DataBuffer; }
00214   const AllocatorType & GetBufferReference() const
00215   { return m_DataBuffer; }
00217 
00219   TPixel & operator[](const OffsetType & o)
00220   { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
00221   const TPixel & operator[](const OffsetType & o) const
00222   { return this->operator[]( this->GetNeighborhoodIndex(o) ); }
00224 
00227   OffsetType GetOffset(NeighborIndexType i) const
00228   { return m_OffsetTable[i]; }
00229 
00230   virtual NeighborIndexType GetNeighborhoodIndex(const OffsetType &) const;
00231 
00232   NeighborIndexType GetCenterNeighborhoodIndex() const
00233   {
00234     return static_cast< NeighborIndexType >( this->Size() / 2 );
00235   }
00236 
00237   std::slice GetSlice(unsigned int) const;
00238 
00239 protected:
00241   void SetSize()
00242   {
00243     for ( DimensionValueType i = 0; i < VDimension; ++i )
00244       {
00245       m_Size[i] = m_Radius[i] * 2 + 1;
00246       }
00247   }
00249 
00251   virtual void Allocate(NeighborIndexType i)
00252   { m_DataBuffer.set_size(i); }
00253 
00255   virtual void PrintSelf(std::ostream &, Indent) const;
00256 
00258   virtual void ComputeNeighborhoodStrideTable();
00259 
00262   virtual void ComputeNeighborhoodOffsetTable();
00263 
00264 private:
00267   SizeType m_Radius;
00268 
00271   SizeType m_Size;
00272 
00274   AllocatorType m_DataBuffer;
00275 
00278   OffsetValueType m_StrideTable[VDimension];
00279 
00281   std::vector< OffsetType > m_OffsetTable;
00282 };
00283 
00284 template< class TPixel, unsigned int VDimension, class TContainer >
00285 std::ostream & operator<<(std::ostream & os, const Neighborhood< TPixel, VDimension, TContainer > & neighborhood)
00286 {
00287   os << "Neighborhood:" << std::endl;
00288   os << "    Radius:" << neighborhood.GetRadius() << std::endl;
00289   os << "    Size:" << neighborhood.GetSize() << std::endl;
00290   os << "    DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
00291 
00292   return os;
00293 }
00294 } // namespace itk
00295 
00296 // Define instantiation macro for this template.
00297 #define ITK_TEMPLATE_Neighborhood(_, EXPORT, TypeX, TypeY)     \
00298   namespace itk                                                \
00299   {                                                            \
00300   _( 2 ( class EXPORT Neighborhood< ITK_TEMPLATE_2 TypeX > ) ) \
00301   namespace Templates                                          \
00302   {                                                            \
00303   typedef Neighborhood< ITK_TEMPLATE_2 TypeX >                 \
00304   Neighborhood##TypeY;                                       \
00305   }                                                            \
00306   }
00307 
00308 #if ITK_TEMPLATE_EXPLICIT
00309 #include "Templates/itkNeighborhood+-.h"
00310 #endif
00311 
00312 #if ITK_TEMPLATE_TXX
00313 #include "itkNeighborhood.hxx"
00314 #endif
00315 
00316 /*
00317 #ifndef ITK_MANUAL_INSTANTIATION
00318 #include "itkNeighborhood.hxx"
00319 #endif
00320 */
00321 
00322 #endif
00323