SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLMat3< T > Class Template Reference

3x3 matrix template class More...

#include <SLMat3.h>

Public Member Functions

 SLMat3 ()
 Sets identity matrix. More...
 
 SLMat3 (const SLMat3 &A)
 Sets mat by other SLMat3. More...
 
 SLMat3 (const T M0, const T M3, const T M6, const T M1, const T M4, const T M7, const T M2, const T M5, const T M8)
 Sets matrix by components. More...
 
 SLMat3 (const T angleDEG, const T axis_x, const T axis_y, const T axis_z)
 Sets rotate matrix from axis & angle. More...
 
 SLMat3 (const T angleDEG, const SLVec3< T > axis)
 Sets rotate matrix. More...
 
 SLMat3 (const T scale_xyz)
 Sets uniform scaling matrix. More...
 
 SLMat3 (const T angleXRAD, const T angleYRAD, const T angleZRAD)
 Sets rotation matrix from Euler angles in radians. More...
 
void setMatrix (const SLMat3 &A)
 
void setMatrix (const SLMat3 *A)
 
void setMatrix (const T *M)
 
void setMatrix (T M0, T M3, T M6, T M1, T M4, T M7, T M2, T M5, T M8)
 
void m (int i, T val)
 
const T * m () const
 
m (int i) const
 
SLMat3< T > & operator= (const SLMat3 &A)
 
SLMat3< T > & operator*= (const SLMat3 &A)
 
SLMat3< T > & operator= (const T *a)
 
SLMat3< T > operator+ (const SLMat3 &A) const
 
SLMat3< T > operator- (const SLMat3 &A) const
 
SLMat3< T > operator* (const SLMat3 &A) const
 
SLVec3< T > operator* (const SLVec3< T > &v) const
 
SLMat3< T > operator* (T a) const
 scalar multiplication More...
 
SLMat3< T > & operator*= (T a)
 scalar multiplication More...
 
SLMat3< T > operator/ (T a) const
 scalar division More...
 
SLMat3< T > & operator/= (T a)
 scalar division More...
 
 operator const T * () const
 
 operator T* ()
 
T & operator() (SLint row, SLint col)
 
const T & operator() (SLint row, SLint col) const
 
void rotation (const T angleDEG, const SLVec3< T > &axis)
 Sets the rotation components
More...
 
void rotation (const T angleDEG, const T axisx, const T axisy, const T axisz)
 
void rotation (const T zAngleRAD, const T yAngleRAD, const T xAngleRAD)
 
void scale (const T sx, const T sy, const T sz)
 Sets the scaling components. More...
 
void scale (const SLVec3< T > &s)
 
void scale (const T s)
 
void identity ()
 
void transpose ()
 Transposes the matrix. More...
 
SLMat3< T > transposed () const
 Returns the transposed of the matrix and leaves the itself unchanged. More...
 
void invert ()
 Inverts the matrix. More...
 
SLMat3< T > inverted ()
 Returns the inverse of the matrix and leaves itself unchanged. More...
 
trace () const
 
det () const
 det returns the determinant More...
 
void toAngleAxis (T &angleDEG, SLVec3< T > &axis) const
 Conversion to axis and angle in radians. More...
 
void toEulerAnglesXYZ (T &xRotRAD, T &yRotRAD, T &zRotRAD)
 
void toEulerAnglesZYX (T &zRotRAD, T &yRotRAD, T &xRotRAD)
 
void fromEulerAnglesXYZ (const T angleXRAD, const T angleYRAD, const T angleZRAD)
 
void fromEulerAnglesZYX (const T angleZ1RAD, const T angleY2RAD, const T angleX3RAD)
 
void fromEulerAnglesZXZ (const T angleZ1RAD, const T angleX2RAD, const T angleZ2RAD)
 
void print (const SLchar *str) const
 

Static Public Member Functions

static void swap (T &a, T &b)
 

Public Attributes

_m [9]
 

Detailed Description

template<class T>
class SLMat3< T >

3x3 matrix template class

Implements a 3 by 3 matrix template. 9 floats were used instead of the normal [3][3] array. The order is columnwise as in OpenGL

| 0 3 6 | | 1 4 7 | | 2 5 8 |


type definitions for 3x3 matrice:
Use SLMat3f for a specific float type 3x3 matrix
Use SLMat3d for a specific double type 3x3 matrix

Definition at line 38 of file SLMat3.h.

Constructor & Destructor Documentation

◆ SLMat3() [1/7]

template<class T >
SLMat3< T >::SLMat3

Sets identity matrix.

Definition at line 133 of file SLMat3.h.

134 {
135  identity();
136 }
void identity()
Definition: SLMat3.h:329

◆ SLMat3() [2/7]

template<class T >
SLMat3< T >::SLMat3 ( const SLMat3< T > &  A)

Sets mat by other SLMat3.

Definition at line 139 of file SLMat3.h.

140 {
141  setMatrix(A);
142 }
void setMatrix(const SLMat3 &A)
Definition: SLMat3.h:299

◆ SLMat3() [3/7]

template<class T >
SLMat3< T >::SLMat3 ( const T  M0,
const T  M3,
const T  M6,
const T  M1,
const T  M4,
const T  M7,
const T  M2,
const T  M5,
const T  M8 
)

Sets matrix by components.

Definition at line 145 of file SLMat3.h.

148 {
149  _m[0]=M0; _m[3]=M3; _m[6]=M6;
150  _m[1]=M1; _m[4]=M4; _m[7]=M7;
151  _m[2]=M2; _m[5]=M5; _m[8]=M8;
152 }
T _m[9]
Definition: SLMat3.h:41

◆ SLMat3() [4/7]

template<class T >
SLMat3< T >::SLMat3 ( const T  angleDEG,
const T  axis_x,
const T  axis_y,
const T  axis_z 
)

Sets rotate matrix from axis & angle.

Definition at line 155 of file SLMat3.h.

156 {
157  rotation(angleDEG, axisx, axisy, axisz);
158 }
void rotation(const T angleDEG, const SLVec3< T > &axis)
Sets the rotation components
Definition: SLMat3.h:392

◆ SLMat3() [5/7]

template<class T >
SLMat3< T >::SLMat3 ( const T  angleDEG,
const SLVec3< T >  axis 
)

Sets rotate matrix.

Definition at line 161 of file SLMat3.h.

162 {
163  rotation(angleDEG, axis);
164 }

◆ SLMat3() [6/7]

template<class T >
SLMat3< T >::SLMat3 ( const T  scale_xyz)

Sets uniform scaling matrix.

Definition at line 167 of file SLMat3.h.

168 {
169  scale(scale_xyz);
170 }
void scale(const T sx, const T sy, const T sz)
Sets the scaling components.
Definition: SLMat3.h:462

◆ SLMat3() [7/7]

template<class T >
SLMat3< T >::SLMat3 ( const T  angleXRAD,
const T  angleYRAD,
const T  angleZRAD 
)

Sets rotation matrix from Euler angles in radians.

Definition at line 173 of file SLMat3.h.

176 {
177  fromEulerAnglesXYZ(angleXRAD,
178  angleYRAD,
179  angleZRAD);
180 }
void fromEulerAnglesXYZ(const T angleXRAD, const T angleYRAD, const T angleZRAD)
Definition: SLMat3.h:683

Member Function Documentation

◆ det()

template<class T >
T SLMat3< T >::det
inline

det returns the determinant

Definition at line 493 of file SLMat3.h.

494 {
495  return _m[0]*(_m[4]*_m[8] - _m[7]*_m[5]) -
496  _m[3]*(_m[1]*_m[8] - _m[7]*_m[2]) +
497  _m[6]*(_m[1]*_m[5] - _m[4]*_m[2]);
498 }

◆ fromEulerAnglesXYZ()

template<class T >
void SLMat3< T >::fromEulerAnglesXYZ ( const T  angleX1RAD,
const T  angleY2RAD,
const T  angleZ3RAD 
)

Sets the linear 3x3 sub-matrix as a rotation matrix from the 3 euler angles in radians around the z-axis, y-axis & x-axis := Rx * Ry * Rz See: http://en.wikipedia.org/wiki/Euler_angles

Definition at line 683 of file SLMat3.h.

686 {
687  T s1 = sin(angleX1RAD), c1 = cos(angleX1RAD);
688  T s2 = sin(angleY2RAD), c2 = cos(angleY2RAD);
689  T s3 = sin(angleZ3RAD), c3 = cos(angleZ3RAD);
690 
691  _m[0]=(T) (c2*c3); _m[3]=(T)-(c2*s3); _m[6] =(T) s2;
692  _m[1]=(T) (s1*s2*c3) + (c1*s3); _m[4]=(T)-(s1*s2*s3) + (c1*c3); _m[7] =(T)-(c2*s1);
693  _m[2]=(T)-(c1*s2*c3) + (s1*s3); _m[5]=(T) (c1*s2*s3) + (s1*c3); _m[8] =(T) (c1*c2);
694 }

◆ fromEulerAnglesZXZ()

template<class T >
void SLMat3< T >::fromEulerAnglesZXZ ( const T  angleZ1RAD,
const T  angleX2RAD,
const T  angleZ3RAD 
)

Sets the linear 3x3 sub-matrix as a rotation matrix from the 3 euler angles in radians around the z-axis, x-axis & z-axis:= Z1*X2*Z3 See: http://en.wikipedia.org/wiki/Euler_angles

Definition at line 721 of file SLMat3.h.

724 {
725  T s1 = sin(angleZ1RAD), c1 = cos(angleZ1RAD);
726  T s2 = sin(angleX2RAD), c2 = cos(angleX2RAD);
727  T s3 = sin(angleZ3RAD), c3 = cos(angleZ3RAD);
728 
729  _m[0]=(T)( c1*c3 - s1*c2*s3); _m[3]=(T)( s1*c3 + c1*c2*s3); _m[6] =(T)( s2*s3);
730  _m[1]=(T)(-c1*s3 - s1*c2*c3); _m[4]=(T)( c1*c2*c3 - s1*s3); _m[7] =(T)( s2*c3);
731  _m[2]=(T)( s1*s2); _m[5]=(T)(-c1*s2); _m[8] =(T)( c2);
732 }

◆ fromEulerAnglesZYX()

template<class T >
void SLMat3< T >::fromEulerAnglesZYX ( const T  angleZ1RAD,
const T  angleY2RAD,
const T  angleX3RAD 
)

Sets the linear 3x3 sub-matrix as a rotation matrix from the 3 euler angles in radians around the x-axis, y-axis & z-axis := Rz * Ry * Rx See: http://en.wikipedia.org/wiki/Euler_angles

Definition at line 702 of file SLMat3.h.

705 {
706  T s1 = sin(angleZ1RAD), c1 = cos(angleZ1RAD);
707  T s2 = sin(angleY2RAD), c2 = cos(angleY2RAD);
708  T s3 = sin(angleX3RAD), c3 = cos(angleX3RAD);
709 
710  _m[0]=(T) (c1*c2); _m[3]=(T) (c1*s2*s3) - (c3*s1); _m[6] =(T) (s1*s3) + (c1*c3*s2);
711  _m[1]=(T) (c2*s1); _m[4]=(T) (c1*c3) + (s1*s2*s3); _m[7] =(T) (c3*s1*s2) - (c1*s3);
712  _m[2]=(T)-s2; _m[5]=(T) (c2*s3); _m[8] =(T) (c2*c3);
713 }

◆ identity()

template<class T >
void SLMat3< T >::identity

Definition at line 329 of file SLMat3.h.

330 {
331  _m[0]=_m[4]=_m[8]=1;
332  _m[1]=_m[2]=_m[3]=_m[5]=_m[6]=_m[7]=0;
333 }

◆ invert()

template<class T >
void SLMat3< T >::invert

Inverts the matrix.

Definition at line 356 of file SLMat3.h.

357 {
358  setMatrix(inverted());
359 }
SLMat3< T > inverted()
Returns the inverse of the matrix and leaves itself unchanged.
Definition: SLMat3.h:363

◆ inverted()

template<class T >
SLMat3< T > SLMat3< T >::inverted

Returns the inverse of the matrix and leaves itself unchanged.

Definition at line 363 of file SLMat3.h.

364 {
365  // Compute determinant as early as possible using these cofactors.
366  T d = det();
367 
368  if (fabs(d) < FLT_EPSILON)
369  {
370  SLMATH_LOG("3x3-Matrix is singular. Inversion impossible.");
371  exit(0);
372  }
373 
374  SLMat3<T> i;
375  i._m[0] = _m[4]*_m[8] - _m[7]*_m[5];
376  i._m[1] = _m[7]*_m[2] - _m[1]*_m[8];
377  i._m[2] = _m[1]*_m[5] - _m[4]*_m[2];
378  i._m[3] = _m[6]*_m[5] - _m[3]*_m[8];
379  i._m[4] = _m[0]*_m[8] - _m[6]*_m[2];
380  i._m[5] = _m[3]*_m[2] - _m[0]*_m[5];
381  i._m[6] = _m[3]*_m[7] - _m[6]*_m[4];
382  i._m[7] = _m[6]*_m[1] - _m[0]*_m[7];
383  i._m[8] = _m[0]*_m[4] - _m[3]*_m[1];
384 
385  i /= d;
386  return i;
387 }
#define SLMATH_LOG(...)
Definition: SLMath.h:70
3x3 matrix template class
Definition: SLMat3.h:39
T det() const
det returns the determinant
Definition: SLMat3.h:493

◆ m() [1/3]

template<class T >
const T* SLMat3< T >::m ( ) const
inline

Definition at line 70 of file SLMat3.h.

70 {return _m;}

◆ m() [2/3]

template<class T >
T SLMat3< T >::m ( int  i) const
inline

Definition at line 71 of file SLMat3.h.

71 {assert(i>=0 && i<9); return _m[i];}

◆ m() [3/3]

template<class T >
void SLMat3< T >::m ( int  i,
val 
)
inline

Definition at line 67 of file SLMat3.h.

67 {assert(i>=0 && i<9); _m[i] = val;}

◆ operator const T *()

template<class T >
SLMat3< T >::operator const T * ( ) const
inline

Definition at line 86 of file SLMat3.h.

86 {return _m;}

◆ operator T*()

template<class T >
SLMat3< T >::operator T* ( )
inline

Definition at line 87 of file SLMat3.h.

87 {return _m;}

◆ operator()() [1/2]

template<class T >
T& SLMat3< T >::operator() ( SLint  row,
SLint  col 
)
inline

Definition at line 88 of file SLMat3.h.

88 {return _m[3*col+row];}

◆ operator()() [2/2]

template<class T >
const T& SLMat3< T >::operator() ( SLint  row,
SLint  col 
) const
inline

Definition at line 89 of file SLMat3.h.

89 {return _m[3*col+row];}

◆ operator*() [1/3]

template<class T >
SLMat3< T > SLMat3< T >::operator* ( const SLMat3< T > &  A) const

Definition at line 221 of file SLMat3.h.

222 {
223  SLMat3<T> NewM(_m[0]*A._m[0] + _m[3]*A._m[1] + _m[6]*A._m[2], // ROW 1
224  _m[0]*A._m[3] + _m[3]*A._m[4] + _m[6]*A._m[5],
225  _m[0]*A._m[6] + _m[3]*A._m[7] + _m[6]*A._m[8],
226  _m[1]*A._m[0] + _m[4]*A._m[1] + _m[7]*A._m[2], // ROW 2
227  _m[1]*A._m[3] + _m[4]*A._m[4] + _m[7]*A._m[5],
228  _m[1]*A._m[6] + _m[4]*A._m[7] + _m[7]*A._m[8],
229  _m[2]*A._m[0] + _m[5]*A._m[1] + _m[8]*A._m[2], // ROW 3
230  _m[2]*A._m[3] + _m[5]*A._m[4] + _m[8]*A._m[5],
231  _m[2]*A._m[6] + _m[5]*A._m[7] + _m[8]*A._m[8]);
232  return(NewM);
233 }

◆ operator*() [2/3]

template<class T >
SLVec3< T > SLMat3< T >::operator* ( const SLVec3< T > &  v) const

Definition at line 252 of file SLMat3.h.

253 {
254  SLVec3<T> NewV;
255  NewV.x = _m[0]*v.x + _m[3]*v.y + _m[6]*v.z;
256  NewV.y = _m[1]*v.x + _m[4]*v.y + _m[7]*v.z;
257  NewV.z = _m[2]*v.x + _m[5]*v.y + _m[8]*v.z;
258  return(NewV);
259 }
3D vector template class for standard 3D vector algebra.
Definition: SLVec3.h:40
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43

◆ operator*() [3/3]

template<class T >
SLMat3< T > SLMat3< T >::operator* ( a) const

scalar multiplication

Definition at line 262 of file SLMat3.h.

263 {
264  SLMat3<T> NewM(_m[0] * a, _m[3] * a, _m[6] * a,
265  _m[1] * a, _m[4] * a, _m[7] * a,
266  _m[2] * a, _m[5] * a, _m[8] * a);
267  return NewM;
268 }

◆ operator*=() [1/2]

template<class T >
SLMat3< T > & SLMat3< T >::operator*= ( const SLMat3< T > &  A)

Definition at line 237 of file SLMat3.h.

238 {
239  setMatrix(_m[0]*A._m[0] + _m[3]*A._m[1] + _m[6]*A._m[2], // ROW 1
240  _m[0]*A._m[3] + _m[3]*A._m[4] + _m[6]*A._m[5],
241  _m[0]*A._m[6] + _m[3]*A._m[7] + _m[6]*A._m[8],
242  _m[1]*A._m[0] + _m[4]*A._m[1] + _m[7]*A._m[2], // ROW 2
243  _m[1]*A._m[3] + _m[4]*A._m[4] + _m[7]*A._m[5],
244  _m[1]*A._m[6] + _m[4]*A._m[7] + _m[7]*A._m[8],
245  _m[2]*A._m[0] + _m[5]*A._m[1] + _m[8]*A._m[2], // ROW 3
246  _m[2]*A._m[3] + _m[5]*A._m[4] + _m[8]*A._m[5],
247  _m[2]*A._m[6] + _m[5]*A._m[7] + _m[8]*A._m[8]);
248  return *this;
249 }

◆ operator*=() [2/2]

template<class T >
SLMat3< T > & SLMat3< T >::operator*= ( a)

scalar multiplication

Definition at line 272 of file SLMat3.h.

273 {
274  for (int i=0; i<9; ++i) _m[i] *= a;
275  return *this;
276 }

◆ operator+()

template<class T >
SLMat3< T > SLMat3< T >::operator+ ( const SLMat3< T > &  A) const

Definition at line 201 of file SLMat3.h.

202 {
203  SLMat3<T> sum(_m[0]+A._m[0], _m[3]+A._m[3], _m[6]+A._m[6],
204  _m[1]+A._m[1], _m[4]+A._m[4], _m[7]+A._m[7],
205  _m[2]+A._m[2], _m[5]+A._m[5], _m[8]+A._m[8]);
206  return(sum);
207 }

◆ operator-()

template<class T >
SLMat3< T > SLMat3< T >::operator- ( const SLMat3< T > &  A) const

Definition at line 211 of file SLMat3.h.

212 {
213  SLMat3<T> sub(_m[0]-A._m[0], _m[3]-A._m[3], _m[6]-A._m[6],
214  _m[1]-A._m[1], _m[4]-A._m[4], _m[7]-A._m[7],
215  _m[2]-A._m[2], _m[5]-A._m[5], _m[8]-A._m[8]);
216  return(sub);
217 }

◆ operator/()

template<class T >
SLMat3< T > SLMat3< T >::operator/ ( a) const

scalar division

Definition at line 280 of file SLMat3.h.

281 {
282  SLMat3<T> NewM(_m[0] / a, _m[3] / a, _m[6] / a,
283  _m[1] / a, _m[4] / a, _m[7] / a,
284  _m[2] / a, _m[5] / a, _m[8] / a);
285  return NewM;
286 }

◆ operator/=()

template<class T >
SLMat3< T > & SLMat3< T >::operator/= ( a)

scalar division

Definition at line 290 of file SLMat3.h.

291 {
292  for (int i=0; i<9; ++i) _m[i] /= a;
293  return *this;
294 }

◆ operator=() [1/2]

template<class T >
SLMat3< T > & SLMat3< T >::operator= ( const SLMat3< T > &  A)

Definition at line 185 of file SLMat3.h.

186 {
187  _m[0]=A._m[0]; _m[3]=A._m[3]; _m[6]=A._m[6];
188  _m[1]=A._m[1]; _m[4]=A._m[4]; _m[7]=A._m[7];
189  _m[2]=A._m[2]; _m[5]=A._m[5]; _m[8]=A._m[8];
190  return(*this);
191 }

◆ operator=() [2/2]

template<class T >
SLMat3< T > & SLMat3< T >::operator= ( const T *  a)

Definition at line 194 of file SLMat3.h.

195 {
196  for (int i=0; i<9; ++i) _m[i] = a[i];
197 }

◆ print()

template<class T >
void SLMat3< T >::print ( const SLchar str) const

Definition at line 738 of file SLMat3.h.

739 {
740  if (str) SLMATH_LOG("%s",str);
741  SLMATH_LOG("% 3.3f % 3.3f % 3.3f", _m[0],_m[3],_m[6]);
742  SLMATH_LOG("% 3.3f % 3.3f % 3.3f", _m[1],_m[4],_m[7]);
743  SLMATH_LOG("% 3.3f % 3.3f % 3.3f", _m[2],_m[5],_m[8]);
744 }

◆ rotation() [1/3]

template<class T >
void SLMat3< T >::rotation ( const T  angleDEG,
const SLVec3< T > &  axis 
)

Sets the rotation components

Definition at line 392 of file SLMat3.h.

393 {
394  rotation(angleDEG, axis.x, axis.y, axis.z);
395 }

◆ rotation() [2/3]

template<class T >
void SLMat3< T >::rotation ( const T  angleDEG,
const T  axisx,
const T  axisy,
const T  axisz 
)

Definition at line 398 of file SLMat3.h.

400 {
401  T angleRAD = (T)angleDEG * DEG2RAD;
402  T ca = (T)cos(angleRAD);
403  T sa = (T)sin(angleRAD);
404 
405  if (axisx==1 && axisy==0 && axisz==0) // about x-axis
406  { _m[0]=1; _m[3]=0; _m[6]=0;
407  _m[1]=0; _m[4]=ca; _m[7]=-sa;
408  _m[2]=0; _m[5]=sa; _m[8]=ca;
409  }
410  else if (axisx==0 && axisy==1 && axisz==0) // about y-axis
411  { _m[0]=ca; _m[3]=0; _m[6]=sa;
412  _m[1]=0; _m[4]=1; _m[7]=0;
413  _m[2]=-sa; _m[5]=0; _m[8]=ca;
414  }
415  else if (axisx==0 && axisy==0 && axisz==1) // about z-axis
416  { _m[0]=ca; _m[3]=-sa; _m[6]=0;
417  _m[1]=sa; _m[4]=ca; _m[7]=0;
418  _m[2]=0; _m[5]=0; _m[8]=1;
419  } // arbitrary axis
420  else
421  {
422  T l = axisx*axisx + axisy*axisy + axisz*axisz; // length squared
423  T x, y, z;
424  x=axisx, y=axisy, z=axisz;
425  if ((l > T(1.0001) || l < T(0.9999)) && l!=0)
426  { l=T(1.0)/sqrt(l);
427  x*=l; y*=l; z*=l;
428  }
429  T xy=x*y, yz=y*z, xz=x*z, xx=x*x, yy=y*y, zz=z*z;
430  _m[0]=xx + ca*(1-xx); _m[3]=xy - xy*ca - z*sa; _m[6]=xz - xz*ca + y*sa;
431  _m[1]=xy - xy*ca + z*sa; _m[4]=yy + ca*(1-yy); _m[7]=yz - yz*ca - x*sa;
432  _m[2]=xz - xz*ca - y*sa; _m[5]=yz - yz*ca + x*sa; _m[8]=zz + ca*(1-zz);
433  }
434 }
static const float DEG2RAD
Definition: Utils.h:239

◆ rotation() [3/3]

template<class T >
void SLMat3< T >::rotation ( const T  zAngleRAD,
const T  yAngleRAD,
const T  xAngleRAD 
)

Sets the matrix as a rotation matrix from the 3 euler angles in radians around the z-axis, y-axis & x-axis. See Van Verth: Essential Math for Games, chapter 5: Orientation Representation

Definition at line 442 of file SLMat3.h.

443 {
444  T Cx = (T)cos(xAngleRAD); T Sx = (T)sin(xAngleRAD);
445  T Cy = (T)cos(yAngleRAD); T Sy = (T)sin(yAngleRAD);
446  T Cz = (T)cos(zAngleRAD); T Sz = (T)sin(zAngleRAD);
447 
448  _m[0] = (Cy * Cz);
449  _m[3] = -(Cy * Sz);
450  _m[6] = Sy;
451 
452  _m[1] = (Sx * Sy * Cz) + (Cx * Sz);
453  _m[4] = -(Sx * Sy * Sz) + (Cx * Cz);
454  _m[7] = -(Sx * Cy);
455 
456  _m[2] = -(Cx * Sy * Cz) + (Sx * Sz);
457  _m[5] = (Cx * Sy * Sz) + (Sx * Cz);
458  _m[8] = (Cx * Cy);
459 }

◆ scale() [1/3]

template<class T >
void SLMat3< T >::scale ( const SLVec3< T > &  s)

Definition at line 470 of file SLMat3.h.

471 {
472  _m[0]=s.x; _m[3]=0; _m[6]=0;
473  _m[1]=0; _m[4]=s.y; _m[7]=0;
474  _m[2]=0; _m[5]=0; _m[8]=s.z;
475 }
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ scale() [2/3]

template<class T >
void SLMat3< T >::scale ( const T  s)

Definition at line 478 of file SLMat3.h.

479 {
480  _m[0]=s; _m[3]=0; _m[6]=0;
481  _m[1]=0; _m[4]=s; _m[7]=0;
482  _m[2]=0; _m[5]=0; _m[8]=s;
483 }
SLScene * s
Definition: SLScene.h:31

◆ scale() [3/3]

template<class T >
void SLMat3< T >::scale ( const T  sx,
const T  sy,
const T  sz 
)

Sets the scaling components.

Definition at line 462 of file SLMat3.h.

463 {
464  _m[0]=sx; _m[3]=0; _m[6]=0;
465  _m[1]=0; _m[4]=sy; _m[7]=0;
466  _m[2]=0; _m[5]=0; _m[8]=sz;
467 }

◆ setMatrix() [1/4]

template<class T >
void SLMat3< T >::setMatrix ( const SLMat3< T > &  A)

Definition at line 299 of file SLMat3.h.

300 {
301  for (int i=0; i<9; ++i) _m[i] = A._m[i];
302 }

◆ setMatrix() [2/4]

template<class T >
void SLMat3< T >::setMatrix ( const SLMat3< T > *  A)

Definition at line 305 of file SLMat3.h.

306 {
307  for (int i=0; i<9; ++i) _m[i] = A->_m[i];
308 }

◆ setMatrix() [3/4]

template<class T >
void SLMat3< T >::setMatrix ( const T *  M)

Definition at line 311 of file SLMat3.h.

312 {
313  for (int i=0; i<9; ++i) _m[i] = M[i];
314 }

◆ setMatrix() [4/4]

template<class T >
void SLMat3< T >::setMatrix ( M0,
M3,
M6,
M1,
M4,
M7,
M2,
M5,
M8 
)

Definition at line 317 of file SLMat3.h.

320 {
321  _m[0]=M0; _m[3]=M3; _m[6]=M6;
322  _m[1]=M1; _m[4]=M4; _m[7]=M7;
323  _m[2]=M2; _m[5]=M5; _m[8]=M8;
324 }

◆ swap()

template<class T >
static void SLMat3< T >::swap ( T &  a,
T &  b 
)
inlinestatic

Definition at line 126 of file SLMat3.h.

126 {T t; t=a; a=b; b=t;}

◆ toAngleAxis()

template<typename T >
void SLMat3< T >::toAngleAxis ( T &  angleDEG,
SLVec3< T > &  axis 
) const

Conversion to axis and angle in radians.

The matrix must be a rotation matrix for this functions to be valid. The last function uses Gram-Schmidt orthonormalization applied to the columns of the rotation matrix. The angle must be in radians, not degrees.

Definition at line 507 of file SLMat3.h.

508 {
509  // Let (x,y,z) be the unit-length axis and let A be an angle of rotation.
510  // The rotation matrix is R = I + sin(A)*P + (1-cos(A))*P^2 where
511  // I is the identity and
512  //
513  // +- -+
514  // | 0 -z +y |
515  // P = | +z 0 -x |
516  // | -y +x 0 |
517  // +- -+
518  //
519  // If A > 0, R represents a counterclockwise rotation about the axis in
520  // the sense of looking from the tip of the axis vector towards the
521  // origin. Some algebra will show that
522  //
523  // cos(A) = (trace(R)-1)/2 and R - R^t = 2*sin(A)*P
524  //
525  // In the event that A = pi, R-R^t = 0 which prevents us from extracting
526  // the axis through P. Instead note that R = I+2*P^2 when A = pi, so
527  // P^2 = (R-I)/2. The diagonal entries of P^2 are x^2-1, y^2-1, and
528  // z^2-1. We can solve these for axis (x,y,z). Because the angle is pi,
529  // it does not matter which sign you choose on the square roots.
530 
531  T tr = trace();
532  T cs = ((T)0.5) * (tr - (T)1);
533  T angleRAD = acos(cs); // in [0,PI]
534 
535  if (angleRAD > (T)0)
536  {
537  if (angleRAD < PI)
538  {
539  axis.x = _m[5] - _m[7];
540  axis.y = _m[6] - _m[2];
541  axis.z = _m[1] - _m[3];
542  axis.normalize();
543  }
544  else
545  {
546  // angle is PI
547  T halfInverse;
548  if (_m[0] >= _m[4])
549  {
550  // r00 >= r11
551  if (_m[0] >= _m[8])
552  {
553  // r00 is maximum diagonal term
554  axis.x = ((T)0.5)*sqrt((T)1 + _m[0] - _m[4] - _m[8]);
555  halfInverse = ((T)0.5)/axis.x;
556  axis.y = halfInverse*_m[3];
557  axis.z = halfInverse*_m[6];
558  }
559  else
560  {
561  // r22 is maximum diagonal term
562  axis.z = ((T)0.5)*sqrt((T)1 + _m[8] - _m[0] - _m[4]);
563  halfInverse = ((T)0.5)/axis.z;
564  axis.x = halfInverse*_m[6];
565  axis.y = halfInverse*_m[7];
566  }
567  }
568  else
569  {
570  // r11 > r00
571  if (_m[4] >= _m[8])
572  {
573  // r11 is maximum diagonal term
574  axis.y = ((T)0.5)*sqrt((T)1 + _m[4] - _m[0] - _m[8]);
575  halfInverse = ((T)0.5)/axis.y;
576  axis.x = halfInverse*_m[3];
577  axis.z = halfInverse*_m[7];
578  }
579  else
580  {
581  // r22 is maximum diagonal term
582  axis.z = ((T)0.5)*sqrt((T)1 + _m[8] - _m[0] - _m[4]);
583  halfInverse = ((T)0.5)/axis.z;
584  axis.x = halfInverse*_m[6];
585  axis.y = halfInverse*_m[7];
586  }
587  }
588  }
589  }
590  else
591  {
592  // The angle is 0 and the matrix is the identity. Any axis will
593  // work, so just use the x-axis.
594  axis.x = (T)1;
595  axis.y = (T)0;
596  axis.z = (T)0;
597  }
598  angleDEG = angleRAD * RAD2DEG;
599 }
T trace() const
Definition: SLMat3.h:486
SLVec3 & normalize()
Definition: SLVec3.h:124
static const float RAD2DEG
Definition: Utils.h:238
static const float PI
Definition: Utils.h:237

◆ toEulerAnglesXYZ()

template<class T >
void SLMat3< T >::toEulerAnglesXYZ ( T &  xRotRAD,
T &  yRotRAD,
T &  zRotRAD 
)

Gets one set of possible x-y-z euler angles that will generate this matrix Source: Essential Mathematics for Games and Interactive Applications A Programmer's Guide 2nd edition by James M. Van Verth and Lars M. Bishop

Definition at line 608 of file SLMat3.h.

609 {
610  T Cx, Sx;
611  T Cy, Sy;
612  T Cz, Sz;
613 
614  Sy = _m[6];
615  Cy = (T)sqrt(1.0 - Sy*Sy);
616 
617  // normal case
618  if (Utils::abs(Cy) > FLT_EPSILON)
619  {
620  T factor = (T)1.0 / Cy;
621  Sx = -_m[7]*factor;
622  Cx = _m[8]*factor;
623  Sz = -_m[3]*factor;
624  Cz = _m[0]*factor;
625  }
626  else // x and z axes aligned
627  {
628  Sz = 0.0f;
629  Cz = 1.0f;
630  Sx = _m[5];
631  Cx = _m[4];
632  }
633 
634  zRotRAD = atan2f(Sz, Cz);
635  yRotRAD = atan2f(Sy, Cy);
636  xRotRAD = atan2f(Sx, Cx);
637 }
T abs(T a)
Definition: Utils.h:249

◆ toEulerAnglesZYX()

template<class T >
void SLMat3< T >::toEulerAnglesZYX ( T &  zRotRAD,
T &  yRotRAD,
T &  xRotRAD 
)

Gets one set of possible z-y-x euler angles that will generate this matrix Source: Essential Mathematics for Games and Interactive Applications A Programmer's Guide 2nd edition by James M. Van Verth and Lars M. Bishop

Definition at line 645 of file SLMat3.h.

646 {
647  T Cx, Sx;
648  T Cy, Sy;
649  T Cz, Sz;
650 
651  Sy = -_m[2];
652  Cy = (T)sqrt(1.0 - Sy*Sy);
653 
654  // normal case
655  if (Utils::abs(Cy) > FLT_EPSILON)
656  {
657  T factor = (T)1.0 / Cy;
658  Sx = _m[5]*factor;
659  Cx = _m[8]*factor;
660  Sz = _m[1]*factor;
661  Cz = _m[0]*factor;
662  }
663  else // x and z axes aligned
664  {
665  Sz = 0.0f;
666  Cz = 1.0f;
667  Sx = _m[3];
668  Cx = _m[6];
669  }
670 
671  zRotRAD = atan2f(Sz, Cz);
672  yRotRAD = atan2f(Sy, Cy);
673  xRotRAD = atan2f(Sx, Cx);
674 }

◆ trace()

template<class T >
T SLMat3< T >::trace
inline

Definition at line 486 of file SLMat3.h.

487 {
488  return _m[0] + _m[4] + _m[8];
489 }

◆ transpose()

template<class T >
void SLMat3< T >::transpose

Transposes the matrix.

Definition at line 337 of file SLMat3.h.

338 {
339  swap(_m[1],_m[3]);
340  swap(_m[2],_m[6]);
341  swap(_m[5],_m[7]);
342 }
static void swap(T &a, T &b)
Definition: SLMat3.h:126

◆ transposed()

template<class T >
SLMat3< T > SLMat3< T >::transposed

Returns the transposed of the matrix and leaves the itself unchanged.

Definition at line 346 of file SLMat3.h.

347 {
348  SLMat3<T> t(_m[0],_m[1],_m[2],
349  _m[3],_m[4],_m[5],
350  _m[6],_m[7],_m[8]);
351  return t;
352 }

Member Data Documentation

◆ _m

template<class T >
T SLMat3< T >::_m[9]

Definition at line 41 of file SLMat3.h.


The documentation for this class was generated from the following file: