diff --git a/Matrix.cpp b/Matrix.cpp index 89af44d..7115dbe 100644 --- a/Matrix.cpp +++ b/Matrix.cpp @@ -1,24 +1,17 @@ #include "Matrix.h" -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 - // We cannot check the data size directly, but the row*col should be equal +template <> MatrixOf::MatrixOf(unsigned int rows, unsigned int cols) { + this->rows = rows; + this->cols = cols; + unsigned int matrixSize = this->cols * this->rows; - if (matrixSize != r->rows * r->cols) - // Exception??? For now we don't do anything - return; + this->data = new float[matrixSize]{0.0f}; +} - 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]; - } +template <> MatrixOf::MatrixOf(Vector3 v) : MatrixOf(3, 1) { + Set(0, 0, v.x); + Set(1, 0, v.y); + Set(2, 0, v.z); } template <> @@ -37,23 +30,14 @@ void MatrixOf::Multiply(const MatrixOf *m1, MatrixOf *m2, } } -template <> -void MatrixOf::Multiply(MatrixOf *m2, MatrixOf *r) { - Multiply(this, m2, r); -} - template <> 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]{}; - MatrixOf r_m = MatrixOf(3, 1, rData); + MatrixOf v_m = MatrixOf(v); + MatrixOf r_m = MatrixOf(3, 1); Multiply(m, &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; } @@ -70,12 +54,3 @@ template Vector3 MatrixOf::operator*(const Vector3 v) const { 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::ColCount() { return cols; } \ No newline at end of file diff --git a/Matrix.h b/Matrix.h index 46f1d45..adaa8e2 100644 --- a/Matrix.h +++ b/Matrix.h @@ -6,23 +6,83 @@ /// @brief Single precision float matrix 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(unsigned int rows, unsigned int cols); + MatrixOf(unsigned int rows, unsigned int cols, T *source) + : 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 /// @param r The matrix in which the transposed matrix is stored - void Transpose(MatrixOf *r); + void Transpose(MatrixOf *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 *m1, MatrixOf *m2, MatrixOf *r); - void Multiply(MatrixOf *m, MatrixOf *r); + void Multiply(MatrixOf *m, MatrixOf *r) { Multiply(this, m, r); } 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(); + T Get(unsigned int rowIx, unsigned int colIx) { + unsigned int dataIx = rowIx * this->cols + colIx; + 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: unsigned int rows;