Control Libraries 7.4.0
Loading...
Searching...
No Matches
Angle.hpp
1#pragma once
2
3#include <cmath>
4
5namespace state_representation::units {
6class Angle;
7
8inline namespace literals {
14constexpr Angle operator ""_rad(long double n);
15
21constexpr Angle operator ""_deg(long double n);
22}
23
24class Angle {
25private:
26 long double value;
27
28public:
33 constexpr Angle(long double n = 0.0);
34
39 constexpr Angle(const Angle& ang);
40
45 constexpr long double get_value() const;
46
52 constexpr Angle& operator=(long double n);
53
58 constexpr Angle& operator-();
59
65 constexpr Angle& operator+=(const Angle& rhs);
66
72 constexpr Angle operator+(const Angle& rhs) const;
73
79 constexpr Angle& operator-=(const Angle& rhs);
80
86 constexpr Angle operator-(const Angle& rhs) const;
87
93 constexpr Angle& operator*=(double lambda);
94
100 constexpr Angle operator*(double lambda) const;
101
107 constexpr Angle& operator/=(double lambda);
108
114 constexpr Angle operator/(double lambda) const;
115
121 constexpr bool operator==(const Angle& rhs) const;
122
128 constexpr bool operator!=(const Angle& rhs) const;
129
135 constexpr bool operator>(const Angle& rhs) const;
136
142 constexpr bool operator>=(const Angle& rhs) const;
143
149 constexpr bool operator<(const Angle& rhs) const;
150
156 constexpr bool operator<=(const Angle& rhs) const;
157
164 friend constexpr double operator/(const Angle& lhs, const Angle& rhs);
165
171 friend constexpr Angle operator*(double lambda, const Angle& rhs);
172
178 friend constexpr Angle literals::operator ""_rad(long double n);
179
185 friend constexpr Angle literals::operator ""_deg(long double n);
186};
187
188constexpr Angle::Angle(long double n) :
189 value(atan2(sin(n), cos(n))) {}
190
191constexpr Angle::Angle(const Angle& ang) :
192 value(ang.value) {}
193
194constexpr long double Angle::get_value() const {
195 return this->value;
196}
197
198constexpr Angle& Angle::operator=(long double n) {
199 this->value = atan2(sin(n), cos(n));
200 return (*this);
201}
202
204 this->value = -this->value;
205 return (*this);
206}
207
208constexpr Angle& Angle::operator+=(const Angle& rhs) {
209 double n = this->value + rhs.value;
210 this->value = atan2(sin(n), cos(n));
211 return (*this);
212}
213
214constexpr Angle Angle::operator+(const Angle& rhs) const {
215 Angle result(*this);
216 result += rhs;
217 return result;
218}
219
220constexpr Angle& Angle::operator-=(const Angle& rhs) {
221 double n = this->value - rhs.value;
222 this->value = atan2(sin(n), cos(n));
223 return (*this);
224}
225
226constexpr Angle Angle::operator-(const Angle& rhs) const {
227 Angle result(*this);
228 result -= rhs;
229 return result;
230}
231
232constexpr Angle& Angle::operator*=(double lambda) {
233 double n = this->value * lambda;
234 this->value = atan2(sin(n), cos(n));
235 return (*this);
236}
237
238constexpr Angle Angle::operator*(double lambda) const {
239 Angle result(*this);
240 result *= lambda;
241 return result;
242}
243
244constexpr Angle& Angle::operator/=(double lambda) {
245 double n = this->value / lambda;
246 this->value = atan2(sin(n), cos(n));
247 return (*this);
248}
249
250constexpr Angle Angle::operator/(double lambda) const {
251 Angle result(*this);
252 result /= lambda;
253 return result;
254}
255
256constexpr bool Angle::operator==(const Angle& rhs) const {
257 return (abs(this->value - rhs.value) < 1e-4);
258}
259
260constexpr bool Angle::operator!=(const Angle& rhs) const {
261 return !((*this) == rhs);
262}
263
264constexpr bool Angle::operator>(const Angle& rhs) const {
265 return ((this->value - rhs.value) > 1e-4);
266}
267
268constexpr bool Angle::operator>=(const Angle& rhs) const {
269 return (((*this) > rhs) or ((*this) == rhs));
270}
271
272constexpr bool Angle::operator<(const Angle& rhs) const {
273 return ((rhs.value - this->value) > 1e-4);
274}
275
276constexpr bool Angle::operator<=(const Angle& rhs) const {
277 return (((*this) < rhs) or ((*this) == rhs));
278}
279
280constexpr double operator/(const Angle& lhs, const Angle& rhs) {
281 return lhs.get_value() / rhs.get_value();
282}
283
284constexpr Angle operator*(double lambda, const Angle& rhs) {
285 return Angle(lambda * rhs.value);
286}
287
288inline namespace literals {
289constexpr Angle operator ""_rad(long double n) {
290 return Angle(n);
291}
292
293constexpr Angle operator ""_deg(long double n) {
294 return Angle(M_PI * n / 180);
295}
296}
297}
constexpr long double get_value() const
Getter of the value attribute.
Definition Angle.hpp:194
friend constexpr Angle operator*(double lambda, const Angle &rhs)
Overload the / operator with a scalar on the left side.
Definition Angle.hpp:284
constexpr Angle & operator=(long double n)
Overload the = operator.
Definition Angle.hpp:198
constexpr bool operator>(const Angle &rhs) const
Overload the > operator.
Definition Angle.hpp:264
constexpr Angle & operator-()
Overload the - operator.
Definition Angle.hpp:203
constexpr Angle(long double n=0.0)
Constructor with a value in radian in [-pi,pi].
Definition Angle.hpp:188
constexpr Angle & operator+=(const Angle &rhs)
Overload the += operator.
Definition Angle.hpp:208
constexpr bool operator!=(const Angle &rhs) const
Overload the != operator.
Definition Angle.hpp:260
constexpr Angle & operator/=(double lambda)
Overload the /= operator with a scalar.
Definition Angle.hpp:244
constexpr Angle operator+(const Angle &rhs) const
Overload the + operator.
Definition Angle.hpp:214
constexpr bool operator<(const Angle &rhs) const
Overload the < operator.
Definition Angle.hpp:272
constexpr bool operator>=(const Angle &rhs) const
Overload the > operator.
Definition Angle.hpp:268
constexpr bool operator==(const Angle &rhs) const
Overload the == operator.
Definition Angle.hpp:256
constexpr Angle & operator-=(const Angle &rhs)
Overload the -= operator.
Definition Angle.hpp:220
constexpr Angle & operator*=(double lambda)
Overload the *= operator with a scalar.
Definition Angle.hpp:232
friend constexpr double operator/(const Angle &lhs, const Angle &rhs)
Overload the / operator between two Angles.
Definition Angle.hpp:280
constexpr bool operator<=(const Angle &rhs) const
Overload the < operator.
Definition Angle.hpp:276