PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Cocos2d-x/Cocos2d-x/libs/cocos2dx/kazmath/src/quaternion.c

https://gitlab.com/Mr.Tomato/Cocos2d-X-text
C | 586 lines | 331 code | 101 blank | 154 comment | 29 complexity | f5d006a8e2ce4f94400f7307ba0edcd4 MD5 | raw file
  1. /*
  2. Copyright (c) 2008, Luke Benstead.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <assert.h>
  23. #include <memory.h>
  24. #include "kazmath/utility.h"
  25. #include "kazmath/mat3.h"
  26. #include "kazmath/vec3.h"
  27. #include "kazmath/quaternion.h"
  28. #ifndef NULL
  29. #define NULL ((void *)0)
  30. #endif
  31. ///< Returns pOut, sets pOut to the conjugate of pIn
  32. kmQuaternion* const kmQuaternionConjugate(kmQuaternion* pOut, const kmQuaternion* pIn)
  33. {
  34. pOut->x = -pIn->x;
  35. pOut->y = -pIn->y;
  36. pOut->z = -pIn->z;
  37. pOut->w = pIn->w;
  38. return pOut;
  39. }
  40. ///< Returns the dot product of the 2 quaternions
  41. const kmScalar kmQuaternionDot(const kmQuaternion* q1, const kmQuaternion* q2)
  42. {
  43. // A dot B = B dot A = AtBt + AxBx + AyBy + AzBz
  44. return (q1->w * q2->w +
  45. q1->x * q2->x +
  46. q1->y * q2->y +
  47. q1->z * q2->z);
  48. }
  49. ///< Returns the exponential of the quaternion
  50. kmQuaternion* kmQuaternionExp(kmQuaternion* pOut, const kmQuaternion* pIn)
  51. {
  52. assert(0);
  53. return pOut;
  54. }
  55. ///< Makes the passed quaternion an identity quaternion
  56. kmQuaternion* kmQuaternionIdentity(kmQuaternion* pOut)
  57. {
  58. pOut->x = 0.0;
  59. pOut->y = 0.0;
  60. pOut->z = 0.0;
  61. pOut->w = 1.0;
  62. return pOut;
  63. }
  64. ///< Returns the inverse of the passed Quaternion
  65. kmQuaternion* kmQuaternionInverse(kmQuaternion* pOut,
  66. const kmQuaternion* pIn)
  67. {
  68. kmScalar l = kmQuaternionLength(pIn);
  69. kmQuaternion tmp;
  70. if (fabs(l) > kmEpsilon)
  71. {
  72. pOut->x = 0.0;
  73. pOut->y = 0.0;
  74. pOut->z = 0.0;
  75. pOut->w = 0.0;
  76. return pOut;
  77. }
  78. ///Get the conjugute and divide by the length
  79. kmQuaternionScale(pOut,
  80. kmQuaternionConjugate(&tmp, pIn), 1.0f / l);
  81. return pOut;
  82. }
  83. ///< Returns true if the quaternion is an identity quaternion
  84. int kmQuaternionIsIdentity(const kmQuaternion* pIn)
  85. {
  86. return (pIn->x == 0.0 && pIn->y == 0.0 && pIn->z == 0.0 &&
  87. pIn->w == 1.0);
  88. }
  89. ///< Returns the length of the quaternion
  90. kmScalar kmQuaternionLength(const kmQuaternion* pIn)
  91. {
  92. return sqrtf(kmQuaternionLengthSq(pIn));
  93. }
  94. ///< Returns the length of the quaternion squared (prevents a sqrt)
  95. kmScalar kmQuaternionLengthSq(const kmQuaternion* pIn)
  96. {
  97. return pIn->x * pIn->x + pIn->y * pIn->y +
  98. pIn->z * pIn->z + pIn->w * pIn->w;
  99. }
  100. ///< Returns the natural logarithm
  101. kmQuaternion* kmQuaternionLn(kmQuaternion* pOut,
  102. const kmQuaternion* pIn)
  103. {
  104. /*
  105. A unit quaternion, is defined by:
  106. Q == (cos(theta), sin(theta) * v) where |v| = 1
  107. The natural logarithm of Q is, ln(Q) = (0, theta * v)
  108. */
  109. assert(0);
  110. return pOut;
  111. }
  112. ///< Multiplies 2 quaternions together
  113. extern
  114. kmQuaternion* kmQuaternionMultiply(kmQuaternion* pOut,
  115. const kmQuaternion* q1,
  116. const kmQuaternion* q2)
  117. {
  118. pOut->w = q1->w * q2->w - q1->x * q2->x - q1->y * q2->y - q1->z * q2->z;
  119. pOut->x = q1->w * q2->x + q1->x * q2->w + q1->y * q2->z - q1->z * q2->y;
  120. pOut->y = q1->w * q2->y + q1->y * q2->w + q1->z * q2->x - q1->x * q2->z;
  121. pOut->z = q1->w * q2->z + q1->z * q2->w + q1->x * q2->y - q1->y * q2->x;
  122. return pOut;
  123. }
  124. ///< Normalizes a quaternion
  125. kmQuaternion* kmQuaternionNormalize(kmQuaternion* pOut,
  126. const kmQuaternion* pIn)
  127. {
  128. kmScalar length = kmQuaternionLength(pIn);
  129. assert(fabs(length) > kmEpsilon);
  130. kmQuaternionScale(pOut, pIn, 1.0f / length);
  131. return pOut;
  132. }
  133. ///< Rotates a quaternion around an axis
  134. kmQuaternion* kmQuaternionRotationAxis(kmQuaternion* pOut,
  135. const kmVec3* pV,
  136. kmScalar angle)
  137. {
  138. kmScalar rad = angle * 0.5f;
  139. kmScalar scale = sinf(rad);
  140. pOut->w = cosf(rad);
  141. pOut->x = pV->x * scale;
  142. pOut->y = pV->y * scale;
  143. pOut->z = pV->z * scale;
  144. return pOut;
  145. }
  146. ///< Creates a quaternion from a rotation matrix
  147. kmQuaternion* kmQuaternionRotationMatrix(kmQuaternion* pOut,
  148. const kmMat3* pIn)
  149. {
  150. /*
  151. Note: The OpenGL matrices are transposed from the description below
  152. taken from the Matrix and Quaternion FAQ
  153. if ( mat[0] > mat[5] && mat[0] > mat[10] ) { // Column 0:
  154. S = sqrt( 1.0 + mat[0] - mat[5] - mat[10] ) * 2;
  155. X = 0.25 * S;
  156. Y = (mat[4] + mat[1] ) / S;
  157. Z = (mat[2] + mat[8] ) / S;
  158. W = (mat[9] - mat[6] ) / S;
  159. } else if ( mat[5] > mat[10] ) { // Column 1:
  160. S = sqrt( 1.0 + mat[5] - mat[0] - mat[10] ) * 2;
  161. X = (mat[4] + mat[1] ) / S;
  162. Y = 0.25 * S;
  163. Z = (mat[9] + mat[6] ) / S;
  164. W = (mat[2] - mat[8] ) / S;
  165. } else { // Column 2:
  166. S = sqrt( 1.0 + mat[10] - mat[0] - mat[5] ) * 2;
  167. X = (mat[2] + mat[8] ) / S;
  168. Y = (mat[9] + mat[6] ) / S;
  169. Z = 0.25 * S;
  170. W = (mat[4] - mat[1] ) / S;
  171. }
  172. */
  173. float x, y, z, w;
  174. float *pMatrix = NULL;
  175. float m4x4[16] = {0};
  176. float scale = 0.0f;
  177. float diagonal = 0.0f;
  178. if(!pIn) {
  179. return NULL;
  180. }
  181. /* 0 3 6
  182. 1 4 7
  183. 2 5 8
  184. 0 1 2 3
  185. 4 5 6 7
  186. 8 9 10 11
  187. 12 13 14 15*/
  188. m4x4[0] = pIn->mat[0];
  189. m4x4[1] = pIn->mat[3];
  190. m4x4[2] = pIn->mat[6];
  191. m4x4[4] = pIn->mat[1];
  192. m4x4[5] = pIn->mat[4];
  193. m4x4[6] = pIn->mat[7];
  194. m4x4[8] = pIn->mat[2];
  195. m4x4[9] = pIn->mat[5];
  196. m4x4[10] = pIn->mat[8];
  197. m4x4[15] = 1;
  198. pMatrix = &m4x4[0];
  199. diagonal = pMatrix[0] + pMatrix[5] + pMatrix[10] + 1;
  200. if(diagonal > kmEpsilon) {
  201. // Calculate the scale of the diagonal
  202. scale = (float)sqrt(diagonal ) * 2;
  203. // Calculate the x, y, x and w of the quaternion through the respective equation
  204. x = ( pMatrix[9] - pMatrix[6] ) / scale;
  205. y = ( pMatrix[2] - pMatrix[8] ) / scale;
  206. z = ( pMatrix[4] - pMatrix[1] ) / scale;
  207. w = 0.25f * scale;
  208. }
  209. else
  210. {
  211. // If the first element of the diagonal is the greatest value
  212. if ( pMatrix[0] > pMatrix[5] && pMatrix[0] > pMatrix[10] )
  213. {
  214. // Find the scale according to the first element, and double that value
  215. scale = (float)sqrt( 1.0f + pMatrix[0] - pMatrix[5] - pMatrix[10] ) * 2.0f;
  216. // Calculate the x, y, x and w of the quaternion through the respective equation
  217. x = 0.25f * scale;
  218. y = (pMatrix[4] + pMatrix[1] ) / scale;
  219. z = (pMatrix[2] + pMatrix[8] ) / scale;
  220. w = (pMatrix[9] - pMatrix[6] ) / scale;
  221. }
  222. // Else if the second element of the diagonal is the greatest value
  223. else if (pMatrix[5] > pMatrix[10])
  224. {
  225. // Find the scale according to the second element, and double that value
  226. scale = (float)sqrt( 1.0f + pMatrix[5] - pMatrix[0] - pMatrix[10] ) * 2.0f;
  227. // Calculate the x, y, x and w of the quaternion through the respective equation
  228. x = (pMatrix[4] + pMatrix[1] ) / scale;
  229. y = 0.25f * scale;
  230. z = (pMatrix[9] + pMatrix[6] ) / scale;
  231. w = (pMatrix[2] - pMatrix[8] ) / scale;
  232. }
  233. // Else the third element of the diagonal is the greatest value
  234. else
  235. {
  236. // Find the scale according to the third element, and double that value
  237. scale = (float)sqrt( 1.0f + pMatrix[10] - pMatrix[0] - pMatrix[5] ) * 2.0f;
  238. // Calculate the x, y, x and w of the quaternion through the respective equation
  239. x = (pMatrix[2] + pMatrix[8] ) / scale;
  240. y = (pMatrix[9] + pMatrix[6] ) / scale;
  241. z = 0.25f * scale;
  242. w = (pMatrix[4] - pMatrix[1] ) / scale;
  243. }
  244. }
  245. pOut->x = x;
  246. pOut->y = y;
  247. pOut->z = z;
  248. pOut->w = w;
  249. return pOut;
  250. #if 0
  251. kmScalar T = pIn->mat[0] + pIn->mat[5] + pIn->mat[10];
  252. if (T > kmEpsilon) {
  253. //If the trace is greater than zero we always use this calculation:
  254. /* S = sqrt(T) * 2;
  255. X = ( mat[9] - mat[6] ) / S;
  256. Y = ( mat[2] - mat[8] ) / S;
  257. Z = ( mat[4] - mat[1] ) / S;
  258. W = 0.25 * S;*/
  259. /* kmScalar s = sqrtf(T) * 2;
  260. pOut->x = (pIn->mat[9] - pIn->mat[6]) / s;
  261. pOut->y = (pIn->mat[8] - pIn->mat[2]) / s;
  262. pOut->z = (pIn->mat[1] - pIn->mat[4]) / s;
  263. pOut->w = 0.25f * s;
  264. kmQuaternionNormalize(pOut, pOut);
  265. return pOut;
  266. }
  267. //Otherwise the calculation depends on which major diagonal element has the greatest value.
  268. if (pIn->mat[0] > pIn->mat[5] && pIn->mat[0] > pIn->mat[10]) {
  269. kmScalar s = sqrtf(1 + pIn->mat[0] - pIn->mat[5] - pIn->mat[10]) * 2;
  270. pOut->x = 0.25f * s;
  271. pOut->y = (pIn->mat[1] + pIn->mat[4]) / s;
  272. pOut->z = (pIn->mat[8] + pIn->mat[2]) / s;
  273. pOut->w = (pIn->mat[9] - pIn->mat[6]) / s;
  274. }
  275. else if (pIn->mat[5] > pIn->mat[10]) {
  276. kmScalar s = sqrtf(1 + pIn->mat[5] - pIn->mat[0] - pIn->mat[10]) * 2;
  277. pOut->x = (pIn->mat[1] + pIn->mat[4]) / s;
  278. pOut->y = 0.25f * s;
  279. pOut->z = (pIn->mat[9] + pIn->mat[6]) / s;
  280. pOut->w = (pIn->mat[8] - pIn->mat[2]) / s;
  281. }
  282. else {
  283. kmScalar s = sqrt(1.0f + pIn->mat[10] - pIn->mat[0] - pIn->mat[5]) * 2.0f;
  284. pOut->x = (pIn->mat[8] + pIn->mat[2] ) / s;
  285. pOut->y = (pIn->mat[6] + pIn->mat[9] ) / s;
  286. pOut->z = 0.25f * s;
  287. pOut->w = (pIn->mat[1] - pIn->mat[4] ) / s;
  288. }
  289. kmQuaternionNormalize(pOut, pOut);
  290. return pOut;*/
  291. #endif // 0
  292. }
  293. ///< Create a quaternion from yaw, pitch and roll
  294. kmQuaternion* kmQuaternionRotationYawPitchRoll(kmQuaternion* pOut,
  295. kmScalar yaw,
  296. kmScalar pitch,
  297. kmScalar roll)
  298. {
  299. kmScalar ex, ey, ez; // temp half euler angles
  300. kmScalar cr, cp, cy, sr, sp, sy, cpcy, spsy; // temp vars in roll,pitch yaw
  301. ex = kmDegreesToRadians(pitch) / 2.0f; // convert to rads and half them
  302. ey = kmDegreesToRadians(yaw) / 2.0f;
  303. ez = kmDegreesToRadians(roll) / 2.0f;
  304. cr = cosf(ex);
  305. cp = cosf(ey);
  306. cy = cosf(ez);
  307. sr = sinf(ex);
  308. sp = sinf(ey);
  309. sy = sinf(ez);
  310. cpcy = cp * cy;
  311. spsy = sp * sy;
  312. pOut->w = cr * cpcy + sr * spsy;
  313. pOut->x = sr * cpcy - cr * spsy;
  314. pOut->y = cr * sp * cy + sr * cp * sy;
  315. pOut->z = cr * cp * sy - sr * sp * cy;
  316. kmQuaternionNormalize(pOut, pOut);
  317. return pOut;
  318. }
  319. ///< Interpolate between 2 quaternions
  320. kmQuaternion* kmQuaternionSlerp(kmQuaternion* pOut,
  321. const kmQuaternion* q1,
  322. const kmQuaternion* q2,
  323. kmScalar t)
  324. {
  325. /*float CosTheta = Q0.DotProd(Q1);
  326. float Theta = acosf(CosTheta);
  327. float SinTheta = sqrtf(1.0f-CosTheta*CosTheta);
  328. float Sin_T_Theta = sinf(T*Theta)/SinTheta;
  329. float Sin_OneMinusT_Theta = sinf((1.0f-T)*Theta)/SinTheta;
  330. Quaternion Result = Q0*Sin_OneMinusT_Theta;
  331. Result += (Q1*Sin_T_Theta);
  332. return Result;*/
  333. if (q1->x == q2->x &&
  334. q1->y == q2->y &&
  335. q1->z == q2->z &&
  336. q1->w == q2->w) {
  337. pOut->x = q1->x;
  338. pOut->y = q1->y;
  339. pOut->z = q1->z;
  340. pOut->w = q1->w;
  341. return pOut;
  342. }
  343. {
  344. kmScalar ct = kmQuaternionDot(q1, q2);
  345. kmScalar theta = acosf(ct);
  346. kmScalar st = sqrtf(1.0 - kmSQR(ct));
  347. kmScalar stt = sinf(t * theta) / st;
  348. kmScalar somt = sinf((1.0 - t) * theta) / st;
  349. kmQuaternion temp, temp2;
  350. kmQuaternionScale(&temp, q1, somt);
  351. kmQuaternionScale(&temp2, q2, stt);
  352. kmQuaternionAdd(pOut, &temp, &temp2);
  353. }
  354. return pOut;
  355. }
  356. ///< Get the axis and angle of rotation from a quaternion
  357. void kmQuaternionToAxisAngle(const kmQuaternion* pIn,
  358. kmVec3* pAxis,
  359. kmScalar* pAngle)
  360. {
  361. kmScalar tempAngle; // temp angle
  362. kmScalar scale; // temp vars
  363. tempAngle = acosf(pIn->w);
  364. scale = sqrtf(kmSQR(pIn->x) + kmSQR(pIn->y) + kmSQR(pIn->z));
  365. if (((scale > -kmEpsilon) && scale < kmEpsilon)
  366. || (scale < 2*kmPI + kmEpsilon && scale > 2*kmPI - kmEpsilon)) // angle is 0 or 360 so just simply set axis to 0,0,1 with angle 0
  367. {
  368. *pAngle = 0.0f;
  369. pAxis->x = 0.0f;
  370. pAxis->y = 0.0f;
  371. pAxis->z = 1.0f;
  372. }
  373. else
  374. {
  375. *pAngle = tempAngle * 2.0f; // angle in radians
  376. pAxis->x = pIn->x / scale;
  377. pAxis->y = pIn->y / scale;
  378. pAxis->z = pIn->z / scale;
  379. kmVec3Normalize(pAxis, pAxis);
  380. }
  381. }
  382. kmQuaternion* kmQuaternionScale(kmQuaternion* pOut,
  383. const kmQuaternion* pIn,
  384. kmScalar s)
  385. {
  386. pOut->x = pIn->x * s;
  387. pOut->y = pIn->y * s;
  388. pOut->z = pIn->z * s;
  389. pOut->w = pIn->w * s;
  390. return pOut;
  391. }
  392. kmQuaternion* kmQuaternionAssign(kmQuaternion* pOut, const kmQuaternion* pIn)
  393. {
  394. memcpy(pOut, pIn, sizeof(float) * 4);
  395. return pOut;
  396. }
  397. kmQuaternion* kmQuaternionAdd(kmQuaternion* pOut, const kmQuaternion* pQ1, const kmQuaternion* pQ2)
  398. {
  399. pOut->x = pQ1->x + pQ2->x;
  400. pOut->y = pQ1->y + pQ2->y;
  401. pOut->z = pQ1->z + pQ2->z;
  402. pOut->w = pQ1->w + pQ2->w;
  403. return pOut;
  404. }
  405. /** Adapted from the OGRE engine!
  406. Gets the shortest arc quaternion to rotate this vector to the destination
  407. vector.
  408. @remarks
  409. If you call this with a dest vector that is close to the inverse
  410. of this vector, we will rotate 180 degrees around the 'fallbackAxis'
  411. (if specified, or a generated axis if not) since in this case
  412. ANY axis of rotation is valid.
  413. */
  414. kmQuaternion* kmQuaternionRotationBetweenVec3(kmQuaternion* pOut, const kmVec3* vec1, const kmVec3* vec2, const kmVec3* fallback) {
  415. kmVec3 v1, v2;
  416. kmScalar a;
  417. kmVec3Assign(&v1, vec1);
  418. kmVec3Assign(&v2, vec2);
  419. kmVec3Normalize(&v1, &v1);
  420. kmVec3Normalize(&v2, &v2);
  421. a = kmVec3Dot(&v1, &v2);
  422. if (a >= 1.0) {
  423. kmQuaternionIdentity(pOut);
  424. return pOut;
  425. }
  426. if (a < (1e-6f - 1.0f)) {
  427. if (fabs(kmVec3LengthSq(fallback)) < kmEpsilon) {
  428. kmQuaternionRotationAxis(pOut, fallback, kmPI);
  429. } else {
  430. kmVec3 axis;
  431. kmVec3 X;
  432. X.x = 1.0;
  433. X.y = 0.0;
  434. X.z = 0.0;
  435. kmVec3Cross(&axis, &X, vec1);
  436. //If axis is zero
  437. if (fabs(kmVec3LengthSq(&axis)) < kmEpsilon) {
  438. kmVec3 Y;
  439. Y.x = 0.0;
  440. Y.y = 1.0;
  441. Y.z = 0.0;
  442. kmVec3Cross(&axis, &Y, vec1);
  443. }
  444. kmVec3Normalize(&axis, &axis);
  445. kmQuaternionRotationAxis(pOut, &axis, kmPI);
  446. }
  447. } else {
  448. kmScalar s = sqrtf((1+a) * 2);
  449. kmScalar invs = 1 / s;
  450. kmVec3 c;
  451. kmVec3Cross(&c, &v1, &v2);
  452. pOut->x = c.x * invs;
  453. pOut->y = c.y * invs;
  454. pOut->z = c.z * invs;
  455. pOut->w = s * 0.5f;
  456. kmQuaternionNormalize(pOut, pOut);
  457. }
  458. return pOut;
  459. }
  460. kmVec3* kmQuaternionMultiplyVec3(kmVec3* pOut, const kmQuaternion* q, const kmVec3* v) {
  461. kmVec3 uv, uuv, qvec;
  462. qvec.x = q->x;
  463. qvec.y = q->y;
  464. qvec.z = q->z;
  465. kmVec3Cross(&uv, &qvec, v);
  466. kmVec3Cross(&uuv, &qvec, &uv);
  467. kmVec3Scale(&uv, &uv, (2.0f * q->w));
  468. kmVec3Scale(&uuv, &uuv, 2.0f);
  469. kmVec3Add(pOut, v, &uv);
  470. kmVec3Add(pOut, pOut, &uuv);
  471. return pOut;
  472. }