diff --git a/src/math.zig b/src/math.zig index 85390b01..9d2812a0 100644 --- a/src/math.zig +++ b/src/math.zig @@ -1063,3 +1063,55 @@ test "mat.rotateZ" { } } } + +test "mat.mul" { + { + const tolerance = 1e-6; + const t = Vec3{ 1, 2, -3 }; + const T = mat.translate3d(t); + const s = Vec3{ 3, 1, -5 }; + const S = mat.scale3d(s); + const r = Vec3{ 30, -40, 235 }; + const R_x = mat.rotateX(degreesToRadians(f32, r[0])); + const R_y = mat.rotateY(degreesToRadians(f32, r[1])); + const R_z = mat.rotateZ(degreesToRadians(f32, r[2])); + + const R_yz = mat.mul(R_y, R_z); + // NOTE: This values are calculated by hand with help of matrix calculator: https://matrix.reshish.com/multCalculation.php + const expected_R_yz = mat.set_3d_matrix(&[_]f32{ + -0.43938504177070496278, -0.8191520442889918, -0.36868782649461236545, 0, + 0.62750687159713312638, -0.573576436351046, 0.52654078451836329713, 0, + -0.6427876096865394, 0, 0.766044443118978, 0, + 0, 0, 0, 1, + }); + try expect(mat.equalsApproximately(R_yz, expected_R_yz, tolerance)); + + const R_xyz = mat.mul(R_x, R_yz); + const expected_R_xyz = mat.set_3d_matrix(&[_]f32{ + -0.439385041770705, -0.52506256666891627986, -0.72886904595489960019, 0, + 0.6275068715971331, -0.76000215715133560834, 0.16920947734596765363, 0, + -0.6427876096865394, -0.383022221559489, 0.66341394816893832989, 0, + 0, 0, 0, 1, + }); + try expect(mat.equalsApproximately(R_xyz, expected_R_xyz, tolerance)); + + const SR = mat.mul(S, R_xyz); + const expected_SR = mat.set_3d_matrix(&[_]f32{ + -1.318155125312115, -0.5250625666689163, 3.6443452297744985, 0, + 1.8825206147913993, -0.7600021571513356, -0.8460473867298382, 0, + -1.9283628290596182, -0.383022221559489, -3.3170697408446915, 0, + 0, 0, 0, 1, + }); + try expect(mat.equalsApproximately(SR, expected_SR, tolerance)); + + const TSR = mat.mul(T, SR); + const expected_TSR = mat.set_3d_matrix(&[_]f32{ + -1.318155125312115, -0.5250625666689163, 3.6443452297744985, 0, + 1.8825206147913993, -0.7600021571513356, -0.8460473867298382, 0, + -1.9283628290596182, -0.383022221559489, -3.3170697408446914, 0, + 1, 2, -3, 1, + }); + + try expect(mat.equalsApproximately(TSR, expected_TSR, tolerance)); + } +}