OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreMatrix3.h
Go to the documentation of this file.
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-2014 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 {
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
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 
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 
193  void Orthonormalize ();
194 
196  void QDUDecomposition (Matrix3& rkQ, Vector3& rkD,
197  Vector3& rkU) const;
198 
199  Real SpectralNorm () const;
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
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:79
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:255
static const Real msSvdEpsilon
Definition: OgreMatrix3.h:276
Matrix3(Real fEntry00, Real fEntry01, Real fEntry02, Real fEntry10, Real fEntry11, Real fEntry12, Real fEntry20, Real fEntry21, Real fEntry22)
Definition: OgreMatrix3.h:84
void swap(Matrix3 &other)
Exchange the contents of this matrix with another.
Definition: OgreMatrix3.h:101
Real m[3][3]
Definition: OgreMatrix3.h:286
static const Matrix3 IDENTITY
Definition: OgreMatrix3.h:268
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:782
static bool RealEqual(Real a, Real b, Real tolerance=std::numeric_limits< Real >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
Matrix3(const Real arr[3][3])
Definition: OgreMatrix3.h:76
Matrix3()
Default constructor.
Definition: OgreMatrix3.h:75
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:100
bool hasScale() const
Determines if this matrix involves a scaling.
Definition: OgreMatrix3.h:239
Matrix3(const Matrix3 &rkMatrix)
Definition: OgreMatrix3.h:80
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Standard 3-dimensional vector.
Definition: OgreVector3.h:50
static const unsigned int msSvdMaxIterations
Definition: OgreMatrix3.h:277
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:49
static const Matrix3 ZERO
Definition: OgreMatrix3.h:267
void ToAngleAxis(Vector3 &rkAxis, Degree &rfAngle) const
Definition: OgreMatrix3.h:203
static const Real EPSILON
Definition: OgreMatrix3.h:266
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)