00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #ifndef __Matrix3_H__ 00026 #define __Matrix3_H__ 00027 00028 #include "OgrePrerequisites.h" 00029 00030 #include "OgreVector3.h" 00031 00032 // NB All code adapted from Wild Magic 0.2 Matrix math (free source code) 00033 // http://www.magic-software.com 00034 00035 // NOTE. The (x,y,z) coordinate system is assumed to be right-handed. 00036 // Coordinate axis rotation matrices are of the form 00037 // RX = 1 0 0 00038 // 0 cos(t) -sin(t) 00039 // 0 sin(t) cos(t) 00040 // where t > 0 indicates a counterclockwise rotation in the yz-plane 00041 // RY = cos(t) 0 sin(t) 00042 // 0 1 0 00043 // -sin(t) 0 cos(t) 00044 // where t > 0 indicates a counterclockwise rotation in the zx-plane 00045 // RZ = cos(t) -sin(t) 0 00046 // sin(t) cos(t) 0 00047 // 0 0 1 00048 // where t > 0 indicates a counterclockwise rotation in the xy-plane. 00049 00050 namespace Ogre 00051 { 00059 class _OgreExport Matrix3 00060 { 00061 public: 00066 inline Matrix3 () {}; 00067 inline Matrix3 (const Real arr[3][3]) 00068 { 00069 memcpy(m,arr,9*sizeof(Real)); 00070 } 00071 inline Matrix3 (const Matrix3& rkMatrix) 00072 { 00073 memcpy(m,rkMatrix.m,9*sizeof(Real)); 00074 } 00075 Matrix3 (Real fEntry00, Real fEntry01, Real fEntry02, 00076 Real fEntry10, Real fEntry11, Real fEntry12, 00077 Real fEntry20, Real fEntry21, Real fEntry22) 00078 { 00079 m[0][0] = fEntry00; 00080 m[0][1] = fEntry01; 00081 m[0][2] = fEntry02; 00082 m[1][0] = fEntry10; 00083 m[1][1] = fEntry11; 00084 m[1][2] = fEntry12; 00085 m[2][0] = fEntry20; 00086 m[2][1] = fEntry21; 00087 m[2][2] = fEntry22; 00088 } 00089 00090 // member access, allows use of construct mat[r][c] 00091 inline Real* operator[] (int iRow) const 00092 { 00093 return (Real*)m[iRow]; 00094 } 00095 inline operator Real* () 00096 { 00097 return (Real*)m[0]; 00098 } 00099 Vector3 GetColumn (int iCol) const; 00100 void SetColumn(int iCol, const Vector3& vec); 00101 void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis); 00102 00103 // assignment and comparison 00104 inline Matrix3& operator= (const Matrix3& rkMatrix) 00105 { 00106 memcpy(m,rkMatrix.m,9*sizeof(Real)); 00107 return *this; 00108 } 00109 bool operator== (const Matrix3& rkMatrix) const; 00110 inline bool operator!= (const Matrix3& rkMatrix) const 00111 { 00112 return !operator==(rkMatrix); 00113 } 00114 00115 // arithmetic operations 00116 Matrix3 operator+ (const Matrix3& rkMatrix) const; 00117 Matrix3 operator- (const Matrix3& rkMatrix) const; 00118 Matrix3 operator* (const Matrix3& rkMatrix) const; 00119 Matrix3 operator- () const; 00120 00121 // matrix * vector [3x3 * 3x1 = 3x1] 00122 Vector3 operator* (const Vector3& rkVector) const; 00123 00124 // vector * matrix [1x3 * 3x3 = 1x3] 00125 friend Vector3 operator* (const Vector3& rkVector, 00126 const Matrix3& rkMatrix); 00127 00128 // matrix * scalar 00129 Matrix3 operator* (Real fScalar) const; 00130 00131 // scalar * matrix 00132 friend Matrix3 operator* (Real fScalar, const Matrix3& rkMatrix); 00133 00134 // utilities 00135 Matrix3 Transpose () const; 00136 bool Inverse (Matrix3& rkInverse, Real fTolerance = 1e-06) const; 00137 Matrix3 Inverse (Real fTolerance = 1e-06) const; 00138 Real Determinant () const; 00139 00140 // singular value decomposition 00141 void SingularValueDecomposition (Matrix3& rkL, Vector3& rkS, 00142 Matrix3& rkR) const; 00143 void SingularValueComposition (const Matrix3& rkL, 00144 const Vector3& rkS, const Matrix3& rkR); 00145 00146 // Gram-Schmidt orthonormalization (applied to columns of rotation matrix) 00147 void Orthonormalize (); 00148 00149 // orthogonal Q, diagonal D, upper triangular U stored as (u01,u02,u12) 00150 void QDUDecomposition (Matrix3& rkQ, Vector3& rkD, 00151 Vector3& rkU) const; 00152 00153 Real SpectralNorm () const; 00154 00155 // matrix must be orthonormal 00156 void ToAxisAngle (Vector3& rkAxis, Real& rfRadians) const; 00157 void FromAxisAngle (const Vector3& rkAxis, Real fRadians); 00158 00159 // The matrix must be orthonormal. The decomposition is yaw*pitch*roll 00160 // where yaw is rotation about the Up vector, pitch is rotation about the 00161 // Right axis, and roll is rotation about the Direction axis. 00162 bool ToEulerAnglesXYZ (float& rfYAngle, float& rfPAngle, 00163 float& rfRAngle) const; 00164 bool ToEulerAnglesXZY (float& rfYAngle, float& rfPAngle, 00165 float& rfRAngle) const; 00166 bool ToEulerAnglesYXZ (float& rfYAngle, float& rfPAngle, 00167 float& rfRAngle) const; 00168 bool ToEulerAnglesYZX (float& rfYAngle, float& rfPAngle, 00169 float& rfRAngle) const; 00170 bool ToEulerAnglesZXY (float& rfYAngle, float& rfPAngle, 00171 float& rfRAngle) const; 00172 bool ToEulerAnglesZYX (float& rfYAngle, float& rfPAngle, 00173 float& rfRAngle) const; 00174 void FromEulerAnglesXYZ (float fYAngle, float fPAngle, float fRAngle); 00175 void FromEulerAnglesXZY (float fYAngle, float fPAngle, float fRAngle); 00176 void FromEulerAnglesYXZ (float fYAngle, float fPAngle, float fRAngle); 00177 void FromEulerAnglesYZX (float fYAngle, float fPAngle, float fRAngle); 00178 void FromEulerAnglesZXY (float fYAngle, float fPAngle, float fRAngle); 00179 void FromEulerAnglesZYX (float fYAngle, float fPAngle, float fRAngle); 00180 00181 // eigensolver, matrix must be symmetric 00182 void EigenSolveSymmetric (Real afEigenvalue[3], 00183 Vector3 akEigenvector[3]) const; 00184 00185 static void TensorProduct (const Vector3& rkU, const Vector3& rkV, 00186 Matrix3& rkProduct); 00187 00188 static const Real EPSILON; 00189 static const Matrix3 ZERO; 00190 static const Matrix3 IDENTITY; 00191 00192 protected: 00193 // support for eigensolver 00194 void Tridiagonal (Real afDiag[3], Real afSubDiag[3]); 00195 bool QLAlgorithm (Real afDiag[3], Real afSubDiag[3]); 00196 00197 // support for singular value decomposition 00198 static const Real ms_fSvdEpsilon; 00199 static const int ms_iSvdMaxIterations; 00200 static void Bidiagonalize (Matrix3& kA, Matrix3& kL, 00201 Matrix3& kR); 00202 static void GolubKahanStep (Matrix3& kA, Matrix3& kL, 00203 Matrix3& kR); 00204 00205 // support for spectral norm 00206 static Real MaxCubicRoot (Real afCoeff[3]); 00207 00208 Real m[3][3]; 00209 00210 // for faster access 00211 friend class Matrix4; 00212 }; 00213 } 00214 #endif
Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:17 2004