sandbox
Loading...
Searching...
No Matches
angle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
24#ifndef LIBSBX_MATH_ANGLE_HPP_
25#define LIBSBX_MATH_ANGLE_HPP_
26
27#include <cmath>
28#include <concepts>
29#include <numbers>
30
32#include <libsbx/math/smooth_value.hpp>
33
34namespace sbx::math {
35
44template<floating_point Type>
46
47public:
48
52 using value_type = Type;
53
59 inline static constexpr basic_degree min{value_type{0}};
60
66 inline static constexpr basic_degree max{value_type{360}};
67
71 constexpr basic_degree() = default;
72
80 template<scalar Other>
81 requires (std::is_convertible_v<Other, Type>)
82 constexpr basic_degree(const Other value) noexcept;
83
91 template<floating_point Other>
92 requires (std::is_convertible_v<Other, Type>)
93 constexpr basic_degree(const basic_degree<Other>& other) noexcept;
94
98 constexpr ~basic_degree() noexcept = default;
99
109 template<floating_point Other>
110 requires (std::is_convertible_v<Other, Type>)
111 constexpr auto operator=(const basic_degree<Other>& other) noexcept -> basic_degree<Type>&;
112
122 template<floating_point Other>
123 requires (std::is_convertible_v<Other, Type>)
124 constexpr auto operator+=(const basic_degree<Other>& rhs) noexcept -> basic_degree<Type>&;
125
135 template<floating_point Other>
136 requires (std::is_convertible_v<Other, Type>)
137 constexpr auto operator-=(const basic_degree<Other>& rhs) noexcept -> basic_degree<Type>&;
138
148 template<floating_point Other>
149 requires (std::is_convertible_v<Other, Type>)
150 constexpr auto operator*=(const Other rhs) noexcept -> basic_degree<Type>&;
151
161 template<floating_point Other>
162 requires (std::is_convertible_v<Other, Type>)
163 constexpr auto operator/=(const Other rhs) noexcept -> basic_degree<Type>&;
164
170 constexpr auto value() const noexcept -> value_type;
171
177 constexpr operator value_type() const noexcept;
178
184 constexpr auto data() noexcept -> value_type*;
185
186private:
187
191 value_type _value{};
192
193}; // class basic_degree
194
205template<floating_point Type>
206constexpr auto operator==(const basic_degree<Type>& lhs, const basic_degree<Type>& rhs) noexcept -> bool;
207
219template<floating_point Type, floating_point Other>
220constexpr auto operator<=>(const basic_degree<Type>& lhs, const basic_degree<Other>& rhs) noexcept -> std::partial_ordering;
221
233template<floating_point Type, floating_point Other>
234requires (std::is_convertible_v<Other, Type>)
235constexpr auto operator+(basic_degree<Type> lhs, const basic_degree<Other>& rhs) noexcept -> basic_degree<Type>;
236
248template<floating_point Type, floating_point Other>
249requires (std::is_convertible_v<Other, Type>)
250constexpr auto operator-(basic_degree<Type> lhs, const basic_degree<Other>& rhs) noexcept -> basic_degree<Type>;
251
263template<floating_point Type, std::convertible_to<Type> Other>
264constexpr auto operator*(basic_degree<Type> lhs, const Other rhs) noexcept -> basic_degree<Type>;
265
277template<floating_point Type, std::convertible_to<Type> Other>
278constexpr auto operator/(basic_degree<Type> lhs, const Other rhs) noexcept -> basic_degree<Type>;
279
291template<floating_point Type>
292constexpr auto clamp(const basic_degree<Type>& value, const basic_degree<Type>& min, const basic_degree<Type>& max) -> const basic_degree<Type>&;
293
298
304template<floating_point Type>
305struct is_smoothable<basic_degree<Type>> : std::true_type { };
306
314template<floating_point Type>
316
318
327 inline static constexpr auto equal(const basic_degree<Type>& lhs, const basic_degree<Type>& rhs) noexcept -> bool;
328
329}; // struct comparision_traits
330
342template<floating_point Type>
343inline constexpr auto mix(const basic_degree<Type> x, const basic_degree<Type> y, const Type a) -> basic_degree<Type>;
344
353template<floating_point Type>
355
356public:
357
361 using value_type = Type;
362
368 inline static constexpr basic_radian min{value_type{0}};
369
375 inline static constexpr basic_radian max{value_type{2 * std::numbers::pi_v<value_type>}};
376
380 constexpr basic_radian() = default;
381
389 template<scalar Other>
390 requires (std::is_convertible_v<Other, Type>)
391 constexpr basic_radian(const Other value) noexcept;
392
400 template<floating_point Other>
401 requires (std::is_convertible_v<Other, Type>)
402 constexpr basic_radian(const basic_radian<Other>& other) noexcept;
403
407 constexpr ~basic_radian() noexcept = default;
408
418 template<floating_point Other>
419 requires (std::is_convertible_v<Other, Type>)
420 constexpr auto operator=(const basic_radian<Other>& other) noexcept -> basic_radian<Type>&;
421
431 template<floating_point Other>
432 requires (std::is_convertible_v<Other, Type>)
433 constexpr auto operator+=(const basic_radian<Other>& rhs) noexcept -> basic_radian<Type>&;
434
444 template<floating_point Other>
445 requires (std::is_convertible_v<Other, Type>)
446 constexpr auto operator-=(const basic_radian<Other>& rhs) noexcept -> basic_radian<Type>&;
447
457 template<floating_point Other>
458 requires (std::is_convertible_v<Other, Type>)
459 constexpr auto operator*=(const Other rhs) noexcept -> basic_radian<Type>&;
460
466 constexpr auto value() const noexcept -> value_type;
467
473 constexpr operator value_type() const noexcept;
474
480 constexpr auto data() noexcept -> value_type*;
481
482private:
483
487 value_type _value{};
488
489}; // class basic_radian
490
501template<floating_point Type>
502constexpr auto operator==(const basic_radian<Type>& lhs, const basic_radian<Type>& rhs) noexcept -> bool;
503
515template<floating_point Type, floating_point Other>
516constexpr auto operator<=>(const basic_radian<Type>& lhs, const basic_radian<Other>& rhs) noexcept -> std::partial_ordering;
517
529template<floating_point Type, floating_point Other>
530requires (std::is_convertible_v<Other, Type>)
531constexpr auto operator+(basic_radian<Type> lhs, const basic_radian<Other>& rhs) noexcept -> basic_radian<Type>;
532
544template<floating_point Type, floating_point Other>
545requires (std::is_convertible_v<Other, Type>)
546constexpr auto operator-(basic_radian<Type> lhs, const basic_radian<Other>& rhs) noexcept -> basic_radian<Type>;
547
559template<floating_point Type, std::convertible_to<Type> Other>
560requires (std::is_convertible_v<Other, Type>)
561constexpr auto operator*(basic_radian<Type> lhs, const Other rhs) noexcept -> basic_radian<Type>;
562
574template<floating_point Type>
575constexpr auto clamp(const basic_radian<Type>& value, const basic_radian<Type>& min, const basic_radian<Type>& max) -> const basic_radian<Type>&;
576
581
590template<floating_point Type>
592
593public:
594
598 using value_type = Type;
599
605 constexpr basic_angle(const basic_degree<value_type>& degree) noexcept;
606
612 constexpr basic_angle(const basic_radian<value_type>& radian) noexcept;
613
621 template<floating_point Other>
622 requires (std::is_convertible_v<Other, Type>)
623 constexpr basic_angle(const basic_angle<Other>& other) noexcept;
624
634 template<floating_point Other>
635 requires (std::is_convertible_v<Other, Type>)
636 constexpr auto operator+=(const basic_angle<Other>& other) noexcept -> basic_angle<Type>&;
637
647 template<floating_point Other>
648 requires (std::is_convertible_v<Other, Type>)
649 constexpr auto operator+=(const basic_degree<Other>& other) noexcept -> basic_angle<Type>&;
650
660 template<floating_point Other>
661 requires (std::is_convertible_v<Other, Type>)
662 constexpr auto operator+=(const basic_radian<Other>& other) noexcept -> basic_angle<Type>&;
663
673 template<floating_point Other>
674 requires (std::is_convertible_v<Other, Type>)
675 constexpr auto operator-=(const basic_angle<Other>& other) noexcept -> basic_angle<Type>&;
676
686 template<floating_point Other>
687 requires (std::is_convertible_v<Other, Type>)
688 constexpr auto operator-=(const basic_degree<Other>& other) noexcept -> basic_angle<Type>&;
689
699 template<floating_point Other>
700 requires (std::is_convertible_v<Other, Type>)
701 constexpr auto operator-=(const basic_radian<Other>& other) noexcept -> basic_angle<Type>&;
702
712 template<floating_point Other>
713 requires (std::is_convertible_v<Other, Type>)
714 constexpr auto operator*=(const Other other) noexcept -> basic_angle<Type>&;
715
721 constexpr auto to_degrees() const noexcept -> basic_degree<value_type>;
722
728 constexpr auto to_radians() const noexcept -> basic_radian<value_type>;
729
730private:
731
735 basic_radian<Type> _radian{};
736
737}; // class basic_angle
738
749template<floating_point Type>
750constexpr auto operator==(const basic_angle<Type>& lhs, const basic_angle<Type>& rhs) noexcept -> bool;
751
763template<floating_point Type, floating_point Other>
764constexpr auto operator<=>(const basic_angle<Type>& lhs, const basic_angle<Other>& rhs) noexcept -> std::partial_ordering;
765
777template<floating_point LhsType, floating_point RhsType>
778requires (std::is_convertible_v<RhsType, LhsType>)
779constexpr auto operator+(basic_angle<LhsType> lhs, const basic_angle<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
780
792template<floating_point LhsType, floating_point RhsType>
793requires (std::is_convertible_v<RhsType, LhsType>)
794constexpr auto operator+(basic_angle<LhsType> lhs, const basic_degree<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
795
807template<floating_point LhsType, floating_point RhsType>
808requires (std::is_convertible_v<RhsType, LhsType>)
809constexpr auto operator+(basic_angle<LhsType> lhs, const basic_radian<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
810
822template<floating_point LhsType, floating_point RhsType>
823requires (std::is_convertible_v<RhsType, LhsType>)
824constexpr auto operator-(basic_angle<LhsType> lhs, const basic_angle<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
825
837template<floating_point LhsType, floating_point RhsType>
838requires (std::is_convertible_v<RhsType, LhsType>)
839constexpr auto operator-(basic_angle<LhsType> lhs, const basic_degree<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
840
852template<floating_point LhsType, floating_point RhsType>
853requires (std::is_convertible_v<RhsType, LhsType>)
854constexpr auto operator-(basic_angle<LhsType> lhs, const basic_radian<RhsType>& rhs) noexcept -> basic_angle<LhsType>;
855
867template<floating_point LhsType, floating_point RhsType>
868requires (std::is_convertible_v<RhsType, LhsType>)
869constexpr auto operator*(basic_angle<LhsType> lhs, const RhsType rhs) noexcept -> basic_angle<LhsType>;
870
882template<floating_point Type>
883constexpr auto clamp(const basic_angle<Type>& value, const basic_angle<Type>& min, const basic_angle<Type>& max) -> const basic_angle<Type>&;
884
889
899template<floating_point Type>
900constexpr auto to_degrees(const basic_radian<Type>& radian) noexcept -> basic_degree<Type>;
901
911template<floating_point Type>
912constexpr auto to_radians(const basic_degree<Type>& degree) noexcept -> basic_radian<Type>;
913
923template<floating_point Type>
924constexpr auto sin(const basic_angle<Type>& angle) noexcept -> Type;
925
935template<floating_point Type>
936constexpr auto sin(const basic_degree<Type>& degree) noexcept -> Type;
937
947template<floating_point Type>
948constexpr auto sin(const basic_radian<Type>& radian) noexcept -> Type;
949
959template<floating_point Type>
960constexpr auto cos(const basic_angle<Type>& angle) noexcept -> Type;
961
971template<floating_point Type>
972constexpr auto cos(const basic_degree<Type>& degree) noexcept -> Type;
973
983template<floating_point Type>
984constexpr auto cos(const basic_radian<Type>& radian) noexcept -> Type;
985
995template<floating_point Type>
996constexpr auto tan(const basic_angle<Type>& angle) noexcept -> Type;
997
1003namespace literals {
1004
1012constexpr auto operator""_deg(long double value) noexcept -> degree;
1013
1021constexpr auto operator""_deg(unsigned long long value) noexcept -> degree;
1022
1030constexpr auto operator""_rad(long double value) noexcept -> radian;
1031
1039constexpr auto operator""_rad(unsigned long long value) noexcept -> radian;
1040
1041} // namespace literals
1042
1043} // namespace sbx::math
1044
1045#include <libsbx/math/angle.ipp>
1046
1047#endif // LIBSBX_MATH_ANGLE_HPP_
constexpr auto sin(const basic_angle< Type > &angle) noexcept -> Type
Computes the sine of an angle.
Definition: angle.ipp:422
constexpr auto to_degrees(const basic_radian< Type > &radian) noexcept -> basic_degree< Type >
Converts radians to degrees.
Definition: angle.ipp:412
constexpr auto cos(const basic_angle< Type > &angle) noexcept -> Type
Computes the cosine of an angle.
Definition: angle.ipp:437
constexpr auto tan(const basic_angle< Type > &angle) noexcept -> Type
Computes the tangent of an angle.
Definition: angle.ipp:452
constexpr auto to_radians(const basic_degree< Type > &degree) noexcept -> basic_radian< Type >
Converts degrees to radians.
Definition: angle.ipp:417
Unified angle type stored internally in radians.
Definition: angle.hpp:591
Type value_type
Underlying scalar value type.
Definition: angle.hpp:598
constexpr auto to_radians() const noexcept -> basic_radian< value_type >
Converts this angle to radians.
Definition: angle.ipp:328
constexpr auto to_degrees() const noexcept -> basic_degree< value_type >
Converts this angle to degrees.
Definition: angle.ipp:323
Strongly-typed degree value wrapper.
Definition: angle.hpp:45
constexpr ~basic_degree() noexcept=default
Destructor.
static constexpr basic_degree max
Maximum representable canonical degree value used by this library.
Definition: angle.hpp:66
static constexpr basic_degree min
Minimum representable canonical degree value used by this library.
Definition: angle.hpp:59
constexpr basic_degree()=default
Constructs a degree value initialized to zero.
constexpr auto value() const noexcept -> value_type
Returns the stored degree value.
Definition: angle.ipp:64
Type value_type
Underlying scalar value type.
Definition: angle.hpp:52
constexpr auto data() noexcept -> value_type *
Returns a pointer to the underlying stored value.
Definition: angle.ipp:74
Strongly-typed radian value wrapper.
Definition: angle.hpp:354
constexpr auto value() const noexcept -> value_type
Returns the stored radian value.
Definition: angle.ipp:190
static constexpr basic_radian min
Minimum representable canonical radian value used by this library.
Definition: angle.hpp:368
Type value_type
Underlying scalar value type.
Definition: angle.hpp:361
static constexpr basic_radian max
Maximum representable canonical radian value used by this library.
Definition: angle.hpp:375
constexpr auto data() noexcept -> value_type *
Returns a pointer to the underlying stored value.
Definition: angle.ipp:200
constexpr ~basic_radian() noexcept=default
Destructor.
constexpr basic_radian()=default
Constructs a radian value initialized to zero.
Concept for floating-point types.
Definition: concepts.hpp:72
constexpr auto mix(const Type x, const Type y, const Type a) -> Type
Linearly interpolates between two values.
Definition: algorithm.ipp:7
Core numeric concepts and type traits.
Definition: traits.hpp:14
Definition: traits.hpp:11
Definition: smooth_value.hpp:26