rename xform.t -> xform.tl
This commit is contained in:
parent
77421d7c09
commit
650c5cd762
@ -415,7 +415,7 @@ struct xform {
|
|||||||
struct {
|
struct {
|
||||||
struct v2 bx; /* X basis vector */
|
struct v2 bx; /* X basis vector */
|
||||||
struct v2 by; /* Y basis vector */
|
struct v2 by; /* Y basis vector */
|
||||||
struct v2 t; /* Translation vector */
|
struct v2 tl; /* Translation vector */
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
152
src/math.h
152
src/math.h
@ -6,11 +6,6 @@
|
|||||||
#define PI ((f32)3.14159265358979323846)
|
#define PI ((f32)3.14159265358979323846)
|
||||||
#define TAU ((f32)6.28318530717958647693)
|
#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
|
* Rounding
|
||||||
* ========================== */
|
* ========================== */
|
||||||
@ -403,7 +398,7 @@ INLINE struct mat4x4 mat4x4_from_xform(struct xform xf)
|
|||||||
{xf.bx.x, xf.bx.y, 0, 0},
|
{xf.bx.x, xf.bx.y, 0, 0},
|
||||||
{xf.by.x, xf.by.y, 0, 0},
|
{xf.by.x, xf.by.y, 0, 0},
|
||||||
{0, 0, 1, 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
|
* 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)
|
INLINE struct xform xform_ident(void)
|
||||||
{
|
{
|
||||||
return (struct xform) {
|
return (struct xform) {
|
||||||
.bx = { 1, 0 },
|
.bx = { 1, 0 },
|
||||||
.by = { 0, 1 },
|
.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) {
|
return (struct xform) {
|
||||||
.bx = {1, 0},
|
.bx = {1, 0},
|
||||||
.by = {0, 1},
|
.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)
|
INLINE struct xform xform_translate(struct xform xf, struct v2 v)
|
||||||
{
|
{
|
||||||
xf.t = V2(
|
xf.tl = V2(
|
||||||
xf.bx.x * v.x + xf.by.x * v.y + xf.t.x,
|
xf.bx.x * v.x + xf.by.x * v.y + xf.tl.x,
|
||||||
xf.bx.y * v.x + xf.by.y * v.y + xf.t.y
|
xf.bx.y * v.x + xf.by.y * v.y + xf.tl.y
|
||||||
);
|
);
|
||||||
return xf;
|
return xf;
|
||||||
}
|
}
|
||||||
@ -497,14 +509,12 @@ INLINE struct xform xform_rotate(struct xform xf, f32 angle)
|
|||||||
f32 s = math_sin(angle);
|
f32 s = math_sin(angle);
|
||||||
|
|
||||||
struct xform res = xf;
|
struct xform res = xf;
|
||||||
res.bx = V2(
|
|
||||||
xf.bx.x * c + xf.by.x * s,
|
res.bx.x = xf.bx.x * c + xf.by.x * s;
|
||||||
xf.bx.y * c + xf.by.y * 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 = V2(
|
res.by.y = xf.bx.y * -s + xf.by.y * c;
|
||||||
xf.bx.x * -s + xf.by.x * c,
|
|
||||||
xf.bx.y * -s + xf.by.y * c
|
|
||||||
);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -515,14 +525,6 @@ INLINE struct xform xform_scale(struct xform xf, struct v2 v)
|
|||||||
return xf;
|
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)
|
INLINE struct xform xform_trs(struct xform xf, struct trs trs)
|
||||||
{
|
{
|
||||||
xf = xform_translate(xf, trs.t);
|
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;
|
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(
|
struct trs trs_a = trs_from_xform(a);
|
||||||
xf.bx.x * v.x + xf.by.x * v.y,
|
struct trs trs_b = trs_from_xform(b);
|
||||||
xf.bx.y * v.x + xf.by.y * v.y
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
INLINE struct v2 xform_mul_v2(struct xform xf, struct v2 v)
|
struct trs trs = trs_lerp(trs_a, trs_b, t);
|
||||||
{
|
|
||||||
struct v2 res = xform_basis_mul_v2(xf, v);
|
|
||||||
res = v2_add(res, xf.t);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
INLINE struct v2 xform_mul_v2_invert(struct xform xf, struct v2 v)
|
return xform_from_trs(trs);
|
||||||
{
|
|
||||||
struct xform inv = xform_invert(xf);
|
|
||||||
struct v2 res = xform_basis_mul_v2(inv, v);
|
|
||||||
res = v2_add(res, inv.t);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
INLINE struct xform xform_invert(struct xform xf)
|
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.bx = v2_mul_v2(xf.bx, V2(inv_det, -inv_det));
|
||||||
xf.by = v2_mul_v2(xf.by, 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;
|
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);
|
return V2(
|
||||||
struct trs trs_b = trs_from_xform(b);
|
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)
|
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)
|
INLINE struct v2 xform_get_pos(struct xform xf)
|
||||||
{
|
{
|
||||||
return xf.t;
|
return xf.tl;
|
||||||
}
|
|
||||||
|
|
||||||
INLINE f32 xform_get_determinant(struct xform xf)
|
|
||||||
{
|
|
||||||
return v2_wedge(xf.bx, xf.by);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
INLINE f32 xform_get_rot(struct xform xf)
|
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));
|
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
|
* Trs
|
||||||
* ========================== */
|
* ========================== */
|
||||||
@ -667,12 +669,12 @@ INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
INLINE struct trs trs_from_xform(struct xform m)
|
INLINE struct trs trs_from_xform(struct xform xf)
|
||||||
{
|
{
|
||||||
struct trs trs = { 0 };
|
struct trs trs = { 0 };
|
||||||
trs.t = xform_get_pos(m);
|
trs.t = xform_get_pos(xf);
|
||||||
trs.r = xform_get_rot(m);
|
trs.r = xform_get_rot(xf);
|
||||||
trs.s = xform_get_scale(m);
|
trs.s = xform_get_scale(xf);
|
||||||
return trs;
|
return trs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user