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 
30  Basic2DVector& operator=(const Basic2DVector&) = default;
31 
32  template <typename U>
33  Basic2DVector(const Basic2DVector<U>& p) : v{T(p.v[0]), T(p.v[1])} {}
34 
42  template <class Other>
43  explicit Basic2DVector(const Other& p) : v{T(p.x()), T(p.y())} {}
44 
46  Basic2DVector(const T& x, const T& y) : v{x, y} {}
47 
48  // constructor from Vec2 or vec4
49  Basic2DVector(MathVector const& iv) : v(iv) {}
50  template <typename U>
51  Basic2DVector(Vec2<U> const& iv) : v{iv[0], iv[1]} {}
52  template <typename U>
53  Basic2DVector(Vec4<U> const& iv) : v{iv[0], iv[1]} {}
54 
55  MathVector const& mathVector() const { return v; }
56  MathVector& mathVector() { return v; }
57 
58  T operator[](int i) const { return v[i]; }
59 
61  T x() const { return v[0]; }
62 
64  T y() const { return v[1]; }
65 
67  T mag2() const { return ::dot(v, v); }
68 
70  T mag() const { return std::sqrt(mag2()); }
71 
73  T r() const { return mag(); }
74 
79  T barePhi() const { return std::atan2(y(), x()); }
80  Geom::Phi<T> phi() const { return Geom::Phi<T>(atan2(y(), x())); }
81 
85  Basic2DVector unit() const {
86  T my_mag = mag();
87  return my_mag == 0 ? *this : *this / my_mag;
88  }
89 
92  template <class U>
94  v = v + p.v;
95  return *this;
96  }
97 
100  template <class U>
102  v = v - p.v;
103  return *this;
104  }
105 
107  Basic2DVector operator-() const { return Basic2DVector(-v); }
108 
111  v = v * t;
112  return *this;
113  }
114 
117  t = T(1) / t;
118  v = v * t;
119  return *this;
120  }
121 
123  T dot(const Basic2DVector& lh) const { return ::dot(v, lh.v); }
124 
130  template <class U>
134  }
135 
137  T cross(const Basic2DVector& lh) const { return ::cross2(v, lh.v); }
138 
144  template <class U>
148  }
149 
150 public:
152 };
153 
154 namespace geometryDetails {
155  std::ostream& print2D(std::ostream& s, double x, double y);
156 
157 }
158 
160 template <class T>
161 inline std::ostream& operator<<(std::ostream& s, const Basic2DVector<T>& v) {
162  return geometryDetails::print2D(s, v.x(), v.y());
163 }
164 
166 template <class T>
168  return a.v + b.v;
169 }
170 template <class T>
172  return a.v - b.v;
173 }
174 
175 template <class T, class U>
177  const Basic2DVector<U>& b) {
179  return RT(a) + RT(b);
180 }
181 
182 template <class T, class U>
184  const Basic2DVector<U>& b) {
186  return RT(a) - RT(b);
187 }
188 
189 // scalar product of vectors of same precision
190 template <class T>
191 inline T operator*(const Basic2DVector<T>& v1, const Basic2DVector<T>& v2) {
192  return v1.dot(v2);
193 }
194 
196 template <class T, class U>
198  return v1.dot(v2);
199 }
200 
204 template <class T>
206  return v.v * t;
207 }
208 
210 template <class T>
212  return v.v * t;
213 }
214 
215 template <class T, class Scalar>
217  T t = static_cast<T>(s);
218  return v * t;
219 }
220 
222 template <class T, class Scalar>
224  T t = static_cast<T>(s);
225  return v * t;
226 }
227 
231 template <class T>
233  return v.v / t;
234 }
235 
236 template <class T, class Scalar>
238  // T t = static_cast<T>(Scalar(1)/s); return v*t;
239  T t = static_cast<T>(s);
240  return v / t;
241 }
242 
245 
246 #endif // GeometryVector_Basic2DVector_h
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:62
Basic2DVector(const T &x, const T &y)
construct from cartesian coordinates
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:111
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.
Basic2DVector & operator=(const Basic2DVector &)=default
Assignment operator.
bool int lh
Definition: SIMDVec.h:27
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:23
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()
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