From 5095002b1e4ec0c9b6998f282e9e996c85224f68 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 8 Sep 2023 17:10:20 -0700 Subject: [PATCH] math: add Matrix scaling constructors Signed-off-by: Stephen Gutekanst --- src/math/mat.zig | 94 +++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/src/math/mat.zig b/src/math/mat.zig index bec09443..25def58d 100644 --- a/src/math/mat.zig +++ b/src/math/mat.zig @@ -59,6 +59,23 @@ pub fn Mat( Vec.init(col2.x(), col2.y(), col2.z(), 1), } }; } + + /// Constructs a 3D matrix which scales each dimension by the given vector. + // TODO: needs tests + pub inline fn scale(v: Vec) Matrix { + return init( + Vec.init(v.x(), 0, 0, 0), + Vec.init(0, v.y(), 0, 0), + Vec.init(0, 0, v.z(), 0), + Vec.init(0, 0, 0, 1), + ); + } + + /// Constructs a 3D matrix which scales each dimension by the given scalar. + // TODO: needs tests + pub inline fn scaleScalar(scalar: Vec.T) Matrix { + return scale(Vec.splat(scalar)); + } }, inline math.Mat4x4, math.Mat4x4h, math.Mat4x4d => struct { pub inline fn init(col0: Vec, col1: Vec, col2: Vec, col3: Vec) Matrix { @@ -69,6 +86,22 @@ pub fn Mat( col3, } }; } + + /// Constructs a 2D matrix which scales each dimension by the given vector. + // TODO: needs tests + pub inline fn scale(v: Vec) Matrix { + return init( + Vec.init(v.x(), 0, 0, 1), + Vec.init(0, v.y(), 0, 1), + Vec.init(0, 0, 1, 1), + ); + } + + /// Constructs a 2D matrix which scales each dimension by the given scalar. + // TODO: needs tests + pub inline fn scaleScalar(scalar: Vec.T) Matrix { + return scale(Vec.splat(scalar)); + } }, else => @compileError("Expected Mat3x3, Mat4x4 found '" ++ @typeName(Matrix) ++ "'"), }; @@ -137,25 +170,6 @@ pub fn Mat( // return .{ mat.index(v, 12), mat.index(v, 13), mat.index(v, 14) }; // } - // /// Constructs a 3D matrix which scales each dimension by the given vector. - // pub inline fn scale3d(v: Vec3) Mat4x4 { - // return init(Mat4x4, .{ - // v[0], 0, 0, 0, - // 0, v[1], 0, 0, - // 0, 0, v[2], 0, - // 0, 0, 0, 1, - // }); - // } - - // /// Constructs a 3D matrix which scales each dimension by the given vector. - // pub inline fn scale2d(v: Vec2) Mat3x3 { - // return init(Mat3x3, .{ - // v[0], 0, 0, 0, - // 0, v[1], 0, 0, - // 0, 0, 1, 0, - // }); - // } - // // Multiplies matrices a * b // pub inline fn mul(a: anytype, b: @TypeOf(a)) @TypeOf(a) { // return if (@TypeOf(a) == Mat3x3) { @@ -470,48 +484,6 @@ test "mat4x4_ident" { // } // } -// test "mat.scale2d" { -// const v = Vec2{ 1.0, -2.5 }; -// const scale_mat = mat.scale2d(v); - -// // Computed Values -// try expectEqual(scale_mat[0][0], v[0]); -// try expectEqual(scale_mat[1][1], v[1]); - -// // Constant values, which should not change but we still check for completeness -// const zero_value_indexes = [_]u8{ -// 1, 2, 3, -// 4, 4 + 2, 4 + 3, -// 4 * 2, 4 * 2 + 1, 4 * 2 + 3, -// }; -// for (zero_value_indexes) |index| { -// try expectEqual(mat.index(scale_mat, index), 0); -// } -// try expectEqual(scale_mat[2][2], 1); -// } - -// test "mat.scale3d" { -// const v = Vec3{ 1.0, -2.5, 0.001 }; -// const scale_mat = mat.scale3d(v); - -// // Computed Values -// try expectEqual(scale_mat[0][0], v[0]); -// try expectEqual(scale_mat[1][1], v[1]); -// try expectEqual(scale_mat[2][2], v[2]); - -// // Constant values, which should not change but we still check for completeness -// const zero_value_indexes = [_]u8{ -// 1, 2, 3, -// 4, 4 + 2, 4 + 3, -// 4 * 2, 4 * 2 + 1, 4 * 2 + 3, -// 4 * 3, 4 * 3 + 1, 4 * 3 + 2, -// }; -// for (zero_value_indexes) |index| { -// try expectEqual(mat.index(scale_mat, index), 0); -// } -// try expectEqual(scale_mat[3][3], 1); -// } - // const degreesToRadians = std.math.degreesToRadians; // // TODO: Maybe reconsider based on feedback to join all test for rotation into one test as only