ITK  4.2.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 #include <cstring>
00024 
00025 namespace itk
00026 {
00052 template< unsigned int VDimension = 2 >
00053 class Size
00054 {
00055 public:
00057   typedef Size Self;
00058 
00060   typedef   Size< VDimension > SizeType;
00061   typedef   itk::SizeValueType SizeValueType;
00062 
00064   itkStaticConstMacro(Dimension, unsigned int, VDimension);
00065 
00067   static unsigned int GetSizeDimension(void) { return VDimension; }
00068 
00070   const Self
00071   operator+(const Self & vec) const
00072   {
00073     Self result;
00074 
00075     for ( unsigned int i = 0; i < VDimension; i++ )
00076           { result[i] = m_Size[i] + vec.m_Size[i]; }
00077     return result;
00078   }
00079 
00081   const Self &
00082   operator+=(const Self & vec)
00083   {
00084     for ( unsigned int i = 0; i < VDimension; i++ )
00085           { m_Size[i] += vec.m_Size[i]; }
00086     return *this;
00087   }
00089 
00091   const Self
00092   operator-(const Self & vec) const
00093   {
00094     Self result;
00095 
00096     for ( unsigned int i = 0; i < VDimension; i++ )
00097           { result[i] = m_Size[i] - vec.m_Size[i]; }
00098     return result;
00099   }
00100 
00102   const Self &
00103   operator-=(const Self & vec)
00104   {
00105     for ( unsigned int i = 0; i < VDimension; i++ )
00106           { m_Size[i] -= vec.m_Size[i]; }
00107     return *this;
00108   }
00110 
00112   const Self
00113   operator*(const Self & vec) const
00114   {
00115     Self result;
00116 
00117     for ( unsigned int i = 0; i < VDimension; i++ )
00118           { result[i] = m_Size[i] * vec.m_Size[i]; }
00119     return result;
00120   }
00121 
00123   const Self &
00124   operator*=(const Self & vec)
00125   {
00126     for ( unsigned int i = 0; i < VDimension; i++ )
00127           { m_Size[i] *= vec.m_Size[i]; }
00128     return *this;
00129   }
00131 
00133   bool
00134   operator==(const Self & vec) const
00135   {
00136     bool same = 1;
00137 
00138     for ( unsigned int i = 0; i < VDimension && same; i++ )
00139           { same = ( m_Size[i] == vec.m_Size[i] ); }
00140     return same;
00141   }
00142 
00144   bool
00145   operator!=(const Self & vec) const
00146   {
00147     bool same = 1;
00148 
00149     for ( unsigned int i = 0; i < VDimension && same; i++ )
00150           { same = ( m_Size[i] == vec.m_Size[i] ); }
00151     return !same;
00152   }
00153 
00156   SizeValueType & operator[](unsigned int dim)
00157   { return m_Size[dim]; }
00158 
00162   SizeValueType operator[](unsigned int dim) const
00163   { return m_Size[dim]; }
00164 
00167   const SizeValueType * GetSize() const { return m_Size; }
00168 
00172   void SetSize(const SizeValueType val[VDimension])
00173   { memcpy(m_Size, val, sizeof( SizeValueType ) * VDimension); }
00174 
00181   void SetElement(unsigned long element, SizeValueType val)
00182   { m_Size[element] = val;  }
00183 
00190   SizeValueType GetElement(unsigned long element) const
00191   { return m_Size[element]; }
00192 
00195   void Fill(SizeValueType value)
00196   { for ( unsigned int i = 0; i < VDimension; ++i ) { m_Size[i] = value; } }
00197 
00208   SizeValueType m_Size[VDimension];
00210 
00211 // force gccxml to find the constructors found before the internal upgrade to
00212 // gcc 4.2
00213 #if defined( CABLE_CONFIGURATION )
00214   Size();                       //purposely not implemented
00215   Size(const Self &);           //purposely not implemented
00216   void operator=(const Self &); //purposely not implemented
00217 
00218 #endif
00219 };
00220 
00221 template< unsigned int VDimension >
00222 std::ostream & operator<<(std::ostream & os, const Size< VDimension > & size)
00223 {
00224   os << "[";
00225   for ( unsigned int i = 0; i + 1 < VDimension; ++i )
00226     {
00227     os << size[i] << ", ";
00228     }
00229   if ( VDimension >= 1 )
00230     {
00231     os << size[VDimension - 1];
00232     }
00233   os << "]";
00234   return os;
00235 }
00236 } // end namespace itk
00237 
00238 #endif
00239