CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/DataFormats/GeometryVector/interface/newBasic3DVector.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_newBasic3DVector_h
00002 #define GeometryVector_newBasic3DVector_h
00003 
00004 #include "DataFormats/GeometryVector/interface/Basic2DVector.h"
00005 #include "DataFormats/GeometryVector/interface/Theta.h"
00006 #include "DataFormats/GeometryVector/interface/Phi.h"
00007 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h"
00008 #include "DataFormats/GeometryVector/interface/CoordinateSets.h"
00009 #include "DataFormats/Math/interface/SSEVec.h"
00010 #include <iosfwd>
00011 #include <cmath>
00012 
00013 namespace detailsBasic3DVector {
00014   inline float __attribute__((always_inline)) __attribute__ ((pure))
00015   eta(float x, float y, float z) { float t(z/std::sqrt(x*x+y*y)); return ::asinhf(t);} 
00016   inline double __attribute__((always_inline)) __attribute__ ((pure))
00017   eta(double x, double y, double z) { double t(z/std::sqrt(x*x+y*y)); return ::asinh(t);} 
00018   inline long double __attribute__((always_inline)) __attribute__ ((pure))
00019   eta(long double x, long double y, long double z) { long double t(z/std::sqrt(x*x+y*y)); return ::asinhl(t);} 
00020 }
00021 
00022 
00023 template < typename T> 
00024 class Basic3DVector {
00025 public:
00026 
00027   typedef T                                   ScalarType;
00028   typedef mathSSE::Vec4<T>                    VectorType;
00029   typedef mathSSE::Vec4<T>                    MathVector;
00030   typedef Geom::Cylindrical2Cartesian<T>      Cylindrical;
00031   typedef Geom::Spherical2Cartesian<T>        Spherical;
00032   typedef Spherical                           Polar; // synonym
00033     
00038   Basic3DVector() {}
00039 
00041   Basic3DVector( const Basic3DVector & p) : 
00042     v(p.v) {}
00043 
00045   template <class U>
00046   Basic3DVector( const Basic3DVector<U> & p) : 
00047     v(p.v) {}
00048 
00049 
00051   Basic3DVector( const Basic2DVector<T> & p) : 
00052     v(p.x(),p.y(),0) {}
00053 
00054  
00063   template <class OtherPoint> 
00064   explicit Basic3DVector( const OtherPoint& p) : 
00065         v(p.x(),p.y(),p.z()) {}
00066 
00067 
00068   // constructor from Vec4
00069   template<class U>
00070   Basic3DVector(mathSSE::Vec4<U> const& iv) : v(iv){}
00071 
00073   Basic3DVector( const T& x, const T& y, const T& z, const T&w=0) : 
00074     v(x,y,z,w){}
00075 
00080   template <typename U>
00081   Basic3DVector( const Geom::Theta<U>& theta, 
00082                  const Geom::Phi<U>& phi, const T& r) {
00083     Polar p( theta.value(), phi.value(), r);
00084     v.o.theX = p.x(); v.o.theY = p.y(); v.o.theZ = p.z();
00085   }
00086 
00087   MathVector const & mathVector() const { return v;}
00088   MathVector & mathVector() { return v;}
00089 
00090 
00092   T x() const { return v.o.theX;}
00093 
00095   T y() const { return v.o.theY;}
00096 
00098   T z() const { return v.o.theZ;}
00099 
00100   T w() const { return v.o.theW;}
00101 
00102   Basic2DVector<T> xy() const { return v.xy();}
00103 
00104   // equality
00105   bool operator==(const Basic3DVector& rh) const {
00106     return v==rh.v;
00107   }
00108 
00110   T mag2() const { return  ::dot(v,v);}
00111 
00113   T mag() const  { return std::sqrt( mag2());}
00114 
00116   T perp2() const { return ::dotxy(v,v);}
00117 
00119   T perp() const { return std::sqrt( perp2());}
00120 
00122   T transverse() const { return perp();}
00123 
00128   T barePhi() const {return std::atan2(y(),x());}
00129   Geom::Phi<T> phi() const {return Geom::Phi<T>(barePhi());}
00130 
00135   T bareTheta() const {return std::atan2(perp(),z());}
00136   Geom::Theta<T> theta() const {return Geom::Theta<T>(std::atan2(perp(),z()));}
00137 
00142   // T eta() const { return -log( tan( theta()/2.));} 
00143   T eta() const { return detailsBasic3DVector::eta(x(),y(),z());} // correct 
00144 
00148   Basic3DVector unit() const {
00149     T my_mag = mag2();
00150     return (0!=my_mag) ? (*this)*(T(1)/std::sqrt(my_mag)) : *this;
00151   }
00152 
00155   template <class U> 
00156   Basic3DVector& operator+= ( const Basic3DVector<U>& p) {
00157     v = v + p.v;
00158     return *this;
00159   } 
00160 
00163   template <class U> 
00164   Basic3DVector& operator-= ( const Basic3DVector<U>& p) {
00165     v = v - p.v;
00166     return *this;
00167   } 
00168 
00170   Basic3DVector operator-() const { return Basic3DVector(-v);}
00171 
00173   Basic3DVector& operator*= ( T t) {
00174     v = t*v;
00175     return *this;
00176   } 
00177 
00179   Basic3DVector& operator/= ( T t) {
00180     //t = T(1)/t;
00181     v = v/t;
00182     return *this;
00183   } 
00184 
00186   T dot( const Basic3DVector& rh) const { 
00187     return ::dot(v,rh.v);
00188   }
00189 
00195   template <class U> 
00196   typename PreciseFloatType<T,U>::Type dot( const Basic3DVector<U>& lh) const { 
00197     return Basic3DVector<typename PreciseFloatType<T,U>::Type>(*this)
00198       .dot(Basic3DVector<typename PreciseFloatType<T,U>::Type>(lh));
00199   }
00200 
00202   Basic3DVector cross( const Basic3DVector& lh) const {
00203     return ::cross(v,lh.v);
00204   }
00205 
00206 
00212   template <class U> 
00213   Basic3DVector<typename PreciseFloatType<T,U>::Type> 
00214   cross( const Basic3DVector<U>& lh) const {
00215     return Basic3DVector<typename PreciseFloatType<T,U>::Type>(*this)
00216       .cross(Basic3DVector<typename PreciseFloatType<T,U>::Type>(lh));
00217   }
00218 
00219 public:
00220   mathSSE::Vec4<T> v;
00221 }  __attribute__ ((aligned (16)));
00222 
00223 
00224 namespace geometryDetails {
00225   std::ostream & print3D(std::ostream& s, double x, double y, double z);
00226 }
00227 
00229 template <class T>
00230 inline std::ostream & operator<<( std::ostream& s, const Basic3DVector<T>& v) {
00231   return geometryDetails::print3D(s, v.x(),v.y(), v.z());
00232 }
00233 
00234 
00236 template <class T>
00237 inline Basic3DVector<T>
00238 operator+( const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
00239   return a.v+b.v;
00240 }
00241 template <class T>
00242 inline Basic3DVector<T>
00243 operator-( const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
00244   return a.v-b.v;
00245 }
00246 
00247 template <class T, class U>
00248 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00249 operator+( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00250   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00251   return RT(a).v+RT(b).v;
00252 }
00253 
00254 template <class T, class U>
00255 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00256 operator-( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00257   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00258   return RT(a).v-RT(b).v;
00259 }
00260 
00262 template <class T>
00263 inline T operator*( const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
00264   return v1.dot(v2);
00265 }
00266 
00268 template <class T, class U>
00269 inline typename PreciseFloatType<T,U>::Type operator*( const Basic3DVector<T>& v1, 
00270                                                        const Basic3DVector<U>& v2) {
00271   return  v1.dot(v2);
00272 }
00273 
00277 template <class T>
00278 inline Basic3DVector<T> operator*( const Basic3DVector<T>& v, T t) {
00279   return v.v*t;
00280 }
00281 
00283 template <class T>
00284 inline Basic3DVector<T> operator*(T t, const Basic3DVector<T>& v) {
00285   return v.v*t;
00286 }
00287 
00288 
00289 
00290 template <class T, typename S>
00291 inline Basic3DVector<T> operator*(S t,  const Basic3DVector<T>& v) {
00292   return static_cast<T>(t)*v;
00293 }
00294 
00295 template <class T, typename S>
00296 inline Basic3DVector<T> operator*(const Basic3DVector<T>& v, S t) {
00297   return static_cast<T>(t)*v;
00298 }
00299 
00300 
00304 template <class T>
00305 inline Basic3DVector<T> operator/(const Basic3DVector<T>& v, T t) {
00306   return v.v/t;
00307 }
00308 
00309 template <class T, typename S>
00310 inline Basic3DVector<T> operator/( const Basic3DVector<T>& v, S s) {
00311   //  T t = S(1)/s; return v*t;
00312   T t = s;
00313   return v/t;
00314 }
00315 
00316 
00317 typedef Basic3DVector<float> Basic3DVectorF;
00318 typedef Basic3DVector<double> Basic3DVectorD;
00319 
00320 
00321 //  add long double specialization
00322 #include "Basic3DVectorLD.h"
00323 
00324 #endif // GeometryVector_Basic3DVector_h
00325 
00326