From 7f0363ed7bf3b22568878f7c4f7c6fd372a50a41 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 2 Jul 2025 12:37:15 +0200 Subject: [PATCH] Improved matrix access --- LinearAlgebra/Matrix.cpp | 15 +++++++++++++++ LinearAlgebra/Matrix.h | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index 73b18c8..6709b23 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -52,6 +52,13 @@ Matrix1 Matrix1::Slice(int start, int stop) { return result; } +const float& Matrix1::operator()(int ix) const { + return data[ix]; +} +float& Matrix1::operator()(int ix) { + return data[ix]; +} + // Matrix1 #pragma endregion @@ -147,6 +154,14 @@ Matrix2 Matrix2::Zero(int nRows, int nCols) { return r; } +const float& Matrix2::operator()(int row, int col) const { + return data[row + this->nCols + col]; +} +float& Matrix2::operator()(int row, int col) { + return data[row + this->nCols + col]; +} + + void Matrix2::Clear() { for (int ix = 0; ix < this->nValues; ix++) this->data[ix] = 0; diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index be94054..de677de 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -21,6 +21,9 @@ class Matrix1 { static Matrix1 FromQuaternion(Quaternion q); Quaternion ToQuaternion(); + const float& operator()(int ix) const; + float& operator()(int ix); + Matrix1 Slice(int start, int stop); void Set(unsigned int start, unsigned int stop, float value) { @@ -66,6 +69,9 @@ class Matrix2 { static Matrix2 Zero(int nRows, int nCols); void Clear(); + const float& operator()(int row, int col) const; + float& operator()(int row, int col); + static Matrix2 Identity(int size); static Matrix2 Diagonal(float f, int size);