CMS 3D CMS Logo

extBasic2DVector.h
Go to the documentation of this file.
1 #ifndef GeometryVector_newBasic2DVector_h
2 #define GeometryVector_newBasic2DVector_h
3 
8 
9 #include <cmath>
10 #include <iosfwd>
11 
12 template <class T>
14 public:
15  typedef T ScalarType;
19 
24  Basic2DVector() : v{0, 0} {}
25 
27  Basic2DVector(const Basic2DVector& p) : v(p.v) {}
28 
29  template <typename U>
30  Basic2DVector(const Basic2DVector<U>& p) : v{T(p.v[0]), T(p.v[1])} {}
31 
39  template <class Other>
40  explicit Basic2DVector(const Other& p) : v{T(p.x()), T(p.y())} {}
41 
43  Basic2DVector(const T& x, const T& y) : v{x, y} {}
44 
45  // constructor from Vec2 or vec4
46  Basic2DVector(MathVector const& iv) : v(iv) {}
47  template <typename U>
48  Basic2DVector(Vec2<U> const& iv) : v{iv[0], iv[1]} {}
49  template <typename U>
50  Basic2DVector(Vec4<U> const& iv) : v{iv[0], iv[1]} {}
51 
52  MathVector const& mathVector() const { return v; }
53  MathVector& mathVector() { return v; }
54 
55  T operator[](int i) const { return v[i]; }
56  T& operator[](int i) { return v[i]; }
57 
59  T x() const { return v[0]; }
60 
62  T y() const { return v[1]; }
63 
65  T mag2() const { return ::dot(v, v); }
66 
68  T mag() const { return std::sqrt(mag2()); }
69 
71  T r() const { return mag(); }
72 
77  T barePhi() const { return std::atan2(y(), x()); }
78  Geom::Phi<T> phi() const { return Geom::Phi<T>(atan2(y(), x())); }
79 
83  Basic2DVector unit() const {
84  T my_mag = mag();
85  return my_mag == 0 ? *this : *this / my_mag;
86  }
87 
90  template <class U>
92  v = v + p.v;
93  return *this;
94  }
95 
98  template <class U>
100  v = v - p.v;
101  return *this;
102  }
103 
105  Basic2DVector operator-() const { return Basic2DVector(-v); }
106 
109  v = v * t;
110  return *this;
111  }
112 
115  t = T(1) / t;
116  v = v * t;
117  return *this;
118  }
119 
121  T dot(const Basic2DVector& lh) const { return ::dot(v, lh.v); }
122 
128  template <class U>
132  }
133 
135  T cross(const Basic2DVector& lh) const { return ::cross2(v, lh.v); }
136 
142  template <class U>
146  }
147 
148 public:
150 };
151 
152 namespace geometryDetails {
153  std::ostream& print2D(std::ostream& s, double x, double y);
154 
155 }
156 
158 template <class T>
159 inline std::ostream& operator<<(std::ostream& s, const Basic2DVector<T>& v) {
160  return geometryDetails::print2D(s, v.x(), v.y());
161 }
162 
164 template <class T>
166  return a.v + b.v;
167 }
168 template <class T>
170  return a.v - b.v;
171 }
172 
173 template <class T, class U>
175  const Basic2DVector<U>& b) {
177  return RT(a) + RT(b);
178 }
179 
180 template <class T, class U>
182  const Basic2DVector<U>& b) {
184  return RT(a) - RT(b);
185 }
186 
187 // scalar product of vectors of same precision
188 template <class T>
189 inline T operator*(const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
190  return v1.dot(v2);
191 }
192 
194 template <class T, class U>
196  return v1.dot(v2);
197 }
198 
202 template <class T>
204  return v.v * t;
205 }
206 
208 template <class T>
210  return v.v * t;
211 }
212 
213 template <class T, class Scalar>
215  T t = static_cast<T>(s);
216  return v * t;
217 }
218 
220 template <class T, class Scalar>
222  T t = static_cast<T>(s);
223  return v * t;
224 }
225 
229 template <class T>
231  return v.v / t;
232 }
233 
234 template <class T, class Scalar>
236  // T t = static_cast<T>(Scalar(1)/s); return v*t;
237  T t = static_cast<T>(s);
238  return v / t;
239 }
240 
243 
244 #endif // GeometryVector_Basic2DVector_h
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:62
Basic2DVector(const T &x, const T &y)
construct from cartesian coordinates
int32_t *__restrict__ iv
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:98
double Scalar
Definition: Definitions.h:25
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Basic2DVector(Vec2< U > const &iv)
MathVector const & mathVector() const
Basic2DVector(const Basic2DVector &p)
Copy constructor from same type. Should not be needed but for gcc bug 12685.
Vec2< T > MathVector
Basic2DVector(Vec4< U > const &iv)
Basic2DVector< T > operator+(const Basic2DVector< T > &a, const Basic2DVector< T > &b)
vector sum and subtraction of vectors of possibly different precision
T y() const
Cartesian y coordinate.
bool int lh
Definition: SIMDVec.h:20
Basic2DVector(const Basic2DVector< U > &p)
Basic2DVector(const Other &p)
Basic2DVector< double > Basic2DVectorD
T dot(const Basic3DVector &v) const
Scalar product, or "dot" product, with a vector of same type.
Basic2DVector< T > operator-(const Basic2DVector< T > &a, const Basic2DVector< T > &b)
Basic2DVector< float > Basic2DVectorF
Geom::Phi< T > phi() const
Geom::Polar2Cartesian< T > Polar
T sqrt(T t)
Definition: SSEVec.h:19
T operator*(const Basic2DVector< T > &v1, const Basic2DVector< T > &v2)
T cross(const Basic2DVector &lh) const
Vector product, or "cross" product, with a vector of same type.
Basic2DVector & operator*=(T t)
Scaling by a scalar value (multiplication)
PreciseFloatType< T, U >::Type cross(const Basic2DVector< U > &lh) const
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(MathVector const &iv)
Basic2DVector unit() const
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:60
Basic2DVector & operator+=(const Basic2DVector< U > &p)
Basic2DVector< T > operator/(const Basic2DVector< T > &v, T t)
MathVector & mathVector()
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 & operator-=(const Basic2DVector< U > &p)
T dot(const Basic2DVector &lh) const
Scalar product, or "dot" product, with a vector of same type.
double b
Definition: hdecay.h:120
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
T barePhi() const
double a
Definition: hdecay.h:121
float x
Vec2< T > VectorType
long double T
Definition: Phi.h:52
T x() const
Cartesian x coordinate.
PreciseFloatType< T, U >::Type dot(const Basic2DVector< U > &lh) const
T operator[](int i) const