/* ScummVM - Graphic Adventure Engine * * ScummVM is the legal property of its developers, whose names * are too numerous to list here. Please refer to the COPYRIGHT * file distributed with this source distribution. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #ifndef MATH_MATRIX_H #define MATH_MATRIX_H #include #include #include "common/streamdebug.h" /** * \namespace Math * This namespace contains some useful classes dealing with math and geometry. * * The most important classes are Matrix and its base classes. * MatrixBase is a template class which is the base of all the matrices with * many convenient functions. * MatrixType is an intermediate class that, using template specialization, * is able to create different kinds of matrices, like vectors or * square matrices. * Matrix is the actual matrix class and it is derived from MatrixType. * * MatrixBase and MatrixType have their constructors protected, so they can't * be instantiated. But while MatrixBase is just a backend class, MatrixType * can be used to create new kinds of matrices: * \code template class MatrixType<1, dim> : public MatrixBase<1, dim> { ... }; * \endcode * Given that declaration, every Matrix<1, dim>, with "dim" whatever positive * number, will have the methods and members defined in MatrixType<1, dim>. * * This design allows us to have the equality of, say, the class "three-dimensional * vector" and Matrix<3, 1>. Vector3d is not a Matrix<3, 1>, it is Matrix<3, 1>. * Every method in MatrixBase and MatrixType returning a matrix returns a Matrix<\r, c>, * and not a MatrixBase<\r, c>. This reduces code duplication, since otherwise many * functions declared for Matrix would need to be declared for MatrixBase too, * like many operators. */ namespace Math { template class Matrix; /** * \class MatrixBase * The base class for all the matrices. */ template class MatrixBase { public: /** * Convenient class for feeding a matrix. */ class Row { public: Row &operator<<(float value); private: Row(MatrixBase *m, int row); MatrixBase *_matrix; int _row; int _col; friend class MatrixBase; }; /** * Returns true if this matrix's values are all 0. */ bool isZero() const; Matrix getNegative() const; /** * Returns an instance of Row for a particular row of this matrix. * Row is a convenient class for feeding a matrix. * \code Matrix<3, 3> m; m.getRow(0) << 0 << 0 << 0; m.getRow(1) << 1 << 2 << 0; m.getRow(2) << 0 << 0.5 << 1; * \endcode * * \param row The row to be feeded. */ Row getRow(int row); /** * Returns a pointer to the internal data of this matrix. */ inline float *getData(); /** * Returns a pointer to the internal data of this matrix. */ inline const float *getData() const; /** * Sets the internal data of this matrix. */ void setData(const float *data); inline float getValue(int row, int col) const; inline void setValue(int row, int col, float value); inline float &operator()(int row, int col); inline float operator()(int row, int col) const; inline operator const Matrix&() const { return getThis(); } inline operator Matrix&() { return getThis(); } static Matrix sum(const Matrix &m1, const Matrix &m2); static Matrix difference(const Matrix &m1, const Matrix &m2); static Matrix product(const Matrix &m1, float factor); static Matrix product(const Matrix &m1, const Matrix &m2); static Matrix quotient(const Matrix &m1, float factor); static Matrix quotient(const Matrix &m1, const Matrix &m2); Matrix &operator=(const Matrix &m); Matrix &operator+=(const Matrix &m); Matrix &operator-=(const Matrix &m); Matrix &operator*=(float factor); Matrix &operator*=(const Matrix &m); Matrix &operator/=(float factor); Matrix &operator/=(const Matrix &m); protected: MatrixBase(); MatrixBase(const float *data); MatrixBase(const MatrixBase &m); MatrixBase &operator=(const MatrixBase &m); inline const Matrix &getThis() const { return *static_cast *>(this); } inline Matrix &getThis() { return *static_cast *>(this); } private: float _values[rows * cols]; }; /** * \class MatrixType * MatrixType is a class used to create different kinds of matrices. */ template class MatrixType : public MatrixBase { protected: MatrixType() : MatrixBase() { } MatrixType(const float *data) : MatrixBase(data) { } MatrixType(const MatrixBase &m) : MatrixBase(m) { } }; #define Vector(dim) Matrix /** * \class Matrix The actual Matrix class. * This template class must be instantiated passing it the number of the rows * and the number of the columns. */ template class Matrix : public MatrixType { public: Matrix() : MatrixType() { } Matrix(const float *data) : MatrixType(data) { } Matrix(const MatrixBase &m) : MatrixType(m) { } }; template Matrix operator*(const Matrix &m1, const Matrix &m2); template inline Matrix operator+(const Matrix &m1, const Matrix &m2); template inline Matrix operator-(const Matrix &m1, const Matrix &m2); template inline Matrix operator*(const Matrix &m1, float factor); template inline Matrix operator/(const Matrix &m1, float factor); template Matrix operator*(float factor, const Matrix &m1); template Matrix operator-(const Matrix &m); template bool operator==(const Matrix &m1, const Matrix &m2); template bool operator!=(const Matrix &m1, const Matrix &m2); // Constructors template MatrixBase::MatrixBase() { for (int i = 0; i < rows * cols; ++i) { _values[i] = 0.f; } } template MatrixBase::MatrixBase(const float *data) { setData(data); } template MatrixBase::MatrixBase(const MatrixBase &m) { setData(m._values); } template MatrixBase &MatrixBase::operator=(const MatrixBase &m) { setData(m._values); return *this; } // Data management template float *MatrixBase::getData() { return _values; } template const float *MatrixBase::getData() const { return _values; } template void MatrixBase::setData(const float *data) { ::memcpy(_values, data, rows * cols * sizeof(float)); } template float MatrixBase::getValue(int row, int col) const { assert(rows > row && cols > col && row >= 0 && col >= 0); return _values[row * cols + col]; } template void MatrixBase::setValue(int row, int col, float v) { operator()(row, col) = v; } // Operations helpers template bool MatrixBase::isZero() const { for (int i = 0; i < rows * cols; ++i) { if (_values[i] != 0.f) { return false; } } return true; } template Matrix MatrixBase::getNegative() const { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = -_values[i]; } return result; } template Matrix MatrixBase::sum(const Matrix &m1, const Matrix &m2) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] + m2._values[i]; } return result; } template Matrix MatrixBase::difference(const Matrix &m1, const Matrix &m2) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] - m2._values[i]; } return result; } template Matrix MatrixBase::product(const Matrix &m1, float factor) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] * factor; } return result; } template Matrix MatrixBase::product(const Matrix &m1, const Matrix &m2) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] * m2._values[i]; } return result; } template Matrix MatrixBase::quotient(const Matrix &m1, float factor) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] / factor; } return result; } template Matrix MatrixBase::quotient(const Matrix &m1, const Matrix &m2) { Matrix result; for (int i = 0; i < r * c; ++i) { result._values[i] = m1._values[i] / m2._values[i]; } return result; } // Member operators template float &MatrixBase::operator()(int row, int col) { assert(rows > row && cols > col && row >= 0 && col >= 0); return _values[row * cols + col]; } template float MatrixBase::operator()(int row, int col) const { return getValue(row, col); } template Matrix &MatrixBase::operator=(const Matrix &m) { setData(m._values); return getThis(); } template Matrix &MatrixBase::operator+=(const Matrix &m) { for (int i = 0; i < rows * cols; ++i) { _values[i] += m._values[i]; } return getThis(); } template Matrix &MatrixBase::operator-=(const Matrix &m) { for (int i = 0; i < rows * cols; ++i) { _values[i] -= m._values[i]; } return getThis(); } template Matrix &MatrixBase::operator*=(float factor) { for (int i = 0; i < rows * cols; ++i) { _values[i] *= factor; } return getThis(); } template Matrix &MatrixBase::operator/=(float factor) { for (int i = 0; i < rows * cols; ++i) { _values[i] /= factor; } return getThis(); } // Row template typename MatrixBase::Row MatrixBase::getRow(int row) { return Row(this, row); } template MatrixBase::Row::Row(MatrixBase *m, int row) : _matrix(m), _row(row), _col(0) { } template typename MatrixBase::Row &MatrixBase::Row::operator<<(float value) { assert(_col < cols); _matrix->setValue(_row, _col++, value); return *this; } // Global operators template Matrix operator*(const Matrix &m1, const Matrix &m2) { Matrix result; for (int row = 0; row < m; ++row) { for (int col = 0; col < n; ++col) { float sum(0.0f); for (int j = 0; j < p; ++j) sum += m1(row, j) * m2(j, col); result(row, col) = sum; } } return result; } template inline Matrix operator+(const Matrix &m1, const Matrix &m2) { return Matrix::sum(m1, m2); } template inline Matrix operator-(const Matrix &m1, const Matrix &m2) { return Matrix::difference(m1, m2); } template inline Matrix operator*(const Matrix &m1, const Matrix &m2) { return Matrix::product(m1, m2); } template inline Matrix operator*(const Matrix &m1, float factor) { return Matrix::product(m1, factor); } template inline Matrix operator/(const Matrix &m1, const Matrix &m2) { return Matrix::quotient(m1, m2); } template inline Matrix operator/(const Matrix &m1, float factor) { return Matrix::quotient(m1, factor); } template Matrix operator*(float factor, const Matrix &m1) { return Matrix::product(m1, factor); } template Matrix operator-(const Matrix &m) { return m.getNegative(); } template bool operator==(const Matrix &m1, const Matrix &m2) { for (int row = 0; row < r; ++row) { for (int col = 0; col < c; ++col) { if (m1(row, col) != m2(row, col)) { return false; } } } return true; } template bool operator!=(const Matrix &m1, const Matrix &m2) { return !(m1 == m2); } template Common::StreamDebug &operator<<(Common::StreamDebug dbg, const Math::Matrix &m) { dbg.nospace() << "Matrix<" << r << ", " << c << ">("; for (int col = 0; col < c; ++col) { dbg << m(0, col) << ", "; } for (int row = 1; row < r; ++row) { dbg << "\n "; for (int col = 0; col < c; ++col) { dbg << m(row, col) << ", "; } } dbg << ')'; return dbg.space(); } } #endif