Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Quaternion.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*!
  2. * \file src/math/Quaternion.cpp
  3. * \brief Quaternion
  4. *
  5. * \author Mongoose
  6. */
  7. #include <math.h>
  8. #include "global.h"
  9. #include "math/Quaternion.h"
  10. Quaternion::Quaternion() {
  11. mW = 0;
  12. mX = 0;
  13. mY = 0;
  14. mZ = 0;
  15. }
  16. Quaternion::Quaternion(float w, float x, float y, float z) {
  17. mW = w;
  18. mX = x;
  19. mY = y;
  20. mZ = z;
  21. }
  22. Quaternion::Quaternion(float v[4]) {
  23. mW = v[0];
  24. mX = v[1];
  25. mY = v[2];
  26. mZ = v[3];
  27. }
  28. void Quaternion::getMatrix(float m[16]) {
  29. m[ 0] = 1.0f - 2.0f * (mY * mY + mZ * mZ);
  30. m[ 1] = 2.0f * (mX * mY - mW * mZ);
  31. m[ 2] = 2.0f * (mX * mZ + mW * mY);
  32. m[ 3] = 0.0f;
  33. m[ 4] = 2.0f * (mX * mY + mW * mZ);
  34. m[ 5] = 1.0f - 2.0f * (mX * mX + mZ * mZ);
  35. m[ 6] = 2.0f * (mY * mZ - mW * mX);
  36. m[ 7] = 0.0f;
  37. m[ 8] = 2.0f * (mX * mZ - mW * mY);
  38. m[ 9] = 2.0f * (mY * mZ + mW * mX);
  39. m[10] = 1.0f - 2.0f * (mX * mX + mY * mY);
  40. m[11] = 0.0f;
  41. m[12] = 0.0f;
  42. m[13] = 0.0f;
  43. m[14] = 0.0f;
  44. m[15] = 1.0f;
  45. }
  46. Quaternion Quaternion::operator *(const Quaternion& q) {
  47. return multiply(*this, q);
  48. }
  49. Quaternion Quaternion::operator /(const Quaternion& q) {
  50. return divide(*this, q);
  51. }
  52. Quaternion Quaternion::operator +(const Quaternion& q) {
  53. return add(*this, q);
  54. }
  55. Quaternion Quaternion::operator -(const Quaternion& q) {
  56. return subtract(*this, q);
  57. }
  58. bool Quaternion::operator ==(const Quaternion& q) {
  59. //return (mX == q.mX && mY == q.mY && mZ == q.mZ && mW == q.mW);
  60. return (equalEpsilon(mX, q.mX) && equalEpsilon(mY, q.mY) &&
  61. equalEpsilon(mZ, q.mZ) && equalEpsilon(mW, q.mW));
  62. }
  63. Quaternion Quaternion::conjugate() {
  64. return Quaternion(mW, -mX, -mY, -mZ);
  65. }
  66. Quaternion Quaternion::scale(float s) {
  67. return Quaternion(mW * s, mX * s, mY * s, mZ * s);
  68. }
  69. Quaternion Quaternion::inverse() {
  70. return conjugate().scale(1 / magnitude());
  71. }
  72. float Quaternion::dot(Quaternion a, Quaternion b) {
  73. return ((a.mW * b.mW) + (a.mX * b.mX) + (a.mY * b.mY) + (a.mZ * b.mZ));
  74. }
  75. float Quaternion::magnitude() {
  76. return sqrtf(dot(*this, *this));
  77. }
  78. void Quaternion::setIdentity() {
  79. mW = 1.0;
  80. mX = 0.0;
  81. mY = 0.0;
  82. mZ = 0.0;
  83. }
  84. void Quaternion::set(float angle, float x, float y, float z) {
  85. float temp, dist;
  86. // Normalize
  87. temp = x * x + y * y + z * z;
  88. dist = 1.0f / sqrtf(temp);
  89. x *= dist;
  90. y *= dist;
  91. z *= dist;
  92. mX = x;
  93. mY = y;
  94. mZ = z;
  95. mW = cosf(angle / 2.0f);
  96. }
  97. void Quaternion::normalize() {
  98. float dist, square;
  99. square = mX * mX + mY * mY + mZ * mZ + mW * mW;
  100. if (square > 0.0) {
  101. dist = 1.0f / sqrtf(square);
  102. } else {
  103. dist = 1;
  104. }
  105. mX *= dist;
  106. mY *= dist;
  107. mZ *= dist;
  108. mW *= dist;
  109. }
  110. void Quaternion::copy(Quaternion q) {
  111. mW = q.mW;
  112. mX = q.mX;
  113. mY = q.mY;
  114. mZ = q.mZ;
  115. }
  116. Quaternion Quaternion::slerp(Quaternion a, Quaternion b, float time) {
  117. /*******************************************************************
  118. * Spherical Linear Interpolation algorthim
  119. *-----------------------------------------------------------------
  120. *
  121. * Interpolate between A and B rotations ( Find qI )
  122. *
  123. * qI = (((qB . qA)^ -1)^ Time) qA
  124. *
  125. * http://www.magic-software.com/Documentation/quat.pdf
  126. *
  127. * Thanks to digiben for algorithms and basis of the notes in
  128. * this func
  129. *
  130. *******************************************************************/
  131. float result, scaleA, scaleB;
  132. Quaternion i;
  133. // Don't bother if it's the same rotation, it's the same as the result
  134. if (a == b)
  135. return a;
  136. // A . B
  137. result = dot(a, b);
  138. // If the dot product is less than 0, the angle is greater than 90 degrees
  139. if (result < 0.0f) {
  140. // Negate quaternion B and the result of the dot product
  141. b = Quaternion(-b.mW, -b.mX, -b.mY, -b.mZ);
  142. result = -result;
  143. }
  144. // Set the first and second scale for the interpolation
  145. scaleA = 1 - time;
  146. scaleB = time;
  147. // Next, we want to actually calculate the spherical interpolation. Since this
  148. // calculation is quite computationally expensive, we want to only perform it
  149. // if the angle between the 2 quaternions is large enough to warrant it. If the
  150. // angle is fairly small, we can actually just do a simpler linear interpolation
  151. // of the 2 quaternions, and skip all the complex math. We create a "delta" value
  152. // of 0.1 to say that if the cosine of the angle (result of the dot product) between
  153. // the 2 quaternions is smaller than 0.1, then we do NOT want to perform the full on
  154. // interpolation using. This is because you won't really notice the difference.
  155. // Check if the angle between the 2 quaternions was big enough
  156. // to warrant such calculations
  157. if (1 - result > 0.1f) {
  158. // Get the angle between the 2 quaternions, and then
  159. // store the sin() of that angle
  160. float theta = (float)acos(result);
  161. float sinTheta = (float)sin(theta);
  162. // Calculate the scale for qA and qB, according to
  163. // the angle and it's sine value
  164. scaleA = (float)sin((1 - time) * theta) / sinTheta;
  165. scaleB = (float)sin((time * theta)) / sinTheta;
  166. }
  167. // Calculate the x, y, z and w values for the quaternion by using a special
  168. // form of linear interpolation for quaternions.
  169. return (a.scale(scaleA) + b.scale(scaleB));
  170. }
  171. void Quaternion::setByMatrix(float matrix[16]) {
  172. float diagonal = matrix[0] + matrix[5] + matrix[10] + 1.0f;
  173. float scale = 0.0f;
  174. float w = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f;
  175. if (diagonal > 0.00000001) {
  176. // Calculate the scale of the diagonal
  177. scale = (float)(sqrt(diagonal) * 2);
  178. w = 0.25f * scale;
  179. x = (matrix[9] - matrix[6]) / scale;
  180. y = (matrix[2] - matrix[8]) / scale;
  181. z = (matrix[4] - matrix[1]) / scale;
  182. } else {
  183. // If the first element of the diagonal is the greatest value
  184. if (matrix[0] > matrix[5] && matrix[0] > matrix[10]) {
  185. // Find the scale according to the first element, and double it
  186. scale = (float)sqrt(1.0f + matrix[0] - matrix[5] - matrix[10]) * 2.0f;
  187. // Calculate the quaternion
  188. w = (matrix[9] - matrix[6]) / scale;
  189. x = 0.25f * scale;
  190. y = (matrix[4] + matrix[1]) / scale;
  191. z = (matrix[2] + matrix[8]) / scale;
  192. } else if (matrix[5] > matrix[10]) {
  193. // The second element of the diagonal is the greatest value
  194. // Find the scale according to the second element, and double it
  195. scale = (float)sqrt(1.0f + matrix[5] - matrix[0] - matrix[10]) * 2.0f;
  196. // Calculate the quaternion
  197. w = (matrix[2] - matrix[8]) / scale;
  198. x = (matrix[4] + matrix[1]) / scale;
  199. y = 0.25f * scale;
  200. z = (matrix[9] + matrix[6]) / scale;
  201. } else { // The third element of the diagonal is the greatest value
  202. // Find the scale according to the third element, and double it
  203. scale = (float)sqrt(1.0f + matrix[10] - matrix[0] - matrix[5]) * 2.0f;
  204. // Calculate the quaternion
  205. w = (matrix[4] - matrix[1]) / scale;
  206. x = (matrix[2] + matrix[8]) / scale;
  207. y = (matrix[9] + matrix[6]) / scale;
  208. z = 0.25f * scale;
  209. }
  210. }
  211. mW = w;
  212. mX = x;
  213. mY = y;
  214. mZ = z;
  215. }
  216. Quaternion Quaternion::multiply(Quaternion a, Quaternion b) {
  217. return Quaternion(a.mW * b.mW - a.mX * b.mX - a.mY * b.mY - a.mZ * b.mZ,
  218. a.mW * b.mX + a.mX * b.mW + a.mY * b.mZ - a.mZ * b.mY,
  219. a.mW * b.mY + a.mY * b.mW + a.mZ * b.mX - a.mX * b.mZ,
  220. a.mW * b.mZ + a.mZ * b.mW + a.mX * b.mY - a.mY * b.mX);
  221. }
  222. Quaternion Quaternion::divide(Quaternion a, Quaternion b) {
  223. return (a * (b.inverse()));
  224. }
  225. Quaternion Quaternion::add(Quaternion a, Quaternion b) {
  226. return Quaternion(a.mW + b.mW,
  227. a.mX + b.mX,
  228. a.mY + b.mY,
  229. a.mZ + b.mZ);
  230. }
  231. Quaternion Quaternion::subtract(Quaternion a, Quaternion b) {
  232. return Quaternion(a.mW - b.mW,
  233. a.mX - b.mX,
  234. a.mY - b.mY,
  235. a.mZ - b.mZ);
  236. }