1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __Matrix3_H__ |
---|
29 | #define __Matrix3_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | |
---|
33 | #include "OgreVector3.h" |
---|
34 | |
---|
35 | // NB All code adapted from Wild Magic 0.2 Matrix math (free source code) |
---|
36 | // http://www.geometrictools.com/ |
---|
37 | |
---|
38 | // NOTE. The (x,y,z) coordinate system is assumed to be right-handed. |
---|
39 | // Coordinate axis rotation matrices are of the form |
---|
40 | // RX = 1 0 0 |
---|
41 | // 0 cos(t) -sin(t) |
---|
42 | // 0 sin(t) cos(t) |
---|
43 | // where t > 0 indicates a counterclockwise rotation in the yz-plane |
---|
44 | // RY = cos(t) 0 sin(t) |
---|
45 | // 0 1 0 |
---|
46 | // -sin(t) 0 cos(t) |
---|
47 | // where t > 0 indicates a counterclockwise rotation in the zx-plane |
---|
48 | // RZ = cos(t) -sin(t) 0 |
---|
49 | // sin(t) cos(t) 0 |
---|
50 | // 0 0 1 |
---|
51 | // where t > 0 indicates a counterclockwise rotation in the xy-plane. |
---|
52 | |
---|
53 | namespace Ogre |
---|
54 | { |
---|
55 | /** \addtogroup Core |
---|
56 | * @{ |
---|
57 | */ |
---|
58 | /** \addtogroup Math |
---|
59 | * @{ |
---|
60 | */ |
---|
61 | /** A 3x3 matrix which can represent rotations around axes. |
---|
62 | @note |
---|
63 | <b>All the code is adapted from the Wild Magic 0.2 Matrix |
---|
64 | library (http://www.geometrictools.com/).</b> |
---|
65 | @par |
---|
66 | The coordinate system is assumed to be <b>right-handed</b>. |
---|
67 | */ |
---|
68 | class _OgreExport Matrix3 |
---|
69 | { |
---|
70 | public: |
---|
71 | /** Default constructor. |
---|
72 | @note |
---|
73 | It does <b>NOT</b> initialize the matrix for efficiency. |
---|
74 | */ |
---|
75 | inline Matrix3 () {} |
---|
76 | inline explicit Matrix3 (const Real arr[3][3]) |
---|
77 | { |
---|
78 | memcpy(m,arr,9*sizeof(Real)); |
---|
79 | } |
---|
80 | inline Matrix3 (const Matrix3& rkMatrix) |
---|
81 | { |
---|
82 | memcpy(m,rkMatrix.m,9*sizeof(Real)); |
---|
83 | } |
---|
84 | Matrix3 (Real fEntry00, Real fEntry01, Real fEntry02, |
---|
85 | Real fEntry10, Real fEntry11, Real fEntry12, |
---|
86 | Real fEntry20, Real fEntry21, Real fEntry22) |
---|
87 | { |
---|
88 | m[0][0] = fEntry00; |
---|
89 | m[0][1] = fEntry01; |
---|
90 | m[0][2] = fEntry02; |
---|
91 | m[1][0] = fEntry10; |
---|
92 | m[1][1] = fEntry11; |
---|
93 | m[1][2] = fEntry12; |
---|
94 | m[2][0] = fEntry20; |
---|
95 | m[2][1] = fEntry21; |
---|
96 | m[2][2] = fEntry22; |
---|
97 | } |
---|
98 | |
---|
99 | /** Exchange the contents of this matrix with another. |
---|
100 | */ |
---|
101 | inline void swap(Matrix3& other) |
---|
102 | { |
---|
103 | std::swap(m[0][0], other.m[0][0]); |
---|
104 | std::swap(m[0][1], other.m[0][1]); |
---|
105 | std::swap(m[0][2], other.m[0][2]); |
---|
106 | std::swap(m[1][0], other.m[1][0]); |
---|
107 | std::swap(m[1][1], other.m[1][1]); |
---|
108 | std::swap(m[1][2], other.m[1][2]); |
---|
109 | std::swap(m[2][0], other.m[2][0]); |
---|
110 | std::swap(m[2][1], other.m[2][1]); |
---|
111 | std::swap(m[2][2], other.m[2][2]); |
---|
112 | } |
---|
113 | |
---|
114 | /// Member access, allows use of construct mat[r][c] |
---|
115 | inline const Real* operator[] (size_t iRow) const |
---|
116 | { |
---|
117 | return m[iRow]; |
---|
118 | } |
---|
119 | |
---|
120 | inline Real* operator[] (size_t iRow) |
---|
121 | { |
---|
122 | return m[iRow]; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | /*inline operator Real* () |
---|
128 | { |
---|
129 | return (Real*)m[0]; |
---|
130 | }*/ |
---|
131 | Vector3 GetColumn (size_t iCol) const; |
---|
132 | void SetColumn(size_t iCol, const Vector3& vec); |
---|
133 | void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis); |
---|
134 | |
---|
135 | /// Assignment and comparison |
---|
136 | inline Matrix3& operator= (const Matrix3& rkMatrix) |
---|
137 | { |
---|
138 | memcpy(m,rkMatrix.m,9*sizeof(Real)); |
---|
139 | return *this; |
---|
140 | } |
---|
141 | |
---|
142 | /** Tests 2 matrices for equality. |
---|
143 | */ |
---|
144 | bool operator== (const Matrix3& rkMatrix) const; |
---|
145 | |
---|
146 | /** Tests 2 matrices for inequality. |
---|
147 | */ |
---|
148 | inline bool operator!= (const Matrix3& rkMatrix) const |
---|
149 | { |
---|
150 | return !operator==(rkMatrix); |
---|
151 | } |
---|
152 | |
---|
153 | // arithmetic operations |
---|
154 | /** Matrix addition. |
---|
155 | */ |
---|
156 | Matrix3 operator+ (const Matrix3& rkMatrix) const; |
---|
157 | |
---|
158 | /** Matrix subtraction. |
---|
159 | */ |
---|
160 | Matrix3 operator- (const Matrix3& rkMatrix) const; |
---|
161 | |
---|
162 | /** Matrix concatenation using '*'. |
---|
163 | */ |
---|
164 | Matrix3 operator* (const Matrix3& rkMatrix) const; |
---|
165 | Matrix3 operator- () const; |
---|
166 | |
---|
167 | /// Matrix * vector [3x3 * 3x1 = 3x1] |
---|
168 | Vector3 operator* (const Vector3& rkVector) const; |
---|
169 | |
---|
170 | /// Vector * matrix [1x3 * 3x3 = 1x3] |
---|
171 | _OgreExport friend Vector3 operator* (const Vector3& rkVector, |
---|
172 | const Matrix3& rkMatrix); |
---|
173 | |
---|
174 | /// Matrix * scalar |
---|
175 | Matrix3 operator* (Real fScalar) const; |
---|
176 | |
---|
177 | /// Scalar * matrix |
---|
178 | _OgreExport friend Matrix3 operator* (Real fScalar, const Matrix3& rkMatrix); |
---|
179 | |
---|
180 | // utilities |
---|
181 | Matrix3 Transpose () const; |
---|
182 | bool Inverse (Matrix3& rkInverse, Real fTolerance = 1e-06) const; |
---|
183 | Matrix3 Inverse (Real fTolerance = 1e-06) const; |
---|
184 | Real Determinant () const; |
---|
185 | |
---|
186 | /// Singular value decomposition |
---|
187 | void SingularValueDecomposition (Matrix3& rkL, Vector3& rkS, |
---|
188 | Matrix3& rkR) const; |
---|
189 | void SingularValueComposition (const Matrix3& rkL, |
---|
190 | const Vector3& rkS, const Matrix3& rkR); |
---|
191 | |
---|
192 | /// Gram-Schmidt orthonormalization (applied to columns of rotation matrix) |
---|
193 | void Orthonormalize (); |
---|
194 | |
---|
195 | /// Orthogonal Q, diagonal D, upper triangular U stored as (u01,u02,u12) |
---|
196 | void QDUDecomposition (Matrix3& rkQ, Vector3& rkD, |
---|
197 | Vector3& rkU) const; |
---|
198 | |
---|
199 | Real SpectralNorm () const; |
---|
200 | |
---|
201 | /// Note: Matrix must be orthonormal |
---|
202 | void ToAngleAxis (Vector3& rkAxis, Radian& rfAngle) const; |
---|
203 | inline void ToAngleAxis (Vector3& rkAxis, Degree& rfAngle) const { |
---|
204 | Radian r; |
---|
205 | ToAngleAxis ( rkAxis, r ); |
---|
206 | rfAngle = r; |
---|
207 | } |
---|
208 | void FromAngleAxis (const Vector3& rkAxis, const Radian& fRadians); |
---|
209 | |
---|
210 | /** The matrix must be orthonormal. The decomposition is yaw*pitch*roll |
---|
211 | where yaw is rotation about the Up vector, pitch is rotation about the |
---|
212 | Right axis, and roll is rotation about the Direction axis. */ |
---|
213 | bool ToEulerAnglesXYZ (Radian& rfYAngle, Radian& rfPAngle, |
---|
214 | Radian& rfRAngle) const; |
---|
215 | bool ToEulerAnglesXZY (Radian& rfYAngle, Radian& rfPAngle, |
---|
216 | Radian& rfRAngle) const; |
---|
217 | bool ToEulerAnglesYXZ (Radian& rfYAngle, Radian& rfPAngle, |
---|
218 | Radian& rfRAngle) const; |
---|
219 | bool ToEulerAnglesYZX (Radian& rfYAngle, Radian& rfPAngle, |
---|
220 | Radian& rfRAngle) const; |
---|
221 | bool ToEulerAnglesZXY (Radian& rfYAngle, Radian& rfPAngle, |
---|
222 | Radian& rfRAngle) const; |
---|
223 | bool ToEulerAnglesZYX (Radian& rfYAngle, Radian& rfPAngle, |
---|
224 | Radian& rfRAngle) const; |
---|
225 | void FromEulerAnglesXYZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
226 | void FromEulerAnglesXZY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
227 | void FromEulerAnglesYXZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
228 | void FromEulerAnglesYZX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
229 | void FromEulerAnglesZXY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
230 | void FromEulerAnglesZYX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle); |
---|
231 | /// Eigensolver, matrix must be symmetric |
---|
232 | void EigenSolveSymmetric (Real afEigenvalue[3], |
---|
233 | Vector3 akEigenvector[3]) const; |
---|
234 | |
---|
235 | static void TensorProduct (const Vector3& rkU, const Vector3& rkV, |
---|
236 | Matrix3& rkProduct); |
---|
237 | |
---|
238 | /** Determines if this matrix involves a scaling. */ |
---|
239 | inline bool hasScale() const |
---|
240 | { |
---|
241 | // check magnitude of column vectors (==local axes) |
---|
242 | Real t = m[0][0] * m[0][0] + m[1][0] * m[1][0] + m[2][0] * m[2][0]; |
---|
243 | if (!Math::RealEqual(t, 1.0, (Real)1e-04)) |
---|
244 | return true; |
---|
245 | t = m[0][1] * m[0][1] + m[1][1] * m[1][1] + m[2][1] * m[2][1]; |
---|
246 | if (!Math::RealEqual(t, 1.0, (Real)1e-04)) |
---|
247 | return true; |
---|
248 | t = m[0][2] * m[0][2] + m[1][2] * m[1][2] + m[2][2] * m[2][2]; |
---|
249 | if (!Math::RealEqual(t, 1.0, (Real)1e-04)) |
---|
250 | return true; |
---|
251 | |
---|
252 | return false; |
---|
253 | } |
---|
254 | |
---|
255 | /** Function for writing to a stream. |
---|
256 | */ |
---|
257 | inline _OgreExport friend std::ostream& operator << |
---|
258 | ( std::ostream& o, const Matrix3& mat ) |
---|
259 | { |
---|
260 | o << "Matrix3(" << mat[0][0] << ", " << mat[0][1] << ", " << mat[0][2] << ", " |
---|
261 | << mat[1][0] << ", " << mat[1][1] << ", " << mat[1][2] << ", " |
---|
262 | << mat[2][0] << ", " << mat[2][1] << ", " << mat[2][2] << ")"; |
---|
263 | return o; |
---|
264 | } |
---|
265 | |
---|
266 | static const Real EPSILON; |
---|
267 | static const Matrix3 ZERO; |
---|
268 | static const Matrix3 IDENTITY; |
---|
269 | |
---|
270 | protected: |
---|
271 | // support for eigensolver |
---|
272 | void Tridiagonal (Real afDiag[3], Real afSubDiag[3]); |
---|
273 | bool QLAlgorithm (Real afDiag[3], Real afSubDiag[3]); |
---|
274 | |
---|
275 | // support for singular value decomposition |
---|
276 | static const Real msSvdEpsilon; |
---|
277 | static const unsigned int msSvdMaxIterations; |
---|
278 | static void Bidiagonalize (Matrix3& kA, Matrix3& kL, |
---|
279 | Matrix3& kR); |
---|
280 | static void GolubKahanStep (Matrix3& kA, Matrix3& kL, |
---|
281 | Matrix3& kR); |
---|
282 | |
---|
283 | // support for spectral norm |
---|
284 | static Real MaxCubicRoot (Real afCoeff[3]); |
---|
285 | |
---|
286 | Real m[3][3]; |
---|
287 | |
---|
288 | // for faster access |
---|
289 | friend class Matrix4; |
---|
290 | }; |
---|
291 | /** @} */ |
---|
292 | /** @} */ |
---|
293 | } |
---|
294 | #endif |
---|