diff --git a/Matrix.cpp b/Matrix.cpp index b6bf89c..89af44d 100644 --- a/Matrix.cpp +++ b/Matrix.cpp @@ -1,7 +1,6 @@ #include "Matrix.h" -template <> -void MatrixOf::Transpose(MatrixOf* r) { +template <> void MatrixOf::Transpose(MatrixOf *r) { // Check dimensions first // We dont care about the rows and cols (we overwrite them) // but the data size should be equal to avoid problems @@ -23,9 +22,8 @@ void MatrixOf::Transpose(MatrixOf* r) { } template <> -void MatrixOf::Multiply(MatrixOf* m1, - MatrixOf* m2, - MatrixOf* r) { +void MatrixOf::Multiply(const MatrixOf *m1, MatrixOf *m2, + MatrixOf *r) { for (unsigned int rowIx1 = 0; rowIx1 < m1->rows; rowIx1++) { for (unsigned int colIx2 = 0; colIx2 < m2->cols; colIx2++) { unsigned int rDataIx = colIx2 * m2->cols + rowIx1; @@ -40,15 +38,15 @@ void MatrixOf::Multiply(MatrixOf* m1, } template <> -void MatrixOf::Multiply(MatrixOf* m2, MatrixOf* r) { +void MatrixOf::Multiply(MatrixOf *m2, MatrixOf *r) { Multiply(this, m2, r); } template <> -Vector3 MatrixOf::Multiply(MatrixOf* m, Vector3 v) { - float* vData = new float[3]{v.x, v.y, v.z}; +Vector3 MatrixOf::Multiply(const MatrixOf *m, Vector3 v) { + float *vData = new float[3]{v.x, v.y, v.z}; MatrixOf v_m = MatrixOf(3, 1, vData); - float* rData = new float[3]{}; + float *rData = new float[3]{}; MatrixOf r_m = MatrixOf(3, 1, rData); Multiply(m, &v_m, &r_m); @@ -59,18 +57,25 @@ Vector3 MatrixOf::Multiply(MatrixOf* m, Vector3 v) { return r; } -template <> -float MatrixOf::Get(unsigned int rowIx, unsigned int colIx) { +template Vector3 MatrixOf::operator*(const Vector3 v) const { + float *vData = new float[3]{v.x, v.y, v.z}; + MatrixOf v_m = MatrixOf(3, 1, vData); + float *rData = new float[3]{}; + MatrixOf r_m = MatrixOf(3, 1, rData); + + Multiply(this, &v_m, &r_m); + + Vector3 r = Vector3(r_m.data[0], r_m.data[1], r_m.data[2]); + delete[] vData; + delete[] rData; + return r; +} + +template <> float MatrixOf::Get(unsigned int rowIx, unsigned int colIx) { unsigned int dataIx = rowIx * this->cols + colIx; return this->data[dataIx]; } -template <> -unsigned int MatrixOf::RowCount() { - return rows; -} +template <> unsigned int MatrixOf::RowCount() { return rows; } -template <> -unsigned int MatrixOf::ColCount() { - return cols; -} \ No newline at end of file +template <> unsigned int MatrixOf::ColCount() { return cols; } \ No newline at end of file diff --git a/Matrix.h b/Matrix.h index 0648562..46f1d45 100644 --- a/Matrix.h +++ b/Matrix.h @@ -4,30 +4,30 @@ #include "Vector3.h" /// @brief Single precision float matrix -template -class MatrixOf { - public: - MatrixOf(unsigned int rows, unsigned int cols, T* data) +template class MatrixOf { +public: + MatrixOf(unsigned int rows, unsigned int cols, T *data) : rows(rows), cols(cols), data(data) {} - MatrixOf(Vector3 v); // creates a 3,1 matrix + // MatrixOf(Vector3 v); // creates a 3,1 matrix /// @brief Transpose with result in matrix m /// @param r The matrix in which the transposed matrix is stored - void Transpose(MatrixOf* r); + void Transpose(MatrixOf *r); - static void Multiply(MatrixOf* m1, MatrixOf* m2, MatrixOf* r); - void Multiply(MatrixOf* m, MatrixOf* r); + static void Multiply(const MatrixOf *m1, MatrixOf *m2, MatrixOf *r); + void Multiply(MatrixOf *m, MatrixOf *r); - static Vector3 Multiply(MatrixOf* m, Vector3 v); + static Vector3 Multiply(const MatrixOf *m, Vector3 v); + Vector3 operator*(const Vector3 v) const; T Get(unsigned int rowIx, unsigned int colIx); unsigned int RowCount(); unsigned int ColCount(); - private: +private: unsigned int rows; unsigned int cols; - T* data; + T *data; }; #endif \ No newline at end of file