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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Hel
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Quaternion
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments: Quaternion now in C++ class form fresh from the grove
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2002.12.16:
  19. * Mongoose - Created, based on mtk3d ( freyja )
  20. =================================================================*/
  21. #include <math.h>
  22. #include <Quaternion.h>
  23. ////////////////////////////////////////////////////////////
  24. // Constructors
  25. ////////////////////////////////////////////////////////////
  26. Quaternion::Quaternion()
  27. {
  28. mW = 0;
  29. mX = 0;
  30. mY = 0;
  31. mZ = 0;
  32. }
  33. Quaternion::Quaternion(vec_t w, vec_t x, vec_t y, vec_t z)
  34. {
  35. mW = w;
  36. mX = x;
  37. mY = y;
  38. mZ = z;
  39. }
  40. Quaternion::Quaternion(vec4_t v)
  41. {
  42. mW = v[0];
  43. mX = v[1];
  44. mY = v[2];
  45. mZ = v[3];
  46. }
  47. Quaternion::~Quaternion()
  48. {
  49. }
  50. ////////////////////////////////////////////////////////////
  51. // Public Accessors
  52. ////////////////////////////////////////////////////////////
  53. void Quaternion::getMatrix(matrix_t m)
  54. {
  55. m[ 0] = 1.0f - 2.0f * (mY*mY + mZ*mZ);
  56. m[ 1] = 2.0f * (mX*mY - mW*mZ);
  57. m[ 2] = 2.0f * (mX*mZ + mW*mY);
  58. m[ 3] = 0.0f;
  59. m[ 4] = 2.0f * (mX*mY + mW*mZ);
  60. m[ 5] = 1.0f - 2.0f * (mX*mX + mZ*mZ);
  61. m[ 6] = 2.0f * (mY*mZ - mW*mX);
  62. m[ 7] = 0.0f;
  63. m[ 8] = 2.0f * (mX*mZ - mW*mY);
  64. m[ 9] = 2.0f * (mY*mZ + mW*mX);
  65. m[10] = 1.0 - 2.0f * (mX*mX + mY*mY);
  66. m[11] = 0.0f;
  67. m[12] = 0.0f;
  68. m[13] = 0.0f;
  69. m[14] = 0.0f;
  70. m[15] = 1.0f;
  71. }
  72. Quaternion Quaternion::operator =(const Quaternion &q)
  73. {
  74. mW = q.mW;
  75. mX = q.mX;
  76. mY = q.mY;
  77. mZ = q.mZ;
  78. return (*this);
  79. }
  80. Quaternion Quaternion::operator *(const Quaternion &q)
  81. {
  82. return multiply(*this, q);
  83. }
  84. Quaternion Quaternion::operator /(const Quaternion &q)
  85. {
  86. return divide(*this, q);
  87. }
  88. Quaternion Quaternion::operator +(const Quaternion &q)
  89. {
  90. return add(*this, q);
  91. }
  92. Quaternion Quaternion::operator -(const Quaternion &q)
  93. {
  94. return subtract(*this, q);
  95. }
  96. bool Quaternion::operator ==(const Quaternion &q)
  97. {
  98. //return (mX == q.mX && mY == q.mY && mZ == q.mZ && mW == q.mW);
  99. return (equalEpsilon(mX, q.mX) && equalEpsilon(mY, q.mY) &&
  100. equalEpsilon(mZ, q.mZ) && equalEpsilon(mW, q.mW));
  101. }
  102. Quaternion Quaternion::conjugate()
  103. {
  104. return Quaternion(mW, -mX, -mY, -mZ);
  105. }
  106. Quaternion Quaternion::scale(vec_t s)
  107. {
  108. return Quaternion(mW * s, mX * s, mY * s, mZ * s);
  109. }
  110. Quaternion Quaternion::inverse()
  111. {
  112. return conjugate().scale(1/magnitude());
  113. }
  114. vec_t Quaternion::dot(Quaternion a, Quaternion b)
  115. {
  116. return ((a.mW * b.mW) + (a.mX * b.mX) + (a.mY * b.mY) + (a.mZ * b.mZ));
  117. }
  118. vec_t Quaternion::magnitude()
  119. {
  120. return sqrt(dot(*this, *this));
  121. }
  122. ////////////////////////////////////////////////////////////
  123. // Public Mutators
  124. ////////////////////////////////////////////////////////////
  125. void Quaternion::setIdentity()
  126. {
  127. mW = 1.0;
  128. mX = 0.0;
  129. mY = 0.0;
  130. mZ = 0.0;
  131. }
  132. void Quaternion::set(vec_t angle, vec_t x, vec_t y, vec_t z)
  133. {
  134. vec_t temp, dist;
  135. // Normalize
  136. temp = x*x + y*y + z*z;
  137. dist = (float)(1.0 / sqrt(temp));
  138. x *= dist;
  139. y *= dist;
  140. z *= dist;
  141. mX = x;
  142. mY = y;
  143. mZ = z;
  144. mW = (float)cos(angle / 2.0f);
  145. }
  146. void Quaternion::normalize()
  147. {
  148. vec_t dist, square;
  149. square = mX * mX + mY * mY + mZ * mZ + mW * mW;
  150. if (square > 0.0)
  151. {
  152. dist = (float)(1.0 / sqrt(square));
  153. }
  154. else
  155. {
  156. dist = 1;
  157. }
  158. mX *= dist;
  159. mY *= dist;
  160. mZ *= dist;
  161. mW *= dist;
  162. }
  163. void Quaternion::copy(Quaternion q)
  164. {
  165. mW = q.mW;
  166. mX = q.mX;
  167. mY = q.mY;
  168. mZ = q.mZ;
  169. }
  170. Quaternion Quaternion::slerp(Quaternion a, Quaternion b, vec_t time)
  171. {
  172. /*******************************************************************
  173. * Spherical Linear Interpolation algorthim
  174. *-----------------------------------------------------------------
  175. *
  176. * Interpolate between A and B rotations ( Find qI )
  177. *
  178. * qI = (((qB . qA)^ -1)^ Time) qA
  179. *
  180. * http://www.magic-software.com/Documentation/quat.pdf
  181. *
  182. * Thanks to digiben for algorithms and basis of the notes in
  183. * this func
  184. *
  185. *******************************************************************/
  186. vec_t result, scaleA, scaleB, theta, sinTheta;
  187. Quaternion i;
  188. // Don't bother if it's the same rotation, it's the same as the result
  189. if (a == b)
  190. {
  191. return a;
  192. }
  193. // A . B
  194. result = dot(a, b);
  195. // If the dot product is less than 0, the angle is greater than 90 degrees
  196. if (result < 0.0f)
  197. {
  198. // Negate quaternion B and the result of the dot product
  199. b = Quaternion(-b.mW, -b.mX, -b.mY, -b.mZ);
  200. result = -result;
  201. }
  202. // Set the first and second scale for the interpolation
  203. scaleA = 1 - time;
  204. scaleB = time;
  205. // Next, we want to actually calculate the spherical interpolation. Since this
  206. // calculation is quite computationally expensive, we want to only perform it
  207. // if the angle between the 2 quaternions is large enough to warrant it. If the
  208. // angle is fairly small, we can actually just do a simpler linear interpolation
  209. // of the 2 quaternions, and skip all the complex math. We create a "delta" value
  210. // of 0.1 to say that if the cosine of the angle (result of the dot product) between
  211. // the 2 quaternions is smaller than 0.1, then we do NOT want to perform the full on
  212. // interpolation using. This is because you won't really notice the difference.
  213. // Check if the angle between the 2 quaternions was big enough
  214. // to warrant such calculations
  215. if (1 - result > 0.1f)
  216. {
  217. // Get the angle between the 2 quaternions, and then
  218. // store the sin() of that angle
  219. theta = (float)acos(result);
  220. sinTheta = (float)sin(theta);
  221. // Calculate the scale for qA and qB, according to
  222. // the angle and it's sine value
  223. scaleA = (float)sin((1 - time) * theta) / sinTheta;
  224. scaleB = (float)sin((time * theta)) / sinTheta;
  225. }
  226. // Calculate the x, y, z and w values for the quaternion by using a special
  227. // form of linear interpolation for quaternions.
  228. return (a.scale(scaleA) + b.scale(scaleB));
  229. }
  230. void Quaternion::setByMatrix(matrix_t matrix)
  231. {
  232. float diagonal = matrix[0] + matrix[5] + matrix[10] + 1.0f;
  233. float scale = 0.0f;
  234. float w = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f;
  235. if (diagonal > 0.00000001)
  236. {
  237. // Calculate the scale of the diagonal
  238. scale = (float)(sqrt(diagonal) * 2);
  239. w = 0.25f * scale;
  240. x = (matrix[9] - matrix[6]) / scale;
  241. y = (matrix[2] - matrix[8]) / scale;
  242. z = (matrix[4] - matrix[1]) / scale;
  243. }
  244. else
  245. {
  246. // If the first element of the diagonal is the greatest value
  247. if (matrix[0] > matrix[5] && matrix[0] > matrix[10])
  248. {
  249. // Find the scale according to the first element, and double it
  250. scale = (float)sqrt(1.0f + matrix[0] - matrix[5] - matrix[10])*2.0f;
  251. // Calculate the quaternion
  252. w = (matrix[9] - matrix[6]) / scale;
  253. x = 0.25f * scale;
  254. y = (matrix[4] + matrix[1]) / scale;
  255. z = (matrix[2] + matrix[8]) / scale;
  256. }
  257. // The second element of the diagonal is the greatest value
  258. else if (matrix[5] > matrix[10])
  259. {
  260. // Find the scale according to the second element, and double it
  261. scale = (float)sqrt(1.0f + matrix[5] - matrix[0] - matrix[10])*2.0f;
  262. // Calculate the quaternion
  263. w = (matrix[2] - matrix[8]) / scale;
  264. x = (matrix[4] + matrix[1]) / scale;
  265. y = 0.25f * scale;
  266. z = (matrix[9] + matrix[6]) / scale;
  267. }
  268. // The third element of the diagonal is the greatest value
  269. else
  270. {
  271. // Find the scale according to the third element, and double it
  272. scale = (float)sqrt(1.0f + matrix[10] - matrix[0] - matrix[5])*2.0f;
  273. // Calculate the quaternion
  274. w = (matrix[4] - matrix[1]) / scale;
  275. x = (matrix[2] + matrix[8]) / scale;
  276. y = (matrix[9] + matrix[6]) / scale;
  277. z = 0.25f * scale;
  278. }
  279. }
  280. mW = w;
  281. mX = x;
  282. mY = y;
  283. mZ = z;
  284. }
  285. ////////////////////////////////////////////////////////////
  286. // Private Accessors
  287. ////////////////////////////////////////////////////////////
  288. Quaternion Quaternion::multiply(Quaternion a, Quaternion b)
  289. {
  290. return Quaternion(a.mW * b.mW - a.mX * b.mX - a.mY * b.mY - a.mZ * b.mZ,
  291. a.mW * b.mX + a.mX * b.mW + a.mY * b.mZ - a.mZ * b.mY,
  292. a.mW * b.mY + a.mY * b.mW + a.mZ * b.mX - a.mX * b.mZ,
  293. a.mW * b.mZ + a.mZ * b.mW + a.mX * b.mY - a.mY * b.mX);
  294. }
  295. Quaternion Quaternion::divide(Quaternion a, Quaternion b)
  296. {
  297. return (a * (b.inverse()));
  298. }
  299. Quaternion Quaternion::add(Quaternion a, Quaternion b)
  300. {
  301. return Quaternion(a.mW + b.mW,
  302. a.mX + b.mX,
  303. a.mY + b.mY,
  304. a.mZ + b.mZ);
  305. }
  306. Quaternion Quaternion::subtract(Quaternion a, Quaternion b)
  307. {
  308. return Quaternion(a.mW - b.mW,
  309. a.mX - b.mX,
  310. a.mY - b.mY,
  311. a.mZ - b.mZ);
  312. }
  313. ////////////////////////////////////////////////////////////
  314. // Private Mutators
  315. ////////////////////////////////////////////////////////////