Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_math_utils.hpp
1#pragma once
2
3#ifndef NOMINMAX
4# define NOMINMAX
5#endif
6
7#ifndef M_PI
8# define M_PI 3.14159265358979323846
9#endif
10
11#include <limits>
12#include <stdint.h>
13
14#include "structure/math/spk_vector2.hpp"
15#include "structure/math/spk_vector3.hpp"
16#include "structure/math/spk_vector4.hpp"
17
18namespace spk
19{
20 namespace MathUtils
21 {
22 float degreeToRadian(float p_degrees);
23 float radianToDegree(float p_radians);
24
25 intmax_t positiveModulo(const intmax_t &p_value, const intmax_t &p_modulo);
26
37 template <typename TScalar>
39 {
45
47 static constexpr bool is_specialized = true;
48
53 static constexpr value_type min() noexcept
54 {
55 return value_type{
56 std::numeric_limits<TScalar>::min(),
57 std::numeric_limits<TScalar>::min()};
58 }
59
64 static constexpr value_type max() noexcept
65 {
66 return value_type{
67 std::numeric_limits<TScalar>::max(),
68 std::numeric_limits<TScalar>::max()};
69 }
70
75 static constexpr value_type lowest() noexcept
76 {
77 return value_type{
78 std::numeric_limits<TScalar>::lowest(),
79 std::numeric_limits<TScalar>::lowest()};
80 }
81
83 static constexpr int digits = std::numeric_limits<TScalar>::digits;
85 static constexpr int digits10 = std::numeric_limits<TScalar>::digits10;
87 static constexpr int max_digits10 = std::numeric_limits<TScalar>::max_digits10;
88
90 static constexpr bool is_signed = std::numeric_limits<TScalar>::is_signed;
92 static constexpr bool is_integer = std::numeric_limits<TScalar>::is_integer;
94 static constexpr bool is_exact = std::numeric_limits<TScalar>::is_exact;
95
97 static constexpr int radix = std::numeric_limits<TScalar>::radix;
98
103 static constexpr value_type epsilon() noexcept
104 {
105 return value_type{
106 std::numeric_limits<TScalar>::epsilon(),
107 std::numeric_limits<TScalar>::epsilon()};
108 }
109
114 static constexpr value_type roundError() noexcept
115 {
116 return value_type{
117 std::numeric_limits<TScalar>::round_error(),
118 std::numeric_limits<TScalar>::round_error()};
119 }
120 };
121
132 template <typename TScalar>
134 {
140
142 static constexpr bool is_specialized = true;
143
148 static constexpr value_type min() noexcept
149 {
150 return value_type{
151 std::numeric_limits<TScalar>::min(),
152 std::numeric_limits<TScalar>::min(),
153 std::numeric_limits<TScalar>::min()};
154 }
155
160 static constexpr value_type max() noexcept
161 {
162 return value_type{
163 std::numeric_limits<TScalar>::max(),
164 std::numeric_limits<TScalar>::max(),
165 std::numeric_limits<TScalar>::max()};
166 }
167
172 static constexpr value_type lowest() noexcept
173 {
174 return value_type{
175 std::numeric_limits<TScalar>::lowest(),
176 std::numeric_limits<TScalar>::lowest(),
177 std::numeric_limits<TScalar>::lowest()};
178 }
179
181 static constexpr int digits = std::numeric_limits<TScalar>::digits;
183 static constexpr int digits10 = std::numeric_limits<TScalar>::digits10;
185 static constexpr int max_digits10 = std::numeric_limits<TScalar>::max_digits10;
186
188 static constexpr bool is_signed = std::numeric_limits<TScalar>::is_signed;
190 static constexpr bool is_integer = std::numeric_limits<TScalar>::is_integer;
192 static constexpr bool is_exact = std::numeric_limits<TScalar>::is_exact;
193
195 static constexpr int radix = std::numeric_limits<TScalar>::radix;
196
201 static constexpr value_type epsilon() noexcept
202 {
203 return value_type{
204 std::numeric_limits<TScalar>::epsilon(),
205 std::numeric_limits<TScalar>::epsilon(),
206 std::numeric_limits<TScalar>::epsilon()};
207 }
208
213 static constexpr value_type roundError() noexcept
214 {
215 return value_type{
216 std::numeric_limits<TScalar>::round_error(),
217 std::numeric_limits<TScalar>::round_error(),
218 std::numeric_limits<TScalar>::round_error()};
219 }
220 };
221
232 template <typename TScalar>
234 {
240
242 static constexpr bool is_specialized = true;
243
248 static constexpr value_type min() noexcept
249 {
250 return value_type{
251 std::numeric_limits<TScalar>::min(),
252 std::numeric_limits<TScalar>::min(),
253 std::numeric_limits<TScalar>::min(),
254 std::numeric_limits<TScalar>::min()};
255 }
256
261 static constexpr value_type max() noexcept
262 {
263 return value_type{
264 std::numeric_limits<TScalar>::max(),
265 std::numeric_limits<TScalar>::max(),
266 std::numeric_limits<TScalar>::max(),
267 std::numeric_limits<TScalar>::max()};
268 }
269
274 static constexpr value_type lowest() noexcept
275 {
276 return value_type{
277 std::numeric_limits<TScalar>::lowest(),
278 std::numeric_limits<TScalar>::lowest(),
279 std::numeric_limits<TScalar>::lowest(),
280 std::numeric_limits<TScalar>::lowest()};
281 }
282
284 static constexpr int digits = std::numeric_limits<TScalar>::digits;
286 static constexpr int digits10 = std::numeric_limits<TScalar>::digits10;
288 static constexpr int max_digits10 = std::numeric_limits<TScalar>::max_digits10;
289
291 static constexpr bool is_signed = std::numeric_limits<TScalar>::is_signed;
293 static constexpr bool is_integer = std::numeric_limits<TScalar>::is_integer;
295 static constexpr bool is_exact = std::numeric_limits<TScalar>::is_exact;
296
298 static constexpr int radix = std::numeric_limits<TScalar>::radix;
299
304 static constexpr value_type epsilon() noexcept
305 {
306 return value_type{
307 std::numeric_limits<TScalar>::epsilon(),
308 std::numeric_limits<TScalar>::epsilon(),
309 std::numeric_limits<TScalar>::epsilon(),
310 std::numeric_limits<TScalar>::epsilon()};
311 }
312
317 static constexpr value_type roundError() noexcept
318 {
319 return value_type{
320 std::numeric_limits<TScalar>::round_error(),
321 std::numeric_limits<TScalar>::round_error(),
322 std::numeric_limits<TScalar>::round_error(),
323 std::numeric_limits<TScalar>::round_error()};
324 }
325 };
326 }
327}
328
329namespace std
330{
331 template <>
332 class numeric_limits<spk::Vector2> : public spk::MathUtils::NumericLimitVector2<spk::Vector2::value_type>
333 {
334 };
335
336 template <>
337 class numeric_limits<spk::Vector2Int> : public spk::MathUtils::NumericLimitVector2<spk::Vector2Int::value_type>
338 {
339 };
340
341 template <>
342 class numeric_limits<spk::Vector2UInt> : public spk::MathUtils::NumericLimitVector2<spk::Vector2UInt::value_type>
343 {
344 };
345
346 template <>
347 class numeric_limits<spk::Vector3> : public spk::MathUtils::NumericLimitVector3<spk::Vector3::value_type>
348 {
349 };
350
351 template <>
352 class numeric_limits<spk::Vector3Int> : public spk::MathUtils::NumericLimitVector3<spk::Vector3Int::value_type>
353 {
354 };
355
356 template <>
357 class numeric_limits<spk::Vector3UInt> : public spk::MathUtils::NumericLimitVector3<spk::Vector3UInt::value_type>
358 {
359 };
360
361 template <>
362 class numeric_limits<spk::Vector4> : public spk::MathUtils::NumericLimitVector4<spk::Vector4::value_type>
363 {
364 };
365
366 template <>
367 class numeric_limits<spk::Vector4Int> : public spk::MathUtils::NumericLimitVector4<spk::Vector4Int::value_type>
368 {
369 };
370
371 template <>
372 class numeric_limits<spk::Vector4UInt> : public spk::MathUtils::NumericLimitVector4<spk::Vector4UInt::value_type>
373 {
374 };
375}
STL namespace.
2D value type holding two components with basic arithmetic helpers.
Definition spk_vector2.hpp:35
3D value type with common arithmetic helpers.
Definition spk_vector3.hpp:34
4D value type with arithmetic helpers and swizzles.
Definition spk_vector4.hpp:34
Numeric limits adapter for 2D vector scalar types.
Definition spk_math_utils.hpp:39
static constexpr value_type lowest() noexcept
Returns the lowest finite value for each component.
Definition spk_math_utils.hpp:75
static constexpr bool is_specialized
Indicates this specialization is provided.
Definition spk_math_utils.hpp:47
static constexpr value_type roundError() noexcept
Returns the rounding error for each component.
Definition spk_math_utils.hpp:114
static constexpr value_type min() noexcept
Returns the minimum finite value for each component.
Definition spk_math_utils.hpp:53
static constexpr int max_digits10
Maximum number of base-10 digits to preserve round-trips.
Definition spk_math_utils.hpp:87
static constexpr value_type epsilon() noexcept
Returns the machine epsilon for each component.
Definition spk_math_utils.hpp:103
IVector2< TScalar > value_type
Vector type using the provided scalar.
Definition spk_math_utils.hpp:44
static constexpr int digits
Number of radix digits for the scalar type.
Definition spk_math_utils.hpp:83
static constexpr bool is_exact
true if the scalar type is exact.
Definition spk_math_utils.hpp:94
static constexpr int radix
The radix of the scalar type representation.
Definition spk_math_utils.hpp:97
static constexpr bool is_signed
true if the scalar type is signed.
Definition spk_math_utils.hpp:90
static constexpr bool is_integer
true if the scalar type is integer.
Definition spk_math_utils.hpp:92
static constexpr value_type max() noexcept
Returns the maximum finite value for each component.
Definition spk_math_utils.hpp:64
static constexpr int digits10
Number of base-10 digits for the scalar type.
Definition spk_math_utils.hpp:85
Numeric limits adapter for 3D vector scalar types.
Definition spk_math_utils.hpp:134
static constexpr value_type min() noexcept
Returns the minimum finite value for each component.
Definition spk_math_utils.hpp:148
static constexpr int max_digits10
Maximum number of base-10 digits to preserve round-trips.
Definition spk_math_utils.hpp:185
static constexpr value_type lowest() noexcept
Returns the lowest finite value for each component.
Definition spk_math_utils.hpp:172
IVector3< TScalar > value_type
Vector type using the provided scalar.
Definition spk_math_utils.hpp:139
static constexpr int digits10
Number of base-10 digits for the scalar type.
Definition spk_math_utils.hpp:183
static constexpr int digits
Number of radix digits for the scalar type.
Definition spk_math_utils.hpp:181
static constexpr value_type epsilon() noexcept
Returns the machine epsilon for each component.
Definition spk_math_utils.hpp:201
static constexpr value_type max() noexcept
Returns the maximum finite value for each component.
Definition spk_math_utils.hpp:160
static constexpr bool is_integer
true if the scalar type is integer.
Definition spk_math_utils.hpp:190
static constexpr bool is_signed
true if the scalar type is signed.
Definition spk_math_utils.hpp:188
static constexpr bool is_exact
true if the scalar type is exact.
Definition spk_math_utils.hpp:192
static constexpr int radix
The radix of the scalar type representation.
Definition spk_math_utils.hpp:195
static constexpr bool is_specialized
Indicates this specialization is provided.
Definition spk_math_utils.hpp:142
static constexpr value_type roundError() noexcept
Returns the rounding error for each component.
Definition spk_math_utils.hpp:213
Numeric limits adapter for 4D vector scalar types.
Definition spk_math_utils.hpp:234
static constexpr value_type roundError() noexcept
Returns the rounding error for each component.
Definition spk_math_utils.hpp:317
IVector4< TScalar > value_type
Vector type using the provided scalar.
Definition spk_math_utils.hpp:239
static constexpr bool is_specialized
Indicates this specialization is provided.
Definition spk_math_utils.hpp:242
static constexpr value_type min() noexcept
Returns the minimum finite value for each component.
Definition spk_math_utils.hpp:248
static constexpr bool is_signed
true if the scalar type is signed.
Definition spk_math_utils.hpp:291
static constexpr value_type lowest() noexcept
Returns the lowest finite value for each component.
Definition spk_math_utils.hpp:274
static constexpr value_type max() noexcept
Returns the maximum finite value for each component.
Definition spk_math_utils.hpp:261
static constexpr int max_digits10
Maximum number of base-10 digits to preserve round-trips.
Definition spk_math_utils.hpp:288
static constexpr int digits
Number of radix digits for the scalar type.
Definition spk_math_utils.hpp:284
static constexpr bool is_exact
true if the scalar type is exact.
Definition spk_math_utils.hpp:295
static constexpr int digits10
Number of base-10 digits for the scalar type.
Definition spk_math_utils.hpp:286
static constexpr bool is_integer
true if the scalar type is integer.
Definition spk_math_utils.hpp:293
static constexpr int radix
The radix of the scalar type representation.
Definition spk_math_utils.hpp:298
static constexpr value_type epsilon() noexcept
Returns the machine epsilon for each component.
Definition spk_math_utils.hpp:304