power_play/src/base/base_math.h
2026-01-30 07:31:57 -06:00

531 lines
14 KiB
C

#define Pi (3.14159265358979323846)
#define Tau (6.28318530717958647693)
#define GoldenRatio (1.61803398874989484820)
////////////////////////////////////////////////////////////
//~ Axis types
Enum(Axis)
{
Axis_X = 0,
Axis_Y = 1,
Axis_Z = 2,
Axis_COUNTXY = 2,
Axis_COUNTXYZ = 3
};
////////////////////////////////////////////////////////////
//~ Vector types
//- Vec2
Union(Vec2) { struct { f32 x, y; }; struct { f32 r, g; }; f32 v[2]; };
Union(Vec2F64) { struct { f64 x, y; }; struct { f64 r, g; }; f64 v[2]; };
Union(Vec2I32) { struct { i32 x, y; }; struct { i32 r, g; }; i32 v[2]; };
Union(Vec2I64) { struct { i64 x, y; }; struct { i64 r, g; }; i64 v[2]; };
Union(Vec2U32) { struct { u32 x, y; }; struct { u32 r, g; }; u32 v[2]; };
Union(Vec2U64) { struct { u64 x, y; }; struct { u64 r, g; }; u64 v[2]; };
Struct(Vec2Array) { Vec2 *points; u64 count; };
#define VEC2(x, y) (Vec2) { (x), (y) }
#define VEC2F64(x, y) (Vec2F64) { (x), (y) }
#define VEC2I32(x, y) (Vec2I32) { (x), (y) }
#define VEC2I64(x, y) (Vec2I64) { (x), (y) }
#define VEC2U32(x, y) (Vec2U32) { (x), (y) }
#define VEC2U64(x, y) (Vec2U64) { (x), (y) }
#define Vec2FromVec(v) VEC2((v).x, (v).y)
#define Vec2F64FromVec(v) VEC2F64((v).x, (v).y)
#define Vec2I32FromVec(v) VEC2I32((v).x, (v).y)
#define Vec2I64FromVec(v) VEC2I64((v).x, (v).y)
#define Vec2U32FromVec(v) VEC2U32((v).x, (v).y)
#define Vec2U64FromVec(v) VEC2U64((v).x, (v).y)
//- Vec3
Union(Vec3) { struct { f32 x, y, z; }; struct { f32 r, g, b; }; f32 v[3]; };
Union(Vec3F64) { struct { f64 x, y, z; }; struct { f64 r, g, b; }; f64 v[3]; };
Union(Vec3I32) { struct { i32 x, y, z; }; struct { i32 r, g, b; }; i32 v[3]; };
Union(Vec3I64) { struct { i64 x, y, z; }; struct { i64 r, g, b; }; i64 v[3]; };
Union(Vec3U32) { struct { u32 x, y, z; }; struct { u32 r, g, b; }; u32 v[3]; };
Union(Vec3U64) { struct { u64 x, y, z; }; struct { u64 r, g, b; }; u64 v[3]; };
Struct(Vec3Array) { Vec3 *points; u64 count; };
#define VEC3(x, y, z) (Vec3) { (x), (y), (z) }
#define VEC3F64(x, y, z) (Vec3F64) { (x), (y), (z) }
#define VEC3I32(x, y, z) (Vec3I32) { (x), (y), (z) }
#define VEC3I64(x, y, z) (Vec3I64) { (x), (y), (z) }
#define VEC3U32(x, y, z) (Vec3U32) { (x), (y), (z) }
#define VEC3U64(x, y, z) (Vec3U64) { (x), (y), (z) }
#define Vec3FromVec(v) VEC3((v).x, (v).y, (v).z)
#define Vec3F64FromVec(v) VEC3F64((v).x, (v).y, (v).z)
#define Vec3I32FromVec(v) VEC3I32((v).x, (v).y, (v).z)
#define Vec3I64FromVec(v) VEC3I64((v).x, (v).y, (v).z)
#define Vec3U32FromVec(v) VEC3U32((v).x, (v).y, (v).z)
#define Vec3U64FromVec(v) VEC3U64((v).x, (v).y, (v).z)
//- Vec4
Union(Vec4) { struct { f32 x, y, z, w; }; struct { f32 r, g, b, a; }; f32 v[4]; };
Union(Vec4F64) { struct { f64 x, y, z, w; }; struct { f64 r, g, b, a; }; f64 v[4]; };
Union(Vec4I32) { struct { i32 x, y, z, w; }; struct { i32 r, g, b, a; }; i32 v[4]; };
Union(Vec4I64) { struct { i64 x, y, z, w; }; struct { i64 r, g, b, a; }; i64 v[4]; };
Union(Vec4U32) { struct { u32 x, y, z, w; }; struct { u32 r, g, b, a; }; u32 v[4]; };
Union(Vec4U64) { struct { u64 x, y, z, w; }; struct { u64 r, g, b, a; }; u64 v[4]; };
Struct(Vec4Array) { Vec4 *points; u64 count; };
#define VEC4(x, y, z, w) (Vec4) { (x), (y), (z), (w) }
#define VEC4F64(x, y, z, w) (Vec4F64) { (x), (y), (z), (w) }
#define VEC4I32(x, y, z, w) (Vec4I32) { (x), (y), (z), (w) }
#define VEC4I64(x, y, z, w) (Vec4I64) { (x), (y), (z), (w) }
#define VEC4U32(x, y, z, w) (Vec4U32) { (x), (y), (z), (w) }
#define VEC4U64(x, y, z, w) (Vec4U64) { (x), (y), (z), (w) }
#define Vec4FromVec(v) VEC4((v).x, (v).y, (v).z, (v).w)
#define Vec4F64FromVec(v) VEC4F64((v).x, (v).y, (v).z, (v).w)
#define Vec4I32FromVec(v) VEC4I32((v).x, (v).y, (v).z, (v).w)
#define Vec4I64FromVec(v) VEC4I64((v).x, (v).y, (v).z, (v).w)
#define Vec4U32FromVec(v) VEC4U32((v).x, (v).y, (v).z, (v).w)
#define Vec4U64FromVec(v) VEC4U64((v).x, (v).y, (v).z, (v).w)
////////////////////////////////////////////////////////////
//~ Range types
//- Rng1
Struct(Rng) { f32 min, max; };
Struct(RngF64) { f64 min, max; };
Struct(RngI32) { i32 min, max; };
Struct(RngI64) { i64 min, max; };
Struct(RngU32) { u32 min, max; };
Struct(RngU64) { u64 min, max; };
#define RNG(min, max) (Rng) { (min), (max) }
#define RNGF64(min, max) (RngF46) { (min), (max) }
#define RNGI32(min, max) (RngI32) { (min), (max) }
#define RNGI64(min, max) (RngI64) { (min), (max) }
#define RNGU32(min, max) (RngU32) { (min), (max) }
#define RNGU64(min, max) (RngU64) { (min), (max) }
#define RngEmpty RNG(Inf, -Inf)
#define RngInf RNG(-Inf, Inf)
//- Rng2
Struct(Rng2) { Vec2 p0, p1; };
Struct(Rng2F64) { Vec2F64 p0, p1; };
Struct(Rng2I32) { Vec2I32 p0, p1; };
Struct(Rng2I64) { Vec2I64 p0, p1; };
Struct(Rng2U32) { Vec2U32 p0, p1; };
Struct(Rng2U64) { Vec2U64 p0, p1; };
#define RNG2(p0, p1) (Rng2) { (p0), (p1) }
#define RNG2F64(p0, p1) (Rng2F46) { (p0), (p1) }
#define RNG2I32(p0, p1) (Rng2I32) { (p0), (p1) }
#define RNG2I64(p0, p1) (Rng2I64) { (p0), (p1) }
#define RNG2U32(p0, p1) (Rng2U32) { (p0), (p1) }
#define RNG2U64(p0, p1) (Rng2U64) { (p0), (p1) }
#define Rng2Empty RNG2(VEC2(Inf, Inf), VEC2(-Inf, -Inf))
#define Rng2Inf RNG2(VEC2(-Inf, -Inf), VEC2(Inf, Inf))
//- Rng3
Struct(Rng3) { Vec3 p0, p1; };
Struct(Rng3F64) { Vec3F64 p0, p1; };
Struct(Rng3I32) { Vec3I32 p0, p1; };
Struct(Rng3I64) { Vec3I64 p0, p1; };
Struct(Rng3U32) { Vec3U32 p0, p1; };
Struct(Rng3U64) { Vec3U64 p0, p1; };
#define RNG3(p0, p1) (Rng3) { (p0), (p1) }
#define RNG3F64(p0, p1) (Rng3F46) { (p0), (p1) }
#define RNG3I32(p0, p1) (Rng3I32) { (p0), (p1) }
#define RNG3I64(p0, p1) (Rng3I64) { (p0), (p1) }
#define RNG3U32(p0, p1) (Rng3U32) { (p0), (p1) }
#define RNG3U64(p0, p1) (Rng3U64) { (p0), (p1) }
#define Rng3Empty RNG3(VEC3(Inf, Inf, Inf), VEC3(-Inf, -Inf, -Inf))
#define Rng3Inf RNG3(VEC3(-Inf, -Inf, -Inf), VEC3(Inf, Inf, Inf))
////////////////////////////////////////////////////////////
//~ Affine types
Struct(Affine)
{
Vec2 bx; // X basis
Vec2 by; // Y basis
Vec2 og; // Origin
};
// (T)ranslation, (R)otation, (S)cale
Struct(Trs)
{
Vec2 t;
Vec2 s;
f32 r;
};
////////////////////////////////////////////////////////////
//~ Xform types
Struct(Xform)
{
Vec2 t; // Translation
Vec2 r; // Rotation
};
////////////////////////////////////////////////////////////
//~ Spring types
Struct(SoftSpring)
{
f32 bias_rate;
f32 mass_scale;
f32 impulse_scale;
};
////////////////////////////////////////////////////////////
//~ Mat4x4 types
Union(Mat4x4)
{
struct { Vec4 bx, by, bz, bw; };
f32 e[4][4];
};
////////////////////////////////////////////////////////////
//~ Min / max
//- Min
u8 MinU8(u8 a, u8 b);
i8 MinI8(i8 a, i8 b);
u32 MinU32(u32 a, u32 b);
i32 MinI32(i32 a, i32 b);
f32 MinF32(f32 a, f32 b);
u64 MinU64(u64 a, u64 b);
i64 MinI64(i64 a, i64 b);
f64 MinF64(f64 a, f64 b);
//- Max
u8 MaxU8(u8 a, u8 b);
i8 MaxI8(i8 a, i8 b);
u32 MaxU32(u32 a, u32 b);
i32 MaxI32(i32 a, i32 b);
f32 MaxF32(f32 a, f32 b);
u64 MaxU64(u64 a, u64 b);
i64 MaxI64(i64 a, i64 b);
f64 MaxF64(f64 a, f64 b);
//- Clamp
u32 ClampU32(u32 v, u32 min, u32 max);
i32 ClampI32(i32 v, i32 min, i32 max);
f32 ClampF32(f32 v, f32 min, f32 max);
u64 ClampU64(u64 v, u64 min, u64 max);
i64 ClampI64(i64 v, i64 min, i64 max);
f64 ClampF64(f64 v, f64 min, f64 max);
//- Saturate
u32 SaturateU32(u32 v);
i32 SaturateI32(i32 v);
f32 SaturateF32(f32 v);
u64 SaturateU64(u64 v);
i64 SaturateI64(i64 v);
f64 SaturateF64(f64 v);
////////////////////////////////////////////////////////////
//~ Float ops
//- Round
#define RoundF32 roundf
#define RoundF64 round
//- Floor
#define FloorF32 floorf
#define FloorF64 floor
//- Ceil
#define CeilF32 ceilf
#define CeilF64 ceil
//- Trunc
#define TruncF32 truncf
#define TruncF64 trunc
//- Mod
#define ModF32 fmodf
#define ModF64 fmod
//- Abs
#define AbsF32 fabsf
#define AbsF64 fabs
//- Sign
i32 SignF32(f32 v);
i32 SignF64(f64 v);
////////////////////////////////////////////////////////////
//~ Exponential ops
u64 PowU64(u64 v, u8 exp);
#define PowF32 powf
#define PowF64 pow
#define SqrtF32 sqrtf
#define SqrtF64 sqrt
#define LnF32 log
#define Log2F32 log2f
#define Log10F32 log10f
////////////////////////////////////////////////////////////
//~ Align
u64 AlignU64(u64 x, u64 align);
u64 NextPow2U64(u64 x);
////////////////////////////////////////////////////////////
//~ Trig
#define SinF32 sinf
#define CosF32 cosf
#define TanF32 tanf
#define ArcTanF32 atanf
#define ArcTan2F32 atan2f
////////////////////////////////////////////////////////////
//~ Angle unwind
// Returns angle in range [-Pi, Pi]
f32 UnwindAngleF32(f32 a);
////////////////////////////////////////////////////////////
//~ Float lerp
f32 LerpF32(f32 a, f32 b, f32 t);
f64 LerpF64(f64 a, f64 b, f64 t);
f32 LerpAngleF32(f32 a, f32 b, f32 t);
////////////////////////////////////////////////////////////
//~ Int lerp
i32 LerpI32(i32 a, i32 b, f32 t);
i64 LerpI64(i64 a, i64 b, f64 t);
i32 LerpU32(u32 a, u32 b, f32 t);
i64 LerpU64(u64 a, u64 b, f64 t);
////////////////////////////////////////////////////////////
//~ Smoothstep
f32 SmoothstepF32(f32 a, f32 b, f32 t);
f64 SmoothstepF64(f64 a, f64 b, f64 t);
////////////////////////////////////////////////////////////
//~ Color
u8 MulNormalizedU8(u8 a, u8 b);
f32 SrgbFromLinearF32(f32 lin);
f32 LinearFromSrgbF32(f32 srgb);
Vec4 LinearFromSrgb(Vec4 srgb);
Vec4 SrgbFromLinear(Vec4 lin);
Vec4 PremulFromLinear(Vec4 lin);
Vec4 PremulFromSrgb(Vec4 srgb);
Vec4 LerpSrgb(Vec4 a, Vec4 b, f32 t);
////////////////////////////////////////////////////////////
//~ Vec2
b32 IsVec2Zero(Vec2 a);
b32 MatchVec2(Vec2 a, Vec2 b);
//- Mul
Vec2 MulVec2(Vec2 a, f32 s);
Vec2 MulVec2Vec2(Vec2 a, Vec2 b);
Vec2 NegVec2(Vec2 a);
//- Div
Vec2 DivVec2(Vec2 a, f32 s);
Vec2 DivVec2Vec2(Vec2 a, Vec2 b);
//- Add
Vec2 AddVec2(Vec2 a, Vec2 b);
Vec2 SubVec2(Vec2 a, Vec2 b);
//- Len
f32 Vec2Len(Vec2 a);
f32 Vec2LenSq(Vec2 a);
Vec2 Vec2WithLen(Vec2 a, f32 len);
f32 Vec2Distance(Vec2 a, Vec2 b);
Vec2 NormVec2(Vec2 a);
Vec2 ClampVec2Len(Vec2 a, f32 max);
//- Clamp
Vec2 ClampVec2(Vec2 v, Rng2 r);
Vec2 SaturateVec2(Vec2 v);
//- Dot
f32 DotVec2(Vec2 a, Vec2 b);
f32 WedgeVec2(Vec2 a, Vec2 b);
Vec2 PerpVec2(Vec2 a);
Vec2 MulPerpVec2(Vec2 a, f32 s);
Vec2 PerpVec2TowardsDir(Vec2 v, Vec2 dir);
//- Round / floor / ceil
Vec2 RoundVec2(Vec2 a);
Vec2 FloorVec2(Vec2 a);
Vec2 CeilVec2(Vec2 a);
//- Angle
i32 WindingFromVec2(Vec2 a, Vec2 b);
Vec2 RotateVec2Angle(Vec2 v, f32 a);
Vec2 RotateVec2(Vec2 a, Vec2 b);
Vec2 Vec2FromAngle(f32 a);
f32 AngleFromVec2(Vec2 v);
//- Area
f32 DimsFromVec2(Vec2 v);
//- Closest point
Vec2 ClosestPointFromRay(Vec2 ray_pos, Vec2 ray_dir_norm, Vec2 p);
//- Lerp
Vec2 LerpVec2(Vec2 a, Vec2 b, f32 t);
Vec2 LerpVec2Vec2(Vec2 a, Vec2 b, Vec2 t);
Vec2 SlerpVec2(Vec2 a, Vec2 b, f32 t);
////////////////////////////////////////////////////////////
//~ Vec2I32
b32 MatchVec2I32(Vec2I32 a, Vec2I32 b);
Vec2I32 NegVec2I32(Vec2I32 a);
Vec2I32 AddVec2I32(Vec2I32 a, Vec2I32 b);
Vec2I32 SubVec2I32(Vec2I32 a, Vec2I32 b);
////////////////////////////////////////////////////////////
//~ Vec4
//- Mul
Vec4 MulVec4(Vec4 v, f32 s);
Vec4 MulVec4Vec4(Vec4 a, Vec4 b);
//- Lerp
Vec4 LerpVec4(Vec4 a, Vec4 b, f32 t);
//- Conversion
Vec4 Vec4FromU32(u32 v);
u32 U32FromVec4(Vec4 v);
////////////////////////////////////////////////////////////
//~ Range
//- Rng1
f32 NormRng(Rng r, f32 v);
#define Norm(min, max, v) NormRng(RNG((min), (max)), (v))
//- Rng2
b32 IsRng2Empty(Rng2 r);
Vec2 DimsFromRng2(Rng2 r);
Vec2 CenterFromRng2(Rng2 r);
Vec2 NormRng2(Rng2 r, Vec2 v);
Rng2 UnionRng2(Rng2 a, Rng2 b);
Rng2 IntersectRng2(Rng2 a, Rng2 b);
Rng2 AddRng2Vec2(Rng2 r, Vec2 v);
Rng2 MulRng2Vec2(Rng2 a, Vec2 v);
Rng2 DivRng2Vec2(Rng2 a, Vec2 v);
//- Rng2I32
b32 IsRng2I32Empty(Rng2I32 r);
Vec2I32 DimsFromRng2I32(Rng2I32 r);
Vec2I32 CenterFromRng2I32(Rng2I32 r);
Rng2I32 UnionRng2I32(Rng2I32 a, Rng2I32 b);
Rng2I32 IntersectRng2I32(Rng2I32 a, Rng2I32 b);
Rng2I32 AddRng2I32Vec2I32(Rng2I32 r, Vec2I32 v);
Rng2I32 MulRng2I32Vec2I32(Rng2I32 a, Vec2I32 v);
Rng2I32 DivRng2I32Vec2I32(Rng2I32 a, Vec2I32 v);
////////////////////////////////////////////////////////////
//~ Affine
#define AffineIdentity ((Affine) { .bx = { .x = 1 }, .by = { .y = 1 } })
#define CompAffineIdentity { .bx = { .x = 1 }, .by = { .y = 1 } }
b32 MatchAffine(Affine af0, Affine af1);
//- Initialization
Affine AffineFromPos(Vec2 v);
Affine AffineFromRot(Vec2 r);
Affine AffineFromAngle(f32 r);
Affine AffineFromScale(Vec2 scale);
//- Translation
Affine TranslateAffine(Affine af, Vec2 v);
Affine WorldTranslateAffine(Affine af, Vec2 v);
//- Rotation
Affine RotateAffine(Affine af, Vec2 r);
Affine RotateAffineAngle(Affine af, f32 r);
Affine WorldRotateAffineAngle(Affine af, f32 r);
Affine WorldRotateAffineBasisAngle(Affine af, f32 r);
Affine AffineWithWorldRotationAngle(Affine af, f32 r);
//- Scale
Affine ScaleAffine(Affine af, Vec2 scale);
Affine WorldScaleAffine(Affine af, Vec2 scale);
//- Lerp
Affine LerpAffine(Affine a, Affine b, f32 t);
//- Invert
Affine InvertAffine(Affine af);
//- Mul
Vec2 MulAffineVec2(Affine af, Vec2 v);
Affine MulAffine(Affine a, Affine b);
Vec2 MulAffineBasisVec2(Affine af, Vec2 v);
Vec2 InvertAffineMulVec2(Affine af, Vec2 v);
Vec2 InvertAffineBasisMulVec2(Affine af, Vec2 v);
//- Helpers
Affine AffineFromXform(Xform xf);
Affine BasisFromAffine(Affine af);
f32 DeterminantFromAffine(Affine af);
Vec2 RightFromAffine(Affine af);
Vec2 LeftFromAffine(Affine af);
Vec2 UpFromAffine(Affine af);
Vec2 DownFromAffine(Affine af);
f32 AngleFromAffine(Affine af);
Vec2 ScaleFromAffine(Affine af);
////////////////////////////////////////////////////////////
//~ Xform
#define XformIdentity ((Xform) { .r = { 1, 0 } })
#define CompXformIdentity { .r = { 1, 0 } }
Xform NormXform(Xform xf);
Xform InvertXform(Xform xf);
Vec2 MulXformVec2(Xform xf, Vec2 v);
////////////////////////////////////////////////////////////
//~ Spring
SoftSpring MakeSpring(f32 hertz, f32 damping_ratio, f32 dt);
////////////////////////////////////////////////////////////
//~ Mat4x4
Mat4x4 Mat4x4FromAffine(Affine af);
Mat4x4 Mat4x4FromOrtho(f32 left, f32 right, f32 bottom, f32 top, f32 near_z, f32 far_z);
Mat4x4 MulMat4x4(Mat4x4 m0, Mat4x4 m1);
Mat4x4 ProjectMat4x4View(Affine view, f32 viewport_width, f32 viewport_height);