Rivet API documentation

Rivet 4.1.3
Vector2.hh
1#ifndef RIVET_MATH_VECTOR2
2#define RIVET_MATH_VECTOR2
3
4#include "Rivet/Math/MathConstants.hh"
5#include "Rivet/Math/MathUtils.hh"
6#include "Rivet/Math/VectorN.hh"
7
8namespace Rivet {
9
10
11 class Vector2;
12 typedef Vector2 TwoVector;
13 typedef Vector2 V2;
14
15 //class Matrix2;
16
17 Vector2 multiply(const double, const Vector2&);
18 Vector2 multiply(const Vector2&, const double);
19 Vector2 add(const Vector2&, const Vector2&);
20 Vector2 operator*(const double, const Vector2&);
21 Vector2 operator*(const Vector2&, const double);
22 Vector2 operator/(const Vector2&, const double);
23 Vector2 operator+(const Vector2&, const Vector2&);
24 Vector2 operator-(const Vector2&, const Vector2&);
25
26
28 class Vector2 : public Vector<2> {
29
30 // friend class Matrix2;
31 friend Vector2 multiply(const double, const Vector2&);
32 friend Vector2 multiply(const Vector2&, const double);
33 friend Vector2 add(const Vector2&, const Vector2&);
34 friend Vector2 subtract(const Vector2&, const Vector2&);
35
36 public:
37
38 Vector2()
39 : Vector<2>() { }
40
41 template <typename V2TYPE>
42 Vector2(const V2TYPE& other) {
43 this->setX(other.x());
44 this->setY(other.y());
45 }
46
47 Vector2(const Vector<2>& other) {
48 this->setX(other.get(0));
49 this->setY(other.get(1));
50 }
51
52 Vector2(double x, double y) {
53 this->setX(x);
54 this->setY(y);
55 }
56
57 ~Vector2() { }
58
59
60 public:
61
62 static Vector2 mkX() {
63 return Vector2(1, 0);
64 }
65 static Vector2 mkY() {
66 return Vector2(0, 1);
67 }
68
69
70 public:
71
72 double x() const {
73 return get(0);
74 }
75 double y() const {
76 return get(1);
77 }
78 Vector2& setX(double x) {
79 set(0, x);
80 return *this;
81 }
82 Vector2& setY(double y) {
83 set(1, y);
84 return *this;
85 }
86
87
89 double dot(const Vector2& v) const {
90 return _vec.dot(v._vec);
91 }
92
94 double angle(const Vector2& v) const {
95 const double localDotOther = unit().dot(v.unit());
96 if (localDotOther > 1.0) return 0.0;
97 if (localDotOther < -1.0) return M_PI;
98 return acos(localDotOther);
99 }
100
101
103 Vector2 unitVec() const {
104 double md = mod();
105 if (fuzzyLessEquals(md, 0.0))
106 return Vector2();
107 else
108 return *this * 1.0 / md;
109 }
110
112 Vector2 unit() const {
113 return unitVec();
114 }
115
116
117 public:
118
119 Vector2& operator*=(const double a) {
120 _vec = multiply(a, *this)._vec;
121 return *this;
122 }
123
124 Vector2& operator/=(const double a) {
125 _vec = multiply(1.0 / a, *this)._vec;
126 return *this;
127 }
128
129 Vector2& operator+=(const Vector2& v) {
130 _vec = add(*this, v)._vec;
131 return *this;
132 }
133
134 Vector2& operator-=(const Vector2& v) {
135 _vec = subtract(*this, v)._vec;
136 return *this;
137 }
138
139 Vector2 operator-() const {
140 Vector2 rtn;
141 rtn._vec = -_vec;
142 return rtn;
143 }
144 };
145
146
147 inline double dot(const Vector2& a, const Vector2& b) {
148 return a.dot(b);
149 }
150
151 inline Vector2 multiply(const double a, const Vector2& v) {
152 Vector2 result;
153 result._vec = a * v._vec;
154 return result;
155 }
156
157 inline Vector2 multiply(const Vector2& v, const double a) {
158 return multiply(a, v);
159 }
160
161 inline Vector2 operator*(const double a, const Vector2& v) {
162 return multiply(a, v);
163 }
164
165 inline Vector2 operator*(const Vector2& v, const double a) {
166 return multiply(a, v);
167 }
168
169 inline Vector2 operator/(const Vector2& v, const double a) {
170 return multiply(1.0 / a, v);
171 }
172
173 inline Vector2 add(const Vector2& a, const Vector2& b) {
174 Vector2 result;
175 result._vec = a._vec + b._vec;
176 return result;
177 }
178
179 inline Vector2 subtract(const Vector2& a, const Vector2& b) {
180 Vector2 result;
181 result._vec = a._vec - b._vec;
182 return result;
183 }
184
185 inline Vector2 operator+(const Vector2& a, const Vector2& b) {
186 return add(a, b);
187 }
188
189 inline Vector2 operator-(const Vector2& a, const Vector2& b) {
190 return subtract(a, b);
191 }
192
194 inline double angle(const Vector2& a, const Vector2& b) {
195 return a.angle(b);
196 }
197
198
199}
200
201#endif
Two-dimensional specialisation of Vector.
Definition Vector2.hh:28
double dot(const Vector2 &v) const
Dot-product with another vector.
Definition Vector2.hh:89
Vector2 unit() const
Synonym for unitVec.
Definition Vector2.hh:112
Vector2 unitVec() const
Unit-normalized version of this vector.
Definition Vector2.hh:103
double angle(const Vector2 &v) const
Angle in radians to another vector.
Definition Vector2.hh:94
double mod() const
Definition VectorN.hh:99
Vector< N > & set(const size_t index, const double value)
Definition VectorN.hh:63
Vector< N > operator-() const
Definition VectorN.hh:106
Definition LHCbCommon.hh:9
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:242
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:248
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:194
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 >, bool > fuzzyLessEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two floating point numbers for <= with a degree of fuzziness.
Definition MathUtils.hh:103