CVB++ 15.0
angle.hpp
1#pragma once
2
3
4#include <cmath>
5
6#include "global.hpp"
7
8
9namespace Cvb
10{
11
12CVB_BEGIN_INLINE_NS
13
14
16
18class Angle final
19{
20 public:
21
22
24
30 static Angle FromDegrees(double deg, bool trim = false) noexcept
31 {
32 Angle res(trim);
33 res.SetDeg(deg);
34 return res;
35 }
36
38
44 static Angle FromRadians(double rad, bool trim = false) noexcept
45 {
46 Angle res(trim);
47 res.SetRad(rad);
48 return res;
49 }
50
52
56 explicit Angle(bool trim = false) noexcept
57 : trim_(trim)
58 {
59 }
60
62
66 double Rad() const noexcept
67 {
68 return rad_;
69 }
70
72
76 void SetRad(double rad) noexcept
77 {
78 rad_ = rad;
79 Trim();
80 }
81
83
87 double Deg() const noexcept
88 {
89 return rad_ * 180.0 / CVB_M_PI;
90 }
91
93
97 void SetDeg(double deg) noexcept
98 {
99 rad_ = deg* CVB_M_PI / 180.0;
100 Trim();
101 }
102
103
105
109 bool IsTrimmed() const noexcept
110 {
111 return trim_;
112 }
113
115
119 void SetIsTrimmed(bool trim) noexcept
120 {
121 trim_ = trim;
122 Trim();
123 }
124
126
131 bool operator==(const Angle& angle) const noexcept
132 {
133 return rad_ == angle.rad_;
134 }
135
137
142 bool operator!=(const Angle & angle) const noexcept
143 {
144 return !(*this == angle);
145 }
146
148
153 bool operator<(const Angle & angle) const noexcept
154 {
155 return rad_ < angle.rad_;
156 }
157
159
164 bool operator <= (const Angle & angle) const noexcept
165 {
166 return (*this == angle) || (*this < angle);
167 }
168
170
175 bool operator>(const Angle & angle) const noexcept
176 {
177 return (*this != angle) && !(*this < angle);
178 }
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
243
244 private:
245
246 void Trim()
247 {
248 if (!trim_)
249 return;
250
251 while (rad_ > CVB_M_PI)
252 rad_ -= CVB_M_PI * 2;
253 while (rad_ <= -CVB_M_PI)
254 rad_ += CVB_M_PI * 2;
255 }
256
257 double rad_ = 0.0;
258
259 bool trim_ = false;
260};
261
262
263
265
273inline Angle operator+(const Angle & lhs, const Angle & rhs)
274{
275 return Angle::FromRadians(lhs.Rad() + rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
276}
277
279
287inline Angle operator-(const Angle & lhs, const Angle & rhs)
288{
289 return Angle::FromRadians(lhs.Rad() - rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
290}
291
293
301inline Angle operator/(const Angle & lhs, const double & rhs)
302{
303 return Angle::FromRadians(lhs.Rad() / rhs, lhs.IsTrimmed());
304}
305
307
315inline Angle operator*(const Angle & lhs, const double & rhs)
316{
317 return Angle::FromRadians(lhs.Rad() * rhs, lhs.IsTrimmed());
318}
319
321
329inline Angle operator*(const double & lhs, const Angle & rhs)
330{
331 return rhs * lhs;
332}
333
334
336
343inline Angle Abs(Angle angle) noexcept
344{
345 return Angle::FromRadians(std::abs(angle.Rad()), angle.IsTrimmed());
346}
347
349
356inline Angle Acos(double d) noexcept
357{
358 return Angle::FromRadians(acos(d), false);
359}
360
362
369inline Angle Asin(double d) noexcept
370{
371 return Angle::FromRadians(asin(d), false);
372}
373
375
382inline Angle Atan(double d) noexcept
383{
384 return Angle::FromRadians(atan(d), false);
385}
386
387
389
397inline Angle Atan2(double y, double x) noexcept
398{
399 return Angle::FromRadians(atan2(y, x), false);
400}
401
402
404
411inline double Cos(Angle angle) noexcept
412{
413 return cos(angle.Rad());
414}
415
417
424inline double Cosh(Angle angle) noexcept
425{
426 return cosh(angle.Rad());
427}
428
429
431
438inline double Sin(Angle angle) noexcept
439{
440 return sin(angle.Rad());
441}
442
444
451inline double Sinh(Angle angle) noexcept
452{
453 return sinh(angle.Rad());
454}
455
457
464inline double Tan(Angle angle) noexcept
465{
466 return tan(angle.Rad());
467}
468
470
477inline double Tanh(Angle angle) noexcept
478{
479 return tanh(angle.Rad());
480}
481
483
490inline int Sign(Angle angle) noexcept
491{
492 return ((0.0 < angle.Rad()) - (angle.Rad() < 0.0));
493}
494
496
504inline Angle Max(Angle a, Angle b) noexcept
505{
506 if (b > a)
507 return b;
508 else
509 return a;
510}
511
513
521inline Angle Min(Angle a, Angle b) noexcept
522{
523 if (b < a)
524 return b;
525 else
526 return a;
527}
528
529CVB_END_INLINE_NS
530
531
532
533}
Object for convenient and type - safe handling of angles.
Definition: angle.hpp:19
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition: angle.hpp:44
void SetRad(double rad) noexcept
Set the value in radians.
Definition: angle.hpp:76
double Rad() const noexcept
Get the value in radians.
Definition: angle.hpp:66
bool operator>(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:175
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:287
double Cosh(Angle angle) noexcept
Returns the hyperbolic cosine of an angle.
Definition: angle.hpp:424
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:164
double Deg() const noexcept
Get the value in degrees.
Definition: angle.hpp:87
bool operator!=(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:142
double Tanh(Angle angle) noexcept
Returns the hyperbolic tangent of an angle.
Definition: angle.hpp:477
bool operator<(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:153
void SetIsTrimmed(bool trim) noexcept
Set trimming of the value of the angle to the range -PI...PI.
Definition: angle.hpp:119
bool operator==(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:131
Angle operator*(const double &lhs, const Angle &rhs)
Multiplies value with an angle.
Definition: angle.hpp:329
double Sinh(Angle angle) noexcept
Returns the hyperbolic sine of an angle.
Definition: angle.hpp:451
Angle & operator+=(const Angle &angle) noexcept
Adds and assigns to this angle.
Definition: angle.hpp:198
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition: angle.hpp:438
Angle operator*(const Angle &lhs, const double &rhs)
Multiplies an angle with a value.
Definition: angle.hpp:315
bool IsTrimmed() const noexcept
Get trimming of the angle's value to the range -PI...PI.
Definition: angle.hpp:109
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition: angle.hpp:397
double Tan(Angle angle) noexcept
Returns the tangent of an angle.
Definition: angle.hpp:464
Angle Abs(Angle angle) noexcept
Absolute value of an angle.
Definition: angle.hpp:343
Angle operator+(const Angle &lhs, const Angle &rhs)
Add two angles.
Definition: angle.hpp:273
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition: angle.hpp:30
Angle(bool trim=false) noexcept
Generate a 0° angle.
Definition: angle.hpp:56
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition: angle.hpp:411
Angle Acos(double d) noexcept
Returns the angle whose cosine is the specified number.
Definition: angle.hpp:356
Angle Asin(double d) noexcept
Returns the angle whose sine is the specified number.
Definition: angle.hpp:369
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:504
int Sign(Angle angle) noexcept
Returns a value indicating the sign of an Angle.
Definition: angle.hpp:490
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition: angle.hpp:521
Angle Atan(double d) noexcept
Returns the angle whose tangent is the specified number.
Definition: angle.hpp:382
void SetDeg(double deg) noexcept
Set the value in degrees.
Definition: angle.hpp:97
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:301
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24