Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
MathHelper.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32/*Code references:
33 Matrix inverse: http://www.devmaster.net/forums/showthread.php?t=14569
34 Matrix multiply:
35 http://fhtr.blogspot.com/2010/02/4x4-float-matrix-multiplication-using.html
36 Square root: http://www.codeproject.com/KB/cpp/Sqrt_Prec_VS_Speed.aspx
37*/
38
39#pragma once
40#ifndef DVD_CORE_MATH_MATH_HELPER_H_
41#define DVD_CORE_MATH_MATH_HELPER_H_
42
43#include <numbers>
44
45namespace Divide {
46
47#define TO_MEGABYTES(X) ((X) * 1024u * 1024u)
48
49template<typename T>
50concept ValidMathType = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
51
52template <typename T>
53class mat2;
54template <typename T>
55class mat3;
56template <typename T>
57class mat4;
58template <typename T>
59class vec2;
60template <typename T>
61class vec3;
62template <typename T>
63class vec4;
64template <typename T>
65class Rect;
66template <typename T>
67class Quaternion;
68
71
74
75#if !defined(M_PI)
76 constexpr D64 M_PI = std::numbers::pi;
77 constexpr D64 M_PI_2 = M_PI / 2;
78#endif
79
80 constexpr D64 M_2PI = M_PI * 2;
81 constexpr D64 M_PIDIV180 = 0.01745329251994329576;
82 constexpr D64 M_180DIVPI = 57.29577951308232087679;
83 constexpr D64 M_PIDIV360 = 0.00872664625997164788;
84 constexpr D64 M_PI2 = M_2PI;
85
86 constexpr F32 M_PI_f = to_F32(M_PI);
87 constexpr F32 M_PI_2_f = to_F32(M_PI_2);
88 constexpr F32 M_PI_4_f = M_PI_f / 4;
89 constexpr F32 M_2PI_f = to_F32(M_2PI);
93 constexpr F32 M_PI2_f = M_2PI_f;
94
95 constexpr F32 INV_RAND_MAX = 0.0000305185094f;
96
97template<typename T>
98using SignedIntegerBasedOnSize = typename std::conditional<sizeof(T) == 8, I64, I32>::type;
99template<typename T>
100using UnsignedIntegerBasedOnSize = typename std::conditional<sizeof(T) == 8, U64, U32>::type;
101template<typename T>
102using IntegerTypeBasedOnSign = typename std::conditional<std::is_unsigned<T>::value,
105
106template<typename T>
107using DefaultDistribution = typename std::conditional<std::is_integral<T>::value,
108 std::uniform_int_distribution<IntegerTypeBasedOnSign<T>>,
109 std::uniform_real_distribution<T>>::type;
110
111template <typename T,
112 typename Engine = std::mt19937_64,
113 typename Distribution = DefaultDistribution<T>> requires std::is_arithmetic_v<T>
114[[nodiscard]] T Random(T min, T max);
115
116template <typename T,
117 typename Engine = std::mt19937_64,
118 typename Distribution = DefaultDistribution<T>> requires std::is_arithmetic_v<T>
119[[nodiscard]] T Random(T max);
120
121template <typename T,
122 typename Engine = std::mt19937_64,
123 typename Distribution = DefaultDistribution<T>> requires std::is_arithmetic_v<T>
124[[nodiscard]] T Random();
125
126template<typename Engine = std::mt19937_64>
127void SeedRandom();
128
129template<typename Engine = std::mt19937_64>
130void SeedRandom(U32 seed);
131
132template <typename T>
133constexpr I32 SIGN(const T val) {
134 return (val < 0) ? -1 : (val > 0) ? 1 : 0;
135}
136
138template <typename T> requires std::is_arithmetic<T>::value
139constexpr void CLAMP(T& n, T min, T max) noexcept;
140
141template <typename T> requires std::is_arithmetic<T>::value
142constexpr void CLAMP_01(T& n) noexcept;
143
144template <typename T> requires std::is_arithmetic<T>::value
145[[nodiscard]] constexpr T CLAMPED(T n, T min, T max) noexcept;
146
147template <typename T> requires std::is_arithmetic<T>::value
148[[nodiscard]] constexpr T CLAMPED_01(T n) noexcept;
149
150template <typename T> requires std::is_arithmetic<T>::value
151[[nodiscard]] constexpr T MAP(T input, T in_min, T in_max, T out_min, T out_max, D64& slopeOut) noexcept;
152
153template <typename T>
154constexpr void REMAP(T& input, T in_min, T in_max, T out_min, T out_max, D64& slopeOut) noexcept;
155
156template <typename T>
157[[nodiscard]] constexpr T SQUARED(T input) noexcept;
158
159template <typename T>
160[[nodiscard]] constexpr T MAP(const T input, const T in_min, const T in_max, const T out_min, const T out_max) noexcept {
161 D64 slope = 0.0;
162 return MAP(input, in_min, in_max, out_min, out_max, slope);
163}
164
165template <typename T>
166constexpr void REMAP(T& input, T in_min, T in_max, T out_min, T out_max) noexcept {
167 D64 slope = 0.0;
168 input = MAP(input, in_min, in_max, out_min, out_max, slope);
169}
170
171template <typename T>
172[[nodiscard]] vec2<T> COORD_REMAP(vec2<T> input, const Rect<T>& in_rect, const Rect<T>& out_rect) noexcept {
173 return vec2<T> {
174 MAP(input.x, in_rect.x, in_rect.x + in_rect.z, out_rect.x, out_rect.x + out_rect.z),
175 MAP(input.y, in_rect.y, in_rect.y + in_rect.w, out_rect.y, out_rect.y + out_rect.w)
176 };
177}
178
179template <typename T>
180[[nodiscard]] vec3<T> COORD_REMAP(vec3<T> input, const Rect<T>& in_rect, const Rect<T>& out_rect) noexcept {
181 return vec3<T>(COORD_REMAP(input.xy(), in_rect, out_rect), input.z);
182}
183
184template <typename T>
185[[nodiscard]] T NORMALIZE(T input, const T range_min, const T range_max) noexcept {
186 return MAP<T>(input, range_min, range_max, T(0), T(1));
187}
188
189template<typename T>
190void CLAMP_IN_RECT(T& inout_x, T& inout_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept;
191
192template<typename T>
193void CLAMP_IN_RECT(T& inout_x, T& inout_y, const Rect<T>& rect) noexcept;
194
195template<typename T>
196void CLAMP_IN_RECT(T& inout_x, T& inout_y, const vec4<T>& rect) noexcept;
197
198template<typename T>
199[[nodiscard]] bool COORDS_IN_RECT(T input_x, T input_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept;
200
201template<typename T>
202[[nodiscard]] bool COORDS_IN_RECT(T input_x, T input_y, const Rect<T>& rect) noexcept;
203
204template<typename T>
205[[nodiscard]] bool COORDS_IN_RECT(T input_x, T input_y, const vec4<T>& rect) noexcept;
206
207[[nodiscard]] constexpr U32 nextPOW2(U32 n) noexcept;
208[[nodiscard]] constexpr U32 prevPOW2(U32 n) noexcept;
209
210// Calculate the smallest NxN matrix that can hold the specified
211// number of elements. Returns N
212[[nodiscard]] constexpr U32 minSquareMatrixSize(U32 elementCount) noexcept;
213
214template <typename T, typename U>
215[[nodiscard]] T Lerp(T v1, T v2, U t) noexcept;
216
217template <typename T, typename U>
218[[nodiscard]] T FastLerp(T v1, T v2, U t) noexcept;
219
220template <typename T>
221[[nodiscard]] T Sqrt(T input) noexcept;
222
223template <typename T, typename U>
224[[nodiscard]] T Sqrt(U input) noexcept;
225
227[[nodiscard]] constexpr U8 PACKED_FLOAT_TO_CHAR_UNORM(F32_SNORM value) noexcept;
228[[nodiscard]] constexpr F32_SNORM UNORM_CHAR_TO_PACKED_FLOAT(U8 value) noexcept;
229
231[[nodiscard]] constexpr I8 FLOAT_TO_CHAR_SNORM(F32_SNORM value) noexcept;
232// Returns clamped value / 128.f
233[[nodiscard]] constexpr F32_SNORM SNORM_CHAR_TO_FLOAT(I8 value) noexcept;
234
236[[nodiscard]] constexpr F32_NORM UNORM_CHAR_TO_FLOAT(U8 value) noexcept;
238[[nodiscard]] constexpr U8 FLOAT_TO_CHAR_UNORM(F32_NORM value) noexcept;
239
241[[nodiscard]] F32 FRACT(F32 floatValue) noexcept;
242
243// bit manipulation
244template<typename T1, typename T2>
245constexpr auto BitSet(T1& arg, const T2 pos) { return arg |= 1 << pos; }
246
247template<typename T1, typename T2>
248constexpr auto BitClr(T1& arg, const T2 pos) { return arg &= ~(1 << pos); }
249
250template<typename T1, typename T2>
251constexpr bool BitTst(const T1 arg, const T2 pos) { return (arg & 1 << pos) != 0; }
252
253template<typename T1, typename T2>
254constexpr bool BitDiff(const T1 arg1, const T2 arg2) { return arg1 ^ arg2; }
255
256template<typename T1, typename T2, typename T3>
257constexpr bool BitCmp(const T1 arg1, const T2 arg2, const T3 pos) { return arg1 << pos == arg2 << pos; }
258
259// bitmask manipulation
260template<typename T1, typename T2>
261constexpr auto BitMaskSet(T1& arg, const T2 mask) { return arg |= mask; }
262template<typename T1, typename T2>
263constexpr auto BitMaskClear(T1& arg, const T2 mask) { return arg &= ~mask; }
264template<typename T1, typename T2>
265constexpr auto BitMaskFlip(T1& arg, const T2 mask) { return arg ^= mask; }
266template<typename T1, typename T2>
267constexpr auto BitMaskCheck(T1& arg, const T2 mask) { return arg & mask; }
268
269namespace Angle {
270
271template<typename T>
272using RADIANS = T;
273
274template<typename T>
275using DEGREES = T;
276
277template <typename T>
278[[nodiscard]] constexpr DEGREES<T> to_VerticalFoV(DEGREES<T> horizontalFoV, D64 aspectRatio) noexcept;
279template <typename T>
280[[nodiscard]] constexpr DEGREES<T> to_HorizontalFoV(DEGREES<T> verticalFoV, D64 aspectRatio) noexcept;
281template <typename T>
282[[nodiscard]] constexpr RADIANS<T> to_RADIANS(DEGREES<T> angle) noexcept;
283template <typename T>
284[[nodiscard]] constexpr DEGREES<T> to_DEGREES(RADIANS<T> angle) noexcept;
285template <typename T>
286[[nodiscard]] constexpr vec2<RADIANS<T>> to_RADIANS(vec2<DEGREES<T>> angle) noexcept;
287template <typename T>
288[[nodiscard]] constexpr vec2<DEGREES<T>> to_DEGREES(vec2<RADIANS<T>> angle) noexcept;
289template <typename T>
290[[nodiscard]] constexpr vec3<RADIANS<T>> to_RADIANS(const vec3<DEGREES<T>>& angle) noexcept;
291template <typename T>
292[[nodiscard]] constexpr vec3<DEGREES<T>> to_DEGREES(const vec3<RADIANS<T>>& angle) noexcept;
293template <typename T>
294[[nodiscard]] constexpr vec4<RADIANS<T>> to_RADIANS(const vec4<DEGREES<T>>& angle) noexcept;
295template <typename T>
296[[nodiscard]] constexpr vec4<DEGREES<T>> to_DEGREES(const vec4<RADIANS<T>>& angle) noexcept;
297
299template <typename T>
300[[nodiscard]] constexpr T DegreesToRadians(T angleDegrees) noexcept;
302template <typename T>
303[[nodiscard]] constexpr T RadiansToDegrees(T angleRadians) noexcept;
305template <typename T>
306[[nodiscard]] constexpr T Degrees(T degrees) noexcept;
308template <typename T>
309[[nodiscard]] constexpr T Radians(T radians) noexcept;
310};
311
312namespace Metric {
314template <typename T>
315[[nodiscard]] constexpr T Tera(T a);
317template <typename T>
318[[nodiscard]] constexpr T Giga(T a);
320template <typename T>
321[[nodiscard]] constexpr T Mega(T a);
323template <typename T>
324[[nodiscard]] constexpr T Kilo(T a);
326template <typename T>
327[[nodiscard]] constexpr T Hecto(T a);
329template <typename T>
330[[nodiscard]] constexpr T Deca(T a);
332template <typename T>
333[[nodiscard]] constexpr T Base(T a);
335template <typename T>
336[[nodiscard]] constexpr T Deci(T a);
338template <typename T>
339[[nodiscard]] constexpr T Centi(T a);
341template <typename T>
342[[nodiscard]] constexpr T Milli(T a);
344template <typename T>
345[[nodiscard]] constexpr T Micro(T a);
347template <typename T>
348[[nodiscard]] constexpr T Nano(T a);
350template <typename T>
351[[nodiscard]] constexpr T Pico(T a);
352
354template <typename T, typename U>
355[[nodiscard]] constexpr T Tera(U a);
357template <typename T, typename U>
358[[nodiscard]] constexpr T Giga(U a);
360template <typename T, typename U>
361[[nodiscard]] constexpr T Mega(U a);
363template <typename T, typename U>
364[[nodiscard]] constexpr T Kilo(U a);
366template <typename T, typename U>
367[[nodiscard]] constexpr T Hecto(U a);
369template <typename T, typename U>
370[[nodiscard]] constexpr T Deca(U a);
372template <typename T, typename U>
373[[nodiscard]] constexpr T Base(U a);
375template <typename T, typename U>
376[[nodiscard]] constexpr T Deci(U a);
378template <typename T, typename U>
379[[nodiscard]] constexpr T Centi(U a);
381template <typename T, typename U>
382[[nodiscard]] constexpr T Milli(U a);
384template <typename T, typename U>
385[[nodiscard]] constexpr T Micro(U a);
387template <typename T, typename U>
388[[nodiscard]] constexpr T Nano(U a);
390template <typename T, typename U>
391[[nodiscard]] constexpr T Pico(U a);
392}
393
395{
396 U8 _hour = 0u;
398};
399
403};
404
405namespace Time {
408template <typename T>
409[[nodiscard]] constexpr T Hours(T a);
410template <typename T>
411[[nodiscard]] constexpr T Minutes( T a );
412template <typename T>
413[[nodiscard]] constexpr T Seconds( T a );
414template <typename T>
415[[nodiscard]] constexpr T Milliseconds(T a);
416template <typename T>
417[[nodiscard]] constexpr T Microseconds(T a);
418template <typename T>
419[[nodiscard]] constexpr T Nanoseconds(T a);
420
421template <typename T, typename U>
422[[nodiscard]] constexpr T Hours( U a );
423template <typename T, typename U>
424[[nodiscard]] constexpr T Minutes( U a );
425template <typename T, typename U>
426[[nodiscard]] constexpr T Seconds(U a);
427template <typename T, typename U>
428[[nodiscard]] constexpr T Milliseconds(U a);
429template <typename T, typename U>
430[[nodiscard]] constexpr T Microseconds(U a);
431template <typename T, typename U>
432[[nodiscard]] constexpr T Nanoseconds(U a);
433
434template <typename T = D64, typename U>
435[[nodiscard]] constexpr T NanosecondsToSeconds(U a) noexcept;
436template <typename T = D64, typename U>
437[[nodiscard]] constexpr T NanosecondsToMilliseconds(U a) noexcept;
438template <typename T = U64, typename U>
439[[nodiscard]] constexpr T NanosecondsToMicroseconds(U a) noexcept;
440
441template <typename T = D64, typename U>
442[[nodiscard]] constexpr T MicrosecondsToSeconds(U a) noexcept;
443template <typename T = U64, typename U>
444[[nodiscard]] constexpr T MicrosecondsToMilliseconds(U a) noexcept;
445template <typename T = U64, typename U>
446[[nodiscard]] constexpr T MicrosecondsToNanoseconds(U a) noexcept;
447
448template <typename T = D64, typename U>
449[[nodiscard]] constexpr T MillisecondsToSeconds(U a) noexcept;
450template <typename T = U64, typename U>
451[[nodiscard]] constexpr T MillisecondsToMicroseconds(U a) noexcept;
452template <typename T = U64, typename U>
453[[nodiscard]] constexpr T MillisecondsToNanoseconds(U a) noexcept;
454
455template <typename T = D64, typename U>
456[[nodiscard]] constexpr T SecondsToMilliseconds(U a) noexcept;
457template <typename T = U64, typename U>
458[[nodiscard]] constexpr T SecondsToMicroseconds(U a) noexcept;
459template <typename T = U64, typename U>
460[[nodiscard]] constexpr T SecondsToNanoseconds(U a) noexcept;
461
462} // namespace Time
463
464namespace Util {
465
466struct Circle {
467 F32 center[2] = {0.0f, 0.0f};
468 F32 radius = 1.f;
469};
470
471[[nodiscard]] bool IntersectCircles(const Circle& cA, const Circle& cB, vec2<F32>* pointsOut) noexcept;
472
473[[nodiscard]] ptrdiff_t size_tGetAlignmentCorrected(const size_t value, const size_t alignment) noexcept;
474
476template <typename T, typename... Rest>
477void Hash_combine(size_t& seed, const T& v, const Rest&... rest) noexcept;
478
479// U = to data type, T = from data type
480template <typename U, typename T>
481[[nodiscard]] U ConvertData(const T& data);
482
483template<class FwdIt, class Compare = std::less<typename std::iterator_traits<FwdIt>::value_type>>
484void InsertionSort(FwdIt first, FwdIt last, Compare cmp = Compare());
485
497void Normalize(vec3<F32>& inputRotation, bool degrees = false,
498 bool normYaw = true, bool normPitch = true,
499 bool normRoll = true) noexcept;
500
501[[nodiscard]] UColour4 ToByteColour(const FColour4& floatColour) noexcept;
502[[nodiscard]] UColour3 ToByteColour(const FColour3& floatColour) noexcept;
503[[nodiscard]] FColour4 ToFloatColour(const UColour4& byteColour) noexcept;
504[[nodiscard]] FColour3 ToFloatColour(const UColour3& byteColour) noexcept;
505[[nodiscard]] FColour4 ToFloatColour( const vec4<U32>& colour ) noexcept;
506[[nodiscard]] FColour3 ToFloatColour( const vec3<U32>& colour ) noexcept;
507
508void ToByteColour(const FColour4& floatColour, UColour4& colourOut) noexcept;
509void ToByteColour(const FColour3& floatColour, UColour3& colourOut) noexcept;
510void ToFloatColour(const UColour4& byteColour, FColour4& colourOut) noexcept;
511void ToFloatColour(const UColour3& byteColour, FColour3& colourOut) noexcept;
512void ToFloatColour( const vec4<U32>& uintColour, FColour4& colourOut ) noexcept;
513void ToFloatColour( const vec3<U32>& uintColour, FColour3& colourOut ) noexcept;
514
515bool decomposeMatrix(const mat4<F32>& transform,
516 vec3<F32>& translationOut,
517 vec3<F32>& scaleOut,
518 vec3<Angle::RADIANS<F32>>& rotationOut,
519 bool& isUniformScaleOut);
520
521bool decomposeMatrix(const mat4<F32>& transform,
522 vec3<F32>& translationOut,
523 vec3<F32>& scaleOut,
524 vec3<Angle::RADIANS<F32>>& rotationOut);
525
526bool decomposeMatrix(const mat4<F32>& transform,
527 vec3<F32>& translationOut,
528 vec3<F32>& scaleOut);
529
530bool decomposeMatrix(const mat4<F32>& transform,
531 vec3<F32>& translationOut);
532
533//ref: https://community.khronos.org/t/glsl-packing-a-normal-in-a-single-float/52039/3
534// Pack 3 values into 1 float
535[[nodiscard]] F32 PACK_VEC3(F32_SNORM x, F32_SNORM y, F32_SNORM z) noexcept;
536[[nodiscard]] F32 PACK_VEC3(U8 x, U8 y, U8 z) noexcept;
537
538[[nodiscard]] F32 PACK_VEC3(const vec3<F32_SNORM>& value) noexcept;
539
540[[nodiscard]] U32 PACK_HALF2x16(vec2<F32> value);
541void UNPACK_HALF2x16(U32 src, vec2<F32>& value);
542[[nodiscard]] vec2<F32> UNPACK_HALF2x16(U32 src);
543
544[[nodiscard]] U32 PACK_HALF2x16(F32 x, F32 y);
545void UNPACK_HALF2x16(U32 src, F32& x, F32& y);
546
548[[nodiscard]] U16 PACK_HALF1x16(F32 value);
550void UNPACK_HALF1x16(U16 src, F32& value);
552[[nodiscard]] F32 UNPACK_HALF1x16(U16 src);
553
554[[nodiscard]] F32 UINT_TO_FLOAT(U32 src);
556
557[[nodiscard]] F32 INT_TO_FLOAT(I32 src);
558I32 FLOAT_TO_INT(F32 src);
559
560[[nodiscard]] U32 PACK_UNORM4x8(const vec4<F32_NORM>& value);
561[[nodiscard]] U32 PACK_UNORM4x8(vec4<U8> value);
562void UNPACK_UNORM4x8(U32 src, vec4<F32_NORM>& value);
563
564[[nodiscard]] U32 PACK_UNORM4x8(F32_NORM x, F32_NORM y, F32_NORM z, F32_NORM w);
565[[nodiscard]] U32 PACK_UNORM4x8(U8 x, U8 y, U8 z, U8 w);
566void UNPACK_UNORM4x8(U32 src, F32_NORM& x, F32_NORM& y, F32_NORM& z, F32_NORM& w);
567void UNPACK_UNORM4x8(U32 src, U8& x, U8& y, U8& z, U8& w);
568[[nodiscard]] vec4<U8> UNPACK_UNORM4x8_U8(U32 src);
569[[nodiscard]] vec4<F32_NORM> UNPACK_UNORM4x8_F32(U32 src);
570
571// UnPack 3 values from 1 float
572void UNPACK_VEC3(F32 src, F32_SNORM& x, F32_SNORM& y, F32_SNORM& z) noexcept;
573void UNPACK_VEC3(F32 src, vec3<F32_SNORM>& res) noexcept;
574[[nodiscard]] vec3<F32_SNORM> UNPACK_VEC3(F32 src) noexcept;
575
576[[nodiscard]] U32 PACK_11_11_10(const vec3<F32_NORM>& value);
577void UNPACK_11_11_10(U32 src, vec3<F32_NORM>& res);
578[[nodiscard]] vec3<F32_NORM> UNPACK_11_11_10(U32 src);
579
580[[nodiscard]] U32 PACK_11_11_10(F32_NORM x, F32_NORM y, F32_NORM z);
581void UNPACK_11_11_10(U32 src, F32_NORM& x, F32_NORM& y, F32_NORM& z);
582
583} // namespace Util
584} // namespace Divide
585
586namespace std {
587 template<typename T, size_t N>
588 struct hash<array<T, N> >
589 {
590 using argument_type = array<T, N>;
591 using result_type = size_t;
592
594 {
595 result_type h = 17;
596 for (const T& elem : a)
597 {
599 }
600 return h;
601 }
602 };
603}
604
605#endif //DVD_CORE_MATH_MATH_HELPER_H_
606
607#include "MathHelper.inl"
constexpr T Degrees(T degrees) noexcept
Returns the specified value. Used only for emphasis.
Definition: MathHelper.inl:442
constexpr T DegreesToRadians(T angleDegrees) noexcept
Return the radian equivalent of the given degree value.
Definition: MathHelper.inl:429
constexpr DEGREES< T > to_DEGREES(RADIANS< T > angle) noexcept
Definition: MathHelper.inl:380
constexpr T RadiansToDegrees(T angleRadians) noexcept
Return the degree equivalent of the given radian value.
Definition: MathHelper.inl:436
constexpr RADIANS< T > to_RADIANS(DEGREES< T > angle) noexcept
Definition: MathHelper.inl:368
constexpr DEGREES< T > to_VerticalFoV(DEGREES< T > horizontalFoV, D64 aspectRatio) noexcept
Definition: MathHelper.inl:352
constexpr T Radians(T radians) noexcept
Returns the specified value. Used only for emphasis.
Definition: MathHelper.inl:449
constexpr DEGREES< T > to_HorizontalFoV(DEGREES< T > verticalFoV, D64 aspectRatio) noexcept
Definition: MathHelper.inl:360
constexpr T Nano(T a)
Base value * 0.000000001.
Definition: MathHelper.inl:538
constexpr T Milli(T a)
Base value * 0.001.
Definition: MathHelper.inl:524
constexpr T Kilo(T a)
Base value * 1000.
Definition: MathHelper.inl:482
constexpr T Mega(T a)
Base value * 1000000.
Definition: MathHelper.inl:475
constexpr T Tera(T a)
Base value * 1000000000000.
Definition: MathHelper.inl:461
constexpr T Centi(T a)
Base value * 0.01.
Definition: MathHelper.inl:517
constexpr T Deci(T a)
Base value * 0.1.
Definition: MathHelper.inl:510
constexpr T Pico(T a)
Base value * 0.000000000001.
Definition: MathHelper.inl:545
constexpr T Deca(T a)
Base value * 10.
Definition: MathHelper.inl:496
constexpr T Giga(T a)
Base value * 1000000000.
Definition: MathHelper.inl:468
constexpr T Base(T a)
Base value.
Definition: MathHelper.inl:503
constexpr T Micro(T a)
Base value * 0.000001.
Definition: MathHelper.inl:531
constexpr T Hecto(T a)
Base value * 100.
Definition: MathHelper.inl:489
constexpr T MillisecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:743
constexpr T MicrosecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:725
constexpr T SecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:773
constexpr T MicrosecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:731
constexpr T NanosecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:713
constexpr T Milliseconds(T a)
Definition: MathHelper.inl:653
constexpr T Hours(T a)
Definition: MathHelper.inl:635
constexpr T SecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:767
constexpr T Minutes(T a)
Definition: MathHelper.inl:641
constexpr T NanosecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:707
constexpr T Microseconds(T a)
Definition: MathHelper.inl:659
constexpr T NanosecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:719
constexpr T SecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:761
constexpr T MicrosecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:737
constexpr T MillisecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:749
constexpr T MillisecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:755
constexpr T Nanoseconds(T a)
Definition: MathHelper.inl:665
constexpr T Seconds(T a)
Definition: MathHelper.inl:647
U ConvertData(const T &data)
F32 INT_TO_FLOAT(I32 src)
Definition: MathHelper.cpp:349
void UNPACK_11_11_10(U32 src, vec3< F32_NORM > &res)
Definition: MathHelper.cpp:430
U32 PACK_11_11_10(const vec3< F32_NORM > &value)
Definition: MathHelper.cpp:426
bool decomposeMatrix(const mat4< F32 > &transform, vec3< F32 > &translationOut, vec3< F32 > &scaleOut, vec3< Angle::RADIANS< F32 > > &rotationOut, bool &isUniformScaleOut)
Definition: MathHelper.cpp:14
F32 PACK_VEC3(F32_SNORM x, F32_SNORM y, F32_SNORM z) noexcept
Definition: MathHelper.inl:316
void Normalize(vec3< F32 > &inputRotation, bool degrees=false, bool normYaw=true, bool normPitch=true, bool normRoll=true) noexcept
Normalise the selected rotations to be within the +/-180 degree range.
Definition: MathHelper.cpp:449
UColour4 ToByteColour(const FColour4 &floatColour) noexcept
Definition: MathHelper.cpp:256
FColour4 ToFloatColour(const UColour4 &byteColour) noexcept
Definition: MathHelper.cpp:268
bool IntersectCircles(const Circle &cA, const Circle &cB, vec2< F32 > *pointsOut) noexcept
Definition: MathHelper.cpp:150
void UNPACK_VEC3(F32 src, F32_SNORM &x, F32_SNORM &y, F32_SNORM &z) noexcept
Definition: MathHelper.inl:335
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
void UNPACK_HALF1x16(U16 src, F32 &value)
Only convert the range [-1024., 1024.] for accurate results.
Definition: MathHelper.cpp:331
U32 PACK_UNORM4x8(const vec4< F32_NORM > &value)
Definition: MathHelper.cpp:369
F32 UINT_TO_FLOAT(U32 src)
Definition: MathHelper.cpp:341
vec4< U8 > UNPACK_UNORM4x8_U8(U32 src)
Definition: MathHelper.cpp:414
ptrdiff_t size_tGetAlignmentCorrected(const size_t value, const size_t alignment) noexcept
U32 FLOAT_TO_UINT(F32 src)
Definition: MathHelper.cpp:345
U16 PACK_HALF1x16(F32 value)
Only convert the range [-1024., 1024.] for accurate results.
Definition: MathHelper.cpp:327
I32 FLOAT_TO_INT(F32 src)
Definition: MathHelper.cpp:353
vec4< F32_NORM > UNPACK_UNORM4x8_F32(U32 src)
Definition: MathHelper.cpp:420
U32 PACK_HALF2x16(vec2< F32 > value)
Definition: MathHelper.cpp:312
void UNPACK_HALF2x16(U32 src, vec2< F32 > &value)
Definition: MathHelper.cpp:316
void InsertionSort(FwdIt first, FwdIt last, Compare cmp=Compare())
Definition: MathHelper.inl:816
void UNPACK_UNORM4x8(U32 src, vec4< F32_NORM > &value)
Definition: MathHelper.cpp:377
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr F32 M_PI_f
Definition: MathHelper.h:86
FORCE_INLINE bool Compare(const GUIDWrapper *const lhs, const GUIDWrapper *const rhs) noexcept
Definition: GUIDWrapper.h:73
constexpr D64 M_PI2
Definition: MathHelper.h:84
constexpr U8 FLOAT_TO_CHAR_UNORM(F32_NORM value) noexcept
Returns round(value * 255)
Definition: MathHelper.inl:307
::value constexpr void CLAMP_01(T &n) noexcept
Definition: MathHelper.inl:120
constexpr I8 FLOAT_TO_CHAR_SNORM(F32_SNORM value) noexcept
Returns clamped value * 128.
Definition: MathHelper.inl:276
constexpr F32_SNORM UNORM_CHAR_TO_PACKED_FLOAT(U8 value) noexcept
Definition: MathHelper.inl:295
constexpr U32 nextPOW2(U32 n) noexcept
Definition: MathHelper.inl:207
T Lerp(T v1, T v2, U t) noexcept
Definition: MathHelper.inl:240
constexpr F32 M_PIDIV360_f
Definition: MathHelper.h:92
constexpr T SQUARED(T input) noexcept
Definition: MathHelper.inl:156
constexpr F32_NORM UNORM_CHAR_TO_FLOAT(U8 value) noexcept
Returns value / 255.f.
Definition: MathHelper.inl:302
typename std::conditional< sizeof(T)==8, U64, U32 >::type UnsignedIntegerBasedOnSize
Definition: MathHelper.h:100
T Sqrt(T input) noexcept
Definition: MathHelper.inl:252
F32 FRACT(F32 floatValue) noexcept
Helper method to emulate GLSL.
Definition: MathHelper.inl:271
int32_t I32
constexpr void REMAP(T &input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
Definition: MathHelper.inl:149
uint8_t U8
constexpr auto BitMaskCheck(T1 &arg, const T2 mask)
Definition: MathHelper.h:267
void CLAMP_IN_RECT(T &inout_x, T &inout_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
Definition: MathHelper.inl:162
constexpr F32 to_F32(const T value)
constexpr F32 M_PI_2_f
Definition: MathHelper.h:87
constexpr bool BitDiff(const T1 arg1, const T2 arg2)
Definition: MathHelper.h:254
T Random()
Definition: MathHelper.inl:95
constexpr F32 M_2PI_f
Definition: MathHelper.h:89
constexpr I32 SIGN(const T val)
Definition: MathHelper.h:133
constexpr auto BitSet(T1 &arg, const T2 pos)
Definition: MathHelper.h:245
constexpr U32 prevPOW2(U32 n) noexcept
Definition: MathHelper.inl:219
constexpr F32 M_180DIVPI_f
Definition: MathHelper.h:91
void SeedRandom()
Definition: MathHelper.inl:101
constexpr auto BitClr(T1 &arg, const T2 pos)
Definition: MathHelper.h:248
constexpr U8 PACKED_FLOAT_TO_CHAR_UNORM(F32_SNORM value) noexcept
Helper methods to go from an snorm float (-1...1) to packed unorm char (value => (value + 1) * 0....
Definition: MathHelper.inl:287
constexpr bool BitCmp(const T1 arg1, const T2 arg2, const T3 pos)
Definition: MathHelper.h:257
uint16_t U16
vec2< T > COORD_REMAP(vec2< T > input, const Rect< T > &in_rect, const Rect< T > &out_rect) noexcept
Definition: MathHelper.h:172
typename std::conditional< sizeof(T)==8, I64, I32 >::type SignedIntegerBasedOnSize
Definition: MathHelper.h:98
constexpr D64 M_PI_2
Definition: MathHelper.h:77
constexpr D64 M_2PI
Definition: MathHelper.h:80
::value constexpr T MAP(T input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
Definition: MathHelper.inl:141
::value constexpr T CLAMPED(T n, T min, T max) noexcept
Definition: MathHelper.inl:126
constexpr F32 M_PI_4_f
Definition: MathHelper.h:88
constexpr D64 M_PIDIV180
Definition: MathHelper.h:81
double D64
constexpr F32 M_PIDIV180_f
Definition: MathHelper.h:90
T NORMALIZE(T input, const T range_min, const T range_max) noexcept
Definition: MathHelper.h:185
constexpr F32_SNORM SNORM_CHAR_TO_FLOAT(I8 value) noexcept
Definition: MathHelper.inl:282
constexpr U32 minSquareMatrixSize(U32 elementCount) noexcept
Definition: MathHelper.inl:229
constexpr D64 M_PI
Definition: MathHelper.h:76
constexpr D64 M_180DIVPI
Definition: MathHelper.h:82
constexpr F32 INV_RAND_MAX
Definition: MathHelper.h:95
constexpr bool BitTst(const T1 arg, const T2 pos)
Definition: MathHelper.h:251
bool COORDS_IN_RECT(T input_x, T input_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
Definition: MathHelper.inl:181
::value constexpr void CLAMP(T &n, T min, T max) noexcept
Clamps value n between min and max.
Definition: MathHelper.inl:114
typename std::conditional< std::is_integral< T >::value, std::uniform_int_distribution< IntegerTypeBasedOnSign< T > >, std::uniform_real_distribution< T > >::type DefaultDistribution
Definition: MathHelper.h:109
T FastLerp(T v1, T v2, U t) noexcept
Definition: MathHelper.inl:246
constexpr D64 M_PIDIV360
Definition: MathHelper.h:83
constexpr auto BitMaskSet(T1 &arg, const T2 mask)
Definition: MathHelper.h:261
int64_t I64
constexpr auto BitMaskClear(T1 &arg, const T2 mask)
Definition: MathHelper.h:263
uint32_t U32
typename std::conditional< std::is_unsigned< T >::value, UnsignedIntegerBasedOnSize< T >, SignedIntegerBasedOnSize< T > >::type IntegerTypeBasedOnSign
Definition: MathHelper.h:104
uint64_t U64
constexpr F32 M_PI2_f
Definition: MathHelper.h:93
constexpr auto BitMaskFlip(T1 &arg, const T2 mask)
Definition: MathHelper.h:265
::value constexpr T CLAMPED_01(T n) noexcept
Definition: MathHelper.inl:134
array< T, N > argument_type
Definition: MathHelper.h:590
result_type operator()(const argument_type &a) const
Definition: MathHelper.h:593