summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-09-12 12:50:44 +0200
committerHampusM <hampus@hampusmat.com>2026-09-12 12:50:44 +0200
commite5b2bc8abbe041d56beb57b95cc444c20e526367 (patch)
treed420f0c85adf6e0ffb47400540fd73c648f75d1a
parent177abc8fed26f2c9c04622e94fa7271629c78396 (diff)
feat(engine): add naive matrix multiplication fn
-rw-r--r--engine/src/data_types/matrix.rs32
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>
{