diff options
| author | HampusM <hampus@hampusmat.com> | 2026-09-14 10:03:43 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-09-14 10:03:43 +0200 |
| commit | 2ade9c8959fc3d6ab595dda62dc1091d9bb679b2 (patch) | |
| tree | 30ca26ba1fe79445d92e3669dc4c0cb73abd294c /engine/src/data_types | |
| parent | 62490a4fa76265c36870935338861d16b4498472 (diff) | |
feat(engine): add matrix impls for multiplication with Vec3 & Vec4
Diffstat (limited to 'engine/src/data_types')
| -rw-r--r-- | engine/src/data_types/matrix.rs | 113 |
1 files changed, 113 insertions, 0 deletions
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<Value, const COLUMNS: usize> Mul<Vec3<Value>> for Matrix<Value, 3, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec3<Value>; + + fn mul(self, rhs: Vec3<Value>) -> Self::Output + { + &self * &rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<&Vec3<Value>> for Matrix<Value, 3, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec3<Value>; + + fn mul(self, rhs: &Vec3<Value>) -> Self::Output + { + &self * rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<Vec3<Value>> for &Matrix<Value, 3, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec3<Value>; + + fn mul(self, rhs: Vec3<Value>) -> Self::Output + { + self * &rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<&Vec3<Value>> for &Matrix<Value, 3, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec3<Value>; + + fn mul(self, rhs: &Vec3<Value>) -> Self::Output + { + let rhs_mat = Matrix::<Value, 3, 1> { 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<Value, const COLUMNS: usize> Mul<Vec4<Value>> for Matrix<Value, 4, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec4<Value>; + + fn mul(self, rhs: Vec4<Value>) -> Self::Output + { + &self * &rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<&Vec4<Value>> for Matrix<Value, 4, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec4<Value>; + + fn mul(self, rhs: &Vec4<Value>) -> Self::Output + { + &self * rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<Vec4<Value>> for &Matrix<Value, 4, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec4<Value>; + + fn mul(self, rhs: Vec4<Value>) -> Self::Output + { + self * &rhs + } +} + +impl<Value, const COLUMNS: usize> Mul<&Vec4<Value>> for &Matrix<Value, 4, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Vec4<Value>; + + fn mul(self, rhs: &Vec4<Value>) -> Self::Output + { + let rhs_mat = Matrix::<Value, 4, 1> { + 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<Value, const ROWS: usize, const COLUMNS: usize> Index<CellPos> for Matrix<Value, ROWS, COLUMNS> { |
