5#ifndef NNGN_MATH_MATH_H
6#define NNGN_MATH_MATH_H
15#include "utils/concepts/fundamental.h"
24 struct rnd_generator_t :
private std::mt19937 {
25 using std::mt19937::default_seed;
26 using std::mt19937::result_type;
27 using std::mt19937::min;
28 using std::mt19937::max;
29 using std::mt19937::mt19937;
30 using std::mt19937::operator();
31 using std::mt19937::operator=;
32 using std::mt19937::seed;
35 using rand_seed_t = std::decay_t<
decltype(rnd_generator_t::default_seed)>;
40 template<
typename T =
double>
static constexpr T sq2_2(
void);
41 template<
typename T =
double>
static constexpr T sq2(
void);
42 template<
typename T =
double>
static constexpr T e(
void);
43 template<
typename T =
double>
static constexpr T pi(
void);
44 template<
typename T =
double>
static constexpr T tau(
void);
45 template<
typename T =
double>
static constexpr T radians(
T d);
46 template<
typename T =
double>
static constexpr T degrees(
T r);
53 template<
unsigned_
integral T>
static constexpr T mip_levels(
T extent);
54 template<
typename T>
static constexpr auto dot(
T u,
T v);
56 template<
typename T>
static constexpr auto length2(
T v);
57 template<
typename T>
static auto length(
T v);
59 template<
template<
typename>
typename V,
typename T>
82 std::span<float> dst,
const float *src0,
const float *src1,
151 T left,
T right,
T bottom,
T top,
T near,
T far);
160 std::size_t size,
float std_dev, std::span<float>
s);
162 std::size_t xsize, std::size_t ysize,
float std_dev,
174template<
typename T>
inline constexpr T Math::sq2(
void) {
175 return static_cast<T>(1.41421356237309514547462185873882845);
178template<
typename T>
inline constexpr T Math::sq2_2(
void) {
179 return static_cast<T>(0.70710678118654757273731092936941423);
182template<
typename T>
inline constexpr T Math::e(
void) {
183 return static_cast<T>(2.7182818284590450907955982984276488);
186template<
typename T>
inline constexpr T Math::pi(
void) {
187 return static_cast<T>(3.14159265358979323846264338327950288);
190template<
typename T>
inline constexpr T Math::tau(
void) {
191 return static_cast<T>(6.28318530717958647692528676655900576);
195 constexpr auto mul = Math::tau<T>() /
T{360};
200 constexpr auto mul =
T{360} * Math::tau<T>();
206 assert(std::popcount(d) == 1);
212 assert(std::popcount(d) == 1);
213 return --d, (
n + d) & ~d;
218 if(
const auto r =
n % d)
225 if(
const auto r =
n % d)
232 return (d - 1 +
n) / d;
235template<
unsigned_
integral T>
237 assert(std::popcount(extent) == 1);
238 return T{1} +
static_cast<T>(std::countr_zero(std::bit_floor(extent)));
243 if constexpr(std::ranges::range<T>)
244 return std::inner_product(
245 std::ranges::begin(u), std::ranges::end(u), std::ranges::begin(
v),
246 std::ranges::range_value_t<T>{});
266template<
template<
typename>
typename V,
typename T>
270 return len2 <= max2 ?
v :
v * std::sqrt(max2 / len2);
280 return std::accumulate(
292 u.
y *
v.z - u.
z *
v.y,
293 u.
z *
v.x - u.
x *
v.z,
294 u.
x *
v.y - u.
y *
v.x,
324 return {
m.row(0),
m.row(1),
m.row(2)};
329 return {
m.row(0),
m.row(1),
m.row(2),
m.row(3)};
337 return ret.xyz() / ret.w;
344 return {
m[
i][0],
m[(
i + 1) % 3][1],
m[(
i + 2) % 3][2]};
349 const mat3_base<T> &
m, std::size_t
i)
351 return {
m[
i][0],
m[(
i + 2) % 3][1],
m[(
i + 1) % 3][2]};
356 const mat4_base<T> &
m, std::size_t
i, std::size_t j)
358 mat3_base<T> ret = {};
362 for( ;
r != j; ++
r) ret[
c][
r ] =
m[
c][
r];
363 for(++
r;
r != 4; ++
r) ret[
c][
r - 1] =
m[
c][
r];
365 for(++
c;
c != 4; ++
c) {
367 for( ;
r != j; ++
r) ret[
c - 1][
r ] =
m[
c][
r];
368 for(++
r;
r != 4; ++
r) ret[
c - 1][
r - 1] =
m[
c][
r];
375 const mat4_base<T> &
m, std::size_t
i, std::size_t j)
383 return M::product(M::diag(
m, 0)) - M::product(M::inv_diag(
m, 0))
384 + M::product(M::diag(
m, 1)) - M::product(M::inv_diag(
m, 1))
385 + M::product(M::diag(
m, 2)) - M::product(M::inv_diag(
m, 2));
391 return m[0][0] * M::minor(
m, 0, 0)
392 -
m[0][1] * M::minor(
m, 0, 1)
393 +
m[0][2] * M::minor(
m, 0, 2)
394 -
m[0][3] * M::minor(
m, 0, 3);
399 mat4_base<T> ret = {};
401 for(std::size_t
c = 0;
c != 4; ++
c,
s = -
s)
402 for(std::size_t
r = 0;
r != 4; ++
r,
s = -
s)
408inline constexpr mat4_base<T>
Math::inverse(
const mat4_base<T> &
m) {
418 {
m[0] *
v[0] +
m[1] *
v[1] +
m[2] *
v[2] +
m[3]}};
425 return {
m[0] *
v[0],
m[1] *
v[1],
m[2] *
v[2],
m[3]};
430 return {
v.x * cos -
v.y * sin,
v.x * sin +
v.y * cos};
444 const auto d =
v - proj;
459 return {
v.x,
r.x,
r.y};
465 return {
r.y,
v.y,
r.x};
471 return {
r.x,
r.y,
v.z};
493 const T cos = std::cos(
angle);
494 const T sin = std::sin(
angle);
496 const auto tmp = (
T{1} - cos) * axis;
498 rot[0][0] = tmp.x * axis.x + cos;
499 rot[0][1] = tmp.y * axis.x + sin * axis.z;
500 rot[0][2] = tmp.z * axis.x - sin * axis.y;
501 rot[1][0] = tmp.x * axis.y - sin * axis.z;
502 rot[1][1] = tmp.y * axis.y + cos;
503 rot[1][2] = tmp.z * axis.y + sin * axis.x;
504 rot[2][0] = tmp.x * axis.z + sin * axis.y;
505 rot[2][1] = tmp.y * axis.z - sin * axis.x;
506 rot[2][2] = tmp.z * axis.z + cos;
513 const auto w = right - left, h = top - bottom;
515 ret[0][0] =
T{2} /
w;
516 ret[1][1] =
T{2} / h;
519 ret[3][0] = -(right + left) /
w;
520 ret[3][1] = -(top + bottom) / h;
526 T left,
T right,
T bottom,
T top,
T near,
T far)
529 const auto z = far - near;
530 ret[2][2] = -
T{2} /
z;
531 ret[3][2] = -(far + near) /
z;
537 T fovy,
T aspect,
T near,
T far)
539 assert(std::abs(aspect) > -std::numeric_limits<T>::epsilon());
540 const auto tan = std::tan(fovy /
T{2});
541 auto ret = mat4_base<T>{0};
542 ret[0][0] =
T{1} / (aspect * tan);
543 ret[1][1] =
T{1} / tan;
544 ret[2][2] = -(far + near) / (far - near);
546 ret[3][2] = -
T{2} * far * near / (far - near);
566 const auto u =
reinterpret_cast<std::uintptr_t
>(
p);
571 const auto u =
reinterpret_cast<std::uintptr_t
>(
p);
573 return reinterpret_cast<void*
>(
r);
static constexpr mat3_base< T > minor_matrix(const mat4_base< T > &m, std::size_t i, std::size_t j)
Definition: math.h:350
static constexpr T pi(void)
Definition: math.h:186
static constexpr mat3_base< T > minor_matrix(const mat4_base< T > &m, std::size_t i, std::size_t j)
static constexpr vector_type< T > sum(const T &v)
Definition: math.h:264
static constexpr mat4_base< T > ortho(T left, T right, T bottom, T top)
Definition: math.h:510
static constexpr T tau(void)
std::decay_t<> rand_seed_t
Definition: math.h:35
static bool is_aligned(void *p, std::size_t a)
static constexpr T tau(void)
Definition: math.h:190
static constexpr T round_down(T n, T d)
static constexpr vec3_base< T > cross(const vec3_base< T > &u, const vec3_base< T > &v)
Definition: math.h:285
static constexpr vector_type< T > avg(const T &v)
Definition: math.h:275
static constexpr vec3_base< T > inv_diag(const mat3_base< T > &m, std::size_t i)
Definition: math.h:343
static constexpr mat4_base< T > transpose(const mat4_base< T > &m)
static constexpr T round_down(T n, T d)
Definition: math.h:217
static constexpr T e(void)
Definition: math.h:182
static constexpr mat4_base< T > inverse(const mat4_base< T > &m)
Definition: math.h:403
static constexpr mat3_base< T > transpose(const mat3_base< T > &m)
static constexpr T determinant(const mat4_base< T > &m)
static constexpr T determinant(const mat3_base< T > &m)
static constexpr T radians(T d)
static constexpr vec3_base< T > normal(const vec3_base< T > &p0, const vec3_base< T > &p1, const vec3_base< T > &p2)
Definition: math.h:319
static constexpr mat4_base< T > adjugate(const mat4_base< T > &m)
static constexpr auto length2(const T &v)
Definition: math.h:242
static constexpr T angle(const vec2_base< T > &u, const vec2_base< T > &v)
Definition: math.h:296
static constexpr mat3_base< T > transpose(const mat3_base< T > &m)
Definition: math.h:326
static constexpr T round_up_div(T n, T d)
Definition: math.h:231
static constexpr vec3_base< T > rotate_x(const vec3_base< T > &v, T sin, T cos)
Definition: math.h:453
static constexpr T determinant(const mat3_base< T > &m)
Definition: math.h:376
static constexpr vector_type< T > product(const T &v)
Definition: math.h:269
static constexpr mat4_base< T > perspective(T fovy, T aspect, T near, T far)
static constexpr mat4_base< T > translate(const mat4_base< T > &m, const vec3_base< T > &v)
Definition: math.h:408
static constexpr vec3_base< T > reflect(const vec3_base< T > &v, const vec3_base< T > &n)
Definition: math.h:312
static constexpr T mip_levels(T extent)
Definition: math.h:236
static constexpr T cofactor(const mat4_base< T > &m, std::size_t i, std::size_t j)
static constexpr T sq2_2(void)
static constexpr vec3_base< T > rotate_y(const vec3_base< T > &v, T sin, T cos)
Definition: math.h:459
static constexpr T round_up_pow2(T n, T d)
Definition: math.h:211
static constexpr vec3_base< T > diag(const mat3_base< T > &m, std::size_t i)
void seed_rand(rand_seed_t s)
static constexpr vec3_base< T > inv_diag(const mat3_base< T > &m, std::size_t i)
static constexpr mat4_base< T > look_at(const vec3_base< T > &eye, const vec3_base< T > ¢er, const vec3_base< T > &up)
Definition: math.h:545
static constexpr mat4_base< T > inverse(const mat4_base< T > &m)
static T normalize(const T &v)
Definition: math.h:252
static constexpr T sq2(void)
Definition: math.h:174
static constexpr T round_down_pow2(T n, T d)
static constexpr T mip_levels(T extent)
static constexpr mat4_base< T > adjugate(const mat4_base< T > &m)
Definition: math.h:393
static V< T > clamp_len(V< T > v, T len)
Definition: math.h:258
static constexpr T sq2_2(void)
Definition: math.h:178
static bool is_aligned(void *p, std::size_t a)
Definition: math.h:559
static constexpr T degrees(T r)
std::optional< rnd_generator_t > m_rnd_generator
Definition: math.h:37
static constexpr vec2_base< T > rotate(const vec2_base< T > &v, T sin, T cos)
Definition: math.h:424
static constexpr T round_down_pow2(T n, T d)
Definition: math.h:205
static constexpr mat4_base< T > ortho(T left, T right, T bottom, T top)
static constexpr vec3_base< T > rotate_z(const vec3_base< T > &v, T sin, T cos)
Definition: math.h:467
static constexpr mat4_base< T > ortho(T left, T right, T bottom, T top, T near, T far)
static auto length(const T &v)
Definition: math.h:247
static constexpr vec3_base< T > diag(const mat3_base< T > &m, std::size_t i)
Definition: math.h:336
static constexpr T round_up_pow2(T n, T d)
static constexpr T radians(T d)
Definition: math.h:194
static constexpr vector_type< T > dot(const T &u, const T &v)
Definition: math.h:280
static constexpr T e(void)
static constexpr T round_up(T n, T d)
Definition: math.h:224
static V< T > clamp_len(V< T > v, T len)
auto * rnd_generator(void)
static constexpr T round_up_div(T n, T d)
static constexpr T degrees(T r)
Definition: math.h:199
static void gaussian_filter(std::size_t size, float std_dev, std::span< float > s)
static vec3_base< T > perspective_transform(const mat4_base< T > &m, vec3_base< T > v)
Definition: math.h:333
static constexpr mat4_base< T > scale(const mat4_base< T > &m, const vec3_base< T > &v)
Definition: math.h:417
static constexpr T minor(const mat4_base< T > &m, std::size_t i, std::size_t j)
static constexpr mat4_base< T > perspective(T fovy, T aspect, T near, T far)
Definition: math.h:531
static void * align_ptr(void *p, std::size_t a)
static T * align_ptr(void *p)
static void gaussian_filter(std::size_t xsize, std::size_t ysize, float std_dev, std::span< float > s)
void rand_mat(std::span< float > m)
static constexpr T sq2(void)
static void mat_mul(std::span< float > dst, const float *src0, const float *src1, std::size_t n)
static constexpr T pi(void)
static constexpr T minor(const mat4_base< T > &m, std::size_t i, std::size_t j)
Definition: math.h:369
static constexpr T round_up(T n, T d)
static void * align_ptr(void *p, std::size_t a)
Definition: math.h:564
function DEMO text end
Definition: demo0.lua:6
for i
Definition: font.lua:5
c
Definition: gamma.lua:11
assert
Definition: debug.lua:3
n
Definition: dump_lights.lua:5
rotate
Definition: camera.lua:49
v[1]
Definition: math.lua:19
w
Definition: strict.lua:12
std::chrono::seconds s
Definition: timing.cpp:6
constexpr const R * begin(const T< R > &v)
Definition: vec.h:207
typename T::type vector_type
Definition: vec.h:62