Go to the documentation of this file.00001 #ifndef GeometryVector_oldBasic2DVector_h
00002 #define GeometryVector_oldBasic2DVector_h
00003 #if ( defined(IN_DICTBUILD) || 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/SIMDVec.h"
00012 #endif
00013
00014
00015 #include <cmath>
00016 #include <iosfwd>
00017
00018
00019 template < class T>
00020 class Basic2DVector {
00021 public:
00022 typedef Basic2DVector<T> MathVector;
00023
00024 typedef T ScalarType;
00025 typedef Geom::Polar2Cartesian<T> Polar;
00026
00031 Basic2DVector() : theX(0), theY(0) {}
00032
00034 Basic2DVector( const Basic2DVector & p) :
00035 theX(p.x()), theY(p.y()) {}
00036
00044 template <class Other>
00045 explicit Basic2DVector( const Other& p) : theX(p.x()), theY(p.y()) {}
00046
00048 Basic2DVector( const T& x, const T& y) : theX(x), theY(y) {}
00049
00050
00051 #if defined(USE_EXTVECT)
00052
00053 template<typename U>
00054 Basic2DVector(Vec2<U> const& iv) :
00055 theX(iv[0]), theY(iv[1]) {}
00056 template<typename U>
00057 Basic2DVector(Vec4<U> const& iv) :
00058 theX(iv.arr[0]), theY(iv.arr[1]) {}
00059 #elif defined(USE_SSEVECT)
00060
00061 template<typename U>
00062 Basic2DVector(mathSSE::Vec2<U> const& iv) :
00063 theX(iv.arr[0]), theY(iv.arr[1]) {}
00064 template<typename U>
00065 Basic2DVector(mathSSE::Vec4<U> const& iv) :
00066 theX(iv.arr[0]), theY(iv.arr[1]) {}
00067 #endif
00068
00069 T operator[](int i) const { return i==0 ? theX : theY ;}
00070 T & operator[](int i) { return i==0 ? theX : theY ;}
00071
00073 T x() const { return theX;}
00074
00076 T y() const { return theY;}
00077
00079 T mag2() const { return theX*theX + theY*theY;}
00080
00082 T mag() const { return std::sqrt( mag2());}
00083
00085 T r() const { return mag();}
00086
00091 T barePhi() const {return std::atan2(theY,theX);}
00092 Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(theY,theX));}
00093
00097 Basic2DVector unit() const {
00098 T my_mag = mag();
00099 return my_mag == 0 ? *this : *this / my_mag;
00100 }
00101
00104 template <class U>
00105 Basic2DVector& operator+= ( const Basic2DVector<U>& p) {
00106 theX += p.x();
00107 theY += p.y();
00108 return *this;
00109 }
00110
00113 template <class U>
00114 Basic2DVector& operator-= ( const Basic2DVector<U>& p) {
00115 theX -= p.x();
00116 theY -= p.y();
00117 return *this;
00118 }
00119
00121 Basic2DVector operator-() const { return Basic2DVector(-x(),-y());}
00122
00124 Basic2DVector& operator*= ( const T& t) {
00125 theX *= t;
00126 theY *= t;
00127 return *this;
00128 }
00129
00131 Basic2DVector& operator/= ( const T& t) {
00132 theX /= t;
00133 theY /= t;
00134 return *this;
00135 }
00136
00138 T dot( const Basic2DVector& v) const { return x()*v.x() + y()*v.y();}
00139
00145 template <class U>
00146 typename PreciseFloatType<T,U>::Type dot( const Basic2DVector<U>& v) const {
00147 return x()*v.x() + y()*v.y();
00148 }
00149
00150
00151
00153 T cross( const Basic2DVector& v) const { return x()*v.y() - y()*v.x();}
00154
00160 template <class U>
00161 typename PreciseFloatType<T,U>::Type cross( const Basic2DVector<U>& v) const {
00162 return x()*v.y() - y()*v.x();
00163 }
00164
00165
00166 private:
00167 T theX;
00168 T theY;
00169 };
00170
00171
00172 namespace geometryDetails {
00173 std::ostream & print2D(std::ostream& s, double x, double y);
00174
00175 }
00176
00178 template <class T>
00179 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
00180 return geometryDetails::print2D(s, v.x(),v.y());
00181 }
00182
00183
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.x()+b.x(), a.y()+b.y());
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.x()-b.x(), a.y()-b.y());
00197 }
00198
00199
00200
00201
00202
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.x()*v2.x() + v1.y()*v2.y();
00213 }
00214
00218 template <class T, class Scalar>
00219 inline Basic2DVector<T> operator*( const Basic2DVector<T>& v, const Scalar& s) {
00220 T t = static_cast<T>(s);
00221 return Basic2DVector<T>(v.x()*t, v.y()*t);
00222 }
00223
00225 template <class T, class Scalar>
00226 inline Basic2DVector<T> operator*( const Scalar& s, const Basic2DVector<T>& v) {
00227 T t = static_cast<T>(s);
00228 return Basic2DVector<T>(v.x()*t, v.y()*t);
00229 }
00230
00234 template <class T, class Scalar>
00235 inline Basic2DVector<T> operator/( const Basic2DVector<T>& v, const Scalar& s) {
00236 T t = static_cast<T>(s);
00237 return Basic2DVector<T>(v.x()/t, v.y()/t);
00238 }
00239
00240
00241 typedef Basic2DVector<float> Basic2DVectorF;
00242 typedef Basic2DVector<double> Basic2DVectorD;
00243
00244 #endif // GeometryVector_Basic2DVector_h