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