NDEVR
API Documentation
Angle.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Angle
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/BufferBase.h>
35#include <cmath>
36
37namespace NDEVR
38{
49
62
67 template<class t_float_type>
68 static constexpr t_float_type PI()
69 {
70 return cast<t_float_type>(3.14159265358979323846264338327950288419716939937510);
71 }
72
81 template<class t_type = fltp08>
82 class Angle
83 {
84 public:
85
89 constexpr Angle()
90 : m_index_angle(0)
91 {}
92
96 constexpr Angle(const Angle& angle)
97 : m_index_angle(angle.m_index_angle)
98 {}
99
103 template<class t_other_type>
104 explicit constexpr Angle(const Angle<t_other_type>& angle)
105 : m_index_angle(cast<t_type>(angle.template internal<false>()))
106 {}
107
113 constexpr explicit Angle(AngleType type, fltp04 value)
114 : m_index_angle(ConvertToInternal(type, value))
115 {}
116
122 constexpr explicit Angle(AngleType type, fltp08 value)
123 : m_index_angle(ConvertToInternal(type, value))
124 {}
125
130 constexpr explicit Angle(t_type value)
131 : m_index_angle(value)
132 {}
133
140 template<AngleType t_angle_type>
141 [[nodiscard]] constexpr fltp08 as() const
142 {
143 switch (t_angle_type)
144 {
145 case INTERNAL_ANGLE:
146 if (IsInvalid(m_index_angle))
147 return Constant<fltp08>::Invalid;
148 else
150 case DEGREES:
151 return as<INTERNAL_ANGLE>() * INV_INDEX_PI * 180.0;
152 case RADIANS:
154 case PERCENT:
155 return as<INTERNAL_ANGLE>() * INV_INDEX_PI * 0.5;
156 }
157 return Constant<fltp08>::Invalid;
158 }
159
165 [[nodiscard]] constexpr fltp08 as(AngleType type) const
166 {
167 switch (type)
168 {
169 case DEGREES: return as<DEGREES>();
170 case RADIANS: return as<RADIANS>();
171 case PERCENT: return as<PERCENT>();
172 case INTERNAL_ANGLE: return as<INTERNAL_ANGLE>();
173 }
174 return Constant<fltp08>::Invalid;
175 }
176 public:
182 template<bool t_normalized>
183 [[nodiscard]] constexpr t_type internal() const
184 {
185 if constexpr(t_normalized)
186 {
187 return Angle<t_type>::template NormalizeInternal<t_type>(m_index_angle);
188 }
189 else
190 {
191 return m_index_angle;
192 }
193 }
194
197 template<class t_new_type>
199 {
200 Angle<t_new_type> angle(cast<t_new_type>(m_index_angle));
201 return angle;
202 }
203
206 [[nodiscard]] constexpr Angle normalized() const
207 {
208 return Angle(internal<true>());
209 }
210
213 [[nodiscard]] constexpr Angle normalizedOffset() const
214 {
215 Angle value = normalized();
216 if (value > Angle(DEGREES, 180.0))
217 value -= Angle(DEGREES, 360.0);
218 return value;
219 }
220
224 constexpr Angle& operator=(const Angle& angle)
225 {
226 m_index_angle = angle.m_index_angle;
227 return (*this);
228 }
229
235 constexpr bool operator==(const Angle& angle) const
236 {
237 return internal<false>() == angle.internal<false>();
238 }
239
245 constexpr bool operator!=(const Angle& angle) const
246 {
247 return internal<false>() != angle.internal<false>();
248 }
249
255 {
256 return Angle(-m_index_angle);
257 }
258 public:
259
265 template<class t_value_type>
266 static Angle acos(t_value_type value)
267 {
268 return Angle(RADIANS, ::acos(value));
269 }
270
276 template<class t_value_type>
277 static Angle asin(t_value_type value)
278 {
279 return Angle(RADIANS, ::asin(value));
280 }
281
287 template<class t_value_type>
288 static Angle atan(t_value_type value)
289 {
290 return Angle(RADIANS, ::atan(value));
291 }
292
299 template<class t_value_type>
300 static Angle atan2(t_value_type x, t_value_type y)
301 {
302 return Angle(RADIANS, ::atan2(x, y));
303 }
304 public:
305 static constexpr sint04 INDEX_PI = 32768;
306 static constexpr fltp08 INV_INDEX_PI = { 1.0 / Angle<t_type>::INDEX_PI };
307 private:
313 template<class t_normal_type>
314 static constexpr typename std::enable_if<!ObjectInfo<t_normal_type>::Float, t_normal_type>::type NormalizeInternal(t_normal_type value)
315 {
316 const t_normal_type angle = (value >= 0 ? value : -value) % (2 * INDEX_PI);
317 if (value >= 0)
318 return angle;
319 else
320 return (2 * INDEX_PI) - angle;
321 }
322
328 template<class t_normal_type>
329 static constexpr typename std::enable_if<ObjectInfo<t_normal_type>::Float, t_normal_type>::type NormalizeInternal(t_normal_type value)
330 {
331 const t_normal_type angle = cast<t_normal_type>(fmod(std::abs(value), (2 * INDEX_PI)));
332 if (value >= 0)
333 return angle;
334 else
335 return 2 * cast<t_normal_type>(INDEX_PI) - angle;
336 }
343 template<class t_value_type>
344 static constexpr t_type ConvertToInternal(AngleType type, t_value_type value)
345 {
346 switch (type)
347 {
348 case INTERNAL_ANGLE: return cast<t_type>(value);
349 case RADIANS: return cast<t_type>((value / PI<fltp08>()) * INDEX_PI);
350 case DEGREES: return cast<t_type>((value / cast<t_value_type>(180)) * INDEX_PI);
351 case PERCENT: return cast<t_type>(value * cast<t_value_type>(2) * INDEX_PI);
352 default: return cast<t_type>(0); // Default to 0 for unsupported types
353 }
354 }
355
356 private:
358 t_type m_index_angle;
359 };
360 template<> constexpr const Angle<sint04> Constant<Angle<sint04>>::Invalid = Angle<sint04>(Constant<sint04>::Invalid);
361 template<> constexpr const Angle<sint04> Constant<Angle<sint04>>::Max = Angle<sint04>(Constant<sint04>::Max);
362 template<> constexpr const Angle<sint04> Constant<Angle<sint04>>::Min = Angle<sint04>(Constant<sint04>::Min);
363
364 template<> constexpr const Angle<fltp08> Constant<Angle<fltp08>>::Invalid = Angle<fltp08>(Constant<fltp08>::Invalid);
365 template<> constexpr const Angle<fltp08> Constant<Angle<fltp08>>::Max = Angle<fltp08>(Constant<fltp08>::Max);
366 template<> constexpr const Angle<fltp08> Constant<Angle<fltp08>>::Min = Angle<fltp08>(Constant<fltp08>::Min);
367
368 template<> constexpr const Angle<fltp04> Constant<Angle<fltp04>>::Invalid = Angle<fltp04>(Constant<fltp04>::Invalid);
369 template<> constexpr const Angle<fltp04> Constant<Angle<fltp04>>::Max = Angle<fltp04>(Constant<fltp04>::Max);
370 template<> constexpr const Angle<fltp04> Constant<Angle<fltp04>>::Min = Angle<fltp04>(Constant<fltp04>::Min);
371
377 template<class t_type>
379 {
380 return Angle<t_type>(::sqrt(value.template internal<false>()));
381 }
382
387 template<class t_type>
388 static constexpr bool IsInvalid(const Angle<t_type>& value)
389 {
390 return IsInvalid(value.template internal<false>());
391 }
392
397 template<class t_type>
398 static constexpr bool IsValid(const Angle<t_type>& value)
399 {
400 return IsValid(value.template internal<false>());
401 }
402
407 template<class t_to, class t_from>
408 constexpr t_to cast(const Angle<t_from>& value)
409 {
410 return t_to(INTERNAL_ANGLE, cast<fltp08>(value.template internal<false>()));
411 }
412
415 template<>
416 struct ObjectInfo<Angle<sint04>, false, false>
417 {
418 static const uint01 Dimensions = 0;
419 static const bool Vector = false;
420 static const bool Primitive = true;
421 static const bool Pointer = false;
422 static const bool Unsigned = false;
423 static const bool Float = true;
424 static const bool Integer = false;
425 static const bool Number = true;
426 static const bool String = false;
427 static const bool Color = false;
428 static const bool Buffer = false;
429 static const bool Boolean = false;
434 static constexpr ObjectInfo<Angle<sint04>, false, false> VectorSub() { return ObjectInfo<Angle<sint04>, false, false>(); }
435 };
436
439 template<>
440 struct ObjectInfo<Angle<fltp08>, false, false>
441 {
442 static const uint01 Dimensions = 0;
443 static const bool Vector = false;
444 static const bool Primitive = true;
445 static const bool Pointer = false;
446 static const bool Unsigned = false;
447 static const bool Float = true;
448 static const bool Integer = false;
449 static const bool Number = true;
450 static const bool String = false;
451 static const bool Color = false;
452 static const bool Buffer = false;
453 static const bool Boolean = false;
458 static constexpr ObjectInfo<Angle<fltp08>, false, false> VectorSub() { return ObjectInfo<Angle<fltp08>, false, false>(); }
459 };
460
463 template<>
464 struct ObjectInfo<Angle<fltp04>, false, false>
465 {
466 static const uint01 Dimensions = 0;
467 static const bool Vector = false;
468 static const bool Primitive = true;
469 static const bool Pointer = false;
470 static const bool Unsigned = false;
471 static const bool Float = true;
472 static const bool Integer = false;
473 static const bool Number = true;
474 static const bool String = false;
475 static const bool Color = false;
476 static const bool Buffer = false;
477 static const bool Boolean = false;
482 static constexpr ObjectInfo<Angle<fltp04>, false, false> VectorSub() { return ObjectInfo<Angle<fltp04>, false, false>(); }
483 };
484};
485
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
constexpr Angle(AngleType type, fltp04 value)
Angle Constructor.
Definition Angle.h:113
constexpr Angle(const Angle &angle)
Default copy constructor.
Definition Angle.h:96
constexpr Angle(AngleType type, fltp08 value)
Angle Constructor.
Definition Angle.h:122
static Angle asin(t_value_type value)
Computes the principal value of the arc sine of the given value.
Definition Angle.h:277
constexpr Angle(const Angle< t_other_type > &angle)
Explicit conversion of an angle from one container type to another.
Definition Angle.h:104
constexpr bool operator!=(const Angle &angle) const
Inequality operator.
Definition Angle.h:245
static Angle atan(t_value_type value)
Computes the principal value of the arc tangent of the given value.
Definition Angle.h:288
static Angle acos(t_value_type value)
Computes the principal value of the arc cosine of the given value.
Definition Angle.h:266
constexpr Angle normalized() const
returns the normalized angle which lies from 0 to 360 degrees.
Definition Angle.h:206
constexpr Angle & operator=(const Angle &angle)
Definition Angle.h:224
constexpr Angle normalizedOffset() const
returns the normalized angle which lies from -180 to 180 degrees.
Definition Angle.h:213
constexpr bool operator==(const Angle &angle) const
Equality operator.
Definition Angle.h:235
constexpr fltp08 as() const
Returns a form of this angle as a given type (Radian, Degree, Percent, etc) The template t_type provi...
Definition Angle.h:141
static Angle atan2(t_value_type x, t_value_type y)
measures the counterclockwise angle between the positive x-axis and the point (x, y)
Definition Angle.h:300
static constexpr fltp08 INV_INDEX_PI
Precomputed inverse of INDEX_PI used for converting internal angles to other representations.
Definition Angle.h:306
constexpr t_type internal() const
Gets the internal index angle which is the default storage unit for the angle data.
Definition Angle.h:183
constexpr fltp08 as(AngleType type) const
Returns a form of this angle as a given type (Radian, Degree, Percent, etc).
Definition Angle.h:165
static constexpr sint04 INDEX_PI
Optimized angle constant representing PI; allows integers to store angles to some degree of accuracy.
Definition Angle.h:305
constexpr Angle(t_type value)
Constructor.
Definition Angle.h:130
constexpr Angle< t_new_type > toTypeAngle() const
Explicit conversion of an angle from one container type to another.
Definition Angle.h:198
Angle operator-() const
negates the angle
Definition Angle.h:254
constexpr Angle()
Default constructor, creates an angle with 0 degrees.
Definition Angle.h:89
The primary namespace for the NDEVR SDK.
EulerPosition
Values that represent euler angles.
Definition Angle.h:44
@ YAW
Rotation about the vertical axis (Z).
Definition Angle.h:47
@ PITCH
Rotation about the lateral axis (Y).
Definition Angle.h:46
@ ROLL
Rotation about the forward axis (X).
Definition Angle.h:45
@ type
The type identifier string for this model node.
Definition Model.h:58
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
Definition Angle.h:398
constexpr HSLColor Constant< HSLColor >::Invalid
The invalid HSLColor constant with all components set to invalid.
Definition HSLColor.h:264
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
AngleType
The possible units that can be used by the angle class.
Definition Angle.h:56
@ PERCENT
Angle measured as a percentage (0.0 to 1.0 for a full circle).
Definition Angle.h:60
@ DEGREES
Angle measured in degrees (0 to 360 for a full circle).
Definition Angle.h:58
@ INTERNAL_ANGLE
The angle internally used by the angle class.
Definition Angle.h:59
@ RADIANS
Angle measured in radians (0 to 2*PI for a full circle).
Definition Angle.h:57
constexpr HSLColor Constant< HSLColor >::Max
The maximum HSLColor constant with all components at their maximum values.
Definition HSLColor.h:265
static constexpr t_float_type PI()
Returns the value of PI to a given precision.
Definition Angle.h:68
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
t_type sqrt(const t_type &value)
constexpr HSLColor Constant< HSLColor >::Min
The minimum HSLColor constant with saturation, brightness, and alpha at zero.
Definition HSLColor.h:266
Angle< t_type > asqrt(const Angle< t_type > &value)
Computes the square root of the internal representation of an angle.
Definition Angle.h:378
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
static const bool Pointer
Whether this type is a pointer.
Definition Angle.h:469
static const bool Vector
Whether this type is a vector.
Definition Angle.h:467
static constexpr ObjectInfo< Angle< fltp04 >, false, false > VectorSub()
Returns the ObjectInfo for the sub-element type of a vector.
Definition Angle.h:482
static const bool Unsigned
Whether this type is unsigned.
Definition Angle.h:470
static const bool Color
Whether this type is a color.
Definition Angle.h:475
static const bool Primitive
Whether this type is a primitive value.
Definition Angle.h:468
static const bool Float
Whether this type behaves as a floating-point value.
Definition Angle.h:471
static const bool Buffer
Whether this type is a buffer.
Definition Angle.h:476
static const bool Integer
Whether this type is an integer.
Definition Angle.h:472
static const bool Number
Whether this type is numeric.
Definition Angle.h:473
static const uint01 Dimensions
Number of vector dimensions (0 for scalar).
Definition Angle.h:466
static const bool String
Whether this type is a string.
Definition Angle.h:474
static const bool Boolean
Whether this type is a boolean.
Definition Angle.h:477
static const bool String
Whether this type is a string.
Definition Angle.h:450
static const bool Primitive
Whether this type is a primitive value.
Definition Angle.h:444
static const uint01 Dimensions
Number of vector dimensions (0 for scalar).
Definition Angle.h:442
static const bool Buffer
Whether this type is a buffer.
Definition Angle.h:452
static const bool Pointer
Whether this type is a pointer.
Definition Angle.h:445
static const bool Unsigned
Whether this type is unsigned.
Definition Angle.h:446
static constexpr ObjectInfo< Angle< fltp08 >, false, false > VectorSub()
Returns the ObjectInfo for the sub-element type of a vector.
Definition Angle.h:458
static const bool Float
Whether this type behaves as a floating-point value.
Definition Angle.h:447
static const bool Integer
Whether this type is an integer.
Definition Angle.h:448
static const bool Boolean
Whether this type is a boolean.
Definition Angle.h:453
static const bool Number
Whether this type is numeric.
Definition Angle.h:449
static const bool Vector
Whether this type is a vector.
Definition Angle.h:443
static const bool Color
Whether this type is a color.
Definition Angle.h:451
static const bool Color
Whether this type is a color.
Definition Angle.h:427
static const bool Number
Whether this type is numeric.
Definition Angle.h:425
static const bool Pointer
Whether this type is a pointer.
Definition Angle.h:421
static const bool Float
Whether this type behaves as a floating-point value.
Definition Angle.h:423
static const bool String
Whether this type is a string.
Definition Angle.h:426
static constexpr ObjectInfo< Angle< sint04 >, false, false > VectorSub()
Returns the ObjectInfo for the sub-element type of a vector.
Definition Angle.h:434
static const bool Primitive
Whether this type is a primitive value.
Definition Angle.h:420
static const bool Vector
Whether this type is a vector.
Definition Angle.h:419
static const bool Buffer
Whether this type is a buffer.
Definition Angle.h:428
static const bool Boolean
Whether this type is a boolean.
Definition Angle.h:429
static const bool Integer
Whether this type is an integer.
Definition Angle.h:424
static const bool Unsigned
Whether this type is unsigned.
Definition Angle.h:422
static const uint01 Dimensions
Number of vector dimensions (0 for scalar).
Definition Angle.h:418
Information about the object.
Definition ObjectInfo.h:55