My Project
ParaMatrix3.h
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-2006 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
9 
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
13 version.
14 
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22 http://www.gnu.org/copyleft/lesser.txt.
23 
24 You may alternatively use this source under the terms of a specific version of
25 the OGRE Unrestricted License provided you have obtained such a license from
26 Torus Knot Software Ltd.
27 -----------------------------------------------------------------------------
28 */
29 #ifndef __Matrix3_H__
30 #define __Matrix3_H__
31 
32 
33 
34 #include "ParaVector3.h"
35 
36 // NB All code adapted from Wild Magic 0.2 Matrix math (free source code)
37 // http://www.geometrictools.com/
38 
39 // NOTE. The (x,y,z) coordinate system is assumed to be right-handed.
40 // Coordinate axis rotation matrices are of the form
41 // RX = 1 0 0
42 // 0 cos(t) -sin(t)
43 // 0 sin(t) cos(t)
44 // where t > 0 indicates a counterclockwise rotation in the yz-plane
45 // RY = cos(t) 0 sin(t)
46 // 0 1 0
47 // -sin(t) 0 cos(t)
48 // where t > 0 indicates a counterclockwise rotation in the zx-plane
49 // RZ = cos(t) -sin(t) 0
50 // sin(t) cos(t) 0
51 // 0 0 1
52 // where t > 0 indicates a counterclockwise rotation in the xy-plane.
53 
54 namespace ParaEngine
55 {
63  class Matrix3
64  {
65  public:
66  union {
67  struct {
68  float _11, _12, _13;
69  float _21, _22, _23;
70  float _31, _32, _33;
71  };
72  float m[3][3];
73  };
74 
75  public:
80  inline Matrix3 () {};
81  inline explicit Matrix3 (const float arr[3][3])
82  {
83  memcpy(m,arr,9*sizeof(float));
84  }
85  inline Matrix3 (const Matrix3& rkMatrix)
86  {
87  memcpy(m,rkMatrix.m,9*sizeof(float));
88  }
89  Matrix3(const Matrix4& rkMatrix);
90  Matrix3 (float fEntry00, float fEntry01, float fEntry02,
91  float fEntry10, float fEntry11, float fEntry12,
92  float fEntry20, float fEntry21, float fEntry22)
93  {
94  m[0][0] = fEntry00;
95  m[0][1] = fEntry01;
96  m[0][2] = fEntry02;
97  m[1][0] = fEntry10;
98  m[1][1] = fEntry11;
99  m[1][2] = fEntry12;
100  m[2][0] = fEntry20;
101  m[2][1] = fEntry21;
102  m[2][2] = fEntry22;
103  }
104 
105  // member access, allows use of construct mat[r][c]
106  inline float* operator[] (size_t iRow) const
107  {
108  return (float*)m[iRow];
109  }
110  /*inline operator float* ()
111  {
112  return (float*)m[0];
113  }*/
114  Vector3 GetColumn (size_t iCol) const;
115  void SetColumn(size_t iCol, const Vector3& vec);
116  void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
117 
118  // assignment and comparison
119  inline Matrix3& operator= (const Matrix3& rkMatrix)
120  {
121  memcpy(m,rkMatrix.m,9*sizeof(float));
122  return *this;
123  }
124  bool operator== (const Matrix3& rkMatrix) const;
125  inline bool operator!= (const Matrix3& rkMatrix) const
126  {
127  return !operator==(rkMatrix);
128  }
129 
130  // arithmetic operations
131  Matrix3 operator+ (const Matrix3& rkMatrix) const;
132  Matrix3 operator- (const Matrix3& rkMatrix) const;
133  Matrix3 operator* (const Matrix3& rkMatrix) const;
134  Matrix3 operator- () const;
135 
136  // matrix * vector [3x3 * 3x1 = 3x1]
137  Vector3 operator* (const Vector3& rkVector) const;
138 
139  // vector * matrix [1x3 * 3x3 = 1x3]
140  friend Vector3 operator* (const Vector3& rkVector,
141  const Matrix3& rkMatrix);
142 
143  // matrix * scalar
144  Matrix3 operator* (float fScalar) const;
145 
146  // scalar * matrix
147  friend Matrix3 operator* (float fScalar, const Matrix3& rkMatrix);
148 
149  // utilities
150  Matrix3 Transpose () const;
151  bool Inverse (Matrix3& rkInverse, float fTolerance = 1e-06) const;
152  Matrix3 Inverse (float fTolerance = 1e-06) const;
153  float Determinant () const;
154 
155  // singular value decomposition
156  void SingularValueDecomposition (Matrix3& rkL, Vector3& rkS,
157  Matrix3& rkR) const;
158  void SingularValueComposition (const Matrix3& rkL,
159  const Vector3& rkS, const Matrix3& rkR);
160 
161  // Gram-Schmidt orthonormalization (applied to columns of rotation matrix)
162  void Orthonormalize ();
163 
164  // orthogonal Q, diagonal D, upper triangular U stored as (u01,u02,u12)
165  void QDUDecomposition (Matrix3& rkQ, Vector3& rkD,
166  Vector3& rkU) const;
167 
168  float SpectralNorm () const;
169 
170  // matrix must be orthonormal
171  void ToAxisAngle (Vector3& rkAxis, Radian& rfAngle) const;
172  inline void ToAxisAngle (Vector3& rkAxis, Degree& rfAngle) const {
173  Radian r;
174  ToAxisAngle ( rkAxis, r );
175  rfAngle = r;
176  }
177  void FromAxisAngle (const Vector3& rkAxis, const Radian& fRadians);
178 
179  // The matrix must be orthonormal. The decomposition is yaw*pitch*roll
180  // where yaw is rotation about the Up vector, pitch is rotation about the
181  // Right axis, and roll is rotation about the Direction axis.
182  bool ToEulerAnglesXYZ (Radian& rfYAngle, Radian& rfPAngle,
183  Radian& rfRAngle) const;
184  bool ToEulerAnglesXZY (Radian& rfYAngle, Radian& rfPAngle,
185  Radian& rfRAngle) const;
186  bool ToEulerAnglesYXZ (Radian& rfYAngle, Radian& rfPAngle,
187  Radian& rfRAngle) const;
188  bool ToEulerAnglesYZX (Radian& rfYAngle, Radian& rfPAngle,
189  Radian& rfRAngle) const;
190  bool ToEulerAnglesZXY (Radian& rfYAngle, Radian& rfPAngle,
191  Radian& rfRAngle) const;
192  bool ToEulerAnglesZYX (Radian& rfYAngle, Radian& rfPAngle,
193  Radian& rfRAngle) const;
194  void FromEulerAnglesXYZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
195  void FromEulerAnglesXZY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
196  void FromEulerAnglesYXZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
197  void FromEulerAnglesYZX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
198  void FromEulerAnglesZXY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
199  void FromEulerAnglesZYX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
200  // eigensolver, matrix must be symmetric
201  void EigenSolveSymmetric (float afEigenvalue[3],
202  Vector3 akEigenvector[3]) const;
203 
204  static void TensorProduct (const Vector3& rkU, const Vector3& rkV,
205  Matrix3& rkProduct);
206 
208  inline bool hasScale() const
209  {
210  // check magnitude of column vectors (==local axes)
211  float t = m[0][0] * m[0][0] + m[1][0] * m[1][0] + m[2][0] * m[2][0];
212  if (!Math::RealEqual(t, 1.0f, 1e-04f))
213  return true;
214  t = m[0][1] * m[0][1] + m[1][1] * m[1][1] + m[2][1] * m[2][1];
215  if (!Math::RealEqual(t, 1.0f, 1e-04f))
216  return true;
217  t = m[0][2] * m[0][2] + m[1][2] * m[1][2] + m[2][2] * m[2][2];
218  if (!Math::RealEqual(t, 1.0f, 1e-04f))
219  return true;
220 
221  return false;
222  }
227  void RotX(float angle);
228 
233  void RotY(float angle);
234 
239  void RotZ(float angle);
240 
241  static const float EPSILON;
242  static const Matrix3 ZERO;
243  static const Matrix3 IDENTITY;
244 
245  protected:
246  // support for eigensolver
247  void Tridiagonal (float afDiag[3], float afSubDiag[3]);
248  bool QLAlgorithm (float afDiag[3], float afSubDiag[3]);
249 
250  // support for singular value decomposition
251  static const float ms_fSvdEpsilon;
252  static const unsigned int ms_iSvdMaxIterations;
253  static void Bidiagonalize (Matrix3& kA, Matrix3& kL,
254  Matrix3& kR);
255  static void GolubKahanStep (Matrix3& kA, Matrix3& kL,
256  Matrix3& kR);
257 
258  // support for spectral norm
259  static float MaxCubicRoot (float afCoeff[3]);
260 
261  // for faster access
262  friend class Matrix4;
263  };
264 }
265 #endif
Wrapper class which indicates a given angle value is in Radians.
Definition: ParaAngle.h:10
different physics engine has different winding order.
Definition: EventBinding.h:32
static bool RealEqual(float a, float b, float tolerance=std::numeric_limits< float >::epsilon())
Compare 2 reals, using tolerance for inaccuracies.
Definition: ParaMath.cpp:307
A 3x3 matrix which can represent rotations around axes.
Definition: ParaMatrix3.h:63
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Wrapper class which indicates a given angle value is in Degrees.
Definition: ParaAngle.h:56
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Matrix3()
Default constructor.
Definition: ParaMatrix3.h:80
bool hasScale() const
Determines if this matrix involves a scaling.
Definition: ParaMatrix3.h:208
void RotY(float angle)
Set a rotation matrix around the Y axis.
Definition: ParaMatrix3.cpp:1502
void RotZ(float angle)
Set a rotation matrix around the Z axis.
Definition: ParaMatrix3.cpp:1508
void RotX(float angle)
Set a rotation matrix around the X axis.
Definition: ParaMatrix3.cpp:1496