#ifndef MATRIX_H #define MATRIX_H #include "Vector3.h" /// @brief Single precision float matrix template class MatrixOf { public: 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) 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, const MatrixOf *m2, MatrixOf *r); void Multiply(const MatrixOf *m, MatrixOf *r) const { 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 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; unsigned int cols; T *data; }; #endif