CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DataFormats/GeometryVector/interface/Basic2DVector.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_Basic2DVector_h
00002 #define GeometryVector_Basic2DVector_h
00003 #if defined(__CINT__)  && !defined(__REFLEX__)
00004 #define __REFLEX__
00005 #endif
00006 
00007 #include "DataFormats/GeometryVector/interface/Phi.h"
00008 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h"
00009 #include "DataFormats/GeometryVector/interface/CoordinateSets.h"
00010 #ifndef __REFLEX__ 
00011 #include "DataFormats/Math/interface/SSEVec.h"
00012 #endif
00013 
00014 
00015 #include <cmath>
00016 #include <iosfwd>
00017 
00018 
00019 template < class T> 
00020 class Basic2DVector {
00021 public:
00022 
00023   typedef T    ScalarType;
00024   typedef Geom::Polar2Cartesian<T>        Polar;
00025 
00030   Basic2DVector() : theX(0), theY(0) {}
00031 
00033   Basic2DVector( const Basic2DVector & p) : 
00034     theX(p.x()), theY(p.y()) {}
00035 
00043   template <class Other> 
00044   explicit Basic2DVector( const Other& p) : theX(p.x()), theY(p.y()) {}
00045 
00047   Basic2DVector( const T& x, const T& y) : theX(x), theY(y) {}
00048 
00049 
00050 #ifndef __REFLEX__
00051   // constructor from Vec2 or vec4
00052   template<typename U>
00053   Basic2DVector(mathSSE::Vec2<U> const& iv) :
00054     theX(iv.arr[0]), theY(iv.arr[1]) {}
00055   template<typename U>
00056   Basic2DVector(mathSSE::Vec4<U> const& iv) :
00057     theX(iv.arr[0]), theY(iv.arr[1]) {}
00058 #endif  
00059 
00060 
00062   T x() const { return theX;}
00063 
00065   T y() const { return theY;}
00066 
00068   T mag2() const { return theX*theX + theY*theY;}
00069 
00071   T mag() const  { return std::sqrt( mag2());}
00072 
00074   T r() const    { return mag();}
00075 
00080   T barePhi() const {return std::atan2(theY,theX);}
00081   Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(theY,theX));}
00082 
00086   Basic2DVector unit() const {
00087     T my_mag = mag();
00088     return my_mag == 0 ? *this : *this / my_mag;
00089   }
00090 
00093   template <class U> 
00094   Basic2DVector& operator+= ( const Basic2DVector<U>& p) {
00095     theX += p.x();
00096     theY += p.y();
00097     return *this;
00098   } 
00099 
00102   template <class U> 
00103   Basic2DVector& operator-= ( const Basic2DVector<U>& p) {
00104     theX -= p.x();
00105     theY -= p.y();
00106     return *this;
00107   } 
00108 
00110   Basic2DVector operator-() const { return Basic2DVector(-x(),-y());}
00111 
00113   Basic2DVector& operator*= ( const T& t) {
00114     theX *= t;
00115     theY *= t;
00116     return *this;
00117   } 
00118 
00120   Basic2DVector& operator/= ( const T& t) {
00121     theX /= t;
00122     theY /= t;
00123     return *this;
00124   } 
00125 
00127   T dot( const Basic2DVector& v) const { return x()*v.x() + y()*v.y();}
00128 
00134   template <class U> 
00135   typename PreciseFloatType<T,U>::Type dot( const Basic2DVector<U>& v) const { 
00136     return x()*v.x() + y()*v.y();
00137   }
00138 
00139 private:
00140   T theX;
00141   T theY;
00142 };
00143 
00144 
00145 namespace geometryDetails {
00146   std::ostream & print2D(std::ostream& s, double x, double y);
00147 
00148 }
00149 
00151 template <class T>
00152 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
00153   return geometryDetails::print2D(s, v.x(),v.y());
00154 }
00155 
00156 
00158 template <class T, class U>
00159 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00160 operator+( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00161   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00162   return RT(a.x()+b.x(), a.y()+b.y());
00163 }
00164 
00165 template <class T, class U>
00166 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00167 operator-( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00168   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00169   return RT(a.x()-b.x(), a.y()-b.y());
00170 }
00171 
00172 
00173 
00174 
00175 // scalar product of vectors of same precision
00176 template <class T>
00177 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
00178   return v1.dot(v2);
00179 }
00180 
00182 template <class T, class U>
00183 inline typename PreciseFloatType<T,U>::Type operator*( const Basic2DVector<T>& v1, 
00184                                                        const Basic2DVector<U>& v2) {
00185   return v1.x()*v2.x() + v1.y()*v2.y();
00186 }
00187 
00191 template <class T, class Scalar>
00192 inline Basic2DVector<T> operator*( const Basic2DVector<T>& v, const Scalar& s) {
00193   T t = static_cast<T>(s);
00194   return Basic2DVector<T>(v.x()*t, v.y()*t);
00195 }
00196 
00198 template <class T, class Scalar>
00199 inline Basic2DVector<T> operator*( const Scalar& s, const Basic2DVector<T>& v) {
00200   T t = static_cast<T>(s);
00201   return Basic2DVector<T>(v.x()*t, v.y()*t);
00202 }
00203 
00207 template <class T, class Scalar>
00208 inline Basic2DVector<T> operator/( const Basic2DVector<T>& v, const Scalar& s) {
00209   T t = static_cast<T>(s);
00210   return Basic2DVector<T>(v.x()/t, v.y()/t);
00211 }
00212 
00213 #endif // GeometryVector_Basic2DVector_h