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 Angle() noexcept = default;
57
59
63 explicit Angle(bool trim) noexcept
64 : trim_(trim)
65 {
66 }
67
69
73 double Rad() const noexcept
74 {
75 return rad_;
76 }
77
79
83 void SetRad(double rad) noexcept
84 {
85 rad_ = rad;
86 Trim();
87 }
88
90
94 double Deg() const noexcept
95 {
96 return rad_ * 180.0 / CVB_M_PI;
97 }
98
100
104 void SetDeg(double deg) noexcept
105 {
106 rad_ = deg* CVB_M_PI / 180.0;
107 Trim();
108 }
109
110
112
116 bool IsTrimmed() const noexcept
117 {
118 return trim_;
119 }
120
122
126 void SetIsTrimmed(bool trim) noexcept
127 {
128 trim_ = trim;
129 Trim();
130 }
131
133
138 bool operator==(const Angle& angle) const noexcept
139 {
140 return rad_ == angle.rad_;
141 }
142
144
149 bool operator!=(const Angle & angle) const noexcept
150 {
151 return !(*this == angle);
152 }
153
155
160 bool operator<(const Angle & angle) const noexcept
161 {
162 return rad_ < angle.rad_;
163 }
164
166
171 bool operator <= (const Angle & angle) const noexcept
172 {
173 return (*this == angle) || (*this < angle);
174 }
175
177
182 bool operator>(const Angle & angle) const noexcept
183 {
184 return (*this != angle) && !(*this < angle);
185 }
186
187
189
194 bool operator>=(const Angle & angle) const noexcept
195 {
196 return !(*this < angle);
197 }
198
200
205 Angle& operator +=(const Angle & angle) noexcept
206 {
207 rad_ += angle.rad_;
208 trim_ &= angle.trim_;
209 return *this;
210 }
211
213
218 Angle& operator -=(const Angle & angle) noexcept
219 {
220 rad_ -= angle.rad_;
221 trim_ &= angle.trim_;
222 return *this;
223 }
224
226
231 Angle& operator *=(const double & value) noexcept
232 {
233 rad_ *= value;
234 return *this;
235 }
236
238
243 Angle& operator /=(const double & value) noexcept
244 {
245 rad_ /= value;
246 return *this;
247 }
248
249
250
251 private:
252
253 void Trim()
254 {
255 if (!trim_)
256 return;
257
258 while (rad_ > CVB_M_PI)
259 rad_ -= CVB_M_PI * 2;
260 while (rad_ <= -CVB_M_PI)
261 rad_ += CVB_M_PI * 2;
262 }
263
264 double rad_ = 0.0;
265
266 bool trim_ = false;
267};
268
269
270
272
280inline Angle operator+(const Angle & lhs, const Angle & rhs)
281{
282 return Angle::FromRadians(lhs.Rad() + rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
283}
284
286
294inline Angle operator-(const Angle & lhs, const Angle & rhs)
295{
296 return Angle::FromRadians(lhs.Rad() - rhs.Rad(), lhs.IsTrimmed() && rhs.IsTrimmed());
297}
298
300
308inline Angle operator/(const Angle & lhs, const double & rhs)
309{
310 return Angle::FromRadians(lhs.Rad() / rhs, lhs.IsTrimmed());
311}
312
314
322inline Angle operator*(const Angle & lhs, const double & rhs)
323{
324 return Angle::FromRadians(lhs.Rad() * rhs, lhs.IsTrimmed());
325}
326
328
336inline Angle operator*(const double & lhs, const Angle & rhs)
337{
338 return rhs * lhs;
339}
340
341
343
350inline Angle Abs(Angle angle) noexcept
351{
352 return Angle::FromRadians(std::abs(angle.Rad()), angle.IsTrimmed());
353}
354
356
363inline Angle Acos(double d) noexcept
364{
365 return Angle::FromRadians(acos(d), false);
366}
367
369
376inline Angle Asin(double d) noexcept
377{
378 return Angle::FromRadians(asin(d), false);
379}
380
382
389inline Angle Atan(double d) noexcept
390{
391 return Angle::FromRadians(atan(d), false);
392}
393
394
396
404inline Angle Atan2(double y, double x) noexcept
405{
406 return Angle::FromRadians(atan2(y, x), false);
407}
408
409
411
418inline double Cos(Angle angle) noexcept
419{
420 return cos(angle.Rad());
421}
422
424
431inline double Cosh(Angle angle) noexcept
432{
433 return cosh(angle.Rad());
434}
435
436
438
445inline double Sin(Angle angle) noexcept
446{
447 return sin(angle.Rad());
448}
449
451
458inline double Sinh(Angle angle) noexcept
459{
460 return sinh(angle.Rad());
461}
462
464
471inline double Tan(Angle angle) noexcept
472{
473 return tan(angle.Rad());
474}
475
477
484inline double Tanh(Angle angle) noexcept
485{
486 return tanh(angle.Rad());
487}
488
490
497inline int Sign(Angle angle) noexcept
498{
499 return ((0.0 < angle.Rad()) - (angle.Rad() < 0.0));
500}
501
503
511inline Angle Max(Angle a, Angle b) noexcept
512{
513 if (b > a)
514 return b;
515 else
516 return a;
517}
518
520
528inline Angle Min(Angle a, Angle b) noexcept
529{
530 if (b < a)
531 return b;
532 else
533 return a;
534}
535
536CVB_END_INLINE_NS
537
538
539
540}
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:83
double Rad() const noexcept
Get the value in radians.
Definition: angle.hpp:73
bool operator>(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:182
Angle & operator-=(const Angle &angle) noexcept
Subtracts and assigns to this angle.
Definition: angle.hpp:218
Angle operator-(const Angle &lhs, const Angle &rhs)
Subtract two angles.
Definition: angle.hpp:294
double Cosh(Angle angle) noexcept
Returns the hyperbolic cosine of an angle.
Definition: angle.hpp:431
Angle & operator/=(const double &value) noexcept
Divides this angle by the given value.
Definition: angle.hpp:243
bool operator<=(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:171
double Deg() const noexcept
Get the value in degrees.
Definition: angle.hpp:94
bool operator!=(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:149
double Tanh(Angle angle) noexcept
Returns the hyperbolic tangent of an angle.
Definition: angle.hpp:484
bool operator<(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:160
void SetIsTrimmed(bool trim) noexcept
Set trimming of the value of the angle to the range -PI...PI.
Definition: angle.hpp:126
bool operator==(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:138
Angle operator*(const double &lhs, const Angle &rhs)
Multiplies value with an angle.
Definition: angle.hpp:336
double Sinh(Angle angle) noexcept
Returns the hyperbolic sine of an angle.
Definition: angle.hpp:458
Angle & operator+=(const Angle &angle) noexcept
Adds and assigns to this angle.
Definition: angle.hpp:205
Angle() noexcept=default
Generate a 0° angle.
double Sin(Angle angle) noexcept
Returns the sine of an angle.
Definition: angle.hpp:445
Angle operator*(const Angle &lhs, const double &rhs)
Multiplies an angle with a value.
Definition: angle.hpp:322
bool IsTrimmed() const noexcept
Get trimming of the angle's value to the range -PI...PI.
Definition: angle.hpp:116
Angle Atan2(double y, double x) noexcept
Returns the angle whose tangent is the quotient of two specified numbers.
Definition: angle.hpp:404
double Tan(Angle angle) noexcept
Returns the tangent of an angle.
Definition: angle.hpp:471
Angle Abs(Angle angle) noexcept
Absolute value of an angle.
Definition: angle.hpp:350
Angle operator+(const Angle &lhs, const Angle &rhs)
Add two angles.
Definition: angle.hpp:280
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition: angle.hpp:30
double Cos(Angle angle) noexcept
Returns the cosine of an angle.
Definition: angle.hpp:418
Angle Acos(double d) noexcept
Returns the angle whose cosine is the specified number.
Definition: angle.hpp:363
Angle Asin(double d) noexcept
Returns the angle whose sine is the specified number.
Definition: angle.hpp:376
bool operator>=(const Angle &angle) const noexcept
Compares to an other angle.
Definition: angle.hpp:194
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition: angle.hpp:511
int Sign(Angle angle) noexcept
Returns a value indicating the sign of an Angle.
Definition: angle.hpp:497
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition: angle.hpp:528
Angle Atan(double d) noexcept
Returns the angle whose tangent is the specified number.
Definition: angle.hpp:389
void SetDeg(double deg) noexcept
Set the value in degrees.
Definition: angle.hpp:104
Angle & operator*=(const double &value) noexcept
Multiplies this angle with the given value.
Definition: angle.hpp:231
Angle operator/(const Angle &lhs, const double &rhs)
Divides an angle by a value.
Definition: angle.hpp:308
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15