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

itkSize.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkSize.h,v $
00005   Language:  C++
00006   Date:      $Date: 2006/04/13 17:57:26 $
00007   Version:   $Revision: 1.20 $
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 __itkSize_h
00018 #define __itkSize_h
00019 
00020 #include "itkMacro.h"
00021 #include "itkExceptionObject.h"
00022 
00023 namespace itk
00024 {
00025 
00046 template<unsigned int VDimension=2>
00047 class Size {
00048 public:
00050   typedef Size  Self;
00051 
00053   typedef   Size<VDimension>  SizeType;
00054   typedef   unsigned long     SizeValueType;
00055 
00057   static unsigned int GetSizeDimension(void) { return VDimension; }
00058 
00060   const Self
00061   operator+(const Self &vec)
00062     {
00063     Self result;
00064     for (unsigned int i=0; i < VDimension; i++)
00065       { result[i] = m_Size[i] + vec.m_Size[i]; }
00066     return result;
00067     }
00069 
00071   const Self &
00072   operator+=(const Self &vec)
00073     {
00074     for (unsigned int i=0; i < VDimension; i++)
00075       { m_Size[i] += vec.m_Size[i]; }
00076     return *this;
00077     }
00079 
00081   const Self
00082   operator-(const Self &vec)
00083     {
00084     Self result;
00085     for (unsigned int i=0; i < VDimension; i++)
00086       { result[i] = m_Size[i] - vec.m_Size[i]; }
00087     return result;
00088     }
00090 
00092   const Self &
00093   operator-=(const Self &vec)
00094     {
00095     for (unsigned int i=0; i < VDimension; i++)
00096       { m_Size[i] -= vec.m_Size[i]; }
00097     return *this;
00098     }
00100 
00102   const Self
00103   operator*(const Self &vec)
00104     {
00105     Self result;
00106     for (unsigned int i=0; i < VDimension; i++)
00107       { result[i] = m_Size[i] * vec.m_Size[i]; }
00108     return result;
00109     }
00111 
00113   const Self &
00114   operator*=(const Self &vec)
00115     {
00116     for (unsigned int i=0; i < VDimension; i++)
00117       { m_Size[i] *= vec.m_Size[i]; }
00118     return *this;
00119     }
00121 
00123   bool
00124   operator==(const Self &vec) const
00125     {
00126     bool same=1;
00127     for (unsigned int i=0; i < VDimension && same; i++)
00128       { same = (m_Size[i] == vec.m_Size[i]); }
00129     return same;
00130     }
00132 
00134   bool
00135   operator!=(const Self &vec) const
00136     {
00137     bool same=1;
00138     for (unsigned int i=0; i < VDimension && same; i++)
00139       { same = (m_Size[i] == vec.m_Size[i]); }
00140     return !same;
00141     }
00143 
00146   SizeValueType & operator[](unsigned int dim)
00147     { return m_Size[dim]; }
00148 
00152   SizeValueType operator[](unsigned int dim) const
00153     { return m_Size[dim]; }
00154 
00157   const SizeValueType *GetSize() const { return m_Size; };
00158 
00162   void SetSize(const SizeValueType val[VDimension])
00163     { memcpy(m_Size, val, sizeof(SizeValueType)*VDimension); }
00164 
00171   void SetElement(unsigned long element, SizeValueType val )
00172     { m_Size[ element ] = val;  }
00173 
00180   SizeValueType GetElement( unsigned long element ) const
00181     { return m_Size[ element ]; }
00182 
00185   void Fill(SizeValueType value)
00186     { for(unsigned int i=0;i < VDimension; ++i) m_Size[i] = value; }
00187 
00198   SizeValueType m_Size[VDimension];
00200 
00201 
00202   const SizeValueType __getitem__(unsigned int dim) const 
00203     {
00204     if (dim >= VDimension)
00205       {throw ExceptionObject(__FILE__, __LINE__, "itk::ERROR: Size: index out of range");}
00206     return GetElement(dim);
00207     }
00208   void __setitem__(unsigned int dim, SizeValueType value)
00209     {
00210     if (dim >= VDimension)
00211       {throw ExceptionObject(__FILE__, __LINE__, "itk::ERROR: Size: index out of range");}
00212     SetElement(dim, value);
00213     }
00214   
00215   const unsigned int __len__() const
00216     {
00217     return VDimension;
00218     }
00219   
00220   const char * __str__() const
00221     {
00222     OStringStream msg;
00223     msg << "<Size " << *this << ">";
00224     return msg.str().c_str();
00225     }  
00226 
00227 
00228 };
00229 
00230 
00231 template<unsigned int VDimension>
00232 std::ostream & operator<<(std::ostream &os, const Size<VDimension> &size)
00233 {
00234   os << "[";
00235   for (unsigned int i=0; i+1 < VDimension; ++i)
00236     {
00237     os << size[i] << ", ";
00238     }
00239   if (VDimension >= 1)
00240     {
00241     os << size[VDimension-1];
00242     }
00243   os << "]";
00244   return os;
00245 }
00246 
00247 } // end namespace itk
00248 
00249 #endif 
00250 

Generated at Fri Sep 8 04:08:11 2006 for ITK by doxygen 1.4.7 written by Dimitri van Heesch, © 1997-2000