CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Angle.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11#include "Math.hpp"
12
15
17
18#include <iosfwd>
19
21
22 template<CArithmetic T> struct TRadian;
23
28 template<CArithmetic T> struct TDegree {
29 T raw = 0.0;
30
31 public:
32 constexpr TDegree() = default;
33
34 constexpr TDegree(const TDegree& d) = default;
35 constexpr TDegree& operator=(const TDegree& d) = default;
36
37 constexpr explicit TDegree(T d) : raw(d) {}
38
39 constexpr TDegree(const TRadian<T>& r) : raw(degrees(r.raw)) {} // NOLINT
40 constexpr TDegree& operator=(const TRadian<T>& r) {
41 raw = degrees(r.raw);
42 return *this;
43 }
44
45 template<CArithmetic U> constexpr explicit TDegree(const TDegree<U>& deg) : raw(T(deg.raw)) {}
46
48 [[nodiscard]] constexpr TDegree wrap() const {
49 T newRaw = mod(raw, 360.0);
50 if(newRaw < 0) {
51 newRaw += 360.0;
52 }
53 return TDegree(newRaw);
54 }
55
56 constexpr const TDegree& operator+() const { return *this; }
57 constexpr TDegree operator+(const TDegree& d) const { return TDegree(raw + d.raw); }
58 constexpr TDegree& operator+=(const TDegree& d) {
59 raw += d.raw;
60 return *this;
61 }
62 friend constexpr TDegree operator+(T lhs, const TDegree& rhs) { return TDegree(lhs + rhs.raw); }
63
64 constexpr TDegree operator-() const { return TDegree(-raw); }
65 constexpr TDegree operator-(const TDegree& d) const { return TDegree(raw - d.raw); }
66 constexpr TDegree& operator-=(const TDegree& d) {
67 raw -= d.raw;
68 return *this;
69 }
70 friend constexpr TDegree operator-(T lhs, const TDegree& rhs) { return TDegree(lhs - rhs.raw); }
71
72 constexpr TDegree operator*(T f) const { return TDegree(raw * f); }
73 constexpr TDegree& operator*=(T f) {
74 raw *= f;
75 return *this;
76 }
77 friend constexpr TDegree operator*(T lhs, const TDegree& rhs) { return TDegree(lhs * rhs.raw); }
78
79 constexpr TDegree operator/(T f) const { return TDegree(raw / f); }
80 constexpr TDegree& operator/=(T f) {
81 raw /= f;
82 return *this;
83 }
84 friend constexpr TDegree operator/(T lhs, const TDegree& rhs) { return TDegree(lhs / rhs.raw); }
85
86 constexpr bool operator<(const TDegree& d) const { return raw < d.raw; }
87 constexpr bool operator<(T d) const { return raw < d; }
88
89 constexpr bool operator<=(const TDegree& d) const { return raw <= d.raw; }
90 constexpr bool operator<=(T d) const { return raw <= d; }
91
92 constexpr bool operator==(const TDegree& d) const { return raw == d.raw; }
93 constexpr bool operator==(T d) const { return raw == d; }
94
95 constexpr bool operator!=(const TDegree& d) const { return raw != d.raw; }
96 constexpr bool operator!=(T d) const { return raw != d; }
97
98 constexpr bool operator>=(const TDegree& d) const { return raw >= d.raw; }
99 constexpr bool operator>=(T d) const { return raw >= d; }
100
101 constexpr bool operator>(const TDegree& d) const { return raw > d.raw; }
102 constexpr bool operator>(T d) const { return raw > d; }
103 };
104
105 template<CArithmetic T> std::ostream& operator<<(std::ostream& os, const TDegree<T>& deg) { return os << deg.raw << "°"; }
106
108 using Degree CE_SCRIPT_EXPORT(name = "Degree<double>", external = true, plain = true)
110
112 using Degreef CE_SCRIPT_EXPORT(name = "Quaternion<float>", external = true, plain = true)
114
116 constexpr Degree operator"" _deg(const long double degree) { return Degree(double(degree)); }
117
119 constexpr Degreef operator"" _degf(const long double degree) { return Degreef(float(degree)); }
120
125 template<CArithmetic T> struct TRadian {
126 T raw = 0.0;
127
128 public:
129 constexpr TRadian() = default;
130
131 constexpr TRadian(const TRadian&) = default;
132 constexpr TRadian& operator=(const TRadian&) = default;
133
134 constexpr explicit TRadian(T r) : raw(r) {}
135
136 constexpr TRadian(const TDegree<T>& d) : raw(radians(d.raw)) {} // NOLINT
137 constexpr TRadian& operator=(const TDegree<T>& d) {
138 raw = radians(d.raw);
139 return *this;
140 }
141
142 template<CArithmetic U> constexpr explicit TRadian(const TRadian<U>& rad) : raw(T(rad.raw)) {}
143
145 [[nodiscard]] constexpr TRadian wrap() {
146 const constexpr T twoPi = glm::two_pi<T>();
147 T newRaw = mod(raw, twoPi);
148 if(newRaw < 0) {
149 newRaw += twoPi;
150 }
151 return TRadian(newRaw);
152 }
153
154 constexpr const TRadian& operator+() const { return *this; }
155 constexpr TRadian operator+(const TRadian& r) const { return TRadian(raw + r.raw); }
156 constexpr TRadian& operator+=(const TRadian& r) {
157 raw += r.raw;
158 return *this;
159 }
160 friend constexpr TRadian operator+(T lhs, const TRadian& rhs) { return TRadian(lhs + rhs.raw); }
161
162 constexpr TRadian operator-() const { return TRadian(-raw); }
163 constexpr TRadian operator-(const TRadian& r) const { return TRadian(raw - r.raw); }
164 constexpr TRadian& operator-=(const TRadian& r) {
165 raw -= r.raw;
166 return *this;
167 }
168 friend constexpr TRadian operator-(T lhs, const TRadian& rhs) { return TRadian(lhs - rhs.raw); }
169
170 constexpr TRadian operator*(T f) const { return TRadian(raw * f); }
171 constexpr TRadian& operator*=(T f) {
172 raw *= f;
173 return *this;
174 }
175 friend constexpr TRadian operator*(T lhs, const TRadian& rhs) { return TRadian(lhs * rhs.raw); }
176
177 constexpr TRadian operator/(T f) const { return TRadian(raw / f); }
178 constexpr TRadian& operator/=(T f) {
179 raw /= f;
180 return *this;
181 }
182 friend constexpr TRadian operator/(T lhs, const TRadian& rhs) { return TRadian(lhs / rhs.raw); }
183
184 [[nodiscard]] constexpr bool operator<(const TRadian& d) const { return raw < d.raw; }
185 [[nodiscard]] constexpr bool operator<(T d) const { return raw < d; }
186
187 [[nodiscard]] constexpr bool operator<=(const TRadian& d) const { return raw <= d.raw; }
188 [[nodiscard]] constexpr bool operator<=(T d) const { return raw <= d; }
189
190 [[nodiscard]] constexpr bool operator==(const TRadian& d) const { return raw == d.raw; }
191 [[nodiscard]] constexpr bool operator==(T d) const { return raw == d; }
192
193 [[nodiscard]] constexpr bool operator!=(const TRadian& d) const { return raw != d.raw; }
194 [[nodiscard]] constexpr bool operator!=(T d) const { return raw != d; }
195
196 [[nodiscard]] constexpr bool operator>=(const TRadian& d) const { return raw >= d.raw; }
197 [[nodiscard]] constexpr bool operator>=(T d) const { return raw >= d; }
198
199 [[nodiscard]] constexpr bool operator>(const TRadian& d) const { return raw > d.raw; }
200 [[nodiscard]] constexpr bool operator>(T d) const { return raw > d; }
201 };
202
203 template<CArithmetic T> std::ostream& operator<<(std::ostream& os, const TRadian<T>& rad) { return os << rad.raw << " rad"; }
204
206 using Radian CE_SCRIPT_EXPORT(name = "Radian", external = true, plain = true)
208
210 using Radianf CE_SCRIPT_EXPORT(name = "Radian", external = true, plain = true)
212
214 constexpr Radian operator"" _rad(const long double radian) { return Radian(double(radian)); }
215
217 constexpr Radianf operator"" _radf(const long double radian) { return Radianf(float(radian)); }
218
219} // namespace CeresEngine::inline Math
220
221template<typename T> struct std::hash<CeresEngine::Math::TDegree<T>> {
222 using Type = CeresEngine::Math::TDegree<T>;
223 constexpr size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.raw); }
224};
225
226template<typename T> struct std::hash<CeresEngine::Math::TRadian<T>> {
227 using Type = CeresEngine::Math::TRadian<T>;
228 constexpr size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.raw); }
229};
230
231#define REFL_DECLARE_CE_ANGLE(T) \
232 \
233
#define REFL_DECLARE_CE_ANGLE(T)
Definition Angle.hpp:231
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
Definition Application.hpp:19
constexpr Byte operator<<(const Byte arg, const _IntType shift) noexcept
Definition DataTypes.hpp:44
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Angle.hpp:20
Wrapper class which indicates a given angle value is in degrees.
Definition Angle.hpp:28
constexpr bool operator>=(const TDegree &d) const
Definition Angle.hpp:98
constexpr bool operator>(const TDegree &d) const
Definition Angle.hpp:101
constexpr TDegree & operator=(const TRadian< T > &r)
Definition Angle.hpp:40
constexpr TDegree operator*(T f) const
Definition Angle.hpp:72
constexpr TDegree operator-() const
Definition Angle.hpp:64
friend constexpr TDegree operator*(T lhs, const TDegree &rhs)
Definition Angle.hpp:77
constexpr TDegree(const TRadian< T > &r)
Definition Angle.hpp:39
constexpr TDegree operator+(const TDegree &d) const
Definition Angle.hpp:57
T raw
Definition Angle.hpp:29
constexpr TDegree()=default
constexpr bool operator>(T d) const
Definition Angle.hpp:102
constexpr bool operator>=(T d) const
Definition Angle.hpp:99
constexpr bool operator!=(T d) const
Definition Angle.hpp:96
constexpr TDegree operator/(T f) const
Definition Angle.hpp:79
constexpr TDegree(const TDegree< U > &deg)
Definition Angle.hpp:45
constexpr TDegree & operator+=(const TDegree &d)
Definition Angle.hpp:58
constexpr TDegree & operator=(const TDegree &d)=default
constexpr TDegree & operator/=(T f)
Definition Angle.hpp:80
constexpr TDegree(const TDegree &d)=default
constexpr bool operator<(const TDegree &d) const
Definition Angle.hpp:86
constexpr bool operator==(const TDegree &d) const
Definition Angle.hpp:92
constexpr TDegree & operator-=(const TDegree &d)
Definition Angle.hpp:66
friend constexpr TDegree operator/(T lhs, const TDegree &rhs)
Definition Angle.hpp:84
constexpr bool operator<(T d) const
Definition Angle.hpp:87
constexpr TDegree wrap() const
Wraps the angle in [0, 360) range.
Definition Angle.hpp:48
constexpr bool operator!=(const TDegree &d) const
Definition Angle.hpp:95
friend constexpr TDegree operator+(T lhs, const TDegree &rhs)
Definition Angle.hpp:62
constexpr bool operator<=(T d) const
Definition Angle.hpp:90
constexpr TDegree(T d)
Definition Angle.hpp:37
constexpr const TDegree & operator+() const
Definition Angle.hpp:56
constexpr TDegree & operator*=(T f)
Definition Angle.hpp:73
constexpr bool operator==(T d) const
Definition Angle.hpp:93
constexpr TDegree operator-(const TDegree &d) const
Definition Angle.hpp:65
constexpr bool operator<=(const TDegree &d) const
Definition Angle.hpp:89
friend constexpr TDegree operator-(T lhs, const TDegree &rhs)
Definition Angle.hpp:70
Wrapper class which indicates a given angle value is in radians.
Definition Angle.hpp:125
constexpr bool operator>(T d) const
Definition Angle.hpp:200
constexpr bool operator>=(T d) const
Definition Angle.hpp:197
constexpr bool operator<(const TRadian &d) const
Definition Angle.hpp:184
constexpr TRadian & operator-=(const TRadian &r)
Definition Angle.hpp:164
constexpr TRadian(const TRadian< U > &rad)
Definition Angle.hpp:142
constexpr bool operator<(T d) const
Definition Angle.hpp:185
constexpr TRadian & operator*=(T f)
Definition Angle.hpp:171
constexpr TRadian operator*(T f) const
Definition Angle.hpp:170
constexpr TRadian(T r)
Definition Angle.hpp:134
constexpr bool operator>=(const TRadian &d) const
Definition Angle.hpp:196
constexpr TRadian operator+(const TRadian &r) const
Definition Angle.hpp:155
friend constexpr TRadian operator+(T lhs, const TRadian &rhs)
Definition Angle.hpp:160
constexpr TRadian()=default
friend constexpr TRadian operator/(T lhs, const TRadian &rhs)
Definition Angle.hpp:182
constexpr bool operator==(T d) const
Definition Angle.hpp:191
constexpr TRadian & operator+=(const TRadian &r)
Definition Angle.hpp:156
constexpr const TRadian & operator+() const
Definition Angle.hpp:154
constexpr TRadian operator-() const
Definition Angle.hpp:162
constexpr TRadian & operator=(const TRadian &)=default
constexpr bool operator>(const TRadian &d) const
Definition Angle.hpp:199
constexpr bool operator==(const TRadian &d) const
Definition Angle.hpp:190
constexpr TRadian & operator=(const TDegree< T > &d)
Definition Angle.hpp:137
constexpr TRadian & operator/=(T f)
Definition Angle.hpp:178
constexpr bool operator!=(T d) const
Definition Angle.hpp:194
constexpr bool operator<=(T d) const
Definition Angle.hpp:188
constexpr TRadian(const TDegree< T > &d)
Definition Angle.hpp:136
constexpr TRadian(const TRadian &)=default
constexpr TRadian wrap()
Wraps the angle in [0, 2 * PI) range.
Definition Angle.hpp:145
constexpr TRadian operator-(const TRadian &r) const
Definition Angle.hpp:163
friend constexpr TRadian operator*(T lhs, const TRadian &rhs)
Definition Angle.hpp:175
constexpr bool operator!=(const TRadian &d) const
Definition Angle.hpp:193
constexpr TRadian operator/(T f) const
Definition Angle.hpp:177
T raw
Definition Angle.hpp:126
friend constexpr TRadian operator-(T lhs, const TRadian &rhs)
Definition Angle.hpp:168
constexpr bool operator<=(const TRadian &d) const
Definition Angle.hpp:187