Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_vec3f 5 #define tools_vec3f 6 7 #include "vec3" 8 #include "../S_STRING" 9 #include <cmath> //sqrt 10 11 namespace tools { 12 13 class vec3f : public vec3<float> { 14 typedef vec3<float> parent; 15 public: 16 TOOLS_SCLASS(tools::vec3f) //for stype() 17 public: 18 vec3f():parent(){} 19 vec3f(const float a_vec[3]):parent(a_vec){} 20 vec3f(float a0,float a1,float a2):parent(a0,a1,a2){} 21 virtual ~vec3f() {} 22 public: 23 vec3f(const vec3f& a_from):parent(a_from){} 24 vec3f& operator=(const vec3f& a_from){ 25 parent::operator=(a_from); 26 return *this; 27 } 28 29 vec3f(const parent& a_from):parent(a_from){} 30 31 public: //operators 32 vec3f operator*(float a_v) const { 33 return vec3f(m_data[0]*a_v, 34 m_data[1]*a_v, 35 m_data[2]*a_v); 36 } 37 vec3f operator+(const vec3f& a_v) const { 38 return vec3f(m_data[0]+a_v.m_data[0], 39 m_data[1]+a_v.m_data[1], 40 m_data[2]+a_v.m_data[2]); 41 } 42 vec3f operator-(const vec3f& a_v) const { 43 return vec3f(m_data[0]-a_v.m_data[0], 44 m_data[1]-a_v.m_data[1], 45 m_data[2]-a_v.m_data[2]); 46 } 47 vec3f& operator+=(const vec3f& a_v) { 48 m_data[0] += a_v.m_data[0]; 49 m_data[1] += a_v.m_data[1]; 50 m_data[2] += a_v.m_data[2]; 51 return *this; 52 } 53 vec3f& operator-=(const vec3f& a_v) { 54 m_data[0] -= a_v.m_data[0]; 55 m_data[1] -= a_v.m_data[1]; 56 m_data[2] -= a_v.m_data[2]; 57 return *this; 58 } 59 vec3f& operator*=(float a_v) { 60 m_data[0] *= a_v; 61 m_data[1] *= a_v; 62 m_data[2] *= a_v; 63 return *this; 64 } 65 vec3f operator-() const { 66 return vec3f(-m_data[0],-m_data[1],-m_data[2]); 67 } 68 public: 69 #define TOOLS_VEC3F_MORE_PREC 70 #ifdef TOOLS_VEC3F_MORE_PREC 71 float length() const { 72 return float(::sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2])); 73 } 74 float normalize() { 75 float norme = length(); 76 if(!norme) return 0; 77 divide(norme); 78 return norme; 79 } 80 bool cos_angle(const vec3f& a_v,float& a_cos) const { 81 //WARNING : if ret false, a_cos is not set. 82 float this_length = length(); 83 if(this_length==0.0f) return false; 84 float a_v_length = a_v.length(); 85 if(a_v_length==0.0f) return false; 86 a_cos = dot(a_v)/(this_length*a_v_length); 87 return true; 88 } 89 #else 90 float length() const {return parent::length(::sqrtf);} 91 float normalize() {return parent::normalize(::sqrtf);} 92 bool cos_angle(const vec3f& a_v,float& a_cos) const {return parent::cos_angle(a_v,a_cos,::sqrtf);} 93 #endif 94 95 bool theta_phi(float& a_theta,float& a_phi) const { 96 return parent::theta_phi(a_theta,a_phi,::sqrtf,::atan2f); 97 } 98 public: //iv2sg 99 bool equals(const vec3f& a_v,const float a_epsil) const { 100 //if(a_epsil<0.0f)) 101 float d0 = m_data[0]-a_v.m_data[0]; 102 float d1 = m_data[1]-a_v.m_data[1]; 103 float d2 = m_data[2]-a_v.m_data[2]; 104 return ((d0*d0+d1*d1+d2*d2)<=a_epsil); 105 } 106 void negate() { 107 m_data[0] = -m_data[0]; 108 m_data[1] = -m_data[1]; 109 m_data[2] = -m_data[2]; 110 } 111 112 private:static void check_instantiation() {vec3f v(0,0,0);v.set_value(1,1,1);} 113 }; 114 115 inline vec3f operator*(float a_f,const vec3f& a_v) { 116 vec3f res(a_v); 117 res *= a_f; 118 return res; 119 } 120 121 #define TOOLS_VEC3F_MORE_PREC 122 #ifdef TOOLS_VEC3F_MORE_PREC 123 inline void get_normal(const vec3f& a_p0,const vec3f& a_p1,const vec3f& a_p2,vec3f& a_nm, 124 vec3f& a_tmp_1,vec3f& a_tmp_2) { 125 // Used to optimize sg::bin(). 126 //(a_p1-a_p0).cross(a_p2-a_p1,a_nm); 127 128 a_tmp_1 = a_p1; 129 a_tmp_1.subtract(a_p0); 130 131 a_tmp_2 = a_p2; 132 a_tmp_2.subtract(a_p1); 133 134 a_tmp_1.cross(a_tmp_2,a_nm); 135 136 a_nm.normalize(); 137 } 138 #else 139 inline void get_normal(const vec3f& a_p0,const vec3f& a_p1,const vec3f& a_p2,vec3f& a_nm, 140 vec3f& a_tmp_1,vec3f& a_tmp_2) { 141 get_normal<float>(a_p0,a_p1,a_p2,a_nm,a_tmp_1,a_tmp_2,::sqrtf); 142 } 143 #endif 144 145 } 146 147 #include <vector> 148 149 namespace tools { 150 151 #ifndef SWIG 152 //for sf, mf : 153 inline bool set_from_vec(vec3f& a_v,const std::vector<float>& a_sv) { 154 if(a_sv.size()!=3) return false; 155 a_v[0] = a_sv[0]; 156 a_v[1] = a_sv[1]; 157 a_v[2] = a_sv[2]; 158 return true; 159 } 160 #endif 161 162 } 163 164 #endif