Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_vec4 5 #define tools_vec4 6 7 #include <cstddef> //size_t 8 9 #ifdef TOOLS_MEM 10 #include "../mem" 11 #endif 12 13 namespace tools { 14 15 template <class T> 16 class vec4 { 17 #ifdef TOOLS_MEM 18 public: 19 static const std::string& s_class() { 20 static const std::string s_v("tools::vec4" 21 return s_v; 22 } 23 #endif 24 protected: 25 static T zero() {return T();} 26 static T minus_one() {return T(-1);} 27 public: 28 typedef T elem_t; 29 unsigned int dimension() const {return 4;} 30 public: 31 vec4(){ 32 #ifdef TOOLS_MEM 33 mem::increment(s_class().c_str()); 34 #endif 35 m_data[0] = T(); 36 m_data[1] = T(); 37 m_data[2] = T(); 38 m_data[3] = T(); 39 } 40 vec4(const T a_vec[4]) { 41 #ifdef TOOLS_MEM 42 mem::increment(s_class().c_str()); 43 #endif 44 m_data[0] = a_vec[0]; 45 m_data[1] = a_vec[1]; 46 m_data[2] = a_vec[2]; 47 m_data[3] = a_vec[3]; 48 } 49 vec4(const T& a0,const T& a1,const T& a2,con 50 #ifdef TOOLS_MEM 51 ,bool a_inc = true 52 #endif 53 ) { 54 #ifdef TOOLS_MEM 55 if(a_inc) mem::increment(s_class().c_str() 56 #endif 57 m_data[0] = a0; 58 m_data[1] = a1; 59 m_data[2] = a2; 60 m_data[3] = a3; 61 } 62 virtual ~vec4() { 63 #ifdef TOOLS_MEM 64 mem::decrement(s_class().c_str()); 65 #endif 66 } 67 public: 68 vec4(const vec4& a_from){ 69 #ifdef TOOLS_MEM 70 mem::increment(s_class().c_str()); 71 #endif 72 m_data[0] = a_from.m_data[0]; 73 m_data[1] = a_from.m_data[1]; 74 m_data[2] = a_from.m_data[2]; 75 m_data[3] = a_from.m_data[3]; 76 } 77 vec4& operator=(const vec4& a_from) { 78 m_data[0] = a_from.m_data[0]; 79 m_data[1] = a_from.m_data[1]; 80 m_data[2] = a_from.m_data[2]; 81 m_data[3] = a_from.m_data[3]; 82 return *this; 83 } 84 public: 85 const T& v0() const { return m_data[0];} 86 const T& v1() const { return m_data[1];} 87 const T& v2() const { return m_data[2];} 88 const T& v3() const { return m_data[3];} 89 90 void v0(const T& a_value) { m_data[0] = a_va 91 void v1(const T& a_value) { m_data[1] = a_va 92 void v2(const T& a_value) { m_data[2] = a_va 93 void v3(const T& a_value) { m_data[3] = a_va 94 95 const T& x() const { return m_data[0];} 96 const T& y() const { return m_data[1];} 97 const T& z() const { return m_data[2];} 98 const T& t() const { return m_data[3];} 99 100 void set_value(const T& a0,const T& a1,const 101 m_data[0] = a0; 102 m_data[1] = a1; 103 m_data[2] = a2; 104 m_data[3] = a3; 105 } 106 void set_value(const T aV[4]) { 107 m_data[0] = aV[0]; 108 m_data[1] = aV[1]; 109 m_data[2] = aV[2]; 110 m_data[3] = aV[3]; 111 } 112 void value(T& a0,T& a1,T& a2,T& a3) const { 113 a0 = m_data[0]; 114 a1 = m_data[1]; 115 a2 = m_data[2]; 116 a3 = m_data[3]; 117 } 118 119 bool set_value(unsigned int a_index,const T& 120 if(a_index>=4) return false; 121 m_data[a_index] = a_value; 122 return true; 123 } 124 125 T length(T(*a_sqrt)(T)) const { 126 return a_sqrt(m_data[0]*m_data[0]+m_data[1 127 } 128 129 T normalize(T(*a_sqrt)(T)) { 130 T norme = length(a_sqrt); 131 if(norme==T()) return T(); 132 divide(norme); 133 return norme; 134 } 135 136 bool equal(const vec4& a_vec) const { 137 if(m_data[0]!=a_vec.m_data[0]) return fals 138 if(m_data[1]!=a_vec.m_data[1]) return fals 139 if(m_data[2]!=a_vec.m_data[2]) return fals 140 if(m_data[3]!=a_vec.m_data[3]) return fals 141 return true; 142 } 143 bool equal(const vec4& a_vec,const T& a_epsi 144 T* tp = (T*)m_data; 145 T* ap = (T*)a_vec.m_data; 146 for(unsigned int i=0;i<4;i++,tp++,ap++) { 147 T diff = (*tp) - (*ap); 148 if(diff<zero()) diff *= minus_one(); 149 if(diff>=a_epsil) return false; 150 } 151 return true; 152 } 153 154 bool is_proportional(const vec4& a_vec,T& a_ 155 // If true, then : a_vec = a_factor * this 156 a_factor = zero(); 157 bool first = true; 158 T* tp = (T*)m_data; 159 T* ap = (T*)a_vec.m_data; 160 for(unsigned int i=0;i<4;i++,tp++,ap++) { 161 if( ((*tp)==zero()) && ((*ap)==ze 162 continue; 163 } else if( ((*tp)!=zero()) && ((*ap)==ze 164 return false; 165 } else if( ((*tp)==zero()) && ((*ap)!=ze 166 return false; 167 } else { 168 if(first) { 169 a_factor = (*ap)/(*tp); 170 first = false; 171 } else { 172 if((*ap)!=(*tp)*a_factor) return fal 173 } 174 } 175 } 176 return true; 177 } 178 179 void multiply(const T& a_T) { 180 m_data[0] *= a_T; 181 m_data[1] *= a_T; 182 m_data[2] *= a_T; 183 m_data[3] *= a_T; 184 } 185 186 bool divide(const T& a_T) { 187 if(a_T==T()) return false; 188 m_data[0] /= a_T; 189 m_data[1] /= a_T; 190 m_data[2] /= a_T; 191 m_data[3] /= a_T; 192 return true; 193 } 194 195 void add(const vec4& a_v) { 196 m_data[0] += a_v.m_data[0]; 197 m_data[1] += a_v.m_data[1]; 198 m_data[2] += a_v.m_data[2]; 199 m_data[3] += a_v.m_data[3]; 200 } 201 202 void add(const T& a0,const T& a1,const T& a2 203 m_data[0] += a0; 204 m_data[1] += a1; 205 m_data[2] += a2; 206 m_data[3] += a3; 207 } 208 209 void subtract(const vec4& a_v) { 210 m_data[0] -= a_v.m_data[0]; 211 m_data[1] -= a_v.m_data[1]; 212 m_data[2] -= a_v.m_data[2]; 213 m_data[3] -= a_v.m_data[3]; 214 } 215 216 void subtract(const T& a0,const T& a1,const 217 m_data[0] -= a0; 218 m_data[1] -= a1; 219 m_data[2] -= a2; 220 m_data[3] -= a3; 221 } 222 223 public: //operators 224 T& operator[](size_t a_index) { 225 //WARNING : no check on a_index. 226 return m_data[a_index]; 227 } 228 const T& operator[](size_t a_index) const { 229 //WARNING : no check on a_index. 230 return m_data[a_index]; 231 } 232 233 vec4 operator+(const vec4& a_v) const { 234 return vec4(m_data[0]+a_v.m_data[0], 235 m_data[1]+a_v.m_data[1], 236 m_data[2]+a_v.m_data[2], 237 m_data[3]+a_v.m_data[3]); 238 } 239 240 vec4 operator-(const vec4& a_v) const { 241 return vec4(m_data[0]-a_v.m_data[0], 242 m_data[1]-a_v.m_data[1], 243 m_data[2]-a_v.m_data[2], 244 m_data[3]-a_v.m_data[3]); 245 } 246 247 vec4 operator*(const T& a_v) const { 248 return vec4(m_data[0]*a_v, 249 m_data[1]*a_v, 250 m_data[2]*a_v, 251 m_data[3]*a_v); 252 } 253 254 bool operator==(const vec4& a_v) const {retu 255 bool operator!=(const vec4& a_v) const {retu 256 257 public: //for tools/sg/sf_vec 258 typedef unsigned int size_type; 259 size_type size() const {return 4;} 260 const T* data() const {return m_data;} 261 protected: 262 T m_data[4]; 263 private:static void check_instantiation() {vec 264 }; 265 266 //for sf, mf : 267 template <class T> 268 inline const T* get_data(const vec4<T>& a_v) { 269 270 } 271 272 #include <ostream> 273 274 namespace tools { 275 276 // for sf_vec::dump(). 277 template <class T> 278 inline std::ostream& operator<<(std::ostream& 279 a_out << "x = " << a_this.v0() 280 << ",y = " << a_this.v1() 281 << ",z = " << a_this.v2() 282 << ",t = " << a_this.v3(); 283 return a_out; 284 } 285 286 } 287 288 #endif