Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/lina/vec3f is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_vec3f
0005 #define tools_vec3f
0006 
0007 #include "vec3"
0008 #include "../S_STRING"
0009 #include <cmath> //sqrt
0010 
0011 namespace tools {
0012 
0013 class vec3f : public vec3<float> {
0014   typedef vec3<float> parent;
0015 public:
0016   TOOLS_SCLASS(tools::vec3f) //for stype()
0017 public:
0018   vec3f():parent(){}
0019   vec3f(const float a_vec[3]):parent(a_vec){}
0020   vec3f(float a0,float a1,float a2):parent(a0,a1,a2){}
0021   virtual ~vec3f() {}
0022 public:
0023   vec3f(const vec3f& a_from):parent(a_from){}
0024   vec3f& operator=(const vec3f& a_from){
0025     parent::operator=(a_from);
0026     return *this;
0027   }
0028 
0029   vec3f(const parent& a_from):parent(a_from){}
0030 
0031 public: //operators
0032   vec3f operator*(float a_v) const {
0033     return vec3f(m_data[0]*a_v,
0034                  m_data[1]*a_v,
0035                  m_data[2]*a_v);
0036   }
0037   vec3f operator+(const vec3f& a_v) const {
0038     return vec3f(m_data[0]+a_v.m_data[0],
0039                  m_data[1]+a_v.m_data[1],
0040                  m_data[2]+a_v.m_data[2]);
0041   }
0042   vec3f operator-(const vec3f& a_v) const {
0043     return vec3f(m_data[0]-a_v.m_data[0],
0044                  m_data[1]-a_v.m_data[1],
0045                  m_data[2]-a_v.m_data[2]);
0046   }
0047   vec3f& operator+=(const vec3f& a_v) {
0048     m_data[0] += a_v.m_data[0];
0049     m_data[1] += a_v.m_data[1];
0050     m_data[2] += a_v.m_data[2];
0051     return *this;
0052   }
0053   vec3f& operator-=(const vec3f& a_v) {
0054     m_data[0] -= a_v.m_data[0];
0055     m_data[1] -= a_v.m_data[1];
0056     m_data[2] -= a_v.m_data[2];
0057     return *this;
0058   }
0059   vec3f& operator*=(float a_v) {
0060     m_data[0] *= a_v;
0061     m_data[1] *= a_v;
0062     m_data[2] *= a_v;
0063     return *this;
0064   }
0065   vec3f operator-() const {
0066     return vec3f(-m_data[0],-m_data[1],-m_data[2]);
0067   }
0068 public:
0069 #define TOOLS_VEC3F_MORE_PREC
0070 #ifdef TOOLS_VEC3F_MORE_PREC
0071   float length() const {
0072     return float(::sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2]));
0073   }
0074   float normalize() {
0075     float norme = length();
0076     if(!norme) return 0;
0077     divide(norme);
0078     return norme;
0079   }
0080   bool cos_angle(const vec3f& a_v,float& a_cos) const {
0081     //WARNING : if ret false, a_cos is not set.
0082     float this_length = length();
0083     if(this_length==0.0f) return false;
0084     float a_v_length = a_v.length();
0085     if(a_v_length==0.0f) return false;
0086     a_cos = dot(a_v)/(this_length*a_v_length);
0087     return true;
0088   }
0089 #else
0090   float length() const {return parent::length(::sqrtf);}
0091   float normalize() {return parent::normalize(::sqrtf);}
0092   bool cos_angle(const vec3f& a_v,float& a_cos) const {return parent::cos_angle(a_v,a_cos,::sqrtf);}
0093 #endif
0094 
0095   bool theta_phi(float& a_theta,float& a_phi) const {
0096     return parent::theta_phi(a_theta,a_phi,::sqrtf,::atan2f);
0097   }
0098 public: //iv2sg
0099   bool equals(const vec3f& a_v,const float a_epsil) const {
0100     //if(a_epsil<0.0f))
0101     float d0 = m_data[0]-a_v.m_data[0];
0102     float d1 = m_data[1]-a_v.m_data[1];
0103     float d2 = m_data[2]-a_v.m_data[2];
0104     return ((d0*d0+d1*d1+d2*d2)<=a_epsil);
0105   }
0106   void negate() {
0107     m_data[0] = -m_data[0];
0108     m_data[1] = -m_data[1];
0109     m_data[2] = -m_data[2];
0110   }
0111 
0112 private:static void check_instantiation() {vec3f v(0,0,0);v.set_value(1,1,1);}
0113 };
0114 
0115 inline vec3f operator*(float a_f,const vec3f& a_v) {
0116   vec3f res(a_v);
0117   res *= a_f;
0118   return res;
0119 }
0120 
0121 #define TOOLS_VEC3F_MORE_PREC
0122 #ifdef TOOLS_VEC3F_MORE_PREC
0123 inline void get_normal(const vec3f& a_p0,const vec3f& a_p1,const vec3f& a_p2,vec3f& a_nm,
0124                        vec3f& a_tmp_1,vec3f& a_tmp_2) {
0125   // Used to optimize sg::bin().
0126   //(a_p1-a_p0).cross(a_p2-a_p1,a_nm);
0127 
0128   a_tmp_1 = a_p1;
0129   a_tmp_1.subtract(a_p0);
0130 
0131   a_tmp_2 = a_p2;
0132   a_tmp_2.subtract(a_p1);
0133 
0134   a_tmp_1.cross(a_tmp_2,a_nm);
0135 
0136   a_nm.normalize();
0137 }
0138 #else
0139 inline void get_normal(const vec3f& a_p0,const vec3f& a_p1,const vec3f& a_p2,vec3f& a_nm,
0140                        vec3f& a_tmp_1,vec3f& a_tmp_2) {
0141   get_normal<float>(a_p0,a_p1,a_p2,a_nm,a_tmp_1,a_tmp_2,::sqrtf);
0142 }
0143 #endif
0144 
0145 }
0146 
0147 #include <vector>
0148 
0149 namespace tools {
0150 
0151 #ifndef SWIG
0152 //for sf, mf :
0153 inline bool set_from_vec(vec3f& a_v,const std::vector<float>& a_sv) {
0154   if(a_sv.size()!=3) return false;
0155   a_v[0] = a_sv[0];
0156   a_v[1] = a_sv[1];
0157   a_v[2] = a_sv[2];
0158   return true;
0159 }
0160 #endif
0161 
0162 }
0163 
0164 #endif