ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

matrix.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef MATH_MATRIX_H
00024 #define MATH_MATRIX_H
00025 
00026 #include <string.h>
00027 #include <assert.h>
00028 
00029 #include "common/streamdebug.h"
00030 
00062 namespace Math {
00063 
00064 template<int rows, int cols> class Matrix;
00065 
00070 template<int rows, int cols>
00071 class MatrixBase {
00072 public:
00076     class Row {
00077     public:
00078         Row &operator=(const Row &r);
00079         Row &operator<<(float value);
00080 
00081     private:
00082         Row(MatrixBase<rows, cols> *m, int row);
00083 
00084         MatrixBase<rows, cols> *_matrix;
00085         int _row;
00086         int _col;
00087 
00088         friend class MatrixBase<rows, cols>;
00089     };
00090 
00094     bool isZero() const;
00095     Matrix<rows, cols> getNegative() const;
00096 
00109     Row getRow(int row);
00110 
00114     inline float *getData();
00118     inline const float *getData() const;
00122     void setData(const float *data);
00123     inline float getValue(int row, int col) const;
00124     inline void setValue(int row, int col, float value);
00125 
00126     inline float &operator()(int row, int col);
00127     inline float operator()(int row, int col) const;
00128 
00129     inline operator const Matrix<rows, cols>&() const { return getThis(); }
00130     inline operator Matrix<rows, cols>&() { return getThis(); }
00131 
00132     static Matrix<rows, cols> sum(const Matrix<rows, cols> &m1, const Matrix<rows, cols> &m2);
00133     static Matrix<rows, cols> difference(const Matrix<rows, cols> &m1, const Matrix<rows, cols> &m2);
00134     static Matrix<rows, cols> product(const Matrix<rows, cols> &m1, float factor);
00135     static Matrix<rows, cols> quotient(const Matrix<rows, cols> &m1, float factor);
00136 
00137     Matrix<rows, cols> &operator=(const Matrix<rows, cols> &m);
00138     Matrix<rows, cols> &operator+=(const Matrix<rows, cols> &m);
00139     Matrix<rows, cols> &operator-=(const Matrix<rows, cols> &m);
00140     Matrix<rows, cols> &operator*=(float factor);
00141     Matrix<rows, cols> &operator/=(float factor);
00142 
00143 protected:
00144     MatrixBase();
00145     MatrixBase(const float *data);
00146     MatrixBase(const MatrixBase<rows, cols> &m);
00147 
00148     inline const Matrix<rows, cols> &getThis() const {
00149         return *static_cast<const Matrix<rows, cols> *>(this); }
00150     inline Matrix<rows, cols> &getThis() {
00151         return *static_cast<Matrix<rows, cols> *>(this); }
00152 
00153 private:
00154     float _values[rows * cols];
00155 };
00156 
00161 template<int r, int c>
00162 class MatrixType : public MatrixBase<r, c> {
00163 protected:
00164     MatrixType() : MatrixBase<r, c>() { }
00165     MatrixType(const float *data) : MatrixBase<r, c>(data) { }
00166     MatrixType(const MatrixBase<r, c> &m) : MatrixBase<r, c>(m) { }
00167 };
00168 
00169 #define Vector(dim) Matrix<dim, 1>
00170 
00176 template<int r, int c>
00177 class Matrix : public MatrixType<r, c> {
00178 public:
00179     Matrix() : MatrixType<r, c>() { }
00180     Matrix(const float *data) : MatrixType<r, c>(data) { }
00181     Matrix(const MatrixBase<r, c> &m) : MatrixType<r, c>(m) { }
00182 };
00183 
00184 
00185 template <int m, int n, int p>
00186 Matrix<m, n> operator*(const Matrix<m, p> &m1, const Matrix<p, n> &m2);
00187 
00188 template <int r, int c>
00189 inline Matrix<r, c> operator+(const Matrix<r, c> &m1, const Matrix<r, c> &m2);
00190 
00191 template <int r, int c>
00192 inline Matrix<r, c> operator-(const Matrix<r, c> &m1, const Matrix<r, c> &m2);
00193 
00194 template <int r, int c>
00195 inline Matrix<r, c> operator*(const Matrix<r, c> &m1, float factor);
00196 
00197 template <int r, int c>
00198 inline Matrix<r, c> operator/(const Matrix<r, c> &m1, float factor);
00199 
00200 template <int r, int c>
00201 Matrix<r, c> operator*(float factor, const Matrix<r, c> &m1);
00202 
00203 template <int r, int c>
00204 Matrix<r, c> operator-(const Matrix<r, c> &m);
00205 
00206 template <int r, int c>
00207 bool operator==(const Matrix<r, c> &m1, const Matrix<r, c> &m2);
00208 
00209 template <int r, int c>
00210 bool operator!=(const Matrix<r, c> &m1, const Matrix<r, c> &m2);
00211 
00212 
00213 // Constructors
00214 template<int rows, int cols>
00215 MatrixBase<rows, cols>::MatrixBase() {
00216     for (int i = 0; i < rows * cols; ++i) {
00217         _values[i] = 0.f;
00218     }
00219 }
00220 
00221 template<int rows, int cols>
00222 MatrixBase<rows, cols>::MatrixBase(const float *data) {
00223     setData(data);
00224 }
00225 
00226 template<int rows, int cols>
00227 MatrixBase<rows, cols>::MatrixBase(const MatrixBase<rows, cols> &m) {
00228     setData(m._values);
00229 }
00230 
00231 
00232 
00233 
00234 // Data management
00235 template<int rows, int cols>
00236 float *MatrixBase<rows, cols>::getData() {
00237     return _values;
00238 }
00239 
00240 template<int rows, int cols>
00241 const float *MatrixBase<rows, cols>::getData() const {
00242     return _values;
00243 }
00244 
00245 template<int rows, int cols>
00246 void MatrixBase<rows, cols>::setData(const float *data) {
00247     ::memcpy(_values, data, rows * cols * sizeof(float));
00248 }
00249 
00250 template<int rows, int cols>
00251 float MatrixBase<rows, cols>::getValue(int row, int col) const {
00252     assert(rows > row && cols > col && row >= 0 && col >= 0);
00253     return _values[row * cols + col];
00254 }
00255 
00256 template<int rows, int cols>
00257 void MatrixBase<rows, cols>::setValue(int row, int col, float v) {
00258     operator()(row, col) = v;
00259 }
00260 
00261 
00262 
00263 // Operations helpers
00264 template<int rows, int cols>
00265 bool MatrixBase<rows, cols>::isZero() const {
00266     for (int i = 0; i < rows * cols; ++i) {
00267         if (_values[i] != 0.f) {
00268             return false;
00269         }
00270     }
00271     return true;
00272 }
00273 
00274 template <int r, int c>
00275 Matrix<r, c> MatrixBase<r, c>::getNegative() const {
00276     Matrix<r, c> result;
00277     for (int i = 0; i < r * c; ++i) {
00278         result._values[i] = -_values[i];
00279     }
00280     return result;
00281 }
00282 
00283 template <int r, int c>
00284 Matrix<r, c> MatrixBase<r, c>::sum(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00285     Matrix<r, c> result;
00286     for (int i = 0; i < r * c; ++i) {
00287         result._values[i] = m1._values[i] + m2._values[i];
00288     }
00289     return result;
00290 }
00291 
00292 template <int r, int c>
00293 Matrix<r, c> MatrixBase<r, c>::difference(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00294     Matrix<r, c> result;
00295     for (int i = 0; i < r * c; ++i) {
00296         result._values[i] = m1._values[i] - m2._values[i];
00297     }
00298     return result;
00299 }
00300 
00301 template <int r, int c>
00302 Matrix<r, c> MatrixBase<r, c>::product(const Matrix<r, c> &m1, float factor) {
00303     Matrix<r, c> result;
00304     for (int i = 0; i < r * c; ++i) {
00305         result._values[i] = m1._values[i] * factor;
00306     }
00307     return result;
00308 }
00309 
00310 template <int r, int c>
00311 Matrix<r, c> MatrixBase<r, c>::quotient(const Matrix<r, c> &m1, float factor) {
00312     Matrix<r, c> result;
00313     for (int i = 0; i < r * c; ++i) {
00314         result._values[i] = m1._values[i] / factor;
00315     }
00316     return result;
00317 }
00318 
00319 
00320 
00321 
00322 // Member operators
00323 template<int rows, int cols>
00324 float &MatrixBase<rows, cols>::operator()(int row, int col) {
00325     assert(rows > row && cols > col && row >= 0 && col >= 0);
00326     return _values[row * cols + col];
00327 }
00328 
00329 template<int rows, int cols>
00330 float MatrixBase<rows, cols>::operator()(int row, int col) const {
00331     return getValue(row, col);
00332 }
00333 
00334 template<int rows, int cols>
00335 Matrix<rows, cols> &MatrixBase<rows, cols>::operator=(const Matrix<rows, cols> &m) {
00336     setData(m._values);
00337 
00338     return getThis();
00339 }
00340 
00341 template<int rows, int cols>
00342 Matrix<rows, cols> &MatrixBase<rows, cols>::operator+=(const Matrix<rows, cols> &m) {
00343     for (int i = 0; i < rows * cols; ++i) {
00344         _values[i] += m._values[i];
00345     }
00346 
00347     return getThis();
00348 }
00349 
00350 template<int rows, int cols>
00351 Matrix<rows, cols> &MatrixBase<rows, cols>::operator-=(const Matrix<rows, cols> &m) {
00352     for (int i = 0; i < rows * cols; ++i) {
00353         _values[i] -= m._values[i];
00354     }
00355 
00356     return getThis();
00357 }
00358 
00359 template<int rows, int cols>
00360 Matrix<rows, cols> &MatrixBase<rows, cols>::operator*=(float factor) {
00361     for (int i = 0; i < rows * cols; ++i) {
00362         _values[i] *= factor;
00363     }
00364 
00365     return getThis();
00366 }
00367 
00368 template<int rows, int cols>
00369 Matrix<rows, cols> &MatrixBase<rows, cols>::operator/=(float factor) {
00370     for (int i = 0; i < rows * cols; ++i) {
00371         _values[i] /= factor;
00372     }
00373 
00374     return getThis();
00375 }
00376 
00377 
00378 
00379 // Row
00380 template<int rows, int cols>
00381 typename MatrixBase<rows, cols>::Row MatrixBase<rows, cols>::getRow(int row) {
00382     return Row(this, row);
00383 }
00384 
00385 template<int rows, int cols>
00386 MatrixBase<rows, cols>::Row::Row(MatrixBase<rows, cols> *m, int row) :
00387     _matrix(m), _row(row), _col(0) {
00388 
00389 }
00390 
00391 template<int rows, int cols>
00392 typename MatrixBase<rows, cols>::Row &MatrixBase<rows, cols>::Row::operator=(const Row &r) {
00393     _col = r._col;
00394     _row = r._row;
00395     _matrix = r._matrix;
00396 
00397     return *this;
00398 }
00399 
00400 template<int rows, int cols>
00401 typename MatrixBase<rows, cols>::Row &MatrixBase<rows, cols>::Row::operator<<(float value) {
00402     assert(_col < cols);
00403     _matrix->setValue(_row, _col++, value);
00404     return *this;
00405 }
00406 
00407 
00408 
00409 // Global operators
00410 template <int m, int n, int p>
00411 Matrix<m, n> operator*(const Matrix<m, p> &m1, const Matrix<p, n> &m2) {
00412     Matrix<m, n> result;
00413     for (int row = 0; row < m; ++row) {
00414         for (int col = 0; col < n; ++col) {
00415             float sum(0.0f);
00416             for (int j = 0; j < p; ++j)
00417                 sum += m1(row, j) * m2(j, col);
00418             result(row, col) = sum;
00419         }
00420     }
00421     return result;
00422 }
00423 
00424 template <int r, int c>
00425 inline Matrix<r, c> operator+(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00426     return Matrix<r, c>::sum(m1, m2);
00427 }
00428 
00429 template <int r, int c>
00430 inline Matrix<r, c> operator-(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00431     return Matrix<r, c>::difference(m1, m2);
00432 }
00433 
00434 template <int r, int c>
00435 inline Matrix<r, c> operator*(const Matrix<r, c> &m1, float factor) {
00436     return Matrix<r, c>::product(m1, factor);
00437 }
00438 
00439 template <int r, int c>
00440 inline Matrix<r, c> operator/(const Matrix<r, c> &m1, float factor) {
00441     return Matrix<r, c>::quotient(m1, factor);
00442 }
00443 
00444 template <int r, int c>
00445 Matrix<r, c> operator*(float factor, const Matrix<r, c> &m1) {
00446     return Matrix<r, c>::product(m1, factor);
00447 }
00448 
00449 template <int r, int c>
00450 Matrix<r, c> operator-(const Matrix<r, c> &m) {
00451     return m.getNegative();
00452 }
00453 
00454 template <int r, int c>
00455 bool operator==(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00456     for (int row = 0; row < r; ++row) {
00457         for (int col = 0; col < c; ++col) {
00458             if (m1(row, col) != m2(row, col)) {
00459                 return false;
00460             }
00461         }
00462     }
00463     return true;
00464 }
00465 
00466 template <int r, int c>
00467 bool operator!=(const Matrix<r, c> &m1, const Matrix<r, c> &m2) {
00468     return !(m1 == m2);
00469 }
00470 
00471 template<int r, int c>
00472 Common::Debug &operator<<(Common::Debug dbg, const Math::Matrix<r, c> &m) {
00473     dbg.nospace() << "Matrix<" << r << ", " << c << ">(";
00474     for (int col = 0; col < c; ++col) {
00475         dbg << m(0, col) << ", ";
00476     }
00477     for (int row = 1; row < r; ++row) {
00478         dbg << "\n            ";
00479         for (int col = 0; col < c; ++col) {
00480             dbg << m(row, col) << ", ";
00481         }
00482     }
00483     dbg << ')';
00484 
00485     return dbg.space();
00486 }
00487 
00488 }
00489 
00490 #endif
00491 


Generated on Sat Mar 16 2019 05:01:45 for ResidualVM by doxygen 1.7.1
curved edge   curved edge