gKit2 light
List of all members
TQuaternion< Real, Vec3Real > Class Template Reference

A Quaternion class. More...

#include <quaternion.h>

Public Member Functions

Defining a Quaternion
 TQuaternion ()
 
 TQuaternion (const Vec3Real &axis, const Real angle)
 
 TQuaternion (const Vec3Real &from, const Vec3Real &to)
 
 TQuaternion (Real q0, Real q1, Real q2, Real q3)
 
 TQuaternion (const TQuaternion &Q)
 
TQuaternionoperator= (const TQuaternion &Q)
 
TQuaternionoperator+= (const TQuaternion &Q)
 
void setAxisAngle (const Vec3Real &axis, const Real angle)
 
void setValue (Real q0, Real q1, Real q2, Real q3)
 
template<typename MAT >
void setFromRotationMatrix (const MAT &m)
 
void setFromRotatedBase (const Vec3Real &X, const Vec3Real &Y, const Vec3Real &Z)
 
Inversion
TQuaternion inverse () const
 
void invert ()
 
void negate ()
 
Real normalize ()
 
Associated matrix
const Real * matrix () const
 
template<typename MAT >
void getMatrix44 (MAT &m) const
 
template<typename MAT >
void getMatrix33 (MAT &m) const
 
void getMatrix16 (Real m[16]) const
 
void getRotationMatrix (Real m[3][3]) const
 
const Real * inverseMatrix () const
 
void getInverseMatrix (Real m[4][4]) const
 
void getInverseMatrix (Real m[16]) const
 
void getInverseRotationMatrix (Real m[3][3]) const
 

Static Public Member Functions

Random TQuaternion
static TQuaternion randomQuaternion ()
 

XML representation

std::ostream & operator<< (std::ostream &o, const TQuaternion &Q)
 

Accessing values

Vec3Real axis () const
 
Real angle () const
 
void getAxisAngle (Vec3Real &axis, Real &angle) const
 
Real operator[] (int i) const
 
Real & operator[] (int i)
 
TQuaternion operator* (const Real a, const TQuaternion &b)
 

Rotation computations

TQuaternionoperator*= (const TQuaternion &q)
 
Vec3Real rotate (const Vec3Real &v) const
 
Vec3Real inverseRotate (const Vec3Real &v) const
 
TQuaternion operator* (const TQuaternion &a, const TQuaternion &b)
 
Vec3Real operator* (const TQuaternion &q, const Vec3Real &v)
 

Slerp interpolation

TQuaternion log ()
 
TQuaternion exp ()
 
static TQuaternion slerp (const TQuaternion &a, const TQuaternion &b, Real t, bool allowFlip=true)
 
static TQuaternion squad (const TQuaternion &a, const TQuaternion &tgA, const TQuaternion &tgB, const TQuaternion &b, Real t)
 
static Real dot (const TQuaternion &a, const TQuaternion &b)
 
static TQuaternion lnDif (const TQuaternion &a, const TQuaternion &b)
 
static TQuaternion squadTangent (const TQuaternion &before, const TQuaternion &center, const TQuaternion &after)
 

Detailed Description

template<typename Real, typename Vec3Real>
class TQuaternion< Real, Vec3Real >

A Quaternion class.

Definition at line 36 of file quaternion.h.

Constructor & Destructor Documentation

◆ TQuaternion() [1/4]

template<typename Real , typename Vec3Real >
TQuaternion< Real, Vec3Real >::TQuaternion ( )
inline

Default constructor, builds an identity rotation.

Definition at line 42 of file quaternion.h.

43  { q[0]=q[1]=q[2]=0.0; q[3]=1.0; }

◆ TQuaternion() [2/4]

template<typename Real , typename Vec3Real >
TQuaternion< Real, Vec3Real >::TQuaternion ( const Vec3Real &  axis,
const Real  angle 
)
inline

Constructor from rotation axis (non null) and angle (in radians). See also setAxisAngle().

Definition at line 46 of file quaternion.h.

47  {
48  setAxisAngle(axis, angle);
49  }
void setAxisAngle(const Vec3Real &axis, const Real angle)
Definition: quaternion.h:116

◆ TQuaternion() [3/4]

template<typename Real , typename Vec3Real >
TQuaternion< Real, Vec3Real >::TQuaternion ( Real  q0,
Real  q1,
Real  q2,
Real  q3 
)
inline

Constructor from the four values of a TQuaternion. First three values are axis*sin(angle/2) and last one is cos(angle/2).

Attention
The identity TQuaternion is TQuaternion(0,0,0,1) and not TQuaternion(0,0,0,0) (which is not unitary). The default TQuaternion() creates such identity TQuaternion.

Definition at line 90 of file quaternion.h.

91  { q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }

◆ TQuaternion() [4/4]

template<typename Real , typename Vec3Real >
TQuaternion< Real, Vec3Real >::TQuaternion ( const TQuaternion< Real, Vec3Real > &  Q)
inline

Copy constructor.

Definition at line 94 of file quaternion.h.

95  { for (int i=0; i<4; ++i) q[i] = Q.q[i]; }

Member Function Documentation

◆ operator=()

template<typename Real , typename Vec3Real >
TQuaternion& TQuaternion< Real, Vec3Real >::operator= ( const TQuaternion< Real, Vec3Real > &  Q)
inline

Equal operator.

Definition at line 98 of file quaternion.h.

99  {
100  for (int i=0; i<4; ++i)
101  q[i] = Q.q[i];
102  return (*this);
103  }

◆ setAxisAngle()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::setAxisAngle ( const Vec3Real &  axis,
const Real  angle 
)
inline

Sets the TQuaternion as a rotation of axis axis and angle angle (in radians).

axis does not need to be normalized. A null axis will result in an identity TQuaternion.

Definition at line 116 of file quaternion.h.

117  {
118  const Real norm = length(axis); // axis.norm();
119  if (norm < 1E-8)
120  {
121  // Null rotation
122  q[0] = 0.0;
123  q[1] = 0.0;
124  q[2] = 0.0;
125  q[3] = 1.0;
126  }
127  else
128  {
129  const Real sin_half_angle = sin(angle / 2.0);
130  q[0] = sin_half_angle*axis.x/norm;
131  q[1] = sin_half_angle*axis.y/norm;
132  q[2] = sin_half_angle*axis.z/norm;
133  q[3] = cos(angle / 2.0);
134  }
135  }
float length(const Vector &v)
renvoie la longueur d'un vecteur.
Definition: vec.cpp:142

◆ setValue()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::setValue ( Real  q0,
Real  q1,
Real  q2,
Real  q3 
)
inline

Sets the TQuaternion value. See the TQuaternion(Real, Real, Real, Real) constructor documentation.

Definition at line 138 of file quaternion.h.

139  { q[0]=q0; q[1]=q1; q[2]=q2; q[3]=q3; }

◆ operator[]() [1/2]

template<typename Real , typename Vec3Real >
Real TQuaternion< Real, Vec3Real >::operator[] ( int  i) const
inline

Bracket operator, with a constant return value. i must range in [0..3]. See the TQuaternion(Real, Real, Real, Real) documentation.

Definition at line 243 of file quaternion.h.

243 { return q[i]; }

◆ operator[]() [2/2]

template<typename Real , typename Vec3Real >
Real& TQuaternion< Real, Vec3Real >::operator[] ( int  i)
inline

Bracket operator returning an l-value. i must range in [0..3]. See the TQuaternion(Real, Real, Real, Real) documentation.

Definition at line 246 of file quaternion.h.

246 { return q[i]; }

◆ operator*=()

template<typename Real , typename Vec3Real >
TQuaternion& TQuaternion< Real, Vec3Real >::operator*= ( const TQuaternion< Real, Vec3Real > &  q)
inline

TQuaternion rotation is composed with q.

See operator*(), since this is equivalent to this = this * q.

Note
For efficiency reasons, the resulting TQuaternion is not normalized. You may normalize() it after each application in case of numerical drift.

Definition at line 282 of file quaternion.h.

283  {
284  *this = (*this)*q;
285  return *this;
286  }

◆ inverse()

template<typename Real , typename Vec3Real >
TQuaternion TQuaternion< Real, Vec3Real >::inverse ( ) const
inline

Returns the inverse TQuaternion (inverse rotation).

Result has a negated axis() direction and the same angle(). A composition (see operator*()) of a TQuaternion and its inverse() results in an identity function.

Use invert() to actually modify the TQuaternion.

Definition at line 337 of file quaternion.h.

337 { return TQuaternion(-q[0], -q[1], -q[2], q[3]); }

◆ invert()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::invert ( )
inline

Inverses the TQuaternion (same rotation angle(), but negated axis()).

See also inverse().

Definition at line 342 of file quaternion.h.

342 { q[0] = -q[0]; q[1] = -q[1]; q[2] = -q[2]; }

◆ negate()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::negate ( )
inline

Negates all the coefficients of the TQuaternion.

This results in an other representation of the same rotation (opposite rotation angle, but with a negated axis direction: the two cancel out). However, note that the results of axis() and angle() are unchanged after a call to this method since angle() always returns a value in [0,pi].

This method is mainly useful for TQuaternion interpolation, so that the spherical interpolation takes the shortest path on the unit sphere. See slerp() for details.

Definition at line 352 of file quaternion.h.

352 { invert(); q[3] = -q[3]; }
void invert()
Definition: quaternion.h:342

◆ normalize()

template<typename Real , typename Vec3Real >
Real TQuaternion< Real, Vec3Real >::normalize ( )
inline

Normalizes the TQuaternion coefficients.

This method should not need to be called since we only deal with unit TQuaternions. This is however useful to prevent numerical drifts, especially with small rotational increments.

Definition at line 358 of file quaternion.h.

359  {
360  const Real norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
361  for (int i=0; i<4; ++i)
362  q[i] /= norm;
363  return norm;
364  }

◆ matrix()

template<typename Real , typename Vec3Real >
const Real* TQuaternion< Real, Vec3Real >::matrix ( ) const
inline

Returns the TQuaternion associated 4x4 OpenGL rotation matrix.

Use glMultMatrixd(q.matrix()) or glLoadMatrixd(q.matrix()) to apply the TQuaternion rotation to the current OpenGL matrix.

See also getMatrix(), getRotationMatrix() and inverseMatrix().

Attention
The result is only valid until the next call to matrix(). Use it immediately (as shown above) or consider using getMatrix() instead.
The matrix is given in OpenGL format (row-major order) and is the transpose of the actual mathematical European representation. Consider using getRotationMatrix() instead.

Definition at line 381 of file quaternion.h.

382  {
383  static Real m[4][4];
384  getMatrix44(m);
385  return (const Real*)(m);
386  }
void getMatrix44(MAT &m) const
Definition: quaternion.h:395

◆ getMatrix44()

template<typename Real , typename Vec3Real >
template<typename MAT >
void TQuaternion< Real, Vec3Real >::getMatrix44 ( MAT &  m) const
inline

Fills m with the OpenGL representation of the TQuaternion rotation.

Use matrix() if you do not need to store this matrix and simply want to alter the current OpenGL matrix. See also getInverseMatrix() and Frame::getMatrix().

Definition at line 395 of file quaternion.h.

396  {
397  const Real q00 = 2.0l * q[0] * q[0];
398  const Real q11 = 2.0l * q[1] * q[1];
399  const Real q22 = 2.0l * q[2] * q[2];
400 
401  const Real q01 = 2.0l * q[0] * q[1];
402  const Real q02 = 2.0l * q[0] * q[2];
403  const Real q03 = 2.0l * q[0] * q[3];
404 
405  const Real q12 = 2.0l * q[1] * q[2];
406  const Real q13 = 2.0l * q[1] * q[3];
407 
408  const Real q23 = 2.0l * q[2] * q[3];
409 
410  m[0][0] = 1.0l - q11 - q22;
411  m[1][0] = q01 - q23;
412  m[2][0] = q02 + q13;
413 
414  m[0][1] = q01 + q23;
415  m[1][1] = 1.0l - q22 - q00;
416  m[2][1] = q12 - q03;
417 
418  m[0][2] = q02 - q13;
419  m[1][2] = q12 + q03;
420  m[2][2] = 1.0l - q11 - q00;
421 
422  m[0][3] = 0.0l;
423  m[1][3] = 0.0l;
424  m[2][3] = 0.0l;
425 
426  m[3][0] = 0.0l;
427  m[3][1] = 0.0l;
428  m[3][2] = 0.0l;
429  m[3][3] = 1.0l;
430  }

◆ getMatrix33()

template<typename Real , typename Vec3Real >
template<typename MAT >
void TQuaternion< Real, Vec3Real >::getMatrix33 ( MAT &  m) const
inline

Fills m with the OpenGL representation of the TQuaternion rotation.

Use matrix() if you do not need to store this matrix and simply want to alter the current OpenGL matrix. See also getInverseMatrix() and Frame::getMatrix().

Definition at line 438 of file quaternion.h.

439  {
440  const Real q00 = 2.0l * q[0] * q[0];
441  const Real q11 = 2.0l * q[1] * q[1];
442  const Real q22 = 2.0l * q[2] * q[2];
443 
444  const Real q01 = 2.0l * q[0] * q[1];
445  const Real q02 = 2.0l * q[0] * q[2];
446  const Real q03 = 2.0l * q[0] * q[3];
447 
448  const Real q12 = 2.0l * q[1] * q[2];
449  const Real q13 = 2.0l * q[1] * q[3];
450 
451  const Real q23 = 2.0l * q[2] * q[3];
452 
453  m[0][0] = 1.0l - q11 - q22;
454  m[1][0] = q01 - q23;
455  m[2][0] = q02 + q13;
456 
457  m[0][1] = q01 + q23;
458  m[1][1] = 1.0l - q22 - q00;
459  m[2][1] = q12 - q03;
460 
461  m[0][2] = q02 - q13;
462  m[1][2] = q12 + q03;
463  m[2][2] = 1.0l - q11 - q00;
464  }

◆ getRotationMatrix()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::getRotationMatrix ( Real  m[3][3]) const
inline

Fills m with the 3x3 rotation matrix associated with the TQuaternion. See also getInverseRotationMatrix().

Attention
m uses the European mathematical representation of the rotation matrix. Use matrix() and getMatrix() to retrieve the OpenGL transposed version.

Definition at line 481 of file quaternion.h.

482  {
483  static Real mat[4][4];
484  getMatrix(mat);
485  for (int i=0; i<3; ++i)
486  for (int j=0; j<3; ++j)
487  // Beware of transposition
488  m[i][j] = mat[j][i];
489  }

◆ inverseMatrix()

template<typename Real , typename Vec3Real >
const Real* TQuaternion< Real, Vec3Real >::inverseMatrix ( ) const
inline

Returns the associated 4x4 OpenGL inverse rotation matrix. This is simply the matrix() of the inverse().

Attention
The result is only valid until the next call to inverseMatrix(). Use it immediately (as in glMultMatrixd(q.inverseMatrix())) or use getInverseMatrix() instead.
The matrix is given in OpenGL format (row-major order) and is the transpose of the actual mathematical European representation. Consider using getInverseRotationMatrix() instead.

Definition at line 497 of file quaternion.h.

498  {
499  static Real m[4][4];
500  getInverseMatrix(m);
501  return (const Real*)(m);
502  }
void getInverseMatrix(Real m[4][4]) const
Definition: quaternion.h:508

◆ getInverseMatrix()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::getInverseMatrix ( Real  m[4][4]) const
inline

Fills m with the OpenGL matrix corresponding to the inverse() rotation.

Use inverseMatrix() if you do not need to store this matrix and simply want to alter the current OpenGL matrix. See also getMatrix().

Definition at line 508 of file quaternion.h.

509  {
510  inverse().getMatrix(m);
511  }
TQuaternion inverse() const
Definition: quaternion.h:337

◆ getInverseRotationMatrix()

template<typename Real , typename Vec3Real >
void TQuaternion< Real, Vec3Real >::getInverseRotationMatrix ( Real  m[3][3]) const
inline

m is set to the 3x3 inverse rotation matrix associated with the TQuaternion.

Attention
This is the classical mathematical rotation matrix. The OpenGL format uses its transposed version. See inverseMatrix() and getInverseMatrix().

Definition at line 524 of file quaternion.h.

525  {
526  static Real mat[4][4];
527  getInverseMatrix(mat);
528  for (int i=0; i<3; ++i)
529  for (int j=0; j<3; ++j)
530  // Beware of transposition
531  m[i][j] = mat[j][i];
532  }

◆ dot()

template<typename Real , typename Vec3Real >
static Real TQuaternion< Real, Vec3Real >::dot ( const TQuaternion< Real, Vec3Real > &  a,
const TQuaternion< Real, Vec3Real > &  b 
)
inlinestatic

Returns the "dot" product of a and b: a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3].

Definition at line 576 of file quaternion.h.

576 { return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]; }

Friends And Related Function Documentation

◆ operator* [1/3]

template<typename Real , typename Vec3Real >
TQuaternion operator* ( const Real  a,
const TQuaternion< Real, Vec3Real > &  b 
)
friend

multiplication

Definition at line 252 of file quaternion.h.

253  {
254  return TQuaternion<Real,Vec3Real>(a*b[0],a*b[1],a*b[2],a*b[3]);
255  }
A Quaternion class.
Definition: quaternion.h:37

◆ operator* [2/3]

template<typename Real , typename Vec3Real >
TQuaternion operator* ( const TQuaternion< Real, Vec3Real > &  a,
const TQuaternion< Real, Vec3Real > &  b 
)
friend

Returns the composition of the a and b rotations. The order is important. When applied to a Vec v (see operator*(const TQuaternion&, const Vec&) and rotate()) the resulting TQuaternion acts as if b was applied first and then a was applied. This is obvious since the image v' of v by the composited rotation satisfies:

v'= (a*b) * v = a * (b*v)

Note that a*b usually differs from b*a.

Attention
For efficiency reasons, the resulting TQuaternion is not normalized. Use normalize() in case of numerical drift with small rotation composition.

Definition at line 268 of file quaternion.h.

269  {
270  return TQuaternion(a.q[3]*b.q[0] + b.q[3]*a.q[0] + a.q[1]*b.q[2] - a.q[2]*b.q[1],
271  a.q[3]*b.q[1] + b.q[3]*a.q[1] + a.q[2]*b.q[0] - a.q[0]*b.q[2],
272  a.q[3]*b.q[2] + b.q[3]*a.q[2] + a.q[0]*b.q[1] - a.q[1]*b.q[0],
273  a.q[3]*b.q[3] - b.q[0]*a.q[0] - a.q[1]*b.q[1] - a.q[2]*b.q[2]);
274  }

◆ operator* [3/3]

template<typename Real , typename Vec3Real >
Vec3Real operator* ( const TQuaternion< Real, Vec3Real > &  q,
const Vec3Real &  v 
)
friend

Returns the image of v by the rotation q.

Same as q.rotate(v). See rotate() and inverseRotate().

Definition at line 291 of file quaternion.h.

292  {
293  return q.rotate(v);
294  }

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