CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DataFormats/GeometryVector/interface/extBasic2DVector.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_newBasic2DVector_h
00002 #define GeometryVector_newBasic2DVector_h
00003 
00004 #include "DataFormats/GeometryVector/interface/Phi.h"
00005 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h"
00006 #include "DataFormats/GeometryVector/interface/CoordinateSets.h"
00007 #include "DataFormats/Math/interface/ExtVec.h"
00008 
00009 
00010 #include <cmath>
00011 #include <iosfwd>
00012 
00013 
00014 template < class T> 
00015 class Basic2DVector {
00016 public:
00017 
00018   typedef T    ScalarType;
00019   typedef Vec2<T> VectorType;
00020   typedef Vec2<T> MathVector;
00021   typedef Geom::Polar2Cartesian<T>        Polar;
00022 
00027   Basic2DVector() : v{0,0} {}
00028 
00030   Basic2DVector( const Basic2DVector & p) : v(p.v) {}
00031 
00032   template<typename U>
00033   Basic2DVector( const Basic2DVector<U> & p) : v{T(p.v[0]),T(p.v[1])} {}
00034 
00035 
00043   template <class Other> 
00044   explicit Basic2DVector( const Other& p) : v{T(p.x()),T(p.y())} {}
00045 
00047   Basic2DVector( const T& x, const T& y) : v{x,y} {}
00048 
00049   // constructor from Vec2 or vec4
00050   Basic2DVector(MathVector const& iv) : v(iv){}
00051   template<typename U>
00052   Basic2DVector(Vec2<U> const& iv) : v{iv[0],iv[1]}{}
00053   template<typename U>
00054   Basic2DVector(Vec4<U> const& iv) : v{iv[0],iv[1]}{}
00055 
00056   MathVector const & mathVector() const { return v;}
00057   MathVector & mathVector() { return v;}
00058 
00059   T operator[](int i) const { return v[i];}
00060   T & operator[](int i) { return v[i];}
00061 
00062 
00064   T x() const { return v[0];}
00065 
00067   T y() const { return v[1];}
00068 
00070   T mag2() const { return ::dot(v,v);}
00071 
00073   T mag() const  { return std::sqrt( mag2());}
00074 
00076   T r() const    { return mag();}
00077 
00082   T barePhi() const {return std::atan2(y(),x());}
00083   Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(y(),x()));}
00084 
00088   Basic2DVector unit() const {
00089     T my_mag = mag();
00090     return my_mag == 0 ? *this : *this / my_mag;
00091   }
00092 
00095   template <class U> 
00096   Basic2DVector& operator+= ( const Basic2DVector<U>& p) {
00097     v = v + p.v;
00098     return *this;
00099   } 
00100 
00103   template <class U> 
00104   Basic2DVector& operator-= ( const Basic2DVector<U>& p) {
00105     v = v - p.v;
00106     return *this;
00107   } 
00108 
00110   Basic2DVector operator-() const { return Basic2DVector(-v);}
00111 
00113   Basic2DVector& operator*= ( T t) {
00114     v = v*t;
00115     return *this;
00116   } 
00117 
00119   Basic2DVector& operator/= ( T t) {
00120     t = T(1)/t;
00121     v = v*t;
00122     return *this;
00123   } 
00124 
00126   T dot( const Basic2DVector& lh) const { return ::dot(v,lh.v);}
00127 
00133   template <class U> 
00134   typename PreciseFloatType<T,U>::Type dot( const Basic2DVector<U>& lh) const { 
00135     return Basic2DVector<typename PreciseFloatType<T,U>::Type>(*this)
00136       .dot(Basic2DVector<typename PreciseFloatType<T,U>::Type>(lh));
00137   }
00138 
00140   T cross( const Basic2DVector& lh) const { return ::cross2(v,lh.v);}
00141 
00147   template <class U> 
00148   typename PreciseFloatType<T,U>::Type cross( const Basic2DVector<U>& lh) const { 
00149     return Basic2DVector<typename PreciseFloatType<T,U>::Type>(*this)
00150       .cross(Basic2DVector<typename PreciseFloatType<T,U>::Type>(lh));
00151   }
00152 
00153 
00154 public:
00155 
00156   Vec2<T> v;
00157 
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>
00175 inline Basic2DVector<T>
00176 operator+( const Basic2DVector<T>& a, const Basic2DVector<T>& b) {
00177   return a.v+b.v;
00178 }
00179 template <class T>
00180 inline Basic2DVector<T>
00181 operator-( const Basic2DVector<T>& a, const Basic2DVector<T>& b) {
00182   return a.v-b.v;
00183 }
00184 
00185 template <class T, class U>
00186 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00187 operator+( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00188   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00189   return RT(a) + RT(b);
00190 }
00191 
00192 template <class T, class U>
00193 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00194 operator-( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00195   typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00196   return RT(a)-RT(b);
00197 }
00198 
00199 
00200 
00201 
00202 // scalar product of vectors of same precision
00203 template <class T>
00204 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
00205   return v1.dot(v2);
00206 }
00207 
00209 template <class T, class U>
00210 inline typename PreciseFloatType<T,U>::Type operator*( const Basic2DVector<T>& v1, 
00211                                                        const Basic2DVector<U>& v2) {
00212   return v1.dot(v2);
00213 }
00214 
00215 
00219 template <class T>
00220 inline Basic2DVector<T> operator*( const Basic2DVector<T>& v, T t) {
00221   return v.v*t;
00222 }
00223 
00225 template <class T>
00226 inline Basic2DVector<T> operator*(T t, const Basic2DVector<T>& v) {
00227   return v.v*t;
00228 }
00229 
00230 
00231 
00232 template <class T, class Scalar>
00233 inline Basic2DVector<T> operator*( const Basic2DVector<T>& v, const Scalar& s) {
00234   T t = static_cast<T>(s);
00235   return v*t;
00236 }
00237 
00239 template <class T, class Scalar>
00240 inline Basic2DVector<T> operator*( const Scalar& s, const Basic2DVector<T>& v) {
00241   T t = static_cast<T>(s);
00242   return v*t;
00243 }
00244 
00248 template <class T>
00249 inline Basic2DVector<T> operator/(const Basic2DVector<T>& v, T t) {
00250   return v.v/t;
00251 }
00252 
00253 template <class T, class Scalar>
00254 inline Basic2DVector<T> operator/( const Basic2DVector<T>& v, const Scalar& s) {
00255   //   T t = static_cast<T>(Scalar(1)/s); return v*t;
00256    T t = static_cast<T>(s);
00257   return v/t;
00258 }
00259 
00260 typedef Basic2DVector<float> Basic2DVectorF;
00261 typedef Basic2DVector<double> Basic2DVectorD;
00262 
00263 
00264 #endif // GeometryVector_Basic2DVector_h