ltquatbase.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Quaternion identities
  3. //
  4. // Q = [ X Y Z W ]
  5. //
  6. //
  7. // | 2 2 |
  8. // | 1 - 2Y - 2Z 2XY - 2ZW 2XZ + 2YW |
  9. // | |
  10. // | 2 2 |
  11. // M = | 2XY + 2ZW 1 - 2X - 2Z 2YZ - 2XW |
  12. // | |
  13. // | 2 2 |
  14. // | 2XZ - 2YW 2YZ + 2XW 1 - 2X - 2Y |
  15. // | |
  16. //
  17. // X^2 + Y^2 + Z^2 + W^2 = 1
  18. //
  19. // Trace = sum of matrix diagonal
  20. // = 4 - 4( X^2 + Y^2 + Z^2 )
  21. // = 4 - 4( 1 - W^2 )
  22. // = 4W^2
  23. //
  24. // X = ( M(1,2) - M(2,1)) / 4W
  25. // Y = ( M(0,2) - M(0,2)) / 4W
  26. // Z = ( M(0,1) - M(1,0)) / 4W
  27. //
  28. // Implements base quaternion routines without a class.
  29. #ifndef __LTQUATBASE_H__
  30. #define __LTQUATBASE_H__
  31. // ------------------------------------------------------------------------ //
  32. // Definitions.
  33. // ------------------------------------------------------------------------ //
  34. #include <math.h>
  35. #define HALFPI 1.570796326794895
  36. #define QUAT_TOLERANCE 0.00001f
  37. #define QUAT_HIGH_TOLERANCE (1.0f - QUAT_TOLERANCE)
  38. #define QUAT_LOW_TOLERANCE (-QUAT_HIGH_TOLERANCE)
  39. #define QX 0
  40. #define QY 1
  41. #define QZ 2
  42. #define QW 3
  43. // ------------------------------------------------------------------------ //
  44. // Functions.
  45. // ------------------------------------------------------------------------ //
  46. // Convert a quaternion to a matrix.
  47. void quat_ConvertToMatrix(const float *pQuat, float mat[4][4]);
  48. // Gets orientation vectors for the quaternion.
  49. void quat_GetVectors(const float *pQuat, float *pRight, float *pUp, float *pForward);
  50. // Get a quaternion from a matrix.
  51. void quat_ConvertFromMatrix(float *pQuat, const float mat[4][4]);
  52. // Linear interpolate between two quaternions.
  53. void quat_Slerp(float *pDest, const float *pQ1, const float *pQ2, float t);
  54. // ------------------------------------------------------------------------ //
  55. // Inlines.
  56. // ------------------------------------------------------------------------ //
  57. inline void quat_Identity(float *pQuat)
  58. {
  59. pQuat[0] = pQuat[1] = pQuat[2] = 0.0f;
  60. pQuat[3] = 1.0f;
  61. }
  62. inline void quat_Set(float *pQuat, float x, float y, float z, float w)
  63. {
  64. pQuat[0] = x;
  65. pQuat[1] = y;
  66. pQuat[2] = z;
  67. pQuat[3] = w;
  68. }
  69. // Multiply two quaternions. Note: this is exactly the same as converting both
  70. // quaternions to matrices, multiplying the matrices (in the same order, a*b),
  71. // then converting back to a quaternion (except it's MUCH faster and more accurate).
  72. inline void quat_Mul(float *out, const float *a, const float *b)
  73. {
  74. // Note: this can be simplified when the quaternions are written
  75. // as a scalar and vector:
  76. // s1 = a[QW]
  77. // v1 = (a[QX], a[QY], a[QZ])
  78. // s2 = b[QW]
  79. // v2 = (b[QX], b[QY], b[QZ])
  80. // a = [s1,v1]
  81. // b = [s2,v2]
  82. // The quaternion product is then [s1*s2 - v1*v2, s1*v2 + s2*v1 + v1.Cross(v2)]
  83. out[QX] = a[QW]*b[QX] + a[QX]*b[QW] + a[QY]*b[QZ] - a[QZ]*b[QY];
  84. out[QY] = a[QW]*b[QY] - a[QX]*b[QZ] + a[QY]*b[QW] + a[QZ]*b[QX];
  85. out[QZ] = a[QW]*b[QZ] + a[QX]*b[QY] - a[QY]*b[QX] + a[QZ]*b[QW];
  86. out[QW] = a[QW]*b[QW] - a[QX]*b[QX] - a[QY]*b[QY] - a[QZ]*b[QZ];
  87. }
  88. // Gives the conjugate quaternion (which will produce the inverse, or transposed matrix).
  89. inline void quat_Conjugate(float *pOut, const float *pQuat)
  90. {
  91. pOut[QX] = -pQuat[QX];
  92. pOut[QY] = -pQuat[QY];
  93. pOut[QZ] = -pQuat[QZ];
  94. pOut[QW] = pQuat[QW];
  95. }
  96. #endif // __LTQUATBASE_H__