CMS 3D CMS Logo

CMSSW_4_4_3_patch1/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 Geom::Cylindrical2Cartesian<T>      Cylindrical;
00029   typedef Geom::Spherical2Cartesian<T>        Spherical;
00030   typedef Spherical                           Polar; // synonym
00031     
00036   Basic3DVector() {}
00037 
00039   Basic3DVector( const Basic3DVector & p) : 
00040     v(p.v) {}
00041 
00043   template <class U>
00044   Basic3DVector( const Basic3DVector<U> & p) : 
00045     v(p.v) {}
00046 
00047 
00049   Basic3DVector( const Basic2DVector<T> & p) : 
00050     v(p.x(),p.y(),0) {}
00051 
00052  
00061   template <class OtherPoint> 
00062   explicit Basic3DVector( const OtherPoint& p) : 
00063         v(p.x(),p.y(),p.z()) {}
00064 
00065 
00066   // constructor from Vec4
00067   template<class U>
00068   Basic3DVector(mathSSE::Vec4<U> const& iv) : v(iv){}
00069 
00071   Basic3DVector( const T& x, const T& y, const T& z) : 
00072     v(x,y,z){}
00073 
00078   template <typename U>
00079   Basic3DVector( const Geom::Theta<U>& theta, 
00080                  const Geom::Phi<U>& phi, const T& r) {
00081     Polar p( theta.value(), phi.value(), r);
00082     v.o.theX = p.x(); v.o.theY = p.y(); v.o.theZ = p.z();
00083   }
00084 
00086   T x() const { return v.o.theX;}
00087 
00089   T y() const { return v.o.theY;}
00090 
00092   T z() const { return v.o.theZ;}
00093 
00094   Basic2DVector<T> xy() const { return v.xy();}
00095 
00096   // equality
00097   bool operator==(const Basic3DVector& rh) const {
00098     return v==rh.v;
00099   }
00100 
00102   T mag2() const { return  ::dot(v,v);}
00103 
00105   T mag() const  { return std::sqrt( mag2());}
00106 
00108   T perp2() const { return ::dotxy(v,v);}
00109 
00111   T perp() const { return std::sqrt( perp2());}
00112 
00114   T transverse() const { return perp();}
00115 
00120   T barePhi() const {return std::atan2(y(),x());}
00121   Geom::Phi<T> phi() const {return Geom::Phi<T>(barePhi());}
00122 
00127   T bareTheta() const {return std::atan2(perp(),z());}
00128   Geom::Theta<T> theta() const {return Geom::Theta<T>(std::atan2(perp(),z()));}
00129 
00134   // T eta() const { return -log( tan( theta()/2.));} 
00135   T eta() const { return detailsBasic3DVector::eta(x(),y(),z());} // correct 
00136 
00140   Basic3DVector unit() const {
00141     T my_mag = mag2();
00142     return (0!=my_mag) ? (*this)*(T(1)/std::sqrt(my_mag)) : *this;
00143   }
00144 
00147   template <class U> 
00148   Basic3DVector& operator+= ( const Basic3DVector<U>& p) {
00149     v = v + p.v;
00150     return *this;
00151   } 
00152 
00155   template <class U> 
00156   Basic3DVector& operator-= ( const Basic3DVector<U>& p) {
00157     v = v - p.v;
00158     return *this;
00159   } 
00160 
00162   Basic3DVector operator-() const { return Basic3DVector(-v);}
00163 
00165   Basic3DVector& operator*= ( T t) {
00166     v = t*v;
00167     return *this;
00168   } 
00169 
00171   Basic3DVector& operator/= ( T t) {
00172     t = T(1)/t;
00173     v = t*v;
00174     return *this;
00175   } 
00176 
00178   T dot( const Basic3DVector& rh) const { 
00179     return ::dot(v,rh.v);
00180   }
00181 
00187   template <class U> 
00188   typename PreciseFloatType<T,U>::Type dot( const Basic3DVector<U>& lh) const { 
00189     return Basic3DVector<typename PreciseFloatType<T,U>::Type>(*this)
00190       .dot(Basic3DVector<typename PreciseFloatType<T,U>::Type>(lh));
00191   }
00192 
00194   Basic3DVector cross( const Basic3DVector& lh) const {
00195     return ::cross(v,lh.v);
00196   }
00197 
00198 
00204   template <class U> 
00205   Basic3DVector<typename PreciseFloatType<T,U>::Type> 
00206   cross( const Basic3DVector<U>& lh) const {
00207     return Basic3DVector<typename PreciseFloatType<T,U>::Type>(*this)
00208       .cross(Basic3DVector<typename PreciseFloatType<T,U>::Type>(lh));
00209   }
00210 
00211 public:
00212   mathSSE::Vec4<T> v;
00213 }  __attribute__ ((aligned (16)));
00214 
00215 
00216 namespace geometryDetails {
00217   std::ostream & print3D(std::ostream& s, double x, double y, double z);
00218 }
00219 
00221 template <class T>
00222 inline std::ostream & operator<<( std::ostream& s, const Basic3DVector<T>& v) {
00223   return geometryDetails::print3D(s, v.x(),v.y(), v.z());
00224 }
00225 
00226 
00228 template <class T>
00229 inline Basic3DVector<T>
00230 operator+( const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
00231   return a.v+b.v;
00232 }
00233 template <class T>
00234 inline Basic3DVector<T>
00235 operator-( const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
00236   return a.v-b.v;
00237 }
00238 
00239 template <class T, class U>
00240 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00241 operator+( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00242   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00243   return RT(a).v+RT(b).v;
00244 }
00245 
00246 template <class T, class U>
00247 inline Basic3DVector<typename PreciseFloatType<T,U>::Type>
00248 operator-( const Basic3DVector<T>& a, const Basic3DVector<U>& b) {
00249   typedef Basic3DVector<typename PreciseFloatType<T,U>::Type> RT;
00250   return RT(a).v-RT(b).v;
00251 }
00252 
00254 template <class T>
00255 inline T operator*( const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
00256   return v1.dot(v2);
00257 }
00258 
00260 template <class T, class U>
00261 inline typename PreciseFloatType<T,U>::Type operator*( const Basic3DVector<T>& v1, 
00262                                                        const Basic3DVector<U>& v2) {
00263   return  v1.dot(v2);
00264 }
00265 
00269 template <class T>
00270 inline Basic3DVector<T> operator*( const Basic3DVector<T>& v, T t) {
00271   return v.v*t;
00272 }
00273 
00275 template <class T>
00276 inline Basic3DVector<T> operator*(T t, const Basic3DVector<T>& v) {
00277   return v.v*t;
00278 }
00279 
00280 template <class T, typename S>
00281 inline Basic3DVector<T> operator*(S t,  const Basic3DVector<T>& v) {
00282   return static_cast<T>(t)*v;
00283 }
00284 
00285 template <class T, typename S>
00286 inline Basic3DVector<T> operator*(const Basic3DVector<T>& v, S t) {
00287   return static_cast<T>(t)*v;
00288 }
00289 
00290 
00294 template <class T, typename S>
00295 inline Basic3DVector<T> operator/( const Basic3DVector<T>& v, S s) {
00296   T t = T(1)/s;
00297   return v*t;
00298 }
00299 
00300 
00301 typedef Basic3DVector<float> Basic3DVectorF;
00302 typedef Basic3DVector<double> Basic3DVectorD;
00303 
00304 
00305 //  add long double specialization
00306 #include "Basic3DVectorLD.h"
00307 
00308 #endif // GeometryVector_Basic3DVector_h
00309 
00310