|
| 1 | +// |
| 2 | +// Created by Orange on 11/30/2024. |
| 3 | +// |
| 4 | + |
| 5 | +#pragma once |
| 6 | +#include "omath/Angles.hpp" |
| 7 | + |
| 8 | + |
| 9 | +namespace omath |
| 10 | +{ |
| 11 | + enum class AngleFlags |
| 12 | + { |
| 13 | + Normalized = 0, |
| 14 | + Clamped = 1, |
| 15 | + }; |
| 16 | + |
| 17 | + template<class Type, Type min = 0, Type max = 360, AngleFlags flags = AngleFlags::Normalized> |
| 18 | + requires std::is_arithmetic_v<Type> |
| 19 | + class Angle |
| 20 | + { |
| 21 | + Type m_angle; |
| 22 | + public: |
| 23 | + |
| 24 | + constexpr explicit Angle(const Type& degrees) |
| 25 | + { |
| 26 | + if constexpr (flags == AngleFlags::Normalized) |
| 27 | + m_angle = angles::WrapAngle(degrees, min, max); |
| 28 | + |
| 29 | + else if constexpr (flags == AngleFlags::Clamped) |
| 30 | + m_angle = std::clamp(degrees, min, max); |
| 31 | + else |
| 32 | + { |
| 33 | + static_assert(false); |
| 34 | + std::unreachable(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [[nodiscard]] |
| 39 | + constexpr static Angle FromDegrees(const Type& degrees) |
| 40 | + { |
| 41 | + return {degrees}; |
| 42 | + } |
| 43 | + |
| 44 | + [[nodiscard]] |
| 45 | + constexpr static Angle FromRadians(const Type& degrees) |
| 46 | + { |
| 47 | + return {angles::RadiansToDegrees(degrees)}; |
| 48 | + } |
| 49 | + |
| 50 | + [[nodiscard]] |
| 51 | + constexpr const Type& operator*() const |
| 52 | + { |
| 53 | + return m_angle; |
| 54 | + } |
| 55 | + |
| 56 | + [[nodiscard]] |
| 57 | + constexpr Type& operator*() |
| 58 | + { |
| 59 | + return m_angle; |
| 60 | + } |
| 61 | + |
| 62 | + [[nodiscard]] |
| 63 | + constexpr const Type& Value() const |
| 64 | + { |
| 65 | + return **std::as_const(this); |
| 66 | + } |
| 67 | + |
| 68 | + [[nodiscard]] |
| 69 | + constexpr Type& Value() |
| 70 | + { |
| 71 | + return **this; |
| 72 | + } |
| 73 | + |
| 74 | + [[nodiscard]] |
| 75 | + constexpr Type AsRadians() const |
| 76 | + { |
| 77 | + return angles::RadiansToDegrees(m_angle); |
| 78 | + } |
| 79 | + |
| 80 | + [[nodiscard]] |
| 81 | + Type Sin() const |
| 82 | + { |
| 83 | + return std::sin(AsRadians()); |
| 84 | + } |
| 85 | + |
| 86 | + [[nodiscard]] |
| 87 | + Type Cos() const |
| 88 | + { |
| 89 | + return std::sin(AsRadians()); |
| 90 | + } |
| 91 | + |
| 92 | + [[nodiscard]] |
| 93 | + Type Tan() const |
| 94 | + { |
| 95 | + return std::tan(AsRadians()); |
| 96 | + } |
| 97 | + |
| 98 | + [[nodiscard]] |
| 99 | + Type Atan() const |
| 100 | + { |
| 101 | + return std::atan(AsRadians()); |
| 102 | + } |
| 103 | + |
| 104 | + [[nodiscard]] |
| 105 | + Type Cot() const |
| 106 | + { |
| 107 | + return Cos() / Sin(); |
| 108 | + } |
| 109 | + |
| 110 | + [[nodiscard]] |
| 111 | + constexpr Angle& operator+=(const Type& other) |
| 112 | + { |
| 113 | + if constexpr (flags == AngleFlags::Normalized) |
| 114 | + m_angle = angles::WrapAngle(m_angle + other, min, max); |
| 115 | + |
| 116 | + else if constexpr (flags == AngleFlags::Clamped) |
| 117 | + m_angle = std::clamp(m_angle + other, min, max); |
| 118 | + else |
| 119 | + { |
| 120 | + static_assert(false); |
| 121 | + std::unreachable(); |
| 122 | + } |
| 123 | + |
| 124 | + return *this; |
| 125 | + } |
| 126 | + |
| 127 | + [[nodiscard]] |
| 128 | + constexpr Angle& operator-=(const Type& other) |
| 129 | + { |
| 130 | + return operator+=(-other); |
| 131 | + } |
| 132 | + |
| 133 | + [[nodiscard]] |
| 134 | + constexpr Angle& operator+(const Type& other) |
| 135 | + { |
| 136 | + if constexpr (flags == AngleFlags::Normalized) |
| 137 | + return {angles::WrapAngle(m_angle + other, min, max)}; |
| 138 | + |
| 139 | + else if constexpr (flags == AngleFlags::Clamped) |
| 140 | + return {std::clamp(m_angle + other, min, max)}; |
| 141 | + |
| 142 | + else |
| 143 | + static_assert(false); |
| 144 | + |
| 145 | + std::unreachable(); |
| 146 | + } |
| 147 | + |
| 148 | + [[nodiscard]] |
| 149 | + constexpr Angle& operator-(const Type& other) |
| 150 | + { |
| 151 | + return operator+(-other); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + }; |
| 156 | +} |
0 commit comments