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 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 #ifndef __REFLEX__
00052
00053 template<typename U>
00054 Basic2DVector(mathSSE::Vec2<U> const& iv) :
00055 theX(iv.arr[0]), theY(iv.arr[1]) {}
00056 template<typename U>
00057 Basic2DVector(mathSSE::Vec4<U> const& iv) :
00058 theX(iv.arr[0]), theY(iv.arr[1]) {}
00059 #endif
00060
00061
00063 T x() const { return theX;}
00064
00066 T y() const { return theY;}
00067
00069 T mag2() const { return theX*theX + theY*theY;}
00070
00072 T mag() const { return std::sqrt( mag2());}
00073
00075 T r() const { return mag();}
00076
00081 T barePhi() const {return std::atan2(theY,theX);}
00082 Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(theY,theX));}
00083
00087 Basic2DVector unit() const {
00088 T my_mag = mag();
00089 return my_mag == 0 ? *this : *this / my_mag;
00090 }
00091
00094 template <class U>
00095 Basic2DVector& operator+= ( const Basic2DVector<U>& p) {
00096 theX += p.x();
00097 theY += p.y();
00098 return *this;
00099 }
00100
00103 template <class U>
00104 Basic2DVector& operator-= ( const Basic2DVector<U>& p) {
00105 theX -= p.x();
00106 theY -= p.y();
00107 return *this;
00108 }
00109
00111 Basic2DVector operator-() const { return Basic2DVector(-x(),-y());}
00112
00114 Basic2DVector& operator*= ( const T& t) {
00115 theX *= t;
00116 theY *= t;
00117 return *this;
00118 }
00119
00121 Basic2DVector& operator/= ( const T& t) {
00122 theX /= t;
00123 theY /= t;
00124 return *this;
00125 }
00126
00128 T dot( const Basic2DVector& v) const { return x()*v.x() + y()*v.y();}
00129
00135 template <class U>
00136 typename PreciseFloatType<T,U>::Type dot( const Basic2DVector<U>& v) const {
00137 return x()*v.x() + y()*v.y();
00138 }
00139
00140
00141
00143 T cross( const Basic2DVector& v) const { return x()*v.y() - y()*v.x();}
00144
00150 template <class U>
00151 typename PreciseFloatType<T,U>::Type cross( const Basic2DVector<U>& v) const {
00152 return x()*v.y() - y()*v.x();
00153 }
00154
00155
00156 private:
00157 T theX;
00158 T theY;
00159 };
00160
00161
00162 namespace geometryDetails {
00163 std::ostream & print2D(std::ostream& s, double x, double y);
00164
00165 }
00166
00168 template <class T>
00169 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
00170 return geometryDetails::print2D(s, v.x(),v.y());
00171 }
00172
00173
00175 template <class T, class U>
00176 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00177 operator+( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00178 typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00179 return RT(a.x()+b.x(), a.y()+b.y());
00180 }
00181
00182 template <class T, class U>
00183 inline Basic2DVector<typename PreciseFloatType<T,U>::Type>
00184 operator-( const Basic2DVector<T>& a, const Basic2DVector<U>& b) {
00185 typedef Basic2DVector<typename PreciseFloatType<T,U>::Type> RT;
00186 return RT(a.x()-b.x(), a.y()-b.y());
00187 }
00188
00189
00190
00191
00192
00193 template <class T>
00194 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
00195 return v1.dot(v2);
00196 }
00197
00199 template <class T, class U>
00200 inline typename PreciseFloatType<T,U>::Type operator*( const Basic2DVector<T>& v1,
00201 const Basic2DVector<U>& v2) {
00202 return v1.x()*v2.x() + v1.y()*v2.y();
00203 }
00204
00208 template <class T, class Scalar>
00209 inline Basic2DVector<T> operator*( const Basic2DVector<T>& v, const Scalar& s) {
00210 T t = static_cast<T>(s);
00211 return Basic2DVector<T>(v.x()*t, v.y()*t);
00212 }
00213
00215 template <class T, class Scalar>
00216 inline Basic2DVector<T> operator*( const Scalar& s, const Basic2DVector<T>& v) {
00217 T t = static_cast<T>(s);
00218 return Basic2DVector<T>(v.x()*t, v.y()*t);
00219 }
00220
00224 template <class T, class Scalar>
00225 inline Basic2DVector<T> operator/( const Basic2DVector<T>& v, const Scalar& s) {
00226 T t = static_cast<T>(s);
00227 return Basic2DVector<T>(v.x()/t, v.y()/t);
00228 }
00229
00230
00231 typedef Basic2DVector<float> Basic2DVectorF;
00232 typedef Basic2DVector<double> Basic2DVectorD;
00233
00234 #endif // GeometryVector_Basic2DVector_h