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: 2009-04-05 10:56:46 $
00007   Version:   $Revision: 1.48 $
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 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 namespace itk {
00032 
00054 template<class TPixel, unsigned int VDimension = 2,
00055          class TAllocator = NeighborhoodAllocator<TPixel> >
00056 class ITK_EXPORT Neighborhood
00057 {
00058 public:
00060   typedef Neighborhood Self;
00061 
00063   typedef TAllocator AllocatorType;
00064 
00066   itkStaticConstMacro(NeighborhoodDimension, unsigned int, VDimension);
00067 
00069   typedef TPixel PixelType;
00070 
00074   typedef typename AllocatorType::iterator       Iterator;
00075   typedef typename AllocatorType::const_iterator ConstIterator;
00076 
00078   typedef ::itk::Size<VDimension>          SizeType;
00079   typedef typename SizeType::SizeValueType SizeValueType;
00080 
00082   typedef ::itk::Size<VDimension> RadiusType;
00083 
00085   typedef Offset<VDimension> OffsetType;
00086 
00088   typedef SliceIterator<TPixel, Self> SliceIteratorType;
00089 
00091   Neighborhood()
00092     {
00093     m_Radius.Fill(0);
00094     m_Size.Fill(0);
00095     for (unsigned int i = 0; i < VDimension; i++)
00096       {
00097       m_StrideTable[i] = 0;
00098       }
00099     }
00101 
00103   virtual ~Neighborhood() {}
00104 
00106   Neighborhood(const Self& other);
00107 
00109   Self &operator=(const Self& other);
00110 
00112   bool
00113   operator==(const Self& other) const
00114     {
00115     return (m_Radius == other.m_Radius &&
00116             m_Size   == other.m_Size &&
00117             m_DataBuffer == other.m_DataBuffer);
00118     }
00119 
00121   bool 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   const SizeType GetRadius() const
00130     { return m_Radius; }
00131 
00134   unsigned long GetRadius(const unsigned long n) const
00135     { return m_Radius[n]; }
00136 
00139   unsigned long GetSize(const unsigned long n) const
00140     { return m_Size[n]; }
00141 
00143   SizeType GetSize() const
00144     { return m_Size; }
00145 
00149   unsigned GetStride(const unsigned axis) const
00150   {     return m_StrideTable[axis];  }
00151 
00153   Iterator End()
00154     { return m_DataBuffer.end(); }
00155   Iterator Begin()
00156     { return m_DataBuffer.begin(); }
00157   ConstIterator End() const
00158     { return m_DataBuffer.end(); }
00159   ConstIterator Begin() const
00160     { return m_DataBuffer.begin(); }
00162 
00164   unsigned int Size() const
00165     { return m_DataBuffer.size(); }
00166 
00168   TPixel &operator[](unsigned int i)
00169     { return m_DataBuffer[i]; }
00170   const TPixel &operator[](unsigned int i) const
00171     { return m_DataBuffer[i]; }
00172   TPixel &GetElement(unsigned int i)
00173     { return m_DataBuffer[i]; }
00175 
00177   TPixel GetCenterValue() const
00178     { return (this->operator[]((this->Size())>>1)); }
00179 
00182   void SetRadius(const SizeType &);
00183 
00186   void SetRadius(const unsigned long *rad)
00187     {
00188     SizeType s;
00189     memcpy(s.m_Size, rad, sizeof(unsigned long) * VDimension);
00190     this->SetRadius(s);
00191     }
00193 
00197   void SetRadius(const unsigned long);
00198 
00200   void Print(std::ostream& os) const
00201     { this->PrintSelf(os, Indent(0));  }
00202 
00204   AllocatorType &GetBufferReference()
00205     { return m_DataBuffer; }
00206   const AllocatorType &GetBufferReference() const
00207     { return m_DataBuffer; }
00209 
00211   TPixel &operator[](const OffsetType &o)
00212     { return this->operator[](this->GetNeighborhoodIndex(o)); }
00213   const TPixel &operator[](const OffsetType &o) const
00214     { return this->operator[](this->GetNeighborhoodIndex(o)); }
00216 
00219   OffsetType GetOffset(unsigned int i) const
00220     { return m_OffsetTable[i]; }
00221 
00222   virtual unsigned int GetNeighborhoodIndex(const OffsetType &) const;
00223 
00224   unsigned int GetCenterNeighborhoodIndex() const
00225     {
00226     return  static_cast<unsigned int>(this->Size()/2);
00227     }
00228 
00229   std::slice GetSlice(unsigned int) const;
00230 
00231 protected:
00233   void SetSize()
00234     {
00235     for (unsigned int i=0; i<VDimension; ++i)
00236       {
00237       m_Size[i] = m_Radius[i]*2+1;
00238       }
00239     }
00241 
00243   virtual void Allocate(unsigned int i)
00244     { m_DataBuffer.set_size(i); }
00245 
00247   virtual void PrintSelf(std::ostream&, Indent) const;
00248 
00250   virtual void ComputeNeighborhoodStrideTable();
00251 
00254   virtual void ComputeNeighborhoodOffsetTable();
00255 
00256 private:
00259   SizeType m_Radius;
00260 
00263   SizeType m_Size;
00264 
00266   AllocatorType m_DataBuffer;
00267 
00270   unsigned int m_StrideTable[VDimension];
00271 
00273   std::vector<OffsetType> m_OffsetTable;
00274 
00275 };
00276 
00277 template <class TPixel, unsigned int VDimension, class TContainer>
00278 std::ostream & operator<<(std::ostream &os, const Neighborhood<TPixel,VDimension,TContainer> &neighborhood)
00279 {
00280   os << "Neighborhood:" << std::endl;
00281   os << "    Radius:" << neighborhood.GetRadius() << std::endl;
00282   os << "    Size:" << neighborhood.GetSize() << std::endl;
00283   os << "    DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
00284 
00285   return os;
00286 }
00287 
00288 } // namespace itk
00289 
00290 // Define instantiation macro for this template.
00291 #define ITK_TEMPLATE_Neighborhood(_, EXPORT, x, y) namespace itk { \
00292   _(2(class EXPORT Neighborhood< ITK_TEMPLATE_2 x >)) \
00293   namespace Templates { typedef Neighborhood< ITK_TEMPLATE_2 x > \
00294                                                   Neighborhood##y; } \
00295   }
00296 
00297 #if ITK_TEMPLATE_EXPLICIT
00298 # include "Templates/itkNeighborhood+-.h"
00299 #endif
00300 
00301 #if ITK_TEMPLATE_TXX
00302 # include "itkNeighborhood.txx"
00303 #endif
00304 
00305 /*
00306 #ifndef ITK_MANUAL_INSTANTIATION
00307 #include "itkNeighborhood.txx"
00308 #endif
00309 */
00310 
00311 #endif
00312 

Generated at Thu May 28 10:57:24 2009 for ITK by doxygen 1.5.5 written by Dimitri van Heesch, © 1997-2000