math: add Matrix translation getters

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-09-08 18:08:11 -07:00
parent 94540a4332
commit 8fd84a6bda

View file

@ -91,6 +91,12 @@ pub fn Mat(
pub inline fn translateScalar(scalar: Vec.T) Matrix { pub inline fn translateScalar(scalar: Vec.T) Matrix {
return translate(Vec.splat(scalar)); return translate(Vec.splat(scalar));
} }
/// Returns the translation component of the matrix.
// TODO: needs tests
pub inline fn translation(v: Matrix) math.Vec2 {
return math.Vec2.init(v.v[2].x(), v.v[2].y());
}
}, },
inline math.Mat4x4, math.Mat4x4h, math.Mat4x4d => struct { inline math.Mat4x4, math.Mat4x4h, math.Mat4x4d => struct {
pub inline fn init(col0: Vec, col1: Vec, col2: Vec, col3: Vec) Matrix { pub inline fn init(col0: Vec, col1: Vec, col2: Vec, col3: Vec) Matrix {
@ -136,6 +142,12 @@ pub fn Mat(
return translate(Vec.splat(scalar)); return translate(Vec.splat(scalar));
} }
/// Returns the translation component of the matrix.
// TODO: needs tests
pub inline fn translation(v: Matrix) math.Vec3 {
return math.Vec3.init(v.v[3].x(), v.v[3].y(), v.v[3].z());
}
/// Constructs an orthographic projection matrix; an orthogonal transformation matrix /// Constructs an orthographic projection matrix; an orthogonal transformation matrix
/// which transforms from the given left, right, bottom, and top dimensions into /// which transforms from the given left, right, bottom, and top dimensions into
/// `(-1, +1)` in `(x, y)`, and `(0, +1)` in `z`. /// `(-1, +1)` in `(x, y)`, and `(0, +1)` in `z`.
@ -176,16 +188,6 @@ pub fn Mat(
// to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3 // to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3
// and 4x4 specific functions into the mixin above, writing new tests, etc. // and 4x4 specific functions into the mixin above, writing new tests, etc.
// /// Returns the translation component of the 2D matrix.
// pub inline fn translation2d(v: Mat3x3) Vec2 {
// return .{ mat.index(v, 8), mat.index(v, 9) };
// }
// /// Returns the translation component of the 3D matrix.
// pub inline fn translation3d(v: Mat4x4) Vec3 {
// return .{ mat.index(v, 12), mat.index(v, 13), mat.index(v, 14) };
// }
// // Multiplies matrices a * b // // Multiplies matrices a * b
// pub inline fn mul(a: anytype, b: @TypeOf(a)) @TypeOf(a) { // pub inline fn mul(a: anytype, b: @TypeOf(a)) @TypeOf(a) {
// return if (@TypeOf(a) == Mat3x3) { // return if (@TypeOf(a) == Mat3x3) {
@ -438,25 +440,6 @@ test "mat4x4_ident" {
// try expectEqual(ortho_mat[3][3], 1); // try expectEqual(ortho_mat[3][3], 1);
// } // }
// test "mat.translation" {
// {
// const v = Vec2{ 1.0, -2.5 };
// const translation_mat = mat.translate2d(v);
// const result = mat.translation2d(translation_mat);
// try expectEqual(result[0], v[0]);
// try expectEqual(result[1], v[1]);
// }
// {
// const v = Vec3{ 1.0, -2.5, 0.001 };
// const translation_mat = mat.translate3d(v);
// const result = mat.translation3d(translation_mat);
// try expectEqual(result[0], v[0]);
// try expectEqual(result[1], v[1]);
// try expectEqual(result[2], v[2]);
// }
// }
// const degreesToRadians = std.math.degreesToRadians; // const degreesToRadians = std.math.degreesToRadians;
// // TODO: Maybe reconsider based on feedback to join all test for rotation into one test as only // // TODO: Maybe reconsider based on feedback to join all test for rotation into one test as only