00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkOffset.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008-03-27 18:05:58 $ 00007 Version: $Revision: 1.16 $ 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 __itkOffset_h 00018 #define __itkOffset_h 00019 00020 #include "itkMacro.h" 00021 #include "itkSize.h" 00022 00023 #include <memory> 00024 00025 #include "itkExceptionObject.h" 00026 00027 namespace itk 00028 { 00029 00047 template<unsigned int VOffsetDimension=2> 00048 class Offset { 00049 public: 00051 typedef Offset Self; 00052 00054 static unsigned int GetOffsetDimension() { return VOffsetDimension; } 00055 00057 typedef Offset<VOffsetDimension> OffsetType; 00058 typedef long OffsetValueType; 00059 00061 const Self 00062 operator+(const Self &offset) const 00063 { 00064 Self result; 00065 for (unsigned int i=0; i < VOffsetDimension; i++) 00066 { result[i] = m_Offset[i] + offset[i]; } 00067 return result; 00068 } 00070 00072 const Self 00073 operator+(const Size<VOffsetDimension> &size) const 00074 { 00075 Self result; 00076 for (unsigned int i=0; i < VOffsetDimension; i++) 00077 { result[i] = m_Offset[i] + size[i]; } 00078 return result; 00079 } 00081 00083 const Self & 00084 operator+=(const Size<VOffsetDimension> &size) 00085 { 00086 for (unsigned int i=0; i < VOffsetDimension; i++) 00087 { m_Offset[i] += size[i]; } 00088 return *this; 00089 } 00091 00093 const Self & 00094 operator-=(const Size<VOffsetDimension> &size) 00095 { 00096 for (unsigned int i=0; i < VOffsetDimension; i++) 00097 { m_Offset[i] -= size[i]; } 00098 return *this; 00099 } 00101 00103 const Self 00104 operator-(const Self &vec) 00105 { 00106 Self result; 00107 for (unsigned int i=0; i < VOffsetDimension; i++) 00108 { result[i] = m_Offset[i] - vec.m_Offset[i]; } 00109 return result; 00110 } 00112 00114 const Self & 00115 operator+=(const Self &vec) 00116 { 00117 for (unsigned int i=0; i < VOffsetDimension; i++) 00118 { m_Offset[i] += vec.m_Offset[i]; } 00119 return *this; 00120 } 00122 00124 const Self & 00125 operator-=(const Self &vec) 00126 { 00127 for (unsigned int i=0; i < VOffsetDimension; i++) 00128 { m_Offset[i] -= vec.m_Offset[i]; } 00129 return *this; 00130 } 00132 00134 bool 00135 operator==(const Self &vec) const 00136 { 00137 bool same=1; 00138 for (unsigned int i=0; i < VOffsetDimension && same; i++) 00139 { same = (m_Offset[i] == vec.m_Offset[i]); } 00140 return same; 00141 } 00143 00145 bool 00146 operator!=(const Self &vec) const 00147 { 00148 bool same=1; 00149 for (unsigned int i=0; i < VOffsetDimension && same; i++) 00150 { same = (m_Offset[i] == vec.m_Offset[i]); } 00151 return !same; 00152 } 00154 00157 OffsetValueType & operator[](unsigned int dim) 00158 { return m_Offset[dim]; } 00159 00163 OffsetValueType operator[](unsigned int dim) const 00164 { return m_Offset[dim]; } 00165 00168 const OffsetValueType *GetOffset() const { return m_Offset; }; 00169 00174 void SetOffset(const OffsetValueType val[VOffsetDimension]) 00175 { memcpy(m_Offset, val, sizeof(OffsetValueType)*VOffsetDimension); } 00176 00180 static Self GetBasisOffset(unsigned int dim); 00181 00184 void Fill(OffsetValueType value) 00185 { for(unsigned int i=0;i < VOffsetDimension; ++i) m_Offset[i] = value; } 00186 00192 OffsetValueType m_Offset[VOffsetDimension]; 00193 00194 00195 // force gccxml to find the constructors found before the internal upgrade to gcc 4.2 00196 #if defined(CABLE_CONFIGURATION) 00197 Offset(); //purposely not implemented 00198 Offset(const Self&); //purposely not implemented 00199 void operator=(const Self&); //purposely not implemented 00200 #endif 00201 00202 }; 00203 00204 00205 template<unsigned int VOffsetDimension> 00206 Offset<VOffsetDimension> 00207 Offset<VOffsetDimension> 00208 ::GetBasisOffset(unsigned int dim) 00209 { 00210 Self ind; 00211 00212 memset(ind.m_Offset, 0, sizeof(OffsetValueType)*VOffsetDimension); 00213 ind.m_Offset[dim] = 1; 00214 return ind; 00215 } 00216 00217 template<unsigned int VOffsetDimension> 00218 std::ostream & operator<<(std::ostream &os, const Offset<VOffsetDimension> &ind) 00219 { 00220 os << "["; 00221 unsigned int dimlim = VOffsetDimension - 1; 00222 for (unsigned int i=0; i < dimlim; ++i) 00223 { 00224 os << ind[i] << ", "; 00225 } 00226 if (VOffsetDimension >= 1) 00227 { 00228 os << ind[VOffsetDimension-1]; 00229 } 00230 os << "]"; 00231 return os; 00232 } 00233 00234 } // end namespace itk 00235 00236 #endif 00237