5#ifndef NNGN_MATH_MATH_H
6#define NNGN_MATH_MATH_H
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);
51 template<
typename T>
static constexpr T round_up(
T n,
T d);
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,
165 static void *
align_ptr(
void *p, std::size_t
a);
175 return static_cast<T>(1.41421356237309514547462185873882845);
179 return static_cast<T>(0.70710678118654757273731092936941423);
182template<
typename T>
inline constexpr T Math::e(
void) {
183 return static_cast<T>(2.7182818284590450907955982984276488);
187 return static_cast<T>(3.14159265358979323846264338327950288);
191 return static_cast<T>(6.28318530717958647692528676655900576);
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]};
351 return {
m[
i][0],
m[(
i + 2) % 3][1],
m[(
i + 1) % 3][2]};
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];
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);
401 for(std::size_t
c = 0;
c != 4; ++
c,
s = -
s)
402 for(std::size_t
r = 0;
r != 4; ++
r,
s = -
s)
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});
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);
std::decay_t< > rand_seed_t
Definition math.h:35
static constexpr mat3_base< T > minor_matrix(const mat4_base< T > &m, std::size_t i, std::size_t j)
Definition math.h:355
static constexpr T pi(void)
Definition math.h:186
static constexpr mat4_base< T > ortho(T left, T right, T bottom, T top)
Definition math.h:512
static constexpr T tau(void)
Definition math.h:190
void init(void)
Definition math.cpp:56
static constexpr vec3_base< T > rotate_z(vec3_base< T > v, T sin, T cos)
Definition math.h:469
static constexpr vec3_base< T > inv_diag(const mat3_base< T > &m, std::size_t i)
Definition math.h:348
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:408
static void mat_mul(std::span< float > dst, const float *src0, const float *src1, std::size_t n)
Definition math.cpp:44
static constexpr mat4_base< T > translate(const mat4_base< T > &m, vec3_base< T > v)
Definition math.h:413
static constexpr vec3_base< T > normal(vec3_base< T > p0, vec3_base< T > p1, vec3_base< T > p2)
Definition math.h:316
static constexpr mat3_base< T > transpose(const mat3_base< T > &m)
Definition math.h:323
static constexpr T round_up_div(T n, T d)
Definition math.h:231
static constexpr T determinant(const mat3_base< T > &m)
Definition math.h:381
static auto length(T v)
Definition math.h:257
static constexpr vector_type< T > product(T v)
Definition math.h:279
static constexpr vector_type< T > avg(T v)
Definition math.h:285
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 mat4_base< T > look_at(vec3_base< T > eye, vec3_base< T > center, vec3_base< T > up)
Definition math.h:551
static constexpr T round_up_pow2(T n, T d)
Definition math.h:211
void seed_rand(rand_seed_t s)
Definition math.cpp:58
static constexpr vector_type< T > sum(T v)
Definition math.h:274
std::optional< rnd_generator_t > m_rnd_generator
Definition math.h:37
static constexpr T sq2(void)
Definition math.h:174
static constexpr mat4_base< T > adjugate(const mat4_base< T > &m)
Definition math.h:398
static V< T > clamp_len(V< T > v, T len)
Definition math.h:268
static constexpr T sq2_2(void)
Definition math.h:178
static bool is_aligned(void *p, std::size_t a)
Definition math.h:565
static constexpr auto dot(T u, T v)
Definition math.h:242
static constexpr T round_down_pow2(T n, T d)
Definition math.h:205
static constexpr vec3_base< T > rotate_y(vec3_base< T > v, T sin, T cos)
Definition math.h:463
static constexpr vec3_base< T > diag(const mat3_base< T > &m, std::size_t i)
Definition math.h:341
static constexpr T radians(T d)
Definition math.h:194
static constexpr T angle(vec2_base< T > u, vec2_base< T > v)
Definition math.h:299
static constexpr T round_up(T n, T d)
Definition math.h:224
static constexpr auto length2(T v)
Definition math.h:252
auto * rnd_generator(void)
Definition math.h:581
static constexpr T degrees(T r)
Definition math.h:199
static constexpr vec2_base< T > rotate(vec2_base< T > v, T sin, T cos)
Definition math.h:429
static vec3_base< T > perspective_transform(const mat4_base< T > &m, vec3_base< T > v)
Definition math.h:333
static T normalize(T v)
Definition math.h:262
static constexpr mat4_base< T > perspective(T fovy, T aspect, T near, T far)
Definition math.h:536
void rand_mat(std::span< float > m)
Definition math.cpp:67
static constexpr vec3_base< T > reflect(vec3_base< T > v, vec3_base< T > n)
Definition math.h:311
static constexpr mat4_base< T > scale(const mat4_base< T > &m, vec3_base< T > v)
Definition math.h:422
static constexpr T minor(const mat4_base< T > &m, std::size_t i, std::size_t j)
Definition math.h:374
static constexpr vec3_base< T > cross(vec3_base< T > u, vec3_base< T > v)
Definition math.h:290
static constexpr vec3_base< T > rotate_x(vec3_base< T > v, T sin, T cos)
Definition math.h:457
static void gaussian_filter(std::size_t size, float std_dev, std::span< float > s)
Definition math.cpp:10
static void * align_ptr(void *p, std::size_t a)
Definition math.h:570
assert
Definition debug.lua:3
function DEMO text end
Definition demo0.lua:6
round_down_pow2
Definition common.lua:27
round_up
Definition common.lua:26
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
local c
Definition gamma.lua:11
local r
Definition gamma.lua:7
constexpr const R * begin(const T< R > &v)
Definition vec.h:207
typename T::type vector_type
Definition vec.h:62
rotate
Definition camera.lua:58
v[1]
Definition math.lua:22
local w
Definition strict.lua:12
constexpr vec_type row(std::size_t i) const
Definition mat.h:41
constexpr vec3_base< T > xyz() const
Definition vec4.h:43
std::chrono::seconds s
Definition timing.cpp:6