From 2ade9c8959fc3d6ab595dda62dc1091d9bb679b2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Sep 2026 10:03:43 +0200 Subject: feat(engine): add matrix impls for multiplication with Vec3 & Vec4 --- engine/src/data_types/matrix.rs | 113 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) (limited to 'engine/src/data_types') diff --git a/engine/src/data_types/matrix.rs b/engine/src/data_types/matrix.rs index a1e9bb6..b480c35 100644 --- a/engine/src/data_types/matrix.rs +++ b/engine/src/data_types/matrix.rs @@ -353,6 +353,119 @@ where } } +impl Mul> for Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec3; + + fn mul(self, rhs: Vec3) -> Self::Output + { + &self * &rhs + } +} + +impl Mul<&Vec3> for Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec3; + + fn mul(self, rhs: &Vec3) -> Self::Output + { + &self * rhs + } +} + +impl Mul> for &Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec3; + + fn mul(self, rhs: Vec3) -> Self::Output + { + self * &rhs + } +} + +impl Mul<&Vec3> for &Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec3; + + fn mul(self, rhs: &Vec3) -> Self::Output + { + let rhs_mat = Matrix:: { items: [[rhs.x], [rhs.y], [rhs.z]] }; + + let Matrix { items: [[out_x], [out_y], [out_z]] } = self * &rhs_mat; + + Vec3 { x: out_x, y: out_y, z: out_z } + } +} + +impl Mul> for Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec4; + + fn mul(self, rhs: Vec4) -> Self::Output + { + &self * &rhs + } +} + +impl Mul<&Vec4> for Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec4; + + fn mul(self, rhs: &Vec4) -> Self::Output + { + &self * rhs + } +} + +impl Mul> for &Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec4; + + fn mul(self, rhs: Vec4) -> Self::Output + { + self * &rhs + } +} + +impl Mul<&Vec4> for &Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Vec4; + + fn mul(self, rhs: &Vec4) -> Self::Output + { + let rhs_mat = Matrix:: { + items: [[rhs.x], [rhs.y], [rhs.z], [rhs.w]], + }; + + let Matrix { + items: [[out_x], [out_y], [out_z], [out_w]], + } = self * &rhs_mat; + + Vec4 { + x: out_x, + y: out_y, + z: out_z, + w: out_w, + } + } +} + impl Index for Matrix { -- cgit v1.2.3-18-g5258