#include "Matrix.h" template <> MatrixOf::MatrixOf(unsigned int rows, unsigned int cols) { if (rows <= 0 || cols <= 0) { this->rows = 0; this->cols = 0; this->data = nullptr; return; } this->rows = rows; this->cols = cols; unsigned int matrixSize = this->cols * this->rows; this->data = new float[matrixSize]{0.0f}; } template <> MatrixOf::MatrixOf(Vector3 v) : MatrixOf(3, 1) { Set(0, 0, v.Right()); Set(1, 0, v.Up()); Set(2, 0, v.Forward()); } template <> void MatrixOf::Multiply(const MatrixOf *m1, const 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; r->data[rDataIx] = 0.0F; for (unsigned int kIx = 0; kIx < m2->rows; kIx++) { unsigned int dataIx1 = rowIx1 * m1->cols + kIx; unsigned int dataIx2 = kIx * m2->cols + colIx2; r->data[rDataIx] += m1->data[dataIx1] * m2->data[dataIx2]; } } } } template <> Vector3 MatrixOf::Multiply(const MatrixOf *m, Vector3 v) { 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]); return r; } template Vector3 MatrixOf::operator*(const Vector3 v) const { float *vData = new float[3]{v.Right(), v.Up(), v.Forward()}; 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; }