gKit2 light
Loading...
Searching...
No Matches
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/5]

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/5]

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/5]

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

Definition at line 51 of file quaternion.h.

52 {
53 const Real epsilon = 1E-10f;
54
55 const Real fromSqNorm = from.squaredNorm();
56 const Real toSqNorm = to.squaredNorm();
57 // Identity TQuaternion when one vector is null
58 if ((fromSqNorm < epsilon) || (toSqNorm < epsilon))
59 {
60 q[0]=q[1]=q[2]=0.0;
61 q[3]=1.0;
62 }
63 else
64 {
65 Vec3Real axis = cross(from, to);
66 const Real axisSqNorm = axis.squaredNorm();
67
68 // Aligned vectors, pick any axis, not aligned with from or to
69 if (axisSqNorm < epsilon)
70 {
71 axis = Vec3Real(1.0, 0.0, 0.0);
72 if (axis*from < 0.1*sqrt(fromSqNorm))
73 axis = Vec3Real(0.0, 1.0, 0.0);
74 }
75
77
78 if (from*to < 0.0)
79 angle = M_PI-angle;
80
81 setAxisAngle(axis, angle);
82 }
83 }
A Quaternion class.
Definition quaternion.h:37
Vector cross(const Vector &u, const Vector &v)
renvoie le produit vectoriel de 2 vecteurs.
Definition vec.cpp:173

◆ TQuaternion() [4/5]

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() [5/5]

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 }

◆ operator+=()

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

Definition at line 105 of file quaternion.h.

106 {
107 for (int i = 0; i < 4; ++i)
108 q[i] += Q.q[i];
109 return (*this);
110 }

◆ 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 }

◆ 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; }

◆ setFromRotationMatrix()

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

Definition at line 143 of file quaternion.h.

144 {
145 // First, find largest diagonal in matrix:
146 int i = 2;
147 if (m[0][0] > m[1][1])
148 {
149 if (m[0][0] > m[2][2])
150 {
151 i = 0;
152 }
153 }
154 else
155 {
156 if (m[1][1] > m[2][2])
157 {
158 i = 1;
159 }
160 }
161
162 if (m[0][0]+m[1][1]+m[2][2] > m[i][i])
163 {
164 // Compute w first:
165 q[3] = sqrt(m[0][0]+m[1][1]+m[2][2]+1.0)/2.0;
166 // And compute other values:
167 q[0] = (m[2][1]-m[1][2])/(4.0*q[3]);
168 q[1] = (m[0][2]-m[2][0])/(4.0*q[3]);
169 q[2] = (m[1][0]-m[0][1])/(4.0*q[3]);
170 }
171 else
172 {
173 // Compute x, y, or z first:
174 int j = (i+1)%3;
175 int k = (i+2)%3;
176
177 // Compute first value:
178 q[i] = sqrt(m[i][i]-m[j][j]-m[k][k]+1.0)/2.0;
179
180 // And the others:
181 q[j] = (m[i][j]+m[j][i])/(4.0*q[i]);
182 q[k] = (m[i][k]+m[k][i])/(4.0*q[i]);
183
184 q[3] = (m[k][j]-m[j][k])/(4.0*q[i]);
185 }
186 }

◆ setFromRotatedBase()

template<typename Real, typename Vec3Real>
void TQuaternion< Real, Vec3Real >::setFromRotatedBase ( const Vec3Real & X,
const Vec3Real & Y,
const Vec3Real & Z )
inline

Definition at line 188 of file quaternion.h.

189 {
190 Real m[3][3];
191 Real normX = X.norm();
192 Real normY = Y.norm();
193 Real normZ = Z.norm();
194
195 for (int i=0; i<3; ++i)
196 {
197 m[i][0] = X[i] / normX;
198 m[i][1] = Y[i] / normY;
199 m[i][2] = Z[i] / normZ;
200 }
201 setFromRotationMatrix(m);
202 }

◆ axis()

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

Definition at line 209 of file quaternion.h.

210 {
211 Vec3Real res = Vec3Real(q[0], q[1], q[2]);
212 const Real sinus = res.norm();
213 if (sinus > 1E-8)
214 res /= sinus;
215 return (acos(q[3]) <= M_PI/2.0) ? res : -res;
216 }

◆ angle()

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

Definition at line 219 of file quaternion.h.

220 {
221 const Real angle = 2.0 * acos(q[3]);
222 return (angle <= M_PI) ? angle : 2.0*M_PI - angle;
223 }

◆ getAxisAngle()

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

Definition at line 226 of file quaternion.h.

227 {
228 angle = 2.0*acos(q[3]);
229 axis = Vec3Real(q[0], q[1], q[2]);
230 const Real sinus = axis.norm();
231 if (sinus > 1E-8)
232 axis /= sinus;
233
234 if (angle > M_PI)
235 {
236 angle = 2.0*M_PI - angle;
237 axis = -axis;
238 }
239 }

◆ 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 }

◆ rotate()

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

Definition at line 298 of file quaternion.h.

299 {
300 const Real q00 = 2.0l * q[0] * q[0];
301 const Real q11 = 2.0l * q[1] * q[1];
302 const Real q22 = 2.0l * q[2] * q[2];
303
304 const Real q01 = 2.0l * q[0] * q[1];
305 const Real q02 = 2.0l * q[0] * q[2];
306 const Real q03 = 2.0l * q[0] * q[3];
307
308 const Real q12 = 2.0l * q[1] * q[2];
309 const Real q13 = 2.0l * q[1] * q[3];
310
311 const Real q23 = 2.0l * q[2] * q[3];
312
313 return Vec3Real((1.0 - q11 - q22)*v.x + ( q01 - q23)*v.y + ( q02 + q13)*v.z,
314 ( q01 + q23)*v.x + (1.0 - q22 - q00)*v.y + ( q12 - q03)*v.z,
315 ( q02 - q13)*v.x + ( q12 + q03)*v.y + (1.0 - q11 - q00)*v.z );
316 }

◆ inverseRotate()

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

Definition at line 319 of file quaternion.h.

320 {
321 return inverse().rotate(v);
322 }
TQuaternion inverse() const
Definition quaternion.h:337

◆ 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 }

◆ getMatrix16()

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

Definition at line 466 of file quaternion.h.

467 {
468 static Real mat[4][4];
470 int count = 0;
471 for (int i=0; i<4; ++i)
472 for (int j=0; j<4; ++j)
473 m[count++] = mat[i][j];
474 }

◆ 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];
501 return (const Real*)(m);
502 }
void getInverseMatrix(Real m[4][4]) const
Definition quaternion.h:508

◆ getInverseMatrix() [1/2]

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 }

◆ getInverseMatrix() [2/2]

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

Definition at line 514 of file quaternion.h.

515 {
516 inverse().getMatrix(m);
517 }

◆ 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];
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 }

◆ slerp()

template<typename Real, typename Vec3Real>
TQuaternion TQuaternion< Real, Vec3Real >::slerp ( const TQuaternion< Real, Vec3Real > & a,
const TQuaternion< Real, Vec3Real > & b,
Real t,
bool allowFlip = true )
inlinestatic

Definition at line 538 of file quaternion.h.

539 {
541
542 Real c1, c2;
543 // Linear interpolation for close orientations
544 if ((1.0 - fabs(cosAngle)) < 0.01)
545 {
546 c1 = 1.0 - t;
547 c2 = t;
548 }
549 else
550 {
551 // Spherical interpolation
552 Real angle = acos(fabs(cosAngle));
553 Real sinAngle = sin(angle);
554 c1 = sin(angle * (1.0 - t)) / sinAngle;
555 c2 = sin(angle * t) / sinAngle;
556 }
557
558 // Use the shortest path
559 if (allowFlip && (cosAngle < 0.0))
560 c1 = -c1;
561
562 return TQuaternion(c1*a[0] + c2*b[0], c1*a[1] + c2*b[1], c1*a[2] + c2*b[2], c1*a[3] + c2*b[3]);
563 }
static Real dot(const TQuaternion &a, const TQuaternion &b)
Definition quaternion.h:576

◆ squad()

template<typename Real, typename Vec3Real>
TQuaternion TQuaternion< Real, Vec3Real >::squad ( const TQuaternion< Real, Vec3Real > & a,
const TQuaternion< Real, Vec3Real > & tgA,
const TQuaternion< Real, Vec3Real > & tgB,
const TQuaternion< Real, Vec3Real > & b,
Real t )
inlinestatic

Definition at line 566 of file quaternion.h.

567 {
570 return TQuaternion::slerp(ab, tg, 2.0*t*(1.0-t), false);
571 }

◆ dot()

template<typename Real, typename Vec3Real>
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]; }

◆ log()

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

Definition at line 578 of file quaternion.h.

579 {
580 Real len = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2]);
581
582 if (len < 1E-6)
583 return TQuaternion(q[0], q[1], q[2], 0.0);
584 else
585 {
586 Real coef = acos(q[3]) / len;
587 return TQuaternion(q[0]*coef, q[1]*coef, q[2]*coef, 0.0);
588 }
589 }

◆ exp()

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

Definition at line 592 of file quaternion.h.

593 {
594 Real theta = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2]);
595
596 if (theta < 1E-6)
597 return TQuaternion(q[0], q[1], q[2], cos(theta));
598 else
599 {
600 Real coef = sin(theta) / theta;
601 return TQuaternion(q[0]*coef, q[1]*coef, q[2]*coef, cos(theta));
602 }
603 }

◆ lnDif()

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

Definition at line 606 of file quaternion.h.

607 {
609 dif.normalize();
610 return dif.log();
611 }
Real normalize()
Definition quaternion.h:358

◆ squadTangent()

template<typename Real, typename Vec3Real>
TQuaternion TQuaternion< Real, Vec3Real >::squadTangent ( const TQuaternion< Real, Vec3Real > & before,
const TQuaternion< Real, Vec3Real > & center,
const TQuaternion< Real, Vec3Real > & after )
inlinestatic

Definition at line 614 of file quaternion.h.

615 {
619 for (int i=0; i<4; ++i)
620 e.q[i] = -0.25 * (l1.q[i] + l2.q[i]);
621 e = center*(e.exp());
622
623 // if (TQuaternion::dot(e,b) < 0.0)
624 // e.negate();
625
626 return e;
627 }

◆ randomQuaternion()

template<typename Real, typename Vec3Real>
TQuaternion TQuaternion< Real, Vec3Real >::randomQuaternion ( )
inlinestatic

Definition at line 632 of file quaternion.h.

633 {
634 // The rand() function is not very portable and may not be available on your system.
635 // Add the appropriate include or replace by an other random function in case of problem.
637 Real r1 = sqrt(1.0 - seed);
638 Real r2 = sqrt(seed);
639 Real t1 = 2.0 * M_PI * (rand()/(Real)RAND_MAX);
640 Real t2 = 2.0 * M_PI * (rand()/(Real)RAND_MAX);
641 return TQuaternion(sin(t1)*r1, cos(t1)*r1, sin(t2)*r2, cos(t2)*r2);
642 }

◆ 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 }

◆ 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 }

◆ operator<<

template<typename Real, typename Vec3Real>
std::ostream & operator<< ( std::ostream & o,
const TQuaternion< Real, Vec3Real > & Q )
friend

Definition at line 665 of file quaternion.h.

666 {
667 o << "(" << Q[0] << ',' << Q[1] << ',' << Q[2] << ',' << Q[3] << ")";
668 return o;
669 }

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