CMS 3D CMS Logo

oldBasic2DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_oldBasic2DVector_h
2 #define GeometryVector_oldBasic2DVector_h
3 
7 #if (!defined(__CLING__))
9 #endif
10 
11 
12 #include <cmath>
13 #include <iosfwd>
14 
15 
16 template < class T>
17 class Basic2DVector {
18 public:
20 
21  typedef T ScalarType;
23 
28  Basic2DVector() : theX(0), theY(0) {}
29 
32  theX(p.x()), theY(p.y()) {}
33 
41  template <class Other>
42  explicit Basic2DVector( const Other& p) : theX(p.x()), theY(p.y()) {}
43 
45  Basic2DVector( const T& x, const T& y) : theX(x), theY(y) {}
46 
47 
48 #if defined(USE_EXTVECT)
49  // constructor from Vec2 or vec4
50  template<typename U>
51  Basic2DVector(Vec2<U> const& iv) :
52  theX(iv[0]), theY(iv[1]) {}
53  template<typename U>
54  Basic2DVector(Vec4<U> const& iv) :
55  theX(iv.arr[0]), theY(iv.arr[1]) {}
56 #elif defined(USE_SSEVECT)
57  // constructor from Vec2 or vec4
58  template<typename U>
60  theX(iv.arr[0]), theY(iv.arr[1]) {}
61  template<typename U>
63  theX(iv.arr[0]), theY(iv.arr[1]) {}
64 #endif
65 
66  T operator[](int i) const { return i==0 ? theX : theY ;}
67  T & operator[](int i) { return i==0 ? theX : theY ;}
68 
70  T x() const { return theX;}
71 
73  T y() const { return theY;}
74 
76  T mag2() const { return theX*theX + theY*theY;}
77 
79  T mag() const { return std::sqrt( mag2());}
80 
82  T r() const { return mag();}
83 
88  T barePhi() const {return std::atan2(theY,theX);}
89  Geom::Phi<T> phi() const {return Geom::Phi<T>(atan2(theY,theX));}
90 
94  Basic2DVector unit() const {
95  T my_mag = mag();
96  return my_mag == 0 ? *this : *this / my_mag;
97  }
98 
101  template <class U>
103  theX += p.x();
104  theY += p.y();
105  return *this;
106  }
107 
110  template <class U>
112  theX -= p.x();
113  theY -= p.y();
114  return *this;
115  }
116 
118  Basic2DVector operator-() const { return Basic2DVector(-x(),-y());}
119 
122  theX *= t;
123  theY *= t;
124  return *this;
125  }
126 
129  theX /= t;
130  theY /= t;
131  return *this;
132  }
133 
135  T dot( const Basic2DVector& v) const { return x()*v.x() + y()*v.y();}
136 
142  template <class U>
144  return x()*v.x() + y()*v.y();
145  }
146 
147 
148 
150  T cross( const Basic2DVector& v) const { return x()*v.y() - y()*v.x();}
151 
157  template <class U>
159  return x()*v.y() - y()*v.x();
160  }
161 
162 
163 private:
166 };
167 
168 
169 namespace geometryDetails {
170  std::ostream & print2D(std::ostream& s, double x, double y);
171 
172 }
173 
175 template <class T>
176 inline std::ostream & operator<<( std::ostream& s, const Basic2DVector<T>& v) {
177  return geometryDetails::print2D(s, v.x(),v.y());
178 }
179 
180 
182 template <class T, class U>
186  return RT(a.x()+b.x(), a.y()+b.y());
187 }
188 
189 template <class T, class U>
193  return RT(a.x()-b.x(), a.y()-b.y());
194 }
195 
196 
197 
198 
199 // scalar product of vectors of same precision
200 template <class T>
201 inline T operator*( const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
202  return v1.dot(v2);
203 }
204 
206 template <class T, class U>
208  const Basic2DVector<U>& v2) {
209  return v1.x()*v2.x() + v1.y()*v2.y();
210 }
211 
215 template <class T, class Scalar>
217  T t = static_cast<T>(s);
218  return Basic2DVector<T>(v.x()*t, v.y()*t);
219 }
220 
222 template <class T, class Scalar>
224  T t = static_cast<T>(s);
225  return Basic2DVector<T>(v.x()*t, v.y()*t);
226 }
227 
231 template <class T, class Scalar>
233  T t = static_cast<T>(s);
234  return Basic2DVector<T>(v.x()/t, v.y()/t);
235 }
236 
237 
240 
241 #endif // GeometryVector_Basic2DVector_h
T dot(const Basic2DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:66
Basic2DVector(const T &x, const T &y)
construct from cartesian coordinates
int32_t *__restrict__ iv
double Scalar
Definition: Definitions.h:25
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Basic2DVector< typename PreciseFloatType< T, U >::Type > operator-(const Basic2DVector< T > &a, const Basic2DVector< U > &b)
Basic2DVector(const Basic2DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
PreciseFloatType< T, U >::Type dot(const Basic2DVector< U > &v) const
T y() const
Cartesian y coordinate.
Basic2DVector(const Other &p)
Basic2DVector< typename PreciseFloatType< T, U >::Type > operator+(const Basic2DVector< T > &a, const Basic2DVector< U > &b)
vector sum and subtraction of vectors of possibly different precision
Geom::Phi< T > phi() const
Geom::Polar2Cartesian< T > Polar
T sqrt(T t)
Definition: SSEVec.h:19
Basic2DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
Basic2DVector & operator/=(T t)
Scaling by a scalar value (division)
std::ostream & print2D(std::ostream &s, double x, double y)
Definition: print.cc:8
Basic2DVector unit() const
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:64
Basic2DVector & operator+=(const Basic2DVector< U > &p)
Basic2DVector operator-() const
Unary minus, returns a vector with components (-x(),-y(),-z())
T r() const
Radius, same as mag()
T & operator[](int i)
Basic2DVector< float > Basic2DVectorF
Basic2DVector & operator-=(const Basic2DVector< U > &p)
double b
Definition: hdecay.h:118
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Basic2DVector< double > Basic2DVectorD
Basic2DVector< T > operator/(const Basic2DVector< T > &v, const Scalar &s)
T barePhi() const
double a
Definition: hdecay.h:119
T operator*(const Basic2DVector< T > &v1, const Basic2DVector< T > &v2)
Basic2DVector< T > MathVector
float x
long double T
T cross(const Basic2DVector &v) const
Vector product, or "cross" product, with a vector of same type.
PreciseFloatType< T, U >::Type cross(const Basic2DVector< U > &v) const
Definition: Phi.h:52
T x() const
Cartesian x coordinate.
T operator[](int i) const