Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkNeighborhood.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkNeighborhood.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-05-26 00:29:28 $
00007   Version:   $Revision: 1.44 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkNeighborhood_h
00018 #define __itkNeighborhood_h
00019 
00020 #include <iostream>
00021 #include "itkNeighborhoodAllocator.h"
00022 #include "itkSize.h"
00023 #include "itkIndent.h"
00024 #include "itkSliceIterator.h"
00025 #include "vnl/vnl_vector.h"
00026 #include "itkOffset.h"
00027 #include <vector>
00028 
00029 namespace itk {
00030   
00052 template<class TPixel, unsigned int VDimension = 2,
00053          class TAllocator = NeighborhoodAllocator<TPixel> >
00054 class ITK_EXPORT Neighborhood 
00055 {
00056 public:
00058   typedef Neighborhood Self;
00059 
00061   typedef TAllocator AllocatorType;
00062 
00064   itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
00065 
00067   typedef TPixel PixelType;
00068 
00072   typedef typename AllocatorType::iterator Iterator;
00073   typedef typename AllocatorType::const_iterator ConstIterator;
00074 
00076   typedef ::itk::Size<VDimension> SizeType;
00077   typedef typename SizeType::SizeValueType SizeValueType;
00078 
00080   typedef ::itk::Size<VDimension> RadiusType;
00081 
00083   typedef Offset<VDimension> OffsetType;
00084 
00086   typedef SliceIterator<TPixel, Self> SliceIteratorType;
00087 
00089   Neighborhood() { m_Radius.Fill(0); m_Size.Fill(0);  }
00090 
00092   virtual ~Neighborhood() {}
00093 
00095   Neighborhood(const Self& other);
00096 
00098   Self &operator=(const Self& other);
00099 
00101   bool
00102   operator==(const Self& other) const
00103   {
00104     return (m_Radius == other.m_Radius &&
00105             m_Size   == other.m_Size &&
00106             m_DataBuffer == other.m_DataBuffer);
00107   }
00108 
00110   bool operator!=(const Self& other) const
00111   {
00112     return (m_Radius != other.m_Radius ||
00113             m_Size   != other.m_Size ||
00114             m_DataBuffer != other.m_DataBuffer);
00115   }
00116 
00118   const SizeType GetRadius() const
00119     { return m_Radius; }
00120 
00123   unsigned long GetRadius(const unsigned long n) const
00124     { return m_Radius[n]; }
00125 
00128   unsigned long GetSize(const unsigned long n) const
00129     { return m_Size[n]; }
00130 
00132   SizeType GetSize() const
00133     { return m_Size; }
00134 
00138   unsigned GetStride(const unsigned axis) const
00139   {     return m_StrideTable[axis];  }
00140 
00142   Iterator End()
00143     { return m_DataBuffer.end(); }
00144   Iterator Begin()
00145     { return m_DataBuffer.begin(); }
00146   ConstIterator End() const
00147     { return m_DataBuffer.end(); }
00148   ConstIterator Begin() const
00149     { return m_DataBuffer.begin(); }
00151 
00153   unsigned int Size() const
00154     { return m_DataBuffer.size(); }
00155 
00157   TPixel &operator[](unsigned int i)
00158     { return m_DataBuffer[i]; }
00159   const TPixel &operator[](unsigned int i) const
00160   { return m_DataBuffer[i]; }
00161   TPixel &GetElement(unsigned int i) 
00162   { return m_DataBuffer[i]; }
00164 
00166   TPixel GetCenterValue() const
00167   {    return (this->operator[]((this->Size())>>1)); }
00168 
00171   void SetRadius(const SizeType &);
00172 
00175   void SetRadius(const unsigned long *rad)
00176   {
00177     SizeType s;
00178     memcpy(s.m_Size, rad, sizeof(unsigned long) * VDimension);
00179     this->SetRadius(s);
00180   }
00182 
00186   void SetRadius(const unsigned long);
00187 
00189   void Print(std::ostream& os) const
00190     { this->PrintSelf(os, Indent(0));  }
00191 
00193   AllocatorType &GetBufferReference()
00194     { return m_DataBuffer; }
00195   const AllocatorType &GetBufferReference() const
00196     { return m_DataBuffer; }
00198 
00200   TPixel &operator[](const OffsetType &o)
00201   {    return this->operator[](this->GetNeighborhoodIndex(o)); }
00202   const TPixel &operator[](const OffsetType &o) const
00203   { return this->operator[](this->GetNeighborhoodIndex(o)); }
00205 
00208   OffsetType GetOffset(unsigned int i) const
00209   {    return m_OffsetTable[i];  }
00210 
00211   virtual unsigned int GetNeighborhoodIndex(const OffsetType &) const;
00212 
00213   unsigned int GetCenterNeighborhoodIndex() const
00214   { return  static_cast<unsigned int>(this->Size()/2); }
00215     
00216   std::slice GetSlice(unsigned int) const;
00217   
00218 protected:
00220   void SetSize()
00221   {
00222     for (unsigned int i=0; i<VDimension; ++i)
00223       { m_Size[i] = m_Radius[i]*2+1; }
00224   }
00226 
00228   virtual void Allocate(unsigned int i)
00229     { m_DataBuffer.set_size(i); }
00230 
00232   virtual void PrintSelf(std::ostream&, Indent) const;
00233 
00235   virtual void ComputeNeighborhoodStrideTable();
00236 
00239   virtual void ComputeNeighborhoodOffsetTable();
00240 
00241 private:
00244   SizeType m_Radius;
00245 
00248   SizeType m_Size;
00249 
00251   AllocatorType m_DataBuffer;
00252 
00255   unsigned int m_StrideTable[VDimension];
00256 
00258   std::vector<OffsetType> m_OffsetTable;
00259 
00260 };
00261 
00262 template <class TPixel, unsigned int VDimension, class TContainer>
00263 std::ostream & operator<<(std::ostream &os, const Neighborhood<TPixel,VDimension,TContainer> &neighborhood)
00264 {
00265   os << "Neighborhood:" << std::endl;
00266   os << "    Radius:" << neighborhood.GetRadius() << std::endl;
00267   os << "    Size:" << neighborhood.GetSize() << std::endl;
00268   os << "    DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
00269 
00270   return os;
00271 }
00272 
00273 } // namespace itk
00274 
00275 // Define instantiation macro for this template.
00276 #define ITK_TEMPLATE_Neighborhood(_, EXPORT, x, y) namespace itk { \
00277   _(2(class EXPORT Neighborhood< ITK_TEMPLATE_2 x >)) \
00278   namespace Templates { typedef Neighborhood< ITK_TEMPLATE_2 x > \
00279                                                   Neighborhood##y; } \
00280   }
00281 
00282 #if ITK_TEMPLATE_EXPLICIT
00283 # include "Templates/itkNeighborhood+-.h"
00284 #endif
00285 
00286 #if ITK_TEMPLATE_TXX
00287 # include "itkNeighborhood.txx"
00288 #endif
00289 
00290 /*
00291 #ifndef ITK_MANUAL_INSTANTIATION
00292 #include "itkNeighborhood.txx"
00293 #endif
00294 */
00295 
00296 #endif
00297 

Generated at Wed Nov 5 23:08:48 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000