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

matrix4.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_MATRIX4_H
00024 #define MATH_MATRIX4_H
00025 
00026 #include "math/rotation3d.h"
00027 #include "math/squarematrix.h"
00028 #include "math/vector3d.h"
00029 #include "math/vector4d.h"
00030 #include "math/matrix3.h"
00031 
00032 namespace Math {
00033 
00034 // matrix 4 is a rotation matrix + position
00035 template<>
00036 class Matrix<4, 4> : public MatrixType<4, 4>, public Rotation3D<Matrix<4, 4> > {
00037 public:
00038     Matrix();
00039     Matrix(const MatrixBase<4, 4> &m);
00040     Matrix(const Angle &first, const Angle &second, const Angle &third, EulerOrder order) { buildFromEuler(first, second, third, order); }
00041 
00042     void transform(Vector3d *v, bool translate) const;
00043     void inverseTranslate(Vector3d *v) const;
00044     void inverseRotate(Vector3d *v) const;
00045     
00046     Vector3d getPosition() const;
00047     void setPosition(const Vector3d &v);
00048     
00049     Matrix3 getRotation() const;
00050     void setRotation(const Matrix3 &m);
00051 
00052     void translate(const Vector3d &v);
00053 
00066     void buildFromTargetDir(const Math::Vector3d &modelForward, const Math::Vector3d &targetDirection,
00067                            const Math::Vector3d &modelUp, const Math::Vector3d &worldUp);
00068     
00076     void invertAffineOrthonormal();
00077 
00078     void transpose();
00079 
00080     inline Matrix<4, 4> operator*(const Matrix<4, 4> &m2) const {
00081         Matrix<4, 4> result;
00082         const float *d1 = getData();
00083         const float *d2 = m2.getData();
00084         float *r = result.getData();
00085 
00086         for (int i = 0; i < 16; i += 4) {
00087             for (int j = 0; j < 4; ++j) {
00088                 r[i + j] = (d1[i + 0] * d2[j + 0])
00089                     + (d1[i + 1] * d2[j + 4])
00090                     + (d1[i + 2] * d2[j + 8])
00091                     + (d1[i + 3] * d2[j + 12]);
00092             }
00093         }
00094 
00095         return result;
00096     }
00097 
00098     inline Vector4d transform(const Vector4d &v) const {
00099         Vector4d result;
00100         const float *d1 = getData();
00101         const float *d2 = v.getData();
00102         float *r = result.getData();
00103 
00104         for (int i = 0; i < 4; i++) {
00105             r[i] = d2[0] * d1[0 * 4 + i] +
00106                    d2[1] * d1[1 * 4 + i] +
00107                    d2[2] * d1[2 * 4 + i] +
00108                    d2[3] * d1[3 * 4 + i];
00109         }
00110 
00111         return result;
00112     }
00113 
00114     inline bool inverse() {
00115         Matrix<4, 4> invMatrix;
00116         float *inv = invMatrix.getData();
00117         float *m = getData();
00118 
00119         inv[0] = m[5]  * m[10] * m[15] - 
00120             m[5]  * m[11] * m[14] - 
00121             m[9]  * m[6]  * m[15] + 
00122             m[9]  * m[7]  * m[14] +
00123             m[13] * m[6]  * m[11] - 
00124             m[13] * m[7]  * m[10];
00125 
00126         inv[4] = -m[4]  * m[10] * m[15] + 
00127             m[4]  * m[11] * m[14] + 
00128             m[8]  * m[6]  * m[15] - 
00129             m[8]  * m[7]  * m[14] - 
00130             m[12] * m[6]  * m[11] + 
00131             m[12] * m[7]  * m[10];
00132 
00133         inv[8] = m[4]  * m[9] * m[15] - 
00134             m[4]  * m[11] * m[13] - 
00135             m[8]  * m[5] * m[15] + 
00136             m[8]  * m[7] * m[13] + 
00137             m[12] * m[5] * m[11] - 
00138             m[12] * m[7] * m[9];
00139 
00140         inv[12] = -m[4]  * m[9] * m[14] + 
00141             m[4]  * m[10] * m[13] +
00142             m[8]  * m[5] * m[14] - 
00143             m[8]  * m[6] * m[13] - 
00144             m[12] * m[5] * m[10] + 
00145             m[12] * m[6] * m[9];
00146 
00147         inv[1] = -m[1]  * m[10] * m[15] + 
00148             m[1]  * m[11] * m[14] + 
00149             m[9]  * m[2] * m[15] - 
00150             m[9]  * m[3] * m[14] - 
00151             m[13] * m[2] * m[11] + 
00152             m[13] * m[3] * m[10];
00153 
00154         inv[5] = m[0]  * m[10] * m[15] - 
00155             m[0]  * m[11] * m[14] - 
00156             m[8]  * m[2] * m[15] + 
00157             m[8]  * m[3] * m[14] + 
00158             m[12] * m[2] * m[11] - 
00159             m[12] * m[3] * m[10];
00160 
00161         inv[9] = -m[0]  * m[9] * m[15] + 
00162             m[0]  * m[11] * m[13] + 
00163             m[8]  * m[1] * m[15] - 
00164             m[8]  * m[3] * m[13] - 
00165             m[12] * m[1] * m[11] + 
00166             m[12] * m[3] * m[9];
00167 
00168         inv[13] = m[0]  * m[9] * m[14] - 
00169             m[0]  * m[10] * m[13] - 
00170             m[8]  * m[1] * m[14] + 
00171             m[8]  * m[2] * m[13] + 
00172             m[12] * m[1] * m[10] - 
00173             m[12] * m[2] * m[9];
00174 
00175         inv[2] = m[1]  * m[6] * m[15] - 
00176             m[1]  * m[7] * m[14] - 
00177             m[5]  * m[2] * m[15] + 
00178             m[5]  * m[3] * m[14] + 
00179             m[13] * m[2] * m[7] - 
00180             m[13] * m[3] * m[6];
00181 
00182         inv[6] = -m[0]  * m[6] * m[15] + 
00183             m[0]  * m[7] * m[14] + 
00184             m[4]  * m[2] * m[15] - 
00185             m[4]  * m[3] * m[14] - 
00186             m[12] * m[2] * m[7] + 
00187             m[12] * m[3] * m[6];
00188 
00189         inv[10] = m[0]  * m[5] * m[15] - 
00190             m[0]  * m[7] * m[13] - 
00191             m[4]  * m[1] * m[15] + 
00192             m[4]  * m[3] * m[13] + 
00193             m[12] * m[1] * m[7] - 
00194             m[12] * m[3] * m[5];
00195 
00196         inv[14] = -m[0]  * m[5] * m[14] + 
00197             m[0]  * m[6] * m[13] + 
00198             m[4]  * m[1] * m[14] - 
00199             m[4]  * m[2] * m[13] - 
00200             m[12] * m[1] * m[6] + 
00201             m[12] * m[2] * m[5];
00202 
00203         inv[3] = -m[1] * m[6] * m[11] + 
00204             m[1] * m[7] * m[10] + 
00205             m[5] * m[2] * m[11] - 
00206             m[5] * m[3] * m[10] - 
00207             m[9] * m[2] * m[7] + 
00208             m[9] * m[3] * m[6];
00209 
00210         inv[7] = m[0] * m[6] * m[11] - 
00211             m[0] * m[7] * m[10] - 
00212             m[4] * m[2] * m[11] + 
00213             m[4] * m[3] * m[10] + 
00214             m[8] * m[2] * m[7] - 
00215             m[8] * m[3] * m[6];
00216 
00217         inv[11] = -m[0] * m[5] * m[11] + 
00218             m[0] * m[7] * m[9] + 
00219             m[4] * m[1] * m[11] - 
00220             m[4] * m[3] * m[9] - 
00221             m[8] * m[1] * m[7] + 
00222             m[8] * m[3] * m[5];
00223 
00224         inv[15] = m[0] * m[5] * m[10] - 
00225             m[0] * m[6] * m[9] - 
00226             m[4] * m[1] * m[10] + 
00227             m[4] * m[2] * m[9] + 
00228             m[8] * m[1] * m[6] - 
00229             m[8] * m[2] * m[5];
00230 
00231         float det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
00232 
00233         if (det == 0)
00234             return false;
00235 
00236         det = 1.0 / det;
00237 
00238         for (int i = 0; i < 16; i++) {
00239             m[i] = inv[i] * det;
00240         }
00241 
00242         return true;
00243     }
00244 };
00245 
00246 typedef Matrix<4, 4> Matrix4;
00247 
00248 } // end of namespace Math
00249 
00250 #endif


Generated on Sat Jul 13 2019 05:01:21 for ResidualVM by doxygen 1.7.1
curved edge   curved edge