From 650c5cd7624f6aad93943c3f9bf8d93d63d36805 Mon Sep 17 00:00:00 2001 From: jacob Date: Thu, 7 Mar 2024 16:02:43 -0600 Subject: [PATCH] rename xform.t -> xform.tl --- src/common.h | 2 +- src/math.h | 152 ++++++++++++++++++++++++++------------------------- 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/src/common.h b/src/common.h index 12c6a464..2186b54a 100644 --- a/src/common.h +++ b/src/common.h @@ -415,7 +415,7 @@ struct xform { struct { struct v2 bx; /* X basis vector */ struct v2 by; /* Y basis vector */ - struct v2 t; /* Translation vector */ + struct v2 tl; /* Translation vector */ }; }; diff --git a/src/math.h b/src/math.h index 24013e01..649bfa34 100644 --- a/src/math.h +++ b/src/math.h @@ -6,11 +6,6 @@ #define PI ((f32)3.14159265358979323846) #define TAU ((f32)6.28318530717958647693) -INLINE struct trs trs_from_xform(struct xform m); -INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t); -INLINE f32 xform_get_determinant(struct xform xf); -INLINE struct xform xform_invert(struct xform xf); - /* ========================== * * Rounding * ========================== */ @@ -403,7 +398,7 @@ INLINE struct mat4x4 mat4x4_from_xform(struct xform xf) {xf.bx.x, xf.bx.y, 0, 0}, {xf.by.x, xf.by.y, 0, 0}, {0, 0, 1, 0}, - {xf.t.x, xf.t.y, 0, 1}, + {xf.tl.x, xf.tl.y, 0, 1}, } }; } @@ -464,12 +459,21 @@ INLINE struct mat4x4 mat4x4_mul(struct mat4x4 m1, struct mat4x4 m2) * Xform * ========================== */ +INLINE struct xform xform_translate(struct xform xf, struct v2 v); +INLINE struct xform xform_rotate(struct xform xf, f32 angle); +INLINE struct xform xform_scale(struct xform xf, struct v2 v); +INLINE struct xform xform_invert(struct xform xf); +INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v); +INLINE f32 xform_get_determinant(struct xform xf); +INLINE struct trs trs_from_xform(struct xform m); +INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t); + INLINE struct xform xform_ident(void) { return (struct xform) { .bx = { 1, 0 }, .by = { 0, 1 }, - .t = { 0, 0 } + .tl = { 0, 0 } }; } @@ -478,15 +482,23 @@ INLINE struct xform xform_from_translate(struct v2 v) return (struct xform) { .bx = {1, 0}, .by = {0, 1}, - .t = {v.x, v.y} + .tl = {v.x, v.y} }; } +INLINE struct xform xform_from_trs(struct trs trs) +{ + struct xform m = xform_from_translate(trs.t); + m = xform_rotate(m, trs.r); + m = xform_scale(m, trs.s); + return m; +} + INLINE struct xform xform_translate(struct xform xf, struct v2 v) { - xf.t = V2( - xf.bx.x * v.x + xf.by.x * v.y + xf.t.x, - xf.bx.y * v.x + xf.by.y * v.y + xf.t.y + xf.tl = V2( + xf.bx.x * v.x + xf.by.x * v.y + xf.tl.x, + xf.bx.y * v.x + xf.by.y * v.y + xf.tl.y ); return xf; } @@ -497,14 +509,12 @@ INLINE struct xform xform_rotate(struct xform xf, f32 angle) f32 s = math_sin(angle); struct xform res = xf; - res.bx = V2( - xf.bx.x * c + xf.by.x * s, - xf.bx.y * c + xf.by.y * s - ); - res.by = V2( - xf.bx.x * -s + xf.by.x * c, - xf.bx.y * -s + xf.by.y * c - ); + + res.bx.x = xf.bx.x * c + xf.by.x * s; + res.bx.y = xf.bx.y * c + xf.by.y * s; + res.by.x = xf.bx.x * -s + xf.by.x * c; + res.by.y = xf.bx.y * -s + xf.by.y * c; + return res; } @@ -515,14 +525,6 @@ INLINE struct xform xform_scale(struct xform xf, struct v2 v) return xf; } -INLINE struct xform xform_from_trs(struct trs trs) -{ - struct xform m = xform_from_translate(trs.t); - m = xform_rotate(m, trs.r); - m = xform_scale(m, trs.s); - return m; -} - INLINE struct xform xform_trs(struct xform xf, struct trs trs) { xf = xform_translate(xf, trs.t); @@ -549,27 +551,14 @@ INLINE struct xform xform_trs_pivot_rs(struct xform xf, struct trs trs, struct v return xf; } -INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v) +INLINE struct xform xform_lerp(struct xform a, struct xform b, f32 t) { - return V2( - xf.bx.x * v.x + xf.by.x * v.y, - xf.bx.y * v.x + xf.by.y * v.y - ); -} + struct trs trs_a = trs_from_xform(a); + struct trs trs_b = trs_from_xform(b); -INLINE struct v2 xform_mul_v2(struct xform xf, struct v2 v) -{ - struct v2 res = xform_basis_mul_v2(xf, v); - res = v2_add(res, xf.t); - return res; -} + struct trs trs = trs_lerp(trs_a, trs_b, t); -INLINE struct v2 xform_mul_v2_invert(struct xform xf, struct v2 v) -{ - struct xform inv = xform_invert(xf); - struct v2 res = xform_basis_mul_v2(inv, v); - res = v2_add(res, inv.t); - return res; + return xform_from_trs(trs); } INLINE struct xform xform_invert(struct xform xf) @@ -584,19 +573,51 @@ INLINE struct xform xform_invert(struct xform xf) xf.bx = v2_mul_v2(xf.bx, V2(inv_det, -inv_det)); xf.by = v2_mul_v2(xf.by, V2(-inv_det, inv_det)); - xf.t = xform_basis_mul_v2(xf, v2_neg(xf.t)); + xf.tl = xform_basis_mul_v2(xf, v2_neg(xf.tl)); return xf; } -INLINE struct xform xform_lerp(struct xform a, struct xform b, f32 t) +INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v) { - struct trs trs_a = trs_from_xform(a); - struct trs trs_b = trs_from_xform(b); + return V2( + xf.bx.x * v.x + xf.by.x * v.y, + xf.bx.y * v.x + xf.by.y * v.y + ); +} - struct trs trs = trs_lerp(trs_a, trs_b, t); +INLINE struct v2 xform_mul_v2(struct xform xf, struct v2 v) +{ + struct v2 res = xform_basis_mul_v2(xf, v); + res = v2_add(res, xf.tl); + return res; +} - return xform_from_trs(trs); +INLINE struct v2 xform_mul_v2_invert(struct xform xf, struct v2 v) +{ + struct xform inv = xform_invert(xf); + struct v2 res = xform_basis_mul_v2(inv, v); + res = v2_add(res, inv.tl); + return res; +} + +INLINE f32 xform_get_determinant(struct xform xf) +{ + return v2_wedge(xf.bx, xf.by); +} + +INLINE f32 xform_get_skew(struct xform xf) +{ + f32 det = xform_get_determinant(xf); + i32 det_sign = math_sign_f32(det); + + struct v2 bx_norm = v2_norm(xf.bx); + struct v2 by_norm = v2_norm(xf.by); + by_norm = v2_mul(by_norm, det_sign); + + f32 dot = v2_dot(bx_norm, by_norm); + + return math_acos(dot) - (PI / 2.0f); } INLINE struct v2 xform_get_right(struct xform xf) @@ -621,12 +642,7 @@ INLINE struct v2 xform_get_down(struct xform xf) INLINE struct v2 xform_get_pos(struct xform xf) { - return xf.t; -} - -INLINE f32 xform_get_determinant(struct xform xf) -{ - return v2_wedge(xf.bx, xf.by); + return xf.tl; } INLINE f32 xform_get_rot(struct xform xf) @@ -640,20 +656,6 @@ INLINE struct v2 xform_get_scale(struct xform xf) return V2(v2_len(xf.bx), det_sign * v2_len(xf.by)); } -INLINE f32 xform_get_skew(struct xform xf) -{ - f32 det = xform_get_determinant(xf); - i32 det_sign = math_sign_f32(det); - - struct v2 bx_norm = v2_norm(xf.bx); - struct v2 by_norm = v2_norm(xf.by); - by_norm = v2_mul(by_norm, det_sign); - - f32 dot = v2_dot(bx_norm, by_norm); - - return math_acos(dot) - (PI / 2.0f); -} - /* ========================== * * Trs * ========================== */ @@ -667,12 +669,12 @@ INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t) return res; } -INLINE struct trs trs_from_xform(struct xform m) +INLINE struct trs trs_from_xform(struct xform xf) { struct trs trs = { 0 }; - trs.t = xform_get_pos(m); - trs.r = xform_get_rot(m); - trs.s = xform_get_scale(m); + trs.t = xform_get_pos(xf); + trs.r = xform_get_rot(xf); + trs.s = xform_get_scale(xf); return trs; }