OgreMatrix3.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE 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
53namespace Ogre
54{
69 {
70 public:
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
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
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
136 inline Matrix3& operator= (const Matrix3& rkMatrix)
137 {
138 memcpy(m,rkMatrix.m,9*sizeof(Real));
139 return *this;
140 }
141
144 bool operator== (const Matrix3& rkMatrix) const;
145
148 inline bool operator!= (const Matrix3& rkMatrix) const
149 {
150 return !operator==(rkMatrix);
151 }
152
153 // arithmetic operations
156 Matrix3 operator+ (const Matrix3& rkMatrix) const;
157
160 Matrix3 operator- (const Matrix3& rkMatrix) const;
161
164 Matrix3 operator* (const Matrix3& rkMatrix) const;
165 Matrix3 operator- () const;
166
168 Vector3 operator* (const Vector3& rkVector) const;
169
171 _OgreExport friend Vector3 operator* (const Vector3& rkVector,
172 const Matrix3& rkMatrix);
173
175 Matrix3 operator* (Real fScalar) const;
176
178 _OgreExport friend Matrix3 operator* (Real fScalar, const Matrix3& rkMatrix);
179
180 // utilities
182 bool Inverse (Matrix3& rkInverse, Real fTolerance = 1e-06) const;
183 Matrix3 Inverse (Real fTolerance = 1e-06) const;
185
188 Matrix3& rkR) const;
190 const Vector3& rkS, const Matrix3& rkR);
191
194
197 Vector3& rkU) const;
198
200
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
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);
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
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
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 };
293}
294#endif
#define _OgreExport
Definition: OgrePlatform.h:257
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:99
static bool RealEqual(Real a, Real b, Real tolerance=std::numeric_limits< Real >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:69
Matrix3(const Matrix3 &rkMatrix)
Definition: OgreMatrix3.h:80
Vector3 GetColumn(size_t iCol) const
Matrix3()
Default constructor.
Definition: OgreMatrix3.h:75
Real Determinant() const
bool ToEulerAnglesXZY(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
void SingularValueComposition(const Matrix3 &rkL, const Vector3 &rkS, const Matrix3 &rkR)
void FromEulerAnglesXZY(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
static void Bidiagonalize(Matrix3 &kA, Matrix3 &kL, Matrix3 &kR)
static Real MaxCubicRoot(Real afCoeff[3])
void FromEulerAnglesYXZ(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
Matrix3(Real fEntry00, Real fEntry01, Real fEntry02, Real fEntry10, Real fEntry11, Real fEntry12, Real fEntry20, Real fEntry21, Real fEntry22)
Definition: OgreMatrix3.h:84
void ToAngleAxis(Vector3 &rkAxis, Radian &rfAngle) const
Note: Matrix must be orthonormal.
void ToAngleAxis(Vector3 &rkAxis, Degree &rfAngle) const
Definition: OgreMatrix3.h:203
void FromEulerAnglesXYZ(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
static const Matrix3 ZERO
Definition: OgreMatrix3.h:267
bool ToEulerAnglesYZX(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
static const Real EPSILON
Definition: OgreMatrix3.h:266
static const unsigned int msSvdMaxIterations
Definition: OgreMatrix3.h:277
void QDUDecomposition(Matrix3 &rkQ, Vector3 &rkD, Vector3 &rkU) const
Orthogonal Q, diagonal D, upper triangular U stored as (u01,u02,u12)
Matrix3(const Real arr[3][3])
Definition: OgreMatrix3.h:76
void swap(Matrix3 &other)
Exchange the contents of this matrix with another.
Definition: OgreMatrix3.h:101
void EigenSolveSymmetric(Real afEigenvalue[3], Vector3 akEigenvector[3]) const
Eigensolver, matrix must be symmetric.
bool ToEulerAnglesYXZ(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
bool Inverse(Matrix3 &rkInverse, Real fTolerance=1e-06) const
void FromEulerAnglesZYX(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
static void GolubKahanStep(Matrix3 &kA, Matrix3 &kL, Matrix3 &kR)
void FromEulerAnglesYZX(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
bool QLAlgorithm(Real afDiag[3], Real afSubDiag[3])
void SingularValueDecomposition(Matrix3 &rkL, Vector3 &rkS, Matrix3 &rkR) const
Singular value decomposition.
static const Real msSvdEpsilon
Definition: OgreMatrix3.h:276
bool ToEulerAnglesZYX(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
void SetColumn(size_t iCol, const Vector3 &vec)
bool ToEulerAnglesZXY(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
void FromAngleAxis(const Vector3 &rkAxis, const Radian &fRadians)
void Tridiagonal(Real afDiag[3], Real afSubDiag[3])
bool ToEulerAnglesXYZ(Radian &rfYAngle, Radian &rfPAngle, Radian &rfRAngle) const
The matrix must be orthonormal.
void FromEulerAnglesZXY(const Radian &fYAngle, const Radian &fPAngle, const Radian &fRAngle)
Matrix3 Inverse(Real fTolerance=1e-06) const
void FromAxes(const Vector3 &xAxis, const Vector3 &yAxis, const Vector3 &zAxis)
Real SpectralNorm() const
static const Matrix3 IDENTITY
Definition: OgreMatrix3.h:268
void Orthonormalize()
Gram-Schmidt orthonormalization (applied to columns of rotation matrix)
bool hasScale() const
Determines if this matrix involves a scaling.
Definition: OgreMatrix3.h:239
Matrix3 Transpose() const
static void TensorProduct(const Vector3 &rkU, const Vector3 &rkV, Matrix3 &rkProduct)
Real m[3][3]
Definition: OgreMatrix3.h:286
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:79
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:48
Standard 3-dimensional vector.
Definition: OgreVector3.h:52
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:747
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
float Real
Software floating point type.
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.