40#ifndef DVD_CORE_MATH_MATH_HELPER_H_
41#define DVD_CORE_MATH_MATH_HELPER_H_
47#define TO_MEGABYTES(X) ((X) * 1024u * 1024u)
50concept ValidMathType = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
108 std::uniform_int_distribution<IntegerTypeBasedOnSign<T>>,
109 std::uniform_real_distribution<T>>::type;
112 typename Engine = std::mt19937_64,
114[[nodiscard]] T
Random(T min, T max);
117 typename Engine = std::mt19937_64,
119[[nodiscard]] T
Random(T max);
122 typename Engine = std::mt19937_64,
126template<
typename Engine = std::mt19937_64>
129template<
typename Engine = std::mt19937_64>
134 return (val < 0) ? -1 : (val > 0) ? 1 : 0;
138template <
typename T>
requires std::is_arithmetic<T>::value
139constexpr void CLAMP(T& n, T min, T max)
noexcept;
141template <
typename T>
requires std::is_arithmetic<T>::value
142constexpr void CLAMP_01(T& n)
noexcept;
144template <
typename T>
requires std::is_arithmetic<T>::value
145[[nodiscard]]
constexpr T
CLAMPED(T n, T min, T max)
noexcept;
147template <
typename T>
requires std::is_arithmetic<T>::value
148[[nodiscard]]
constexpr T
CLAMPED_01(T n)
noexcept;
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;
154constexpr void REMAP(T& input, T in_min, T in_max, T out_min, T out_max,
D64& slopeOut)
noexcept;
157[[nodiscard]]
constexpr T
SQUARED(T input)
noexcept;
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 {
162 return MAP(input, in_min, in_max, out_min, out_max, slope);
166constexpr void REMAP(T& input, T in_min, T in_max, T out_min, T out_max)
noexcept {
168 input =
MAP(input, in_min, in_max, out_min, out_max, slope);
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)
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));
190void CLAMP_IN_RECT(T& inout_x, T& inout_y, T rect_x, T rect_y, T rect_z, T rect_w)
noexcept;
193void CLAMP_IN_RECT(T& inout_x, T& inout_y,
const Rect<T>& rect)
noexcept;
196void CLAMP_IN_RECT(T& inout_x, T& inout_y,
const vec4<T>& rect)
noexcept;
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;
202[[nodiscard]]
bool COORDS_IN_RECT(T input_x, T input_y,
const Rect<T>& rect)
noexcept;
205[[nodiscard]]
bool COORDS_IN_RECT(T input_x, T input_y,
const vec4<T>& rect)
noexcept;
214template <
typename T,
typename U>
215[[nodiscard]] T
Lerp(T v1, T v2, U t)
noexcept;
217template <
typename T,
typename U>
218[[nodiscard]] T
FastLerp(T v1, T v2, U t)
noexcept;
221[[nodiscard]] T
Sqrt(T input)
noexcept;
223template <
typename T,
typename U>
224[[nodiscard]] T
Sqrt(U input)
noexcept;
244template<
typename T1,
typename T2>
245constexpr auto BitSet(T1& arg,
const T2 pos) {
return arg |= 1 << pos; }
247template<
typename T1,
typename T2>
248constexpr auto BitClr(T1& arg,
const T2 pos) {
return arg &= ~(1 << pos); }
250template<
typename T1,
typename T2>
251constexpr bool BitTst(
const T1 arg,
const T2 pos) {
return (arg & 1 << pos) != 0; }
253template<
typename T1,
typename T2>
254constexpr bool BitDiff(
const T1 arg1,
const T2 arg2) {
return arg1 ^ arg2; }
256template<
typename T1,
typename T2,
typename T3>
257constexpr bool BitCmp(
const T1 arg1,
const T2 arg2,
const T3 pos) {
return arg1 << pos == arg2 << pos; }
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; }
306[[nodiscard]]
constexpr T
Degrees(T degrees)
noexcept;
309[[nodiscard]]
constexpr T
Radians(T radians)
noexcept;
315[[nodiscard]]
constexpr T
Tera(T a);
318[[nodiscard]]
constexpr T
Giga(T a);
321[[nodiscard]]
constexpr T
Mega(T a);
324[[nodiscard]]
constexpr T
Kilo(T a);
327[[nodiscard]]
constexpr T
Hecto(T a);
330[[nodiscard]]
constexpr T
Deca(T a);
333[[nodiscard]]
constexpr T
Base(T a);
336[[nodiscard]]
constexpr T
Deci(T a);
339[[nodiscard]]
constexpr T
Centi(T a);
342[[nodiscard]]
constexpr T
Milli(T a);
345[[nodiscard]]
constexpr T
Micro(T a);
348[[nodiscard]]
constexpr T
Nano(T a);
351[[nodiscard]]
constexpr T
Pico(T a);
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);
409[[nodiscard]]
constexpr T
Hours(T a);
411[[nodiscard]]
constexpr T
Minutes( T a );
413[[nodiscard]]
constexpr T
Seconds( T a );
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>
429template <
typename T,
typename U>
431template <
typename T,
typename U>
434template <
typename T = D64,
typename U>
436template <
typename T = D64,
typename U>
438template <
typename T = U64,
typename U>
441template <
typename T = D64,
typename U>
443template <
typename T = U64,
typename U>
445template <
typename T = U64,
typename U>
448template <
typename T = D64,
typename U>
450template <
typename T = U64,
typename U>
452template <
typename T = U64,
typename U>
455template <
typename T = D64,
typename U>
457template <
typename T = U64,
typename U>
459template <
typename T = U64,
typename U>
476template <
typename T,
typename... Rest>
477void Hash_combine(
size_t& seed,
const T& v,
const Rest&... rest)
noexcept;
480template <
typename U,
typename T>
483template<class FwdIt, class Compare = std::less<typename std::iterator_traits<FwdIt>::value_type>>
498 bool normYaw =
true,
bool normPitch =
true,
499 bool normRoll =
true) noexcept;
518 vec3<Angle::RADIANS<
F32>>& rotationOut,
519 bool& isUniformScaleOut);
524 vec3<Angle::RADIANS<
F32>>& rotationOut);
587 template<
typename T,
size_t N>
588 struct hash<array<T, N> >
596 for (
const T& elem : a)
constexpr T Degrees(T degrees) noexcept
Returns the specified value. Used only for emphasis.
constexpr T DegreesToRadians(T angleDegrees) noexcept
Return the radian equivalent of the given degree value.
constexpr DEGREES< T > to_DEGREES(RADIANS< T > angle) noexcept
constexpr T RadiansToDegrees(T angleRadians) noexcept
Return the degree equivalent of the given radian value.
constexpr RADIANS< T > to_RADIANS(DEGREES< T > angle) noexcept
constexpr DEGREES< T > to_VerticalFoV(DEGREES< T > horizontalFoV, D64 aspectRatio) noexcept
constexpr T Radians(T radians) noexcept
Returns the specified value. Used only for emphasis.
constexpr DEGREES< T > to_HorizontalFoV(DEGREES< T > verticalFoV, D64 aspectRatio) noexcept
constexpr T Nano(T a)
Base value * 0.000000001.
constexpr T Milli(T a)
Base value * 0.001.
constexpr T Kilo(T a)
Base value * 1000.
constexpr T Mega(T a)
Base value * 1000000.
constexpr T Tera(T a)
Base value * 1000000000000.
constexpr T Centi(T a)
Base value * 0.01.
constexpr T Deci(T a)
Base value * 0.1.
constexpr T Pico(T a)
Base value * 0.000000000001.
constexpr T Deca(T a)
Base value * 10.
constexpr T Giga(T a)
Base value * 1000000000.
constexpr T Base(T a)
Base value.
constexpr T Micro(T a)
Base value * 0.000001.
constexpr T Hecto(T a)
Base value * 100.
constexpr T MillisecondsToSeconds(U a) noexcept
constexpr T MicrosecondsToSeconds(U a) noexcept
constexpr T SecondsToNanoseconds(U a) noexcept
constexpr T MicrosecondsToMilliseconds(U a) noexcept
constexpr T NanosecondsToMilliseconds(U a) noexcept
constexpr T Milliseconds(T a)
constexpr T SecondsToMicroseconds(U a) noexcept
constexpr T NanosecondsToSeconds(U a) noexcept
constexpr T Microseconds(T a)
constexpr T NanosecondsToMicroseconds(U a) noexcept
constexpr T SecondsToMilliseconds(U a) noexcept
constexpr T MicrosecondsToNanoseconds(U a) noexcept
constexpr T MillisecondsToMicroseconds(U a) noexcept
constexpr T MillisecondsToNanoseconds(U a) noexcept
constexpr T Nanoseconds(T a)
U ConvertData(const T &data)
F32 INT_TO_FLOAT(I32 src)
void UNPACK_11_11_10(U32 src, vec3< F32_NORM > &res)
U32 PACK_11_11_10(const vec3< F32_NORM > &value)
bool decomposeMatrix(const mat4< F32 > &transform, vec3< F32 > &translationOut, vec3< F32 > &scaleOut, vec3< Angle::RADIANS< F32 > > &rotationOut, bool &isUniformScaleOut)
F32 PACK_VEC3(F32_SNORM x, F32_SNORM y, F32_SNORM z) noexcept
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.
UColour4 ToByteColour(const FColour4 &floatColour) noexcept
FColour4 ToFloatColour(const UColour4 &byteColour) noexcept
bool IntersectCircles(const Circle &cA, const Circle &cB, vec2< F32 > *pointsOut) noexcept
void UNPACK_VEC3(F32 src, F32_SNORM &x, F32_SNORM &y, F32_SNORM &z) noexcept
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
void UNPACK_HALF1x16(U16 src, F32 &value)
Only convert the range [-1024., 1024.] for accurate results.
U32 PACK_UNORM4x8(const vec4< F32_NORM > &value)
F32 UINT_TO_FLOAT(U32 src)
vec4< U8 > UNPACK_UNORM4x8_U8(U32 src)
ptrdiff_t size_tGetAlignmentCorrected(const size_t value, const size_t alignment) noexcept
U32 FLOAT_TO_UINT(F32 src)
U16 PACK_HALF1x16(F32 value)
Only convert the range [-1024., 1024.] for accurate results.
I32 FLOAT_TO_INT(F32 src)
vec4< F32_NORM > UNPACK_UNORM4x8_F32(U32 src)
U32 PACK_HALF2x16(vec2< F32 > value)
void UNPACK_HALF2x16(U32 src, vec2< F32 > &value)
void InsertionSort(FwdIt first, FwdIt last, Compare cmp=Compare())
void UNPACK_UNORM4x8(U32 src, vec4< F32_NORM > &value)
Handle console commands that start with a forward slash.
FORCE_INLINE bool Compare(const GUIDWrapper *const lhs, const GUIDWrapper *const rhs) noexcept
constexpr U8 FLOAT_TO_CHAR_UNORM(F32_NORM value) noexcept
Returns round(value * 255)
::value constexpr void CLAMP_01(T &n) noexcept
constexpr I8 FLOAT_TO_CHAR_SNORM(F32_SNORM value) noexcept
Returns clamped value * 128.
constexpr F32_SNORM UNORM_CHAR_TO_PACKED_FLOAT(U8 value) noexcept
constexpr U32 nextPOW2(U32 n) noexcept
T Lerp(T v1, T v2, U t) noexcept
constexpr F32 M_PIDIV360_f
constexpr T SQUARED(T input) noexcept
constexpr F32_NORM UNORM_CHAR_TO_FLOAT(U8 value) noexcept
Returns value / 255.f.
typename std::conditional< sizeof(T)==8, U64, U32 >::type UnsignedIntegerBasedOnSize
F32 FRACT(F32 floatValue) noexcept
Helper method to emulate GLSL.
constexpr void REMAP(T &input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
constexpr auto BitMaskCheck(T1 &arg, const T2 mask)
void CLAMP_IN_RECT(T &inout_x, T &inout_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
constexpr F32 to_F32(const T value)
constexpr bool BitDiff(const T1 arg1, const T2 arg2)
constexpr I32 SIGN(const T val)
constexpr auto BitSet(T1 &arg, const T2 pos)
constexpr U32 prevPOW2(U32 n) noexcept
constexpr F32 M_180DIVPI_f
constexpr auto BitClr(T1 &arg, const T2 pos)
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....
constexpr bool BitCmp(const T1 arg1, const T2 arg2, const T3 pos)
vec2< T > COORD_REMAP(vec2< T > input, const Rect< T > &in_rect, const Rect< T > &out_rect) noexcept
typename std::conditional< sizeof(T)==8, I64, I32 >::type SignedIntegerBasedOnSize
::value constexpr T MAP(T input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
::value constexpr T CLAMPED(T n, T min, T max) noexcept
constexpr F32 M_PIDIV180_f
T NORMALIZE(T input, const T range_min, const T range_max) noexcept
constexpr F32_SNORM SNORM_CHAR_TO_FLOAT(I8 value) noexcept
constexpr U32 minSquareMatrixSize(U32 elementCount) noexcept
constexpr F32 INV_RAND_MAX
constexpr bool BitTst(const T1 arg, const T2 pos)
bool COORDS_IN_RECT(T input_x, T input_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
::value constexpr void CLAMP(T &n, T min, T max) noexcept
Clamps value n between min and max.
typename std::conditional< std::is_integral< T >::value, std::uniform_int_distribution< IntegerTypeBasedOnSign< T > >, std::uniform_real_distribution< T > >::type DefaultDistribution
T FastLerp(T v1, T v2, U t) noexcept
constexpr auto BitMaskSet(T1 &arg, const T2 mask)
constexpr auto BitMaskClear(T1 &arg, const T2 mask)
typename std::conditional< std::is_unsigned< T >::value, UnsignedIntegerBasedOnSize< T >, SignedIntegerBasedOnSize< T > >::type IntegerTypeBasedOnSign
constexpr auto BitMaskFlip(T1 &arg, const T2 mask)
::value constexpr T CLAMPED_01(T n) noexcept
array< T, N > argument_type
result_type operator()(const argument_type &a) const