Extended matrix functions

This commit is contained in:
Pascal Serrarens 2024-02-22 11:25:39 +01:00
parent f38e01d80a
commit af892bc943
2 changed files with 80 additions and 45 deletions

View File

@ -1,24 +1,17 @@
#include "Matrix.h" #include "Matrix.h"
template <> void MatrixOf<float>::Transpose(MatrixOf<float> *r) { template <> MatrixOf<float>::MatrixOf(unsigned int rows, unsigned int cols) {
// Check dimensions first this->rows = rows;
// We dont care about the rows and cols (we overwrite them) this->cols = cols;
// but the data size should be equal to avoid problems
// We cannot check the data size directly, but the row*col should be equal
unsigned int matrixSize = this->cols * this->rows; unsigned int matrixSize = this->cols * this->rows;
if (matrixSize != r->rows * r->cols) this->data = new float[matrixSize]{0.0f};
// Exception??? For now we don't do anything }
return;
r->cols = this->rows; template <> MatrixOf<float>::MatrixOf(Vector3 v) : MatrixOf(3, 1) {
r->rows = this->cols; Set(0, 0, v.x);
Set(1, 0, v.y);
for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) { Set(2, 0, v.z);
unsigned int rowIx = rDataIx / this->rows;
unsigned int colIx = rDataIx % this->rows;
unsigned int mDataIx = this->cols * colIx + rowIx;
r->data[rDataIx] = this->data[mDataIx];
}
} }
template <> template <>
@ -37,23 +30,14 @@ void MatrixOf<float>::Multiply(const MatrixOf<float> *m1, MatrixOf<float> *m2,
} }
} }
template <>
void MatrixOf<float>::Multiply(MatrixOf<float> *m2, MatrixOf<float> *r) {
Multiply(this, m2, r);
}
template <> template <>
Vector3 MatrixOf<float>::Multiply(const MatrixOf<float> *m, Vector3 v) { Vector3 MatrixOf<float>::Multiply(const MatrixOf<float> *m, Vector3 v) {
float *vData = new float[3]{v.x, v.y, v.z}; MatrixOf<float> v_m = MatrixOf<float>(v);
MatrixOf<float> v_m = MatrixOf<float>(3, 1, vData); MatrixOf<float> r_m = MatrixOf<float>(3, 1);
float *rData = new float[3]{};
MatrixOf<float> r_m = MatrixOf<float>(3, 1, rData);
Multiply(m, &v_m, &r_m); Multiply(m, &v_m, &r_m);
Vector3 r = Vector3(r_m.data[0], r_m.data[1], r_m.data[2]); Vector3 r = Vector3(r_m.data[0], r_m.data[1], r_m.data[2]);
delete[] vData;
delete[] rData;
return r; return r;
} }
@ -70,12 +54,3 @@ template <typename T> Vector3 MatrixOf<T>::operator*(const Vector3 v) const {
delete[] rData; delete[] rData;
return r; return r;
} }
template <> float MatrixOf<float>::Get(unsigned int rowIx, unsigned int colIx) {
unsigned int dataIx = rowIx * this->cols + colIx;
return this->data[dataIx];
}
template <> unsigned int MatrixOf<float>::RowCount() { return rows; }
template <> unsigned int MatrixOf<float>::ColCount() { return cols; }

View File

@ -6,23 +6,83 @@
/// @brief Single precision float matrix /// @brief Single precision float matrix
template <typename T> class MatrixOf { template <typename T> class MatrixOf {
public: public:
MatrixOf(unsigned int rows, unsigned int cols, T *data) MatrixOf(unsigned int rows, unsigned int cols);
: rows(rows), cols(cols), data(data) {} MatrixOf(unsigned int rows, unsigned int cols, T *source)
// MatrixOf(Vector3 v); // creates a 3,1 matrix : MatrixOf(rows, cols) {
Set(source);
}
MatrixOf(Vector3 v); // creates a 3,1 matrix
~MatrixOf() {
if (this->data == nullptr)
return;
delete[] this->data;
}
/// @brief Transpose with result in matrix m /// @brief Transpose with result in matrix m
/// @param r The matrix in which the transposed matrix is stored /// @param r The matrix in which the transposed matrix is stored
void Transpose(MatrixOf<T> *r); void Transpose(MatrixOf<T> *r) const {
// Check dimensions first
// We dont care about the rows and cols (we overwrite them)
// but the data size should be equal to avoid problems
// We cannot check the data size directly, but the row*col should be equal
unsigned int matrixSize = this->cols * this->rows;
if (matrixSize != r->rows * r->cols)
// Exception??? For now we don't do anything
return;
r->cols = this->rows;
r->rows = this->cols;
for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) {
unsigned int rowIx = rDataIx / this->rows;
unsigned int colIx = rDataIx % this->rows;
unsigned int mDataIx = this->cols * colIx + rowIx;
r->data[rDataIx] = this->data[mDataIx];
}
}
static void Multiply(const MatrixOf<T> *m1, MatrixOf<T> *m2, MatrixOf<T> *r); static void Multiply(const MatrixOf<T> *m1, MatrixOf<T> *m2, MatrixOf<T> *r);
void Multiply(MatrixOf<T> *m, MatrixOf<T> *r); void Multiply(MatrixOf<T> *m, MatrixOf<T> *r) { Multiply(this, m, r); }
static Vector3 Multiply(const MatrixOf<T> *m, Vector3 v); static Vector3 Multiply(const MatrixOf<T> *m, Vector3 v);
Vector3 operator*(const Vector3 v) const; Vector3 operator*(const Vector3 v) const;
T Get(unsigned int rowIx, unsigned int colIx); T Get(unsigned int rowIx, unsigned int colIx) {
unsigned int RowCount(); unsigned int dataIx = rowIx * this->cols + colIx;
unsigned int ColCount(); return this->data[dataIx];
}
void Set(unsigned int rowIx, unsigned int colIx, T value) {
unsigned int dataIx = rowIx * this->cols + colIx;
this->data[dataIx] = value;
}
// This function does not check on source size!
void Set(T *source) {
unsigned int matrixSize = this->cols * this->rows;
for (unsigned int dataIx = 0; dataIx < matrixSize; dataIx++)
this->data[dataIx] = source[dataIx];
}
// This function does not check on source size!
void SetRow(unsigned int rowIx, const T *source) {
unsigned int dataIx = rowIx * this->cols;
for (unsigned int sourceIx = 0; sourceIx < this->cols; dataIx++, sourceIx++)
this->data[dataIx] = source[sourceIx];
}
// This function does not check on source size!
void SetCol(unsigned int colIx, const T *source) {
unsigned int dataIx = colIx;
for (unsigned int sourceIx = 0; sourceIx < this->cols;
dataIx += this->cols, sourceIx++)
this->data[dataIx] = source[sourceIx];
}
unsigned int RowCount() { return rows; }
unsigned int ColCount() { return cols; }
private: private:
unsigned int rows; unsigned int rows;