diff options
| -rw-r--r-- | engine/src/data_types/matrix.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/engine/src/data_types/matrix.rs b/engine/src/data_types/matrix.rs index 3c54bb1..e28c73f 100644 --- a/engine/src/data_types/matrix.rs +++ b/engine/src/data_types/matrix.rs @@ -1,4 +1,4 @@ -use std::ops::{Index, IndexMut, Mul}; +use std::ops::{Add, Index, IndexMut, Mul}; use crate::data_types::dimens::Dimens3; use crate::vector::{Vec3, Vec4}; @@ -284,6 +284,36 @@ impl Mul<f32> for Matrix<f32, 4, 4> } } +impl<Value, const ROWS: usize, const COLUMNS: usize, const RHS_COLUMNS: usize> + Mul<Matrix<Value, ROWS, RHS_COLUMNS>> for Matrix<Value, ROWS, COLUMNS> +where + Value: Mul<Output = Value> + Add<Output = Value> + Default + Copy, +{ + type Output = Matrix<Value, ROWS, RHS_COLUMNS>; + + fn mul(self, rhs: Matrix<Value, ROWS, RHS_COLUMNS>) -> Self::Output + { + // https://en.wikipedia.org/wiki/Matrix_multiplication + + let mut out = Self::Output::new(); + + for (row_index, row) in self.items.iter().enumerate() { + for (column, item) in row.iter().enumerate() { + let rhs_items = &rhs.items[column]; + + for (rhs_item_index, rhs_item) in rhs_items.iter().enumerate() { + let prev = out[CellPos { row: row_index, col: rhs_item_index }]; + + out[CellPos { row: row_index, col: rhs_item_index }] = + prev + (*item * *rhs_item); + } + } + } + + out + } +} + impl<Value, const ROWS: usize, const COLUMNS: usize> Index<CellPos> for Matrix<Value, ROWS, COLUMNS> { |
