CVB++ 15.0
angle.hpp
1#pragma once
2
3#include <cmath>
4
5#include "global.hpp"
6
7namespace Cvb
8{
9
10 CVB_BEGIN_INLINE_NS
11
13
15 class Angle final
16 {
17 public:
19
25 static Angle FromDegrees(double deg, bool trim = false) noexcept
26 {
27 Angle res(trim);
28 res.SetDeg(deg);
29 return res;
30 }
31
33
39 static Angle FromRadians(double rad, bool trim = false) noexcept
40 {
41 Angle res(trim);
42 res.SetRad(rad);
43 return res;
44 }
45
47
51 Angle() noexcept = default;
52
54
58 explicit Angle(bool trim) noexcept
59 : trim_(trim)
60 {
61 }
62
64
68 double Rad() const noexcept
69 {
70 return rad_;
71 }
72
74
78 void SetRad(double rad) noexcept
79 {
80 rad_ = rad;
81 Trim();
82 }
83
85
89 double Deg() const noexcept
90 {
91 return rad_ * 180.0 / CVB_M_PI;
92 }
93
95
99 void SetDeg(double deg) noexcept
100 {
101 rad_ = deg * CVB_M_PI / 180.0;
102 Trim();
103 }
104
106
110 bool IsTrimmed() const noexcept
111 {
112 return trim_;
113 }
114
116
120 void SetIsTrimmed(bool trim) noexcept
121 {
122 trim_ = trim;
123 Trim();
124 }
125
127
132 bool operator==(const Angle &angle) const noexcept
133 {
134 return rad_ == angle.rad_;
135 }
136
138
143 bool operator!=(const Angle &angle) const noexcept
144 {
145 return !(*this == angle);
146 }
147
149
154 bool operator<(const Angle &angle) const noexcept
155 {
156 return rad_ < angle.rad_;
157 }
158
160
165 bool operator<=(const Angle &angle) const noexcept
166 {
167 return (*this == angle) || (*this < angle);
168 }
169
171
176 bool operator>(const Angle &angle) const noexcept
177 {
178 return (*this != angle) && !(*this < angle);
179 }
180
182
187 bool operator>=(const Angle &angle) const noexcept
188 {
189 return !(*this < angle);
190 }
191
193
198 Angle &operator+=(const Angle &angle) noexcept
199 {
200 rad_ += angle.rad_;
201 trim_ &= angle.trim_;
202 return *this;
203 }
204
206
211 Angle &operator-=(const Angle &angle) noexcept
212 {
213 rad_ -= angle.rad_;
214 trim_ &= angle.trim_;
215 return *this;
216 }
217
219
224 Angle &operator*=(const double &value) noexcept
225 {
226 rad_ *= value;
227 return *this;
228 }
229
231
236 Angle &operator/=(const double &value) noexcept
237 {
238 rad_ /= value;
239 return *this;
240 }
241
242 private:
243 void Trim()
244 {
245 if (!trim_)
246 return;
247
248 while (rad_ > CVB_M_PI)
249 rad_ -= CVB_M_PI * 2;
250 while (rad_ <= -CVB_M_PI)
251 rad_ += CVB_M_PI * 2;
252 }
253
254 double rad_ = 0.0;
255
256 bool trim_ = false;
257 };
258
260
268 inline Angle operator+(const Angle &lhs, const Angle &rhs)
269 {
270 return Angle::FromRadians(lhs.Rad() + rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
271 }
272
274
282 inline Angle operator-(const Angle &lhs, const Angle &rhs)
283 {
284 return Angle::FromRadians(lhs.Rad() - rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
285 }
286
288
296 inline Angle operator/(const Angle &lhs, const double &rhs)
297 {
298 return Angle::FromRadians(lhs.Rad() / rhs, lhs.IsTrimmed());
299 }
300
302
310 inline Angle operator*(const Angle &lhs, const double &rhs)
311 {
312 return Angle::FromRadians(lhs.Rad() * rhs, lhs.IsTrimmed());
313 }
314
316
324 inline Angle operator*(const double &lhs, const Angle &rhs)
325 {
326 return rhs * lhs;
327 }
328
330
337 inline Angle Abs(Angle angle) noexcept
338 {
339 return Angle::FromRadians(std::abs(angle.Rad()), angle.IsTrimmed());
340 }
341
343
350 inline Angle Acos(double d) noexcept
351 {
352 return Angle::FromRadians(acos(d), false);
353 }
354
356
363 inline Angle Asin(double d) noexcept
364 {
365 return Angle::FromRadians(asin(d), false);
366 }
367
369
376 inline Angle Atan(double d) noexcept
377 {
378 return Angle::FromRadians(atan(d), false);
379 }
380
382
390 inline Angle Atan2(double y, double x) noexcept
391 {
392 return Angle::FromRadians(atan2(y, x), false);
393 }
394
396
403 inline double Cos(Angle angle) noexcept
404 {
405 return cos(angle.Rad());
406 }
407
409
416 inline double Cosh(Angle angle) noexcept
417 {
418 return cosh(angle.Rad());
419 }
420
422
429 inline double Sin(Angle angle) noexcept
430 {
431 return sin(angle.Rad());
432 }
433
435
442 inline double Sinh(Angle angle) noexcept
443 {
444 return sinh(angle.Rad());
445 }
446
448
455 inline double Tan(Angle angle) noexcept
456 {
457 return tan(angle.Rad());
458 }
459
461
468 inline double Tanh(Angle angle) noexcept
469 {
470 return tanh(angle.Rad());
471 }
472
474
481 inline int Sign(Angle angle) noexcept
482 {
483 return ((0.0 < angle.Rad()) - (angle.Rad() < 0.0));
484 }
485
487
495 inline Angle Max(Angle a, Angle b) noexcept
496 {
497 if (b > a)
498 return b;
499 else
500 return a;
501 }
502
504
512 inline Angle Min(Angle a, Angle b) noexcept
513 {
514 if (b < a)
515 return b;
516 else
517 return a;
518 }
519
520 CVB_END_INLINE_NS
521
522} // namespace Cvb
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition angle.hpp:39
void SetRad(double rad) noexcept
Set the value in radians.
Definition angle.hpp:78
double Rad() const noexcept
Get the value in radians.
Definition angle.hpp:68
bool operator>(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:176
Angle & operator-=(const Angle &angle) noexcept
Subtracts and assigns to this angle.
Definition angle.hpp:211
Angle operator-(const Angle &lhs, const Angle &rhs)
Subtract two angles.
Definition angle.hpp:282
double Cosh(Angle angle) noexcept
Returns the hyperbolic cosine of an angle.
Definition angle.hpp:416
Angle & operator/=(const double &value) noexcept
Divides this angle by the given value.
Definition angle.hpp:236
bool operator<=(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:165
double Deg() const noexcept
Get the value in degrees.
Definition angle.hpp:89
bool operator!=(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:143
double Tanh(Angle angle) noexcept
Returns the hyperbolic tangent of an angle.
Definition angle.hpp:468
bool operator<(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:154
void SetIsTrimmed(bool trim) noexcept
Set trimming of the value of the angle to the range -PI...PI.
Definition angle.hpp:120
bool operator==(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:132
Angle operator*(const double &lhs, const Angle &rhs)
Multiplies value with an angle.
Definition angle.hpp:324
double Sinh(Angle angle) noexcept
Returns the hyperbolic sine of an angle.
Definition angle.hpp:442
Angle & operator+=(const Angle &angle) noexcept
Adds and assigns to this angle.
Definition angle.hpp:198
Angle() noexcept=default
Generate a 0° angle.
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition angle.hpp:429
Angle operator*(const Angle &lhs, const double &rhs)
Multiplies an angle with a value.
Definition angle.hpp:310
bool IsTrimmed() const noexcept
Get trimming of the angle's value to the range -PI...PI.
Definition angle.hpp:110
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition angle.hpp:390
double Tan(Angle angle) noexcept
Returns the tangent of an angle.
Definition angle.hpp:455
Angle Abs(Angle angle) noexcept
Absolute value of an angle.
Definition angle.hpp:337
Angle operator+(const Angle &lhs, const Angle &rhs)
Add two angles.
Definition angle.hpp:268
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition angle.hpp:25
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition angle.hpp:403
Angle Acos(double d) noexcept
Returns the angle whose cosine is the specified number.
Definition angle.hpp:350
Angle Asin(double d) noexcept
Returns the angle whose sine is the specified number.
Definition angle.hpp:363
bool operator>=(const Angle &angle) const noexcept
Compares to an other angle.
Definition angle.hpp:187
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition angle.hpp:495
int Sign(Angle angle) noexcept
Returns a value indicating the sign of an Angle.
Definition angle.hpp:481
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition angle.hpp:512
Angle Atan(double d) noexcept
Returns the angle whose tangent is the specified number.
Definition angle.hpp:376
void SetDeg(double deg) noexcept
Set the value in degrees.
Definition angle.hpp:99
Angle & operator*=(const double &value) noexcept
Multiplies this angle with the given value.
Definition angle.hpp:224
Angle operator/(const Angle &lhs, const double &rhs)
Divides an angle by a value.
Definition angle.hpp:296
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17