ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSize.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 __itkSize_h
00019 #define __itkSize_h
00020 
00021 #include "itkMacro.h"
00022 #include "itkIntTypes.h"
00023 
00024 namespace itk
00025 {
00051 template< unsigned int VDimension = 2 >
00052 class Size
00053 {
00054 public:
00056   typedef Size Self;
00057 
00059   typedef   Size< VDimension > SizeType;
00060   typedef   itk::SizeValueType SizeValueType;
00061 
00063   itkStaticConstMacro(Dimension, unsigned int, VDimension);
00064 
00066   static unsigned int GetSizeDimension(void) { return VDimension; }
00067 
00069   const Self
00070   operator+(const Self & vec) const
00071   {
00072     Self result;
00073 
00074     for ( unsigned int i = 0; i < VDimension; i++ )
00075           { result[i] = m_Size[i] + vec.m_Size[i]; }
00076     return result;
00077   }
00078 
00080   const Self &
00081   operator+=(const Self & vec)
00082   {
00083     for ( unsigned int i = 0; i < VDimension; i++ )
00084           { m_Size[i] += vec.m_Size[i]; }
00085     return *this;
00086   }
00088 
00090   const Self
00091   operator-(const Self & vec) const
00092   {
00093     Self result;
00094 
00095     for ( unsigned int i = 0; i < VDimension; i++ )
00096           { result[i] = m_Size[i] - vec.m_Size[i]; }
00097     return result;
00098   }
00099 
00101   const Self &
00102   operator-=(const Self & vec)
00103   {
00104     for ( unsigned int i = 0; i < VDimension; i++ )
00105           { m_Size[i] -= vec.m_Size[i]; }
00106     return *this;
00107   }
00109 
00111   const Self
00112   operator*(const Self & vec) const
00113   {
00114     Self result;
00115 
00116     for ( unsigned int i = 0; i < VDimension; i++ )
00117           { result[i] = m_Size[i] * vec.m_Size[i]; }
00118     return result;
00119   }
00120 
00122   const Self &
00123   operator*=(const Self & vec)
00124   {
00125     for ( unsigned int i = 0; i < VDimension; i++ )
00126           { m_Size[i] *= vec.m_Size[i]; }
00127     return *this;
00128   }
00130 
00132   bool
00133   operator==(const Self & vec) const
00134   {
00135     bool same = 1;
00136 
00137     for ( unsigned int i = 0; i < VDimension && same; i++ )
00138           { same = ( m_Size[i] == vec.m_Size[i] ); }
00139     return same;
00140   }
00141 
00143   bool
00144   operator!=(const Self & vec) const
00145   {
00146     bool same = 1;
00147 
00148     for ( unsigned int i = 0; i < VDimension && same; i++ )
00149           { same = ( m_Size[i] == vec.m_Size[i] ); }
00150     return !same;
00151   }
00152 
00155   SizeValueType & operator[](unsigned int dim)
00156   { return m_Size[dim]; }
00157 
00161   SizeValueType operator[](unsigned int dim) const
00162   { return m_Size[dim]; }
00163 
00166   const SizeValueType * GetSize() const { return m_Size; }
00167 
00171   void SetSize(const SizeValueType val[VDimension])
00172   { memcpy(m_Size, val, sizeof( SizeValueType ) * VDimension); }
00173 
00180   void SetElement(unsigned long element, SizeValueType val)
00181   { m_Size[element] = val;  }
00182 
00189   SizeValueType GetElement(unsigned long element) const
00190   { return m_Size[element]; }
00191 
00194   void Fill(SizeValueType value)
00195   { for ( unsigned int i = 0; i < VDimension; ++i ) { m_Size[i] = value; } }
00196 
00207   SizeValueType m_Size[VDimension];
00209 
00210 // force gccxml to find the constructors found before the internal upgrade to
00211 // gcc 4.2
00212 #if defined( CABLE_CONFIGURATION )
00213   Size();                       //purposely not implemented
00214   Size(const Self &);           //purposely not implemented
00215   void operator=(const Self &); //purposely not implemented
00216 
00217 #endif
00218 };
00219 
00220 template< unsigned int VDimension >
00221 std::ostream & operator<<(std::ostream & os, const Size< VDimension > & size)
00222 {
00223   os << "[";
00224   for ( unsigned int i = 0; i + 1 < VDimension; ++i )
00225     {
00226     os << size[i] << ", ";
00227     }
00228   if ( VDimension >= 1 )
00229     {
00230     os << size[VDimension - 1];
00231     }
00232   os << "]";
00233   return os;
00234 }
00235 } // end namespace itk
00236 
00237 #endif
00238