diff --git a/src/vec3.zig b/src/vec3.zig index e9cdd25..3079253 100644 --- a/src/vec3.zig +++ b/src/vec3.zig @@ -90,7 +90,7 @@ pub fn mat3Mul(a: Mat3F64, b: Mat3F64) Mat3F64 { pub fn mat3Det(a: Mat3F64) f64 { return a.row1.x() * (a.row2.y() * a.row3.z() - a.row2.z() * a.row3.y()) - a.row1.y() * (a.row2.x() * a.row3.z() - a.row2.z() * a.row3.x()) + - a.row1.y() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x()); + a.row1.z() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x()); } pub fn mat3Inv(a: Mat3F64) Mat3F64 { @@ -105,7 +105,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 { ); const row2 = vec3Scale( .init( - a.row2.z() * a.row3.x() + a.row2.x() * a.row3.z(), + a.row2.z() * a.row3.x() - a.row2.x() * a.row3.z(), a.row1.x() * a.row3.z() - a.row1.z() * a.row3.x(), a.row1.z() * a.row2.x() - a.row1.x() * a.row2.z(), ), @@ -113,7 +113,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 { ); const row3 = vec3Scale( .init( - a.row2.x() * a.row3.y() + a.row2.y() * a.row3.x(), + a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x(), a.row1.y() * a.row3.x() - a.row1.x() * a.row3.y(), a.row1.x() * a.row2.y() - a.row1.y() * a.row2.x(), ), @@ -321,30 +321,30 @@ test "Mat3 mul works" { test "mat determinant works" { const a: Mat3F64 = .{ .row1 = Vec3F64.init(1, 2, 3), - .row2 = Vec3F64.init(0, 1, 5), - .row3 = Vec3F64.init(0, 0, 9), + .row2 = Vec3F64.init(4, 1, 5), + .row3 = Vec3F64.init(3, 1, 9), }; - try std.testing.expectApproxEqAbs(9, mat3Det(a), 1e-12); + try std.testing.expectApproxEqAbs(-35, mat3Det(a), 1e-12); } test "mat inverse works" { const a: Mat3F64 = .{ .row1 = Vec3F64.init(1, 2, 3), - .row2 = Vec3F64.init(0, 1, 5), - .row3 = Vec3F64.init(0, 0, 9), + .row2 = Vec3F64.init(4, 1, 5), + .row3 = Vec3F64.init(3, 1, 9), }; const ainv = mat3Inv(a); - try std.testing.expectApproxEqAbs(1, ainv.row1.x(), 1e-12); - try std.testing.expectApproxEqAbs(-2, ainv.row1.y(), 1e-12); - try std.testing.expectApproxEqAbs(7.0 / 9.0, ainv.row1.z(), 1e-12); - try std.testing.expectApproxEqAbs(0, ainv.row2.x(), 1e-12); - try std.testing.expectApproxEqAbs(1, ainv.row2.y(), 1e-12); - try std.testing.expectApproxEqAbs(-5.0 / 9.0, ainv.row2.z(), 1e-12); - try std.testing.expectApproxEqAbs(0, ainv.row3.x(), 1e-12); - try std.testing.expectApproxEqAbs(0, ainv.row3.y(), 1e-12); - try std.testing.expectApproxEqAbs(1.0 / 9.0, ainv.row3.z(), 1e-12); + try std.testing.expectApproxEqAbs(-4.0 / 35.0, ainv.row1.x(), 1e-12); + try std.testing.expectApproxEqAbs(3.0 / 7.0, ainv.row1.y(), 1e-12); + try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row1.z(), 1e-12); + try std.testing.expectApproxEqAbs(3.0 / 5.0, ainv.row2.x(), 1e-12); + try std.testing.expectApproxEqAbs(0, ainv.row2.y(), 1e-12); + try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row2.z(), 1e-12); + try std.testing.expectApproxEqAbs(-1.0 / 35.0, ainv.row3.x(), 1e-12); + try std.testing.expectApproxEqAbs(-1.0 / 7.0, ainv.row3.y(), 1e-12); + try std.testing.expectApproxEqAbs(1.0 / 5.0, ainv.row3.z(), 1e-12); } test "vec3 mul mat3 works" { const b: Mat3F64 = .{