2023-01-10 10:45:11 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 09/01/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_MATH_H
|
|
|
|
#define BLT_MATH_H
|
|
|
|
|
2023-02-28 23:20:56 -05:00
|
|
|
#include <blt/math/vectors.h>
|
|
|
|
#include <blt/math/matrix.h>
|
2023-02-07 22:34:10 -05:00
|
|
|
|
2023-12-29 19:32:16 -05:00
|
|
|
namespace blt
|
|
|
|
{
|
|
|
|
|
|
|
|
static inline constexpr double PI = 3.141592653589793238462643383279502884197;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline T toRadians(T deg)
|
|
|
|
{
|
|
|
|
constexpr double CONV = PI / 180.0;
|
|
|
|
return deg * CONV;
|
|
|
|
}
|
2023-03-14 17:30:22 -04:00
|
|
|
|
2023-02-28 23:50:13 -05:00
|
|
|
/**
|
|
|
|
* fast number integer
|
|
|
|
*/
|
2023-12-29 19:32:16 -05:00
|
|
|
static inline unsigned int f_randi(unsigned int seed)
|
|
|
|
{
|
2023-02-28 23:50:13 -05:00
|
|
|
seed = (seed << 13) ^ seed;
|
|
|
|
return ((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0x7fffffff);
|
|
|
|
}
|
2023-07-25 14:06:04 -04:00
|
|
|
|
2024-02-22 15:54:52 -05:00
|
|
|
#ifdef __GNUC__
|
2023-07-25 14:06:04 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
2024-02-21 20:24:00 -05:00
|
|
|
#endif
|
2023-12-29 19:32:16 -05:00
|
|
|
|
2023-02-28 23:50:13 -05:00
|
|
|
/**
|
|
|
|
* fast inverse sqrt
|
|
|
|
*/
|
2023-12-29 19:32:16 -05:00
|
|
|
static inline float fsqrt(float n)
|
|
|
|
{
|
2023-02-28 23:50:13 -05:00
|
|
|
int i;
|
|
|
|
float x, y;
|
|
|
|
x = n * 0.5f;
|
|
|
|
y = n;
|
2023-04-05 17:21:19 -04:00
|
|
|
i = *reinterpret_cast<int*>(&y);
|
2023-02-28 23:50:13 -05:00
|
|
|
i = 0x5f3759df - (i >> 1);
|
2023-04-05 17:21:19 -04:00
|
|
|
y = *reinterpret_cast<float*>(&i);
|
2023-02-28 23:50:13 -05:00
|
|
|
y = y * (1.5f - (x * y * y));
|
|
|
|
y = y * (1.5f - (x * y * y));
|
|
|
|
return y;
|
|
|
|
}
|
2023-07-25 14:06:04 -04:00
|
|
|
|
2024-02-22 15:54:52 -05:00
|
|
|
#ifdef __GNUC__
|
2023-07-25 14:06:04 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2024-02-21 20:24:00 -05:00
|
|
|
#endif
|
2024-08-23 21:13:16 -04:00
|
|
|
|
|
|
|
|
2024-04-09 18:24:34 -04:00
|
|
|
template<typename B, typename P, typename R = decltype(B() * P())>
|
|
|
|
static inline constexpr R pow(B b, P p)
|
2023-12-29 19:32:16 -05:00
|
|
|
{
|
2024-04-09 18:24:34 -04:00
|
|
|
R collection = 1;
|
2023-07-24 02:44:25 -04:00
|
|
|
for (int i = 0; i < p; i++)
|
|
|
|
collection *= b;
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2024-08-23 21:13:16 -04:00
|
|
|
template<blt::i64 decimal_places>
|
|
|
|
struct round_up_t
|
|
|
|
{
|
|
|
|
constexpr inline double operator()(double value)
|
|
|
|
{
|
|
|
|
if constexpr (decimal_places < 0)
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
constexpr double multiplier = pow(10.0, decimal_places);
|
2024-08-23 22:26:43 -04:00
|
|
|
auto i_value = static_cast<blt::i64>(value * multiplier);
|
|
|
|
auto f_value = (value * multiplier) - static_cast<double>(i_value);
|
2024-08-23 21:13:16 -04:00
|
|
|
if (f_value > 0)
|
2024-08-23 22:26:43 -04:00
|
|
|
return ((static_cast<double>(i_value) + 1) / multiplier);
|
2024-08-23 21:13:16 -04:00
|
|
|
else
|
|
|
|
return static_cast<double>(i_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<blt::i64 decimal_places>
|
|
|
|
struct round_down_t
|
|
|
|
{
|
|
|
|
constexpr inline double operator()(double value)
|
|
|
|
{
|
|
|
|
if constexpr (decimal_places < 0)
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
constexpr double multiplier = pow(10.0, decimal_places);
|
|
|
|
return (static_cast<blt::i64>(value * multiplier)) / multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-24 02:44:25 -04:00
|
|
|
/**
|
|
|
|
* This is a fast rounding function and is not guaranteed to be 100% correct
|
|
|
|
* @tparam decimal_places
|
|
|
|
* @param value
|
|
|
|
* @return
|
|
|
|
*/
|
2024-08-23 21:13:16 -04:00
|
|
|
template<blt::i64 decimal_places>
|
2024-03-11 12:05:37 -04:00
|
|
|
constexpr static inline double round_up(double value)
|
2023-12-29 19:32:16 -05:00
|
|
|
{
|
2024-08-23 21:13:16 -04:00
|
|
|
|
|
|
|
round_up_t<decimal_places> round_func;
|
|
|
|
return round_func(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<blt::i64 decimal_places>
|
|
|
|
constexpr static inline double round_down(double value)
|
|
|
|
{
|
|
|
|
|
|
|
|
round_down_t<decimal_places> round_func;
|
|
|
|
return round_func(value);
|
2023-07-24 02:44:25 -04:00
|
|
|
}
|
|
|
|
|
2023-04-05 17:21:19 -04:00
|
|
|
/*inline std::ostream& operator<<(std::ostream& out, const mat4x4& v) {
|
|
|
|
return out << "\rMatrix4x4{" << v.m00() << ", " << v.m01() << ", " << v.m02() << ", " << v.m03() << "} \n"\
|
|
|
|
<< " {" << v.m10() << ", " << v.m11() << ", " << v.m12() << ", " << v.m13() << "} \n"\
|
|
|
|
<< " {" << v.m20() << ", " << v.m21() << ", " << v.m22() << ", " << v.m23() << "} \n"\
|
|
|
|
<< " {" << v.m30() << ", " << v.m31() << ", " << v.m32() << ", " << v.m33() << "} \n";
|
|
|
|
}*/
|
2023-12-29 19:32:16 -05:00
|
|
|
|
2023-01-10 10:45:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_MATH_H
|