arsa  2.7
Public Member Functions | Public Attributes | List of all members
irr::core::quaternion Class Reference

Quaternion class for representing rotations. More...

#include <quaternion.h>

Public Member Functions

 quaternion ()
 Default Constructor. More...
 
 quaternion (f32 x, f32 y, f32 z, f32 w)
 Constructor. More...
 
 quaternion (f32 x, f32 y, f32 z)
 Constructor which converts Euler angles (radians) to a quaternion. More...
 
 quaternion (const vector3df &vec)
 Constructor which converts Euler angles (radians) to a quaternion. More...
 
 quaternion (const matrix4 &mat)
 Constructor which converts a matrix to a quaternion. More...
 
bool operator== (const quaternion &other) const
 Equality operator. More...
 
bool operator!= (const quaternion &other) const
 inequality operator More...
 
quaternionoperator= (const quaternion &other)
 Assignment operator. More...
 
quaternionoperator= (const matrix4 &other)
 Matrix assignment operator. More...
 
quaternion operator+ (const quaternion &other) const
 Add operator. More...
 
quaternion operator * (const quaternion &other) const
 
quaternion operator * (f32 s) const
 Multiplication operator with scalar. More...
 
quaternionoperator *= (f32 s)
 Multiplication operator with scalar. More...
 
vector3df operator * (const vector3df &v) const
 Multiplication operator. More...
 
quaternionoperator *= (const quaternion &other)
 Multiplication operator. More...
 
f32 dotProduct (const quaternion &other) const
 Calculates the dot product. More...
 
quaternionset (f32 x, f32 y, f32 z, f32 w)
 Sets new quaternion. More...
 
quaternionset (f32 x, f32 y, f32 z)
 Sets new quaternion based on Euler angles (radians) More...
 
quaternionset (const core::vector3df &vec)
 Sets new quaternion based on Euler angles (radians) More...
 
quaternionset (const core::quaternion &quat)
 Sets new quaternion from other quaternion. More...
 
bool equals (const quaternion &other, const f32 tolerance=ROUNDING_ERROR_f32) const
 returns if this quaternion equals the other one, taking floating point rounding errors into account More...
 
quaternionnormalize ()
 Normalizes the quaternion. More...
 
matrix4 getMatrix () const
 Creates a matrix from this quaternion. More...
 
void getMatrixFast (matrix4 &dest) const
 Faster method to create a rotation matrix, you should normalize the quaternion before! More...
 
void getMatrix (matrix4 &dest, const core::vector3df &translation=core::vector3df()) const
 Creates a matrix from this quaternion. More...
 
void getMatrixCenter (matrix4 &dest, const core::vector3df &center, const core::vector3df &translation) const
 
void getMatrix_transposed (matrix4 &dest) const
 Creates a matrix from this quaternion. More...
 
quaternionmakeInverse ()
 Inverts this quaternion. More...
 
quaternionlerp (quaternion q1, quaternion q2, f32 time)
 Set this quaternion to the linear interpolation between two quaternions. More...
 
quaternionlerpN (quaternion q1, quaternion q2, f32 time)
 Set this quaternion to the linear interpolation between two quaternions and normalize the result. More...
 
quaternionslerp (quaternion q1, quaternion q2, f32 time, f32 threshold=.05f)
 Set this quaternion to the result of the spherical interpolation between two quaternions. More...
 
quaternionfromAngleAxis (f32 angle, const vector3df &axis)
 Set this quaternion to represent a rotation from angle and axis. More...
 
void toAngleAxis (f32 &angle, core::vector3df &axis) const
 Fills an angle (radians) around an axis (unit vector) More...
 
void toEuler (vector3df &euler) const
 Output this quaternion to an Euler angle (radians) More...
 
quaternionmakeIdentity ()
 Set quaternion to identity. More...
 
quaternionrotationFromTo (const vector3df &from, const vector3df &to)
 Set quaternion to represent a rotation from one vector to another. More...
 

Public Attributes

f32 X
 Quaternion elements. More...
 
f32 Y
 
f32 Z
 
f32 W
 

Detailed Description

Quaternion class for representing rotations.

It provides cheap combinations and avoids gimbal locks. Also useful for interpolations.

Definition at line 31 of file quaternion.h.

Constructor & Destructor Documentation

◆ quaternion() [1/5]

irr::core::quaternion::quaternion ( )
inline

Default Constructor.

Definition at line 36 of file quaternion.h.

36 : X(0.0f), Y(0.0f), Z(0.0f), W(1.0f) {}
GLfloat f
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ quaternion() [2/5]

irr::core::quaternion::quaternion ( f32  x,
f32  y,
f32  z,
f32  w 
)
inline

Constructor.

Definition at line 39 of file quaternion.h.

39 : X(x), Y(y), Z(z), W(w) { }
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLubyte GLubyte GLubyte GLubyte w
GLdouble GLdouble z

◆ quaternion() [3/5]

irr::core::quaternion::quaternion ( f32  x,
f32  y,
f32  z 
)
inline

Constructor which converts Euler angles (radians) to a quaternion.

Definition at line 208 of file quaternion.h.

209 {
210  set(x,y,z);
211 }
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLdouble GLdouble z
quaternion & set(f32 x, f32 y, f32 z, f32 w)
Sets new quaternion.
Definition: quaternion.h:520

◆ quaternion() [4/5]

irr::core::quaternion::quaternion ( const vector3df vec)
inline

Constructor which converts Euler angles (radians) to a quaternion.

Definition at line 215 of file quaternion.h.

216 {
217  set(vec.X,vec.Y,vec.Z);
218 }
quaternion & set(f32 x, f32 y, f32 z, f32 w)
Sets new quaternion.
Definition: quaternion.h:520

◆ quaternion() [5/5]

irr::core::quaternion::quaternion ( const matrix4 mat)
inline

Constructor which converts a matrix to a quaternion.

Definition at line 222 of file quaternion.h.

223 {
224  (*this) = mat;
225 }

Member Function Documentation

◆ dotProduct()

f32 irr::core::quaternion::dotProduct ( const quaternion other) const
inline

Calculates the dot product.

Definition at line 631 of file quaternion.h.

632 {
633  return (X * q2.X) + (Y * q2.Y) + (Z * q2.Z) + (W * q2.W);
634 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ equals()

bool irr::core::quaternion::equals ( const quaternion other,
const f32  tolerance = ROUNDING_ERROR_f32 
) const
inline

returns if this quaternion equals the other one, taking floating point rounding errors into account

Definition at line 574 of file quaternion.h.

575 {
576  return core::equals( X, other.X, tolerance) &&
577  core::equals( Y, other.Y, tolerance) &&
578  core::equals( Z, other.Z, tolerance) &&
579  core::equals( W, other.W, tolerance);
580 }
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ fromAngleAxis()

quaternion & irr::core::quaternion::fromAngleAxis ( f32  angle,
const vector3df axis 
)
inline

Set this quaternion to represent a rotation from angle and axis.

axis must be unit length, angle in radians

Axis must be unit length. The quaternion representing the rotation is q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k).

Parameters
angleRotation Angle in radians.
axisRotation axis.

Definition at line 638 of file quaternion.h.

639 {
640  const f32 fHalfAngle = 0.5f*angle;
641  const f32 fSin = sinf(fHalfAngle);
642  W = cosf(fHalfAngle);
643  X = fSin*axis.X;
644  Y = fSin*axis.Y;
645  Z = fSin*axis.Z;
646  return *this;
647 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLfloat angle

◆ getMatrix() [1/2]

matrix4 irr::core::quaternion::getMatrix ( ) const
inline

Creates a matrix from this quaternion.

Definition at line 359 of file quaternion.h.

360 {
362  getMatrix(m);
363  return m;
364 }
const GLfloat * m
matrix4 getMatrix() const
Creates a matrix from this quaternion.
Definition: quaternion.h:359
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition: matrix4.h:2377

◆ getMatrix() [2/2]

void irr::core::quaternion::getMatrix ( matrix4 dest,
const core::vector3df center = core::vector3df() 
) const
inline

Creates a matrix from this quaternion.

Creates a matrix from this quaternion

Definition at line 399 of file quaternion.h.

401 {
402  // ok creating a copy may be slower, but at least avoid internal
403  // state chance (also because otherwise we cannot keep this method "const").
404 
405  quaternion q( *this);
406  q.normalize();
407  f32 X = q.X;
408  f32 Y = q.Y;
409  f32 Z = q.Z;
410  f32 W = q.W;
411 
412  dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
413  dest[1] = 2.0f*X*Y + 2.0f*Z*W;
414  dest[2] = 2.0f*X*Z - 2.0f*Y*W;
415  dest[3] = 0.0f;
416 
417  dest[4] = 2.0f*X*Y - 2.0f*Z*W;
418  dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
419  dest[6] = 2.0f*Z*Y + 2.0f*X*W;
420  dest[7] = 0.0f;
421 
422  dest[8] = 2.0f*X*Z + 2.0f*Y*W;
423  dest[9] = 2.0f*Z*Y - 2.0f*X*W;
424  dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
425  dest[11] = 0.0f;
426 
427  dest[12] = center.X;
428  dest[13] = center.Y;
429  dest[14] = center.Z;
430  dest[15] = 1.f;
431 
432  dest.setDefinitelyIdentityMatrix ( false );
433 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
GLdouble GLdouble GLdouble GLdouble q
Definition: SDL_opengl.h:2087
f32 X
Quaternion elements.
Definition: quaternion.h:200
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ getMatrix_transposed()

void irr::core::quaternion::getMatrix_transposed ( matrix4 dest) const
inline

Creates a matrix from this quaternion.

Definition at line 478 of file quaternion.h.

479 {
480  quaternion q(*this);
481  q.normalize();
482  f32 X = q.X;
483  f32 Y = q.Y;
484  f32 Z = q.Z;
485  f32 W = q.W;
486 
487  dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
488  dest[4] = 2.0f*X*Y + 2.0f*Z*W;
489  dest[8] = 2.0f*X*Z - 2.0f*Y*W;
490  dest[12] = 0.0f;
491 
492  dest[1] = 2.0f*X*Y - 2.0f*Z*W;
493  dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
494  dest[9] = 2.0f*Z*Y + 2.0f*X*W;
495  dest[13] = 0.0f;
496 
497  dest[2] = 2.0f*X*Z + 2.0f*Y*W;
498  dest[6] = 2.0f*Z*Y - 2.0f*X*W;
499  dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
500  dest[14] = 0.0f;
501 
502  dest[3] = 0.f;
503  dest[7] = 0.f;
504  dest[11] = 0.f;
505  dest[15] = 1.f;
506 
507  dest.setDefinitelyIdentityMatrix(false);
508 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
GLdouble GLdouble GLdouble GLdouble q
Definition: SDL_opengl.h:2087
f32 X
Quaternion elements.
Definition: quaternion.h:200
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ getMatrixCenter()

void irr::core::quaternion::getMatrixCenter ( matrix4 dest,
const core::vector3df center,
const core::vector3df translation 
) const
inline

Creates a matrix from this quaternion Rotate about a center point shortcut for core::quaternion q; q.rotationFromTo ( vin[i].Normal, forward ); q.getMatrixCenter ( lookat, center, newPos );

core::matrix4 m2; m2.setInverseTranslation ( center ); lookat *= m2;

core::matrix4 m3; m2.setTranslation ( newPos ); lookat *= m3;

Creates a matrix from this quaternion Rotate about a center point shortcut for core::quaternion q; q.rotationFromTo(vin[i].Normal, forward); q.getMatrix(lookat, center);

core::matrix4 m2; m2.setInverseTranslation(center); lookat *= m2;

Definition at line 448 of file quaternion.h.

451 {
452  quaternion q(*this);
453  q.normalize();
454  f32 X = q.X;
455  f32 Y = q.Y;
456  f32 Z = q.Z;
457  f32 W = q.W;
458 
459  dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
460  dest[1] = 2.0f*X*Y + 2.0f*Z*W;
461  dest[2] = 2.0f*X*Z - 2.0f*Y*W;
462  dest[3] = 0.0f;
463 
464  dest[4] = 2.0f*X*Y - 2.0f*Z*W;
465  dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
466  dest[6] = 2.0f*Z*Y + 2.0f*X*W;
467  dest[7] = 0.0f;
468 
469  dest[8] = 2.0f*X*Z + 2.0f*Y*W;
470  dest[9] = 2.0f*Z*Y - 2.0f*X*W;
471  dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
472  dest[11] = 0.0f;
473 
474  dest.setRotationCenter ( center, translation );
475 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
GLdouble GLdouble GLdouble GLdouble q
Definition: SDL_opengl.h:2087
f32 X
Quaternion elements.
Definition: quaternion.h:200
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ getMatrixFast()

void irr::core::quaternion::getMatrixFast ( matrix4 dest) const
inline

Faster method to create a rotation matrix, you should normalize the quaternion before!

Definition at line 368 of file quaternion.h.

369 {
370  // TODO:
371  // gpu quaternion skinning => fast Bones transform chain O_O YEAH!
372  // http://www.mrelusive.com/publications/papers/SIMD-From-Quaternion-to-Matrix-and-Back.pdf
373  dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
374  dest[1] = 2.0f*X*Y + 2.0f*Z*W;
375  dest[2] = 2.0f*X*Z - 2.0f*Y*W;
376  dest[3] = 0.0f;
377 
378  dest[4] = 2.0f*X*Y - 2.0f*Z*W;
379  dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
380  dest[6] = 2.0f*Z*Y + 2.0f*X*W;
381  dest[7] = 0.0f;
382 
383  dest[8] = 2.0f*X*Z + 2.0f*Y*W;
384  dest[9] = 2.0f*Z*Y - 2.0f*X*W;
385  dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
386  dest[11] = 0.0f;
387 
388  dest[12] = 0.f;
389  dest[13] = 0.f;
390  dest[14] = 0.f;
391  dest[15] = 1.f;
392 
393  dest.setDefinitelyIdentityMatrix(false);
394 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ lerp()

quaternion & irr::core::quaternion::lerp ( quaternion  q1,
quaternion  q2,
f32  time 
)
inline

Set this quaternion to the linear interpolation between two quaternions.

NOTE: lerp result is not a normalized quaternion. In most cases you will want to use lerpN instead as most other quaternion functions expect to work with a normalized quaternion.

Parameters
q1First quaternion to be interpolated.
q2Second quaternion to be interpolated.
timeProgress of interpolation. For time=0 the result is q1, for time=1 the result is q2. Otherwise interpolation between q1 and q2. Result is not normalized.

Definition at line 592 of file quaternion.h.

593 {
594  const f32 scale = 1.0f - time;
595  return (*this = (q1*scale) + (q2*time));
596 }
GLenum GLenum GLenum GLenum GLenum scale
float f32
32 bit floating point variable.
Definition: irrTypes.h:108

◆ lerpN()

quaternion & irr::core::quaternion::lerpN ( quaternion  q1,
quaternion  q2,
f32  time 
)
inline

Set this quaternion to the linear interpolation between two quaternions and normalize the result.

Parameters
q1First quaternion to be interpolated.
q2Second quaternion to be interpolated.
timeProgress of interpolation. For time=0 the result is q1, for time=1 the result is q2. Otherwise interpolation between q1 and q2. Result is normalized.

Definition at line 599 of file quaternion.h.

600 {
601  const f32 scale = 1.0f - time;
602  return (*this = ((q1*scale) + (q2*time)).normalize() );
603 }
GLenum GLenum GLenum GLenum GLenum scale
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
quaternion & normalize()
Normalizes the quaternion.
Definition: quaternion.h:584

◆ makeIdentity()

core::quaternion & irr::core::quaternion::makeIdentity ( )
inline

Set quaternion to identity.

Definition at line 724 of file quaternion.h.

725 {
726  W = 1.f;
727  X = 0.f;
728  Y = 0.f;
729  Z = 0.f;
730  return *this;
731 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ makeInverse()

quaternion & irr::core::quaternion::makeInverse ( )
inline

Inverts this quaternion.

Definition at line 512 of file quaternion.h.

513 {
514  X = -X; Y = -Y; Z = -Z;
515  return *this;
516 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ normalize()

quaternion & irr::core::quaternion::normalize ( )
inline

Normalizes the quaternion.

Definition at line 584 of file quaternion.h.

585 {
586  // removed conditional branch since it may slow down and anyway the condition was
587  // false even after normalization in some cases.
588  return (*this *= (f32)reciprocal_squareroot ( (f64)(X*X + Y*Y + Z*Z + W*W) ));
589 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
f32 X
Quaternion elements.
Definition: quaternion.h:200
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:521

◆ operator *() [1/3]

quaternion irr::core::quaternion::operator * ( const quaternion other) const
inline

Multiplication operator Be careful, unfortunately the operator order here is opposite of that in CMatrix4::operator*

Definition at line 315 of file quaternion.h.

316 {
317  quaternion tmp;
318 
319  tmp.W = (other.W * W) - (other.X * X) - (other.Y * Y) - (other.Z * Z);
320  tmp.X = (other.W * X) + (other.X * W) + (other.Y * Z) - (other.Z * Y);
321  tmp.Y = (other.W * Y) + (other.Y * W) + (other.Z * X) - (other.X * Z);
322  tmp.Z = (other.W * Z) + (other.Z * W) + (other.X * Y) - (other.Y * X);
323 
324  return tmp;
325 }
f32 X
Quaternion elements.
Definition: quaternion.h:200
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ operator *() [2/3]

quaternion irr::core::quaternion::operator * ( f32  s) const
inline

Multiplication operator with scalar.

Definition at line 329 of file quaternion.h.

330 {
331  return quaternion(s*X, s*Y, s*Z, s*W);
332 }
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLdouble s
Definition: SDL_opengl.h:2063
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ operator *() [3/3]

vector3df irr::core::quaternion::operator * ( const vector3df v) const
inline

Multiplication operator.

Definition at line 709 of file quaternion.h.

710 {
711  // nVidia SDK implementation
712 
713  vector3df uv, uuv;
714  const vector3df qvec(X, Y, Z);
715  uv = qvec.crossProduct(v);
716  uuv = qvec.crossProduct(uv);
717  uv *= (2.0f * W);
718  uuv *= 2.0f;
719 
720  return v + uv + uuv;
721 }
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition: vector3d.h:461
const GLdouble * v
Definition: SDL_opengl.h:2064
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ operator *=() [1/2]

quaternion & irr::core::quaternion::operator *= ( f32  s)
inline

Multiplication operator with scalar.

Definition at line 336 of file quaternion.h.

337 {
338  X*=s;
339  Y*=s;
340  Z*=s;
341  W*=s;
342  return *this;
343 }
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLdouble s
Definition: SDL_opengl.h:2063

◆ operator *=() [2/2]

quaternion & irr::core::quaternion::operator *= ( const quaternion other)
inline

Multiplication operator.

Definition at line 346 of file quaternion.h.

347 {
348  return (*this = other * (*this));
349 }

◆ operator!=()

bool irr::core::quaternion::operator!= ( const quaternion other) const
inline

inequality operator

Definition at line 238 of file quaternion.h.

239 {
240  return !(*this == other);
241 }

◆ operator+()

quaternion irr::core::quaternion::operator+ ( const quaternion other) const
inline

Add operator.

Definition at line 352 of file quaternion.h.

353 {
354  return quaternion(X+b.X, Y+b.Y, Z+b.Z, W+b.W);
355 }
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLboolean GLboolean GLboolean b
quaternion()
Default Constructor.
Definition: quaternion.h:36

◆ operator=() [1/2]

quaternion & irr::core::quaternion::operator= ( const quaternion other)
inline

Assignment operator.

Definition at line 244 of file quaternion.h.

245 {
246  X = other.X;
247  Y = other.Y;
248  Z = other.Z;
249  W = other.W;
250  return *this;
251 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ operator=() [2/2]

quaternion & irr::core::quaternion::operator= ( const matrix4 other)
inline

Matrix assignment operator.

Definition at line 255 of file quaternion.h.

256 {
257  const f32 diag = m[0] + m[5] + m[10] + 1;
258 
259  if( diag > 0.0f )
260  {
261  const f32 scale = sqrtf(diag) * 2.0f; // get scale from diagonal
262 
263  // TODO: speed this up
264  X = (m[6] - m[9]) / scale;
265  Y = (m[8] - m[2]) / scale;
266  Z = (m[1] - m[4]) / scale;
267  W = 0.25f * scale;
268  }
269  else
270  {
271  if (m[0]>m[5] && m[0]>m[10])
272  {
273  // 1st element of diag is greatest value
274  // find scale according to 1st element, and double it
275  const f32 scale = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f;
276 
277  // TODO: speed this up
278  X = 0.25f * scale;
279  Y = (m[4] + m[1]) / scale;
280  Z = (m[2] + m[8]) / scale;
281  W = (m[6] - m[9]) / scale;
282  }
283  else if (m[5]>m[10])
284  {
285  // 2nd element of diag is greatest value
286  // find scale according to 2nd element, and double it
287  const f32 scale = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f;
288 
289  // TODO: speed this up
290  X = (m[4] + m[1]) / scale;
291  Y = 0.25f * scale;
292  Z = (m[9] + m[6]) / scale;
293  W = (m[8] - m[2]) / scale;
294  }
295  else
296  {
297  // 3rd element of diag is greatest value
298  // find scale according to 3rd element, and double it
299  const f32 scale = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f;
300 
301  // TODO: speed this up
302  X = (m[8] + m[2]) / scale;
303  Y = (m[9] + m[6]) / scale;
304  Z = 0.25f * scale;
305  W = (m[1] - m[4]) / scale;
306  }
307  }
308 
309  return normalize();
310 }
GLenum GLenum GLenum GLenum GLenum scale
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
const GLfloat * m
quaternion & normalize()
Normalizes the quaternion.
Definition: quaternion.h:584
GLfloat f
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ operator==()

bool irr::core::quaternion::operator== ( const quaternion other) const
inline

Equality operator.

Definition at line 229 of file quaternion.h.

230 {
231  return ((X == other.X) &&
232  (Y == other.Y) &&
233  (Z == other.Z) &&
234  (W == other.W));
235 }
f32 X
Quaternion elements.
Definition: quaternion.h:200

◆ rotationFromTo()

core::quaternion & irr::core::quaternion::rotationFromTo ( const vector3df from,
const vector3df to 
)
inline

Set quaternion to represent a rotation from one vector to another.

Definition at line 733 of file quaternion.h.

734 {
735  // Based on Stan Melax's article in Game Programming Gems
736  // Copy, since cannot modify local
737  vector3df v0 = from;
738  vector3df v1 = to;
739  v0.normalize();
740  v1.normalize();
741 
742  const f32 d = v0.dotProduct(v1);
743  if (d >= 1.0f) // If dot == 1, vectors are the same
744  {
745  return makeIdentity();
746  }
747  else if (d <= -1.0f) // exactly opposite
748  {
749  core::vector3df axis(1.0f, 0.f, 0.f);
750  axis = axis.crossProduct(v0);
751  if (axis.getLength()==0)
752  {
753  axis.set(0.f,1.f,0.f);
754  axis = axis.crossProduct(v0);
755  }
756  // same as fromAngleAxis(core::PI, axis).normalize();
757  return set(axis.X, axis.Y, axis.Z, 0).normalize();
758  }
759 
760  const f32 s = sqrtf( (1+d)*2 ); // optimize inv_sqrt
761  const f32 invs = 1.f / s;
762  const vector3df c = v0.crossProduct(v1)*invs;
763  return set(c.X, c.Y, c.Z, s * 0.5f).normalize();
764 }
vector3d< T > crossProduct(const vector3d< T > &p) const
Calculates the cross product with another vector.
Definition: vector3d.h:161
quaternion & makeIdentity()
Set quaternion to identity.
Definition: quaternion.h:724
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
quaternion & normalize()
Normalizes the quaternion.
Definition: quaternion.h:584
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition: vector3d.h:461
GLfloat f
vector3d< T > & normalize()
Normalizes the vector.
Definition: vector3d.h:182
GLdouble s
Definition: SDL_opengl.h:2063
GLfloat v0
const GLubyte * c
GLfloat GLfloat v1
quaternion & set(f32 x, f32 y, f32 z, f32 w)
Sets new quaternion.
Definition: quaternion.h:520

◆ set() [1/4]

quaternion & irr::core::quaternion::set ( f32  x,
f32  y,
f32  z,
f32  w 
)
inline

Sets new quaternion.

Definition at line 520 of file quaternion.h.

521 {
522  X = x;
523  Y = y;
524  Z = z;
525  W = w;
526  return *this;
527 }
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLubyte GLubyte GLubyte GLubyte w
GLdouble GLdouble z

◆ set() [2/4]

quaternion & irr::core::quaternion::set ( f32  x,
f32  y,
f32  z 
)
inline

Sets new quaternion based on Euler angles (radians)

Definition at line 531 of file quaternion.h.

532 {
533  f64 angle;
534 
535  angle = x * 0.5;
536  const f64 sr = sin(angle);
537  const f64 cr = cos(angle);
538 
539  angle = y * 0.5;
540  const f64 sp = sin(angle);
541  const f64 cp = cos(angle);
542 
543  angle = z * 0.5;
544  const f64 sy = sin(angle);
545  const f64 cy = cos(angle);
546 
547  const f64 cpcy = cp * cy;
548  const f64 spcy = sp * cy;
549  const f64 cpsy = cp * sy;
550  const f64 spsy = sp * sy;
551 
552  X = (f32)(sr * cpcy - cr * spsy);
553  Y = (f32)(cr * spcy + sr * cpsy);
554  Z = (f32)(cr * cpsy - sr * spcy);
555  W = (f32)(cr * cpcy + sr * spsy);
556 
557  return normalize();
558 }
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
quaternion & normalize()
Normalizes the quaternion.
Definition: quaternion.h:584
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLfloat angle
GLdouble GLdouble z

◆ set() [3/4]

quaternion & irr::core::quaternion::set ( const core::vector3df vec)
inline

Sets new quaternion based on Euler angles (radians)

Definition at line 561 of file quaternion.h.

562 {
563  return set( vec.X, vec.Y, vec.Z);
564 }
quaternion & set(f32 x, f32 y, f32 z, f32 w)
Sets new quaternion.
Definition: quaternion.h:520

◆ set() [4/4]

quaternion & irr::core::quaternion::set ( const core::quaternion quat)
inline

Sets new quaternion from other quaternion.

Definition at line 567 of file quaternion.h.

568 {
569  return (*this=quat);
570 }

◆ slerp()

quaternion & irr::core::quaternion::slerp ( quaternion  q1,
quaternion  q2,
f32  time,
f32  threshold = .05f 
)
inline

Set this quaternion to the result of the spherical interpolation between two quaternions.

Parameters
q1First quaternion to be interpolated.
q2Second quaternion to be interpolated.
timeProgress of interpolation. For time=0 the result is q1, for time=1 the result is q2. Otherwise interpolation between q1 and q2.
thresholdTo avoid inaccuracies at the end (time=1) the interpolation switches to linear interpolation at some point. This value defines how much of the remaining interpolation will be calculated with lerp. Everything from 1-threshold up will be linear interpolation.

Definition at line 606 of file quaternion.h.

607 {
608  f32 angle = q1.dotProduct(q2);
609 
610  // make sure we use the short rotation
611  if (angle < 0.0f)
612  {
613  q1 *= -1.0f;
614  angle *= -1.0f;
615  }
616 
617  if (angle <= (1-threshold)) // spherical interpolation
618  {
619  const f32 theta = acosf(angle);
620  const f32 invsintheta = reciprocal(sinf(theta));
621  const f32 scale = sinf(theta * (1.0f-time)) * invsintheta;
622  const f32 invscale = sinf(theta * time) * invsintheta;
623  return (*this = (q1*scale) + (q2*invscale));
624  }
625  else // linear interpolation
626  return lerpN(q1,q2,time);
627 }
GLenum GLenum GLenum GLenum GLenum scale
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
REALINLINE f32 reciprocal(const f32 f)
Definition: irrMath.h:562
GLfloat f
GLfloat angle
quaternion & lerpN(quaternion q1, quaternion q2, f32 time)
Set this quaternion to the linear interpolation between two quaternions and normalize the result.
Definition: quaternion.h:599

◆ toAngleAxis()

void irr::core::quaternion::toAngleAxis ( f32 angle,
core::vector3df axis 
) const
inline

Fills an angle (radians) around an axis (unit vector)

Definition at line 650 of file quaternion.h.

651 {
652  const f32 scale = sqrtf(X*X + Y*Y + Z*Z);
653 
654  if (core::iszero(scale) || W > 1.0f || W < -1.0f)
655  {
656  angle = 0.0f;
657  axis.X = 0.0f;
658  axis.Y = 1.0f;
659  axis.Z = 0.0f;
660  }
661  else
662  {
663  const f32 invscale = reciprocal(scale);
664  angle = 2.0f * acosf(W);
665  axis.X = X * invscale;
666  axis.Y = Y * invscale;
667  axis.Z = Z * invscale;
668  }
669 }
GLenum GLenum GLenum GLenum GLenum scale
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
Definition: irrMath.h:307
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
REALINLINE f32 reciprocal(const f32 f)
Definition: irrMath.h:562
GLfloat f
f32 X
Quaternion elements.
Definition: quaternion.h:200
GLfloat angle

◆ toEuler()

void irr::core::quaternion::toEuler ( vector3df euler) const
inline

Output this quaternion to an Euler angle (radians)

Definition at line 671 of file quaternion.h.

672 {
673  const f64 sqw = W*W;
674  const f64 sqx = X*X;
675  const f64 sqy = Y*Y;
676  const f64 sqz = Z*Z;
677  const f64 test = 2.0 * (Y*W - X*Z);
678 
679  if (core::equals(test, 1.0, 0.000001))
680  {
681  // heading = rotation about z-axis
682  euler.Z = (f32) (-2.0*atan2(X, W));
683  // bank = rotation about x-axis
684  euler.X = 0;
685  // attitude = rotation about y-axis
686  euler.Y = (f32) (core::PI64/2.0);
687  }
688  else if (core::equals(test, -1.0, 0.000001))
689  {
690  // heading = rotation about z-axis
691  euler.Z = (f32) (2.0*atan2(X, W));
692  // bank = rotation about x-axis
693  euler.X = 0;
694  // attitude = rotation about y-axis
695  euler.Y = (f32) (core::PI64/-2.0);
696  }
697  else
698  {
699  // heading = rotation about z-axis
700  euler.Z = (f32) atan2(2.0 * (X*Y +Z*W),(sqx - sqy - sqz + sqw));
701  // bank = rotation about x-axis
702  euler.X = (f32) atan2(2.0 * (Y*Z +X*W),(-sqx - sqy + sqz + sqw));
703  // attitude = rotation about y-axis
704  euler.Y = (f32) asin( clamp(test, -1.0, 1.0) );
705  }
706 }
const f64 PI64
Constant for 64bit PI.
Definition: irrMath.h:69
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
f32 X
Quaternion elements.
Definition: quaternion.h:200
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition: irrMath.h:167

Member Data Documentation

◆ W

f32 irr::core::quaternion::W

Definition at line 203 of file quaternion.h.

◆ X

f32 irr::core::quaternion::X

Quaternion elements.

Definition at line 200 of file quaternion.h.

◆ Y

f32 irr::core::quaternion::Y

Definition at line 201 of file quaternion.h.

◆ Z

f32 irr::core::quaternion::Z

Definition at line 202 of file quaternion.h.


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