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

itkIndex.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkIndex.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-07-12 10:52:52 $
00007   Version:   $Revision: 1.64 $
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 __itkIndex_h
00018 #define __itkIndex_h
00019 
00020 #include "itkMacro.h"
00021 #include "itkOffset.h"
00022 #include "itkSize.h"
00023 #include "itkFixedArray.h"
00024 
00025 #include <memory>
00026 
00027 #include "itkExceptionObject.h"
00028 
00029 namespace itk
00030 {
00031 
00032 namespace Functor
00033 {
00034 template<unsigned int VIndexDimension> class IndexLexicographicCompare;
00035 }
00036 
00066 template<unsigned int VIndexDimension=2>
00067 class Index {
00068 public:
00070   typedef Index  Self;
00071 
00073   typedef   Index<VIndexDimension>  IndexType;
00074   typedef   long                    IndexValueType;
00075 
00077   static unsigned int GetIndexDimension() { return VIndexDimension; }
00078 
00080   typedef   Size<VIndexDimension>  SizeType;
00081 
00083   typedef   Offset<VIndexDimension>              OffsetType;
00084   typedef   typename OffsetType::OffsetValueType OffsetValueType;
00085 
00087   typedef Functor::IndexLexicographicCompare<VIndexDimension> LexicographicCompare;
00088 
00090   const Self
00091   operator+(const SizeType &size) const
00092     {
00093     Self result;
00094     for (unsigned int i=0; i < VIndexDimension; i++)
00095       { result[i] = m_Index[i] + static_cast<IndexValueType>(size[i]); }
00096     return result;
00097     }
00099 
00101   const Self &
00102   operator+=(const SizeType &size)
00103     {
00104     for (unsigned int i=0; i < VIndexDimension; i++)
00105       { m_Index[i] += static_cast<IndexValueType>(size[i]); }
00106     return *this;
00107     }
00109 
00111   const Self
00112   operator-(const SizeType &size) const
00113     {
00114     Self result;
00115     for (unsigned int i=0; i < VIndexDimension; i++)
00116       { result[i] = m_Index[i] - static_cast<IndexValueType>(size[i]); }
00117     return result;
00118     }
00120 
00122   const Self &
00123   operator-=(const SizeType &size)
00124     {
00125     for (unsigned int i=0; i < VIndexDimension; i++)
00126       { m_Index[i] -= static_cast<IndexValueType>(size[i]); }
00127     return *this;
00128     }
00130 
00132   const Self
00133   operator+(const OffsetType &offset) const
00134     {
00135     Self result;
00136     for (unsigned int i=0; i < VIndexDimension; i++)
00137       { result[i] = m_Index[i] + offset[i]; }
00138     return result;
00139     }
00141 
00143   const Self &
00144   operator+=(const OffsetType &offset)
00145     {
00146     for (unsigned int i=0; i < VIndexDimension; i++)
00147       { m_Index[i] += offset[i]; }
00148     return *this;
00149     }
00151 
00153   const Self &
00154   operator-=(const OffsetType &offset)
00155     {
00156     for (unsigned int i=0; i < VIndexDimension; i++)
00157       { m_Index[i] -= offset[i]; }
00158     return *this;
00159     }
00161 
00163   const Self
00164   operator-(const OffsetType &off) const
00165     {
00166     Self result;
00167     for (unsigned int i=0; i < VIndexDimension; i++)
00168       { result[i] = m_Index[i] - off.m_Offset[i]; }
00169     return result;
00170     }
00172 
00174   const OffsetType
00175   operator-(const Self &vec) const
00176     {
00177     OffsetType result;
00178     for (unsigned int i=0; i < VIndexDimension; i++)
00179       { result[i] = m_Index[i] - vec.m_Index[i]; }
00180     return result;
00181     }
00183 
00186   const Self
00187   operator*(const SizeType &vec) const
00188     {
00189     Self result;
00190     for (unsigned int i=0; i < VIndexDimension; i++)
00191       { result[i] = m_Index[i] * static_cast<IndexValueType>(vec.m_Size[i]); }
00192     return result;
00193     }
00195 
00197   bool
00198   operator==(const Self &vec) const
00199     {
00200     bool same=true;
00201     for (unsigned int i=0; i < VIndexDimension && same; i++)
00202       { same = (m_Index[i] == vec.m_Index[i]); }
00203     return same;
00204     }
00206 
00208   bool
00209   operator!=(const Self &vec) const
00210     {
00211     bool same=true;
00212     for (unsigned int i=0; i < VIndexDimension && same; i++)
00213       { same = (m_Index[i] == vec.m_Index[i]); }
00214     return !same;
00215     }
00217 
00220   IndexValueType & operator[](unsigned int dim)
00221     { return m_Index[dim]; }
00222 
00226   IndexValueType operator[](unsigned int dim) const
00227     { return m_Index[dim]; }
00228 
00231   const IndexValueType *GetIndex() const { return m_Index; };
00232 
00237   void SetIndex(const IndexValueType val[VIndexDimension])
00238     { memcpy(m_Index, val, sizeof(IndexValueType)*VIndexDimension); }
00239 
00246   void SetElement(unsigned long element, IndexValueType val )
00247     { m_Index[ element ] = val;  }
00248 
00255   IndexValueType GetElement( unsigned long element ) const
00256     { return m_Index[ element ]; }
00257 
00261   static Self GetBasisIndex(unsigned int dim); 
00262 
00265   void Fill(IndexValueType value)
00266     { for(unsigned int i=0;i < VIndexDimension; ++i) m_Index[i] = value; }
00267 
00273   IndexValueType m_Index[VIndexDimension];
00274 
00276   template <class TCoordRep>
00277   inline void CopyWithRound( const FixedArray<TCoordRep,VIndexDimension> & point )
00278     {
00279 #ifdef ITK_USE_TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING
00280     itkForLoopRoundingAndAssignmentMacro(IndexType,ContinuousIndexType,IndexValueType,m_Index,point,VIndexDimension);
00281 #else
00282     for(unsigned int i=0;i < VIndexDimension; ++i)
00283       {
00284       m_Index[i] = static_cast< IndexValueType>( itk::Math::Round( point[i] ) );
00285       }
00286 #endif
00287     }
00289 
00291   template <class TCoordRep>
00292   inline void CopyWithCast( const FixedArray<TCoordRep,VIndexDimension> & point )
00293     {
00294     for(unsigned int i=0;i < VIndexDimension; ++i)
00295       {
00296       m_Index[i] = static_cast< IndexValueType>( point[i] );
00297       }
00298     }
00300 
00301 // force gccxml to find the constructors found before the internal upgrade to gcc 4.2
00302 #if defined(CABLE_CONFIGURATION)
00303   Index(); //purposely not implemented
00304   Index(const Self&); //purposely not implemented
00305   void operator=(const Self&); //purposely not implemented
00306 #endif
00307 
00308 };
00309 
00310 namespace Functor
00311 {
00319 template<unsigned int VIndexDimension>
00320 class IndexLexicographicCompare
00321 {
00322 public:
00323   bool operator()(Index<VIndexDimension> const& l,
00324                   Index<VIndexDimension> const& r) const
00325     {
00326     for(unsigned int i=0; i < VIndexDimension; ++i)
00327       {
00328       if(l.m_Index[i] < r.m_Index[i])
00329         {
00330         return true;
00331         }
00332       else if(l.m_Index[i] > r.m_Index[i])
00333         {
00334         return false;
00335         }
00336       }
00337     return false;
00338     }
00339 };
00340 }
00341 
00342 template<unsigned int VIndexDimension>
00343 Index<VIndexDimension> 
00344 Index<VIndexDimension>
00345 ::GetBasisIndex(unsigned int dim)
00346 {
00347   Self ind;
00348   
00349   memset(ind.m_Index, 0, sizeof(IndexValueType)*VIndexDimension);
00350   ind.m_Index[dim] = 1;
00351   return ind;
00352 }
00353 
00354 template<unsigned int VIndexDimension>
00355 std::ostream & operator<<(std::ostream &os, const Index<VIndexDimension> &ind)
00356 {
00357   os << "[";
00358   for (unsigned int i=0; i+1 < VIndexDimension; ++i)
00359     {
00360     os << ind[i] << ", ";
00361     }
00362   if (VIndexDimension >= 1)
00363     {
00364     os << ind[VIndexDimension-1];
00365     }
00366   os << "]";
00367   return os;
00368 }
00369 
00370 } // end namespace itk
00371 
00372 #endif 
00373 

Generated at Tue Sep 15 03:33:49 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000