diff options
Diffstat (limited to 'src/un_math.c')
-rw-r--r-- | src/un_math.c | 182 |
1 files changed, 100 insertions, 82 deletions
diff --git a/src/un_math.c b/src/un_math.c index a2ff65e..f6b7cab 100644 --- a/src/un_math.c +++ b/src/un_math.c @@ -1,104 +1,113 @@ /* lerps */ -f32 un_m_lerpf(f32 a, f32 b, f32 t) { +UN_INLINE float un_m_lerpf(float a, float b, float t) { return (1.0 - t) * a + b * t; } -f64 un_m_lerpd(f64 a, f64 b, f64 t) { +UN_INLINE double un_m_lerpd(double a, double b, double t) { return (1.0 - t) * a + b * t; } /* easing */ -f32 un_m_ease_isinef(f32 t) { +UN_INLINE float un_m_ease_isinef(float t) { return 1.0 - cosf((t * PI) / 2.0); } -f32 un_m_ease_iosinef(f32 t) { +UN_INLINE float un_m_ease_iosinef(float t) { return sinf((t * PI) / 2.0f); } -f32 un_m_ease_osinef(f32 t) { +UN_INLINE float un_m_ease_osinef(float t) { return -(cosf(t * PI) - 1.0) / 2.0; } -f32 un_m_ease_iquadf(f32 t) { +UN_INLINE float un_m_ease_iquadf(float t) { return t * t; } -f32 un_m_ease_ioquadf(f32 t) { +UN_INLINE float un_m_ease_ioquadf(float t) { return 1.0 - (1.0 - t) * (1.0 - t); } -f32 un_m_ease_oquadf(f32 t) { +UN_INLINE float un_m_ease_oquadf(float t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } -f64 un_m_ease_isined(f64 t) { +UN_INLINE double un_m_ease_isined(double t) { return 1.0 - cos((t * PI) / 2.0); } -f64 un_m_ease_iosined(f64 t) { +UN_INLINE double un_m_ease_iosined(double t) { return sin((t * PI) / 2.0f); } -f64 un_m_ease_osined(f64 t) { +UN_INLINE double un_m_ease_osined(double t) { return -(cos(t * PI) - 1.0) / 2.0; } -f64 un_m_ease_iquadd(f64 t) { +UN_INLINE double un_m_ease_iquadd(double t) { return t * t; } -f64 un_m_ease_ioquadd(f64 t) { +UN_INLINE double un_m_ease_ioquadd(double t) { return 1.0 - (1.0 - t) * (1.0 - t); } -f64 un_m_ease_oquadd(f64 t) { +UN_INLINE double un_m_ease_oquadd(double t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } /* complex */ -void un_m_complex_divf(f32 *v, f32 *a, f32 *b) { - f32 divisor; +UN_INLINE float2 un_m_complex_divf(float2 a, float2 b) { + float f; + float2 result; // un_m_complex_mulf(v, a, conjugated_b) - v[0] = a[0] * b[0] - a[1] * -b[1]; - v[1] = a[0] * -b[1] + a[1] * b[0]; + result.x = a.x * b.x - a.y * -b.y; + result.y = a.x * -b.y + a.y * b.x; - divisor = b[0] * b[0] + b[1] * b[1]; + f = b.x * b.x + b.y * b.y; - v[0] /= divisor; - v[1] /= divisor; + result.x /= f; + result.y /= f; + + return result; } -void un_m_complex_mulf(f32 *v, f32 *a, f32 *b) { - v[0] = a[0] * b[0] - a[1] * b[1]; - v[1] = a[0] * b[1] + a[1] * b[0]; +UN_INLINE float2 un_m_complex_mulf(float2 a, float2 b) { + float2 result; + result.x = a.x * b.x - a.y * b.y; + result.y = a.x * b.y + a.y * b.x; + return result; } +UN_INLINE double2 un_m_complex_divd(double2 a, double2 b) { + double f; + double2 result; -void un_m_complex_divd(f64 *v, f64 *a, f64 *b) { - f64 divisor; + // un_m_complex_mulf(v, a, conjugated_b) + result.x = a.x * b.x - a.y * -b.y; + result.y = a.x * -b.y + a.y * b.x; - // un_m_complex_muld(v, a, conjugated_b) - v[0] = a[0] * b[0] - a[1] * -b[1]; - v[1] = a[0] * -b[1] + a[1] * b[0]; + f = b.x * b.x + b.y * b.y; - divisor = b[0] * b[0] + b[1] * b[1]; + result.x /= f; + result.y /= f; - v[0] /= divisor; - v[1] /= divisor; + return result; } -void un_m_complex_muld(f64 *v, f64 *a, f64 *b) { - v[0] = a[0] * b[0] - a[1] * b[1]; - v[1] = a[0] * b[1] + a[1] * b[0]; +UN_INLINE double2 un_m_complex_muld(double2 a, double2 b) { + double2 result; + result.x = a.x * b.x - a.y * b.y; + result.y = a.x * b.y + a.y * b.x; + return result; } /* splines */ -void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t) { - f32 i0, i1, i2, p0, p1, ti; +UN_INLINE float un_m_bezierf(float a, float q0, float q1, float b, float t) { + float i0, i1, i2, p0, p1, ti; ti = 1.0 - t; @@ -109,11 +118,35 @@ void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t) { p0 = ti * i0 + i1 * t; p1 = ti * i1 + i2 * t; - *v = ti * p0 + p1 * t; + return ti * p0 + p1 * t; } -void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t) { - f64 i0, i1, i2, p0, p1, ti; +UN_INLINE float2 un_m_bezier2f(float2 a, float2 q0, float2 q1, float2 b, float t) { + float2 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + return result; +} + +float3 un_m_bezier3f(float3 a, float3 q0, float3 q1, float3 b, float t) { + float3 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierf(a.z, q0.z, q1.z, b.z, t); + return result; +} + +UN_INLINE float4 un_m_bezier4f(float4 a, float4 q0, float4 q1, float4 b, float t) { + float4 result; + result.x = un_m_bezierf(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierf(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierf(a.z, q0.z, q1.z, b.z, t); + result.w = un_m_bezierf(a.w, q0.w, q1.w, b.w, t); + return result; +} + +UN_INLINE double un_m_bezierd(double a, double q0, double q1, double b, double t) { + double i0, i1, i2, p0, p1, ti; ti = 1.0 - t; @@ -124,48 +157,33 @@ void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t) { p0 = ti * i0 + i1 * t; p1 = ti * i1 + i2 * t; - *v = ti * p0 + p1 * t; + return ti * p0 + p1 * t; } -void un_m_bezier2f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); +UN_INLINE double2 un_m_bezier2d(double2 a, double2 q0, double2 q1, double2 b, double t) { + double2 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + return result; } -void un_m_bezier3f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); +UN_INLINE double3 un_m_bezier3d(double3 a, double3 q0, double3 q1, double3 b, double t) { + double3 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierd(a.z, q0.z, q1.z, b.z, t); + return result; } -void un_m_bezier4f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { - un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); - un_m_bezierf(v + 3, a[3], q0[3], q1[3], b[3], t); +UN_INLINE double4 un_m_bezier4d(double4 a, double4 q0, double4 q1, double4 b, double t) { + double4 result; + result.x = un_m_bezierd(a.x, q0.x, q1.x, b.x, t); + result.y = un_m_bezierd(a.y, q0.y, q1.y, b.y, t); + result.z = un_m_bezierd(a.z, q0.z, q1.z, b.z, t); + result.w = un_m_bezierd(a.w, q0.w, q1.w, b.w, t); + return result; } - -void un_m_bezier2d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); -} - -void un_m_bezier3d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); -} - -void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { - un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); - un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); - un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); - un_m_bezierd(v + 3, a[3], q0[3], q1[3], b[3], t); -} - - - /* matrix and quaternions */ /* convention of matrix is: Column-major @@ -179,16 +197,16 @@ void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { * rotate * quaternion -> matrix * matrix -> quaternion - * matrix to components: un_mat_disassemble(f32 *pos3, f32 *scale3, f32 *quat4) - * un_mat_assemble(f32 *v, f32 *pos3, f32 *scale3, f32 *quat4) + * matrix to components: un_mat_disassemble(float *pos3, float *scale3, float *quat4) + * un_mat_assemble(float *v, float *pos3, float *scale3, float *quat4) * assembe_inverse * * */ -void un_m_mat_identityf(f32 *v) { - un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); +UN_INLINE void un_m_mat_identityf(float *v) { + memset((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); v[0 + 0 * UN_MATRIX_STRIPE] = 1.0f; v[1 + 1 * UN_MATRIX_STRIPE] = 1.0f; @@ -196,9 +214,9 @@ void un_m_mat_identityf(f32 *v) { v[3 + 3 * UN_MATRIX_STRIPE] = 1.0f; } -void un_m_mat_mulf(f32 *v, f32 *left, f32 *right) { +UN_INLINE void un_m_mat_mulf(float *v, float *left, float *right) { u64 row, col, i; - un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); + memset((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); for (col = 0; col < 4; col++) { for (row = 0; row < 4; row++) { @@ -209,7 +227,7 @@ void un_m_mat_mulf(f32 *v, f32 *left, f32 *right) { } } -void un_mat_xformf(f32 *v, f32 *xform) { +UN_INLINE void un_mat_xformf(float *v, float *xform) { un_m_mat_identityf(v); v[3 + 0 * UN_MATRIX_STRIPE] += xform[0]; @@ -217,7 +235,7 @@ void un_mat_xformf(f32 *v, f32 *xform) { v[3 + 2 * UN_MATRIX_STRIPE] += xform[2]; } -void un_mat_scalef(f32 *v, f32 *scale) { +UN_INLINE void un_mat_scalef(float *v, float *scale) { un_m_mat_identityf(v); v[0 + 0 * UN_MATRIX_STRIPE] = scale[0]; |