CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/DataFormats/GeometryVector/interface/oldBasic2DVector.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_oldBasic2DVector_h
00002 #define GeometryVector_oldBasic2DVector_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 
00140 
00142   T cross( const Basic2DVector& v) const { return x()*v.y() - y()*v.x();}
00143 
00149   template <class U> 
00150   typename PreciseFloatType<T,U>::Type cross( const Basic2DVector<U>& v) const { 
00151     return x()*v.y() - y()*v.x();
00152   }
00153 
00154 
00155 private:
00156   T theX;
00157   T theY;
00158 };
00159 
00160 
00161 namespace geometryDetails {
00162   std::ostream & print2D(std::ostream& s, double x, double y);
00163 
00164 }
00165 
00167 template <class T>
00168 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
00169   return geometryDetails::print2D(s, v.x(),v.y());
00170 }
00171 
00172 
00174 template <class T, class U>
00175 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00176 operator+( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00177   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00178   return RT(a.x()+b.x(), a.y()+b.y());
00179 }
00180 
00181 template <class T, class U>
00182 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00183 operator-( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00184   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00185   return RT(a.x()-b.x(), a.y()-b.y());
00186 }
00187 
00188 
00189 
00190 
00191 // scalar product of vectors of same precision
00192 template <class T>
00193 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
00194   return v1.dot(v2);
00195 }
00196 
00198 template <class T, class U>
00199 inline typename PreciseFloatType<T,U>::Type operator*( const Basic2DVector<T>& v1, 
00200                                                        const Basic2DVector<U>& v2) {
00201   return v1.x()*v2.x() + v1.y()*v2.y();
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 
00214 template <class T, class Scalar>
00215 inline Basic2DVector<T> operator*( const Scalar& s, const Basic2DVector<T>& v) {
00216   T t = static_cast<T>(s);
00217   return Basic2DVector<T>(v.x()*t, v.y()*t);
00218 }
00219 
00223 template <class T, class Scalar>
00224 inline Basic2DVector<T> operator/( const Basic2DVector<T>& v, const Scalar& s) {
00225   T t = static_cast<T>(s);
00226   return Basic2DVector<T>(v.x()/t, v.y()/t);
00227 }
00228 
00229 
00230 typedef Basic2DVector<float> Basic2DVectorF;
00231 typedef Basic2DVector<double> Basic2DVectorD;
00232 
00233 #endif // GeometryVector_Basic2DVector_h