Vector2.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/operators.hpp>
4 
5 #include "TVector2.h"
6 
7 #include "boca/generic/Debug.hh"
9 
10 #include "boca/units/Units.hh"
11 #include "boca/math/Math.hh"
12 
13 namespace boca
14 {
15 
21 enum class Dim2
22 {
23  x,
24  y,
25  last
26 };
27 
28 std::string Name(Dim2 dim_2);
29 
30 std::vector<Dim2> Dimensions2();
31 
36 template<typename Value_>
37 class Vector2 : boost::totally_ordered<Vector2<Value_>>
38  , boost::additive<Vector2<Value_>>
39 {
40 
41  template<typename Value_2>
43 
44  template<typename Value_2>
45  using OnlyIfNotOrSameQuantity = typename std::enable_if < !IsQuantity<Value_2>::value || std::is_same<Value_, Value_2>::value >::type;
46 
47 public:
48 
57  constexpr Vector2() :
58  x_(0) ,
59  y_(0)
60  {}
61 
65  constexpr Vector2(Value_ x, Value_ y) :
66  x_(x) ,
67  y_(y)
68  {}
69 
73  Vector2(Value_ value, Dim2 dim)
74  {
75  x_ = dim == Dim2::x ? value : Value_(0);
76  y_ = dim == Dim2::y ? value : Value_(0);
77  }
78 
82  template<typename Value_2>
83  constexpr Vector2(Vector2<Value_2> const &vector) :
84  x_(vector.X()),
85  y_(vector.Y())
86  {}
87 
91  constexpr Vector2(TVector2 const &vector) :
92  x_(vector.X()),
93  y_(vector.Y())
94  {}
95 
97 
106  void SetUniform(Value_ value)
107  {
108  x_ = value;
109  y_ = value;
110  }
111 
115  void SetMagPhi(Value_ magnitude, Angle const &phi)
116  {
117  SetUniform(abs(magnitude));
118  x_ *= static_cast<double>(cos(phi));
119  y_ *= static_cast<double>(sin(phi));
120  }
121 
125  void SetMag(Value_ mag)
126  {
127  auto const old = Mag();
128  if (old == Value_(0)) return;
129  (*this) *= static_cast<double>(mag / old);
130  }
131 
135  void SetPhi(boca::Angle const &phi)
136  {
137  SetMagPhi(Mag(), phi);
138  }
139 
141 
150  constexpr Value_ const& X() const
151  {
152  return x_;
153  }
154 
158  constexpr Value_ const& Y() const
159  {
160  return y_;
161  }
162 
166  Value_ &X()
167  {
168  return x_;
169  }
170 
174  Value_ &Y()
175  {
176  return y_;
177  }
178 
180 
189  constexpr auto Mag2() const
190  {
191  return sqr(x_) + sqr(y_);
192  }
193 
197  constexpr Value_ Mag() const
198  {
199  return sqrt(Mag2());
200  }
201 
203 
212  Angle Phi() const
213  {
214  return Angle(atan2(Y(), X()));
215  }
216 
220  template<typename Value_2>
221  Angle DeltaPhiTo(Vector2<Value_2> const &vector) const
222  {
223  return Restrict(Phi() - vector.Phi());
224  }
225 
227 
236  constexpr auto Unit() const
237  {
238  auto mag = Mag();
239  return mag > Value_(0) ? Vector2<double>{*this / mag} : Vector2<double>{};
240  }
241 
245  template <typename Value_2>
246  constexpr auto Project(Vector2<Value_2> const &vector) const
247  {
248  auto const unit = vector.Unit();
249  return (*this * unit) * unit;
250  }
251 
255  template <typename Value_2>
256  constexpr Vector2 Norm(Vector2<Value_2> const &vector) const
257  {
258  return *this - Project(vector);
259  }
260 
264  Vector2 &Rotate(Angle const &phi)
265  {
266  auto const cos = boost::units::cos(phi);
267  auto const sin = boost::units::sin(phi);
268  *this = {x_ *cos - y_ * sin, x_ *sin + y_ * cos};
269  return *this;
270  }
271 
275  Vector2 Rotate(Angle const &phi) const
276  {
277  auto vector = *this;
278  return vector.Rotate(phi);
279  }
280 
282 
291  template <typename Value_2>
292  constexpr ValueProduct<Value_2> Dot(Vector2<Value_2> const &vector) const
293  {
294  return x_ * vector.X() + y_ * vector.Y();
295  }
296 
300  template <typename Value_2>
301  Vector2 <ValueProduct<Value_2>> Scale(Value_2 const &scalar) const
302  {
303  return {x_ *scalar, y_ *scalar};
304  }
305 
309  template <typename Value_2>
310  constexpr ValueProduct<Value_2> SignedArea(Vector2<Value_2> const &vector) const
311  {
312  return x_ * vector.Y() - y_ * vector.X();
313  }
315 
324  template <typename Value_2/*, typename = OnlyIfQuantity<Value_2>*/>
326  {
327  x_ = Value_(vector.X());
328  y_ = Value_(vector.Y());
329  return *this;
330  }
331 
335  template <typename Value_2, typename = OnlyIfNotOrSameQuantity<Value_2>>
337  {
338  x_ += vector.x_;
339  y_ += vector.y_;
340  return *this;
341  }
342 
346  template <typename Value_2, typename = OnlyIfNotOrSameQuantity<Value_2>>
348  {
349  x_ -= vector.x_;
350  y_ -= vector.y_;
351  return *this;
352  }
353 
357  template < typename Value_2, typename = OnlyIfNotQuantity<Value_2> >
358  Vector2 &operator*=(Value_2 scalar)
359  {
360  x_ *= scalar;
361  y_ *= scalar;
362  return *this;
363  }
364 
368  template < typename Value_2, typename = OnlyIfNotQuantity<Value_2> >
369  Vector2 &operator/=(Value_2 scalar)
370  {
371  x_ /= scalar;
372  y_ /= scalar;
373  return *this;
374  }
375 
379  template<typename Value_2>
380  constexpr friend ValueProduct<Value_2> operator^(Vector2 const &vector_1, Vector2<Value_2> const &vector_2)
381  {
382  return vector_1.SignedArea(vector_2);
383  }
384 
388  template <typename Value_2>
389  constexpr friend auto operator/(Vector2 const &vector, Value_2 const &scalar)
390  {
391  return vector.Scale(1. / scalar);
392  }
393 
397  constexpr Vector2 operator-() const
398  {
399  return { -x_, -y_};
400  }
401 
405  constexpr bool operator<(Vector2 const &vector) const
406  {
407  return Mag2() < vector.Mag2();
408  }
409 
413  constexpr bool operator==(Vector2 const &vector) const
414  {
415  return vector.x_ == x_ && vector.y_ == y_;
416  }
417 
421  Value_ const &operator[](Dim2 dim_2) const
422  {
423  switch (dim_2) {
424  case Dim2::x :
425  return x_;
426  case Dim2::y :
427  return y_;
428  default :
429  Default("Matrix2", Name(dim_2));
430  return x_;
431  }
432  }
433 
437  Value_ &operator[](Dim2 dim_2)
438  {
439  return const_cast<Value_ &>(static_cast<Vector2<Value_> const &>(*this)[dim_2]);
440  }
441 
445  friend auto &operator<<(std::ostream &stream, Vector2<Value_> const &vector)
446  {
447  stream << Stream(vector.X()) << Stream(vector.Y());
448  return stream;
449  }
450 
452 
458  using Dimension = Dim2;
459 
464  {
465  return {this, Dim2::x};
466  }
467 
472  {
473  return {this, Dim2::last};
474  }
475 
480  {
481  return {this, Dim2::x};
482  }
483 
488  {
489  return {this, Dim2::last};
490  }
492 
493 private:
494 
495  Value_ x_;
496 
497  Value_ y_;
498 };
499 
500 template <typename>
501 struct IsVector2 : std::false_type {};
502 
503 template <typename Value_>
504 struct IsVector2<Vector2<Value_>> : std::true_type {};
505 
506 template<typename Value_>
507 using OnlyIfNotVector2 = typename std::enable_if < !IsVector2<Value_>::value >::type;
508 
512 template <class Value_, class Value_2>
513 auto operator*(Vector2<Value_> const &vector_1, const Vector2<Value_2> &vector_2)
514 {
515  return vector_1.Dot(vector_2);
516 }
517 
521 template < class Value_, class Value_2, typename = OnlyIfNotVector2<Value_2> >
522 auto operator*(Vector2<Value_> const &vector, Value_2 const &scalar)
523 {
524  return vector.Scale(scalar);
525 }
526 
530 template < class Value_, class Value_2, typename = OnlyIfNotVector2<Value_> >
531 auto operator*(Value_ const &scalar, Vector2<Value_2> const &vector)
532 {
533  return vector.Scale(scalar);
534 }
535 
536 }
537 
538 namespace boost{
539 
540 template<typename Value_>
541 struct range_const_iterator< boca::Vector2<Value_> > {
543 };
544 
545 template<typename Value_>
546 struct range_mutable_iterator< boca::Vector2<Value_> > {
548 };
549 
550 }
constexpr Vector2(TVector2 const &vector)
Constructor accepting a root::TVector2.
Definition: Vector2.hh:91
constexpr ValueProduct< Value_2 > SignedArea(Vector2< Value_2 > const &vector) const
Signed area.
Definition: Vector2.hh:310
Angle atan2(Value const &value_1, Value const &value_2)
Arctangent2 .
Definition: Units.hh:185
std::vector< Dim2 > Dimensions2()
Definition: Vector2.cpp:15
typename std::enable_if< !IsVector2< Value_ >::value >::type OnlyIfNotVector2
Definition: Vector2.hh:507
Boost provides free peer-reviewed portable C++ source libraries.
Definition: LorentzVectorBase.hh:726
constexpr ValueProduct< Value_2 > Dot(Vector2< Value_2 > const &vector) const
Dot product between two vectors.
Definition: Vector2.hh:292
Vector2 & operator=(Vector2< Value_2 > const &vector)
Assignment operator including casting.
Definition: Vector2.hh:325
Angle Phi() const
The azimuth defined in .
Definition: Vector2.hh:212
Vector2< ValueProduct< Value_2 > > Scale(Value_2 const &scalar) const
Scale this vector with a scalar.
Definition: Vector2.hh:301
boost::units::quantity< boost::units::si::plane_angle > Angle
Angle measured in radian.
Definition: Si.hh:35
const iterator
Definition: Iterator.hh:84
constexpr ConstIterator< boca::Vector2, Value_ > end() const
Const end.
Definition: Vector2.hh:471
void SetUniform(Value_ value)
Set both both entries according to the value.
Definition: Vector2.hh:106
std::string Stream(Value const &message, int width=20, bool right=false)
Definition: Debug.hh:48
constexpr auto operator*(LorentzVectorBase< Value > const &lorentz_vector_1, LorentzVectorBase< Value_2 > const &lorentz_vector_2)
Scalar product of lorentzvectors.
Definition: LorentzVectorBase.hh:701
Iterator< boca::Vector2, Value_ > end()
End.
Definition: Vector2.hh:487
auto sqr(Value const &value)
square of value
Definition: Math.hh:24
void SetPhi(boca::Angle const &phi)
Set azimuth keeping the magnitue constant.
Definition: Vector2.hh:135
void SetMag(Value_ mag)
Set the magnitude keeping constant.
Definition: Vector2.hh:125
boca::ConstIterator< boca::Vector2, Value_ > type
Definition: Vector2.hh:542
ValueSqrt< Value > sqrt(Value const &value)
Square Root.
Definition: Units.hh:160
constexpr ConstIterator< boca::Vector2, Value_ > begin() const
Const begin.
Definition: Vector2.hh:463
constexpr bool operator==(Vector2 const &vector) const
Equal comparison.
Definition: Vector2.hh:413
void Default(std::string const &variable, const Value value)
Definition: Debug.hh:165
Value_ const & operator[](Dim2 dim_2) const
Components by index.
Definition: Vector2.hh:421
constexpr friend auto operator/(Vector2 const &vector, Value_2 const &scalar)
Division by scalar.
Definition: Vector2.hh:389
Vector2 & operator+=(Vector2< Value_2 > const &vector)
Sum of two vectors.
Definition: Vector2.hh:336
Vector2 Rotate(Angle const &phi) const
Rotate this vector by .
Definition: Vector2.hh:275
Value_ & X()
Accessor for X.
Definition: Vector2.hh:166
constexpr auto Project(Vector2< Value_2 > const &vector) const
Projection onto the direction of vector.
Definition: Vector2.hh:246
typename boost::units::multiply_typeof_helper< Value, Value_2 >::type ValueProduct
Definition: Units.hh:134
Iterator< boca::Vector2, Value_ > begin()
Begin.
Definition: Vector2.hh:479
Boosted Collider Analysis.
Definition: Analysis.hh:15
constexpr Vector2 operator-() const
Unary minus.
Definition: Vector2.hh:397
Value abs(Value const &value)
Absolute value.
Definition: Units.hh:235
constexpr Vector2(Value_ x, Value_ y)
Constructor from two scalars.
Definition: Vector2.hh:65
constexpr auto Mag2() const
Square of the magnitude .
Definition: Vector2.hh:189
constexpr Vector2(Vector2< Value_2 > const &vector)
Constructor from a two-vector.
Definition: Vector2.hh:83
Vector2 & operator/=(Value_2 scalar)
Division by scalar.
Definition: Vector2.hh:369
Angle Restrict(Angle phi)
Restrict an angle to the interval .
Definition: Si.cpp:26
constexpr Value_ const & X() const
Getter for X.
Definition: Vector2.hh:150
constexpr Value_ const & Y() const
Getter for Y.
Definition: Vector2.hh:158
Angle DeltaPhiTo(Vector2< Value_2 > const &vector) const
Difference of azimuth defined in .
Definition: Vector2.hh:221
constexpr bool operator<(Vector2 const &vector) const
Less than comparison.
Definition: Vector2.hh:405
Iterator
Definition: Iterator.hh:23
Vector2 & operator-=(Vector2< Value_2 > const &vector)
Difference of two vectors.
Definition: Vector2.hh:347
boca::Iterator< boca::Vector2, Value_ > type
Definition: Vector2.hh:547
Vector2 & operator*=(Value_2 scalar)
Product with scalar.
Definition: Vector2.hh:358
Value_ & operator[](Dim2 dim_2)
Components by index.
Definition: Vector2.hh:437
std::string Name(Output output)
Definition: Base.cpp:23
constexpr auto Unit() const
Unit vector in the direction of this vector.
Definition: Vector2.hh:236
Vector2 & Rotate(Angle const &phi)
Rotate this vector by .
Definition: Vector2.hh:264
constexpr Vector2 Norm(Vector2< Value_2 > const &vector) const
Component normal to the vector.
Definition: Vector2.hh:256
constexpr Value_ Mag() const
Magnitude .
Definition: Vector2.hh:197
Definition: Vector2.hh:501
Two dimensional Vector.
Definition: PseudoJet.hh:23
Dim2
Two dimensionss.
Definition: Vector2.hh:21
constexpr Vector2()
Default constructor.
Definition: Vector2.hh:57
Vector2(Value_ value, Dim2 dim)
Constructor one scalar and its direction.
Definition: Vector2.hh:73
constexpr friend ValueProduct< Value_2 > operator^(Vector2 const &vector_1, Vector2< Value_2 > const &vector_2)
Coefficient of wedge product.
Definition: Vector2.hh:380
Value_ & Y()
Accessor for Y.
Definition: Vector2.hh:174
void SetMagPhi(Value_ magnitude, Angle const &phi)
Setter for the magnitude and angle.
Definition: Vector2.hh:115