00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkOffset.h,v $ 00005 Language: C++ 00006 Date: $Date: 2009-02-06 20:53:15 $ 00007 Version: $Revision: 1.19 $ 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 #if defined(_MSC_VER) 00028 00029 // local variable may be used without having been initialized 00030 #pragma warning ( disable : 4701 ) 00031 #endif 00032 namespace itk 00033 { 00034 00035 namespace Functor 00036 { 00037 template<unsigned int VOffsetDimension> class OffsetLexicographicCompare; 00038 } 00039 00040 00058 template<unsigned int VOffsetDimension=2> 00059 class Offset { 00060 public: 00062 typedef Offset Self; 00063 00065 static unsigned int GetOffsetDimension() { return VOffsetDimension; } 00066 00068 typedef Offset<VOffsetDimension> OffsetType; 00069 typedef long OffsetValueType; 00070 00072 typedef Functor::OffsetLexicographicCompare<VOffsetDimension> LexicographicCompare; 00073 00075 const Self 00076 operator+(const Self &offset) const 00077 { 00078 Self result; 00079 for (unsigned int i=0; i < VOffsetDimension; i++) 00080 { result[i] = m_Offset[i] + offset[i]; } 00081 return result; 00082 } 00084 00086 const Self 00087 operator+(const Size<VOffsetDimension> &size) const 00088 { 00089 Self result; 00090 for (unsigned int i=0; i < VOffsetDimension; i++) 00091 { result[i] = m_Offset[i] + size[i]; } 00092 return result; 00093 } 00095 00097 const Self & 00098 operator+=(const Size<VOffsetDimension> &size) 00099 { 00100 for (unsigned int i=0; i < VOffsetDimension; i++) 00101 { m_Offset[i] += size[i]; } 00102 return *this; 00103 } 00105 00107 const Self & 00108 operator-=(const Size<VOffsetDimension> &size) 00109 { 00110 for (unsigned int i=0; i < VOffsetDimension; i++) 00111 { m_Offset[i] -= size[i]; } 00112 return *this; 00113 } 00115 00117 const Self 00118 operator-(const Self &vec) 00119 { 00120 Self result; 00121 for (unsigned int i=0; i < VOffsetDimension; i++) 00122 { result[i] = m_Offset[i] - vec.m_Offset[i]; } 00123 return result; 00124 } 00126 00128 const Self & 00129 operator+=(const Self &vec) 00130 { 00131 for (unsigned int i=0; i < VOffsetDimension; i++) 00132 { m_Offset[i] += vec.m_Offset[i]; } 00133 return *this; 00134 } 00136 00138 const Self & 00139 operator-=(const Self &vec) 00140 { 00141 for (unsigned int i=0; i < VOffsetDimension; i++) 00142 { m_Offset[i] -= vec.m_Offset[i]; } 00143 return *this; 00144 } 00146 00148 bool 00149 operator==(const Self &vec) const 00150 { 00151 bool same=1; 00152 for (unsigned int i=0; i < VOffsetDimension && same; i++) 00153 { same = (m_Offset[i] == vec.m_Offset[i]); } 00154 return same; 00155 } 00157 00159 bool 00160 operator!=(const Self &vec) const 00161 { 00162 bool same=1; 00163 for (unsigned int i=0; i < VOffsetDimension && same; i++) 00164 { same = (m_Offset[i] == vec.m_Offset[i]); } 00165 return !same; 00166 } 00168 00171 OffsetValueType & operator[](unsigned int dim) 00172 { return m_Offset[dim]; } 00173 00177 OffsetValueType operator[](unsigned int dim) const 00178 { return m_Offset[dim]; } 00179 00182 const OffsetValueType *GetOffset() const { return m_Offset; }; 00183 00188 void SetOffset(const OffsetValueType val[VOffsetDimension]) 00189 { memcpy(m_Offset, val, sizeof(OffsetValueType)*VOffsetDimension); } 00190 00194 static Self GetBasisOffset(unsigned int dim); 00195 00198 void Fill(OffsetValueType value) 00199 { for(unsigned int i=0;i < VOffsetDimension; ++i) m_Offset[i] = value; } 00200 00206 OffsetValueType m_Offset[VOffsetDimension]; 00207 00208 00209 // force gccxml to find the constructors found before the internal upgrade to gcc 4.2 00210 #if defined(CABLE_CONFIGURATION) 00211 Offset(); //purposely not implemented 00212 Offset(const Self&); //purposely not implemented 00213 void operator=(const Self&); //purposely not implemented 00214 #endif 00215 00216 }; 00217 00218 00219 namespace Functor 00220 { 00228 template<unsigned int VOffsetDimension> 00229 class OffsetLexicographicCompare 00230 { 00231 public: 00232 bool operator()(Offset<VOffsetDimension> const& l, 00233 Offset<VOffsetDimension> const& r) const 00234 { 00235 for(unsigned int i=0; i < VOffsetDimension; ++i) 00236 { 00237 if(l.m_Offset[i] < r.m_Offset[i]) 00238 { 00239 return true; 00240 } 00241 else if(l.m_Offset[i] > r.m_Offset[i]) 00242 { 00243 return false; 00244 } 00245 } 00246 return false; 00247 } 00248 }; 00249 } 00250 00251 template<unsigned int VOffsetDimension> 00252 Offset<VOffsetDimension> 00253 Offset<VOffsetDimension> 00254 ::GetBasisOffset(unsigned int dim) 00255 { 00256 Self ind; 00257 00258 memset(ind.m_Offset, 0, sizeof(OffsetValueType)*VOffsetDimension); 00259 ind.m_Offset[dim] = 1; 00260 return ind; 00261 } 00262 00263 template<unsigned int VOffsetDimension> 00264 std::ostream & operator<<(std::ostream &os, const Offset<VOffsetDimension> &ind) 00265 { 00266 os << "["; 00267 unsigned int dimlim = VOffsetDimension - 1; 00268 for (unsigned int i=0; i < dimlim; ++i) 00269 { 00270 os << ind[i] << ", "; 00271 } 00272 if (VOffsetDimension >= 1) 00273 { 00274 os << ind[VOffsetDimension-1]; 00275 } 00276 os << "]"; 00277 return os; 00278 } 00279 00280 } // end namespace itk 00281 00282 #endif 00283