quat2.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.sqrLen = exports.squaredLength = exports.len = exports.length = exports.dot = exports.mul = exports.setReal = exports.getReal = undefined;
  6. exports.create = create;
  7. exports.clone = clone;
  8. exports.fromValues = fromValues;
  9. exports.fromRotationTranslationValues = fromRotationTranslationValues;
  10. exports.fromRotationTranslation = fromRotationTranslation;
  11. exports.fromTranslation = fromTranslation;
  12. exports.fromRotation = fromRotation;
  13. exports.fromMat4 = fromMat4;
  14. exports.copy = copy;
  15. exports.identity = identity;
  16. exports.set = set;
  17. exports.getDual = getDual;
  18. exports.setDual = setDual;
  19. exports.getTranslation = getTranslation;
  20. exports.translate = translate;
  21. exports.rotateX = rotateX;
  22. exports.rotateY = rotateY;
  23. exports.rotateZ = rotateZ;
  24. exports.rotateByQuatAppend = rotateByQuatAppend;
  25. exports.rotateByQuatPrepend = rotateByQuatPrepend;
  26. exports.rotateAroundAxis = rotateAroundAxis;
  27. exports.add = add;
  28. exports.multiply = multiply;
  29. exports.scale = scale;
  30. exports.lerp = lerp;
  31. exports.invert = invert;
  32. exports.conjugate = conjugate;
  33. exports.normalize = normalize;
  34. exports.str = str;
  35. exports.exactEquals = exactEquals;
  36. exports.equals = equals;
  37. var _common = require("./common.js");
  38. var glMatrix = _interopRequireWildcard(_common);
  39. var _quat = require("./quat.js");
  40. var quat = _interopRequireWildcard(_quat);
  41. var _mat = require("./mat4.js");
  42. var mat4 = _interopRequireWildcard(_mat);
  43. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  44. /**
  45. * Dual Quaternion<br>
  46. * Format: [real, dual]<br>
  47. * Quaternion format: XYZW<br>
  48. * Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.<br>
  49. * @module quat2
  50. */
  51. /**
  52. * Creates a new identity dual quat
  53. *
  54. * @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation]
  55. */
  56. function create() {
  57. var dq = new glMatrix.ARRAY_TYPE(8);
  58. if (glMatrix.ARRAY_TYPE != Float32Array) {
  59. dq[0] = 0;
  60. dq[1] = 0;
  61. dq[2] = 0;
  62. dq[4] = 0;
  63. dq[5] = 0;
  64. dq[6] = 0;
  65. dq[7] = 0;
  66. }
  67. dq[3] = 1;
  68. return dq;
  69. }
  70. /**
  71. * Creates a new quat initialized with values from an existing quaternion
  72. *
  73. * @param {quat2} a dual quaternion to clone
  74. * @returns {quat2} new dual quaternion
  75. * @function
  76. */
  77. function clone(a) {
  78. var dq = new glMatrix.ARRAY_TYPE(8);
  79. dq[0] = a[0];
  80. dq[1] = a[1];
  81. dq[2] = a[2];
  82. dq[3] = a[3];
  83. dq[4] = a[4];
  84. dq[5] = a[5];
  85. dq[6] = a[6];
  86. dq[7] = a[7];
  87. return dq;
  88. }
  89. /**
  90. * Creates a new dual quat initialized with the given values
  91. *
  92. * @param {Number} x1 X component
  93. * @param {Number} y1 Y component
  94. * @param {Number} z1 Z component
  95. * @param {Number} w1 W component
  96. * @param {Number} x2 X component
  97. * @param {Number} y2 Y component
  98. * @param {Number} z2 Z component
  99. * @param {Number} w2 W component
  100. * @returns {quat2} new dual quaternion
  101. * @function
  102. */
  103. function fromValues(x1, y1, z1, w1, x2, y2, z2, w2) {
  104. var dq = new glMatrix.ARRAY_TYPE(8);
  105. dq[0] = x1;
  106. dq[1] = y1;
  107. dq[2] = z1;
  108. dq[3] = w1;
  109. dq[4] = x2;
  110. dq[5] = y2;
  111. dq[6] = z2;
  112. dq[7] = w2;
  113. return dq;
  114. }
  115. /**
  116. * Creates a new dual quat from the given values (quat and translation)
  117. *
  118. * @param {Number} x1 X component
  119. * @param {Number} y1 Y component
  120. * @param {Number} z1 Z component
  121. * @param {Number} w1 W component
  122. * @param {Number} x2 X component (translation)
  123. * @param {Number} y2 Y component (translation)
  124. * @param {Number} z2 Z component (translation)
  125. * @returns {quat2} new dual quaternion
  126. * @function
  127. */
  128. function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) {
  129. var dq = new glMatrix.ARRAY_TYPE(8);
  130. dq[0] = x1;
  131. dq[1] = y1;
  132. dq[2] = z1;
  133. dq[3] = w1;
  134. var ax = x2 * 0.5,
  135. ay = y2 * 0.5,
  136. az = z2 * 0.5;
  137. dq[4] = ax * w1 + ay * z1 - az * y1;
  138. dq[5] = ay * w1 + az * x1 - ax * z1;
  139. dq[6] = az * w1 + ax * y1 - ay * x1;
  140. dq[7] = -ax * x1 - ay * y1 - az * z1;
  141. return dq;
  142. }
  143. /**
  144. * Creates a dual quat from a quaternion and a translation
  145. *
  146. * @param {quat2} dual quaternion receiving operation result
  147. * @param {quat} q quaternion
  148. * @param {vec3} t tranlation vector
  149. * @returns {quat2} dual quaternion receiving operation result
  150. * @function
  151. */
  152. function fromRotationTranslation(out, q, t) {
  153. var ax = t[0] * 0.5,
  154. ay = t[1] * 0.5,
  155. az = t[2] * 0.5,
  156. bx = q[0],
  157. by = q[1],
  158. bz = q[2],
  159. bw = q[3];
  160. out[0] = bx;
  161. out[1] = by;
  162. out[2] = bz;
  163. out[3] = bw;
  164. out[4] = ax * bw + ay * bz - az * by;
  165. out[5] = ay * bw + az * bx - ax * bz;
  166. out[6] = az * bw + ax * by - ay * bx;
  167. out[7] = -ax * bx - ay * by - az * bz;
  168. return out;
  169. }
  170. /**
  171. * Creates a dual quat from a translation
  172. *
  173. * @param {quat2} dual quaternion receiving operation result
  174. * @param {vec3} t translation vector
  175. * @returns {quat2} dual quaternion receiving operation result
  176. * @function
  177. */
  178. function fromTranslation(out, t) {
  179. out[0] = 0;
  180. out[1] = 0;
  181. out[2] = 0;
  182. out[3] = 1;
  183. out[4] = t[0] * 0.5;
  184. out[5] = t[1] * 0.5;
  185. out[6] = t[2] * 0.5;
  186. out[7] = 0;
  187. return out;
  188. }
  189. /**
  190. * Creates a dual quat from a quaternion
  191. *
  192. * @param {quat2} dual quaternion receiving operation result
  193. * @param {quat} q the quaternion
  194. * @returns {quat2} dual quaternion receiving operation result
  195. * @function
  196. */
  197. function fromRotation(out, q) {
  198. out[0] = q[0];
  199. out[1] = q[1];
  200. out[2] = q[2];
  201. out[3] = q[3];
  202. out[4] = 0;
  203. out[5] = 0;
  204. out[6] = 0;
  205. out[7] = 0;
  206. return out;
  207. }
  208. /**
  209. * Creates a new dual quat from a matrix (4x4)
  210. *
  211. * @param {quat2} out the dual quaternion
  212. * @param {mat4} a the matrix
  213. * @returns {quat2} dual quat receiving operation result
  214. * @function
  215. */
  216. function fromMat4(out, a) {
  217. //TODO Optimize this
  218. var outer = quat.create();
  219. mat4.getRotation(outer, a);
  220. var t = new glMatrix.ARRAY_TYPE(3);
  221. mat4.getTranslation(t, a);
  222. fromRotationTranslation(out, outer, t);
  223. return out;
  224. }
  225. /**
  226. * Copy the values from one dual quat to another
  227. *
  228. * @param {quat2} out the receiving dual quaternion
  229. * @param {quat2} a the source dual quaternion
  230. * @returns {quat2} out
  231. * @function
  232. */
  233. function copy(out, a) {
  234. out[0] = a[0];
  235. out[1] = a[1];
  236. out[2] = a[2];
  237. out[3] = a[3];
  238. out[4] = a[4];
  239. out[5] = a[5];
  240. out[6] = a[6];
  241. out[7] = a[7];
  242. return out;
  243. }
  244. /**
  245. * Set a dual quat to the identity dual quaternion
  246. *
  247. * @param {quat2} out the receiving quaternion
  248. * @returns {quat2} out
  249. */
  250. function identity(out) {
  251. out[0] = 0;
  252. out[1] = 0;
  253. out[2] = 0;
  254. out[3] = 1;
  255. out[4] = 0;
  256. out[5] = 0;
  257. out[6] = 0;
  258. out[7] = 0;
  259. return out;
  260. }
  261. /**
  262. * Set the components of a dual quat to the given values
  263. *
  264. * @param {quat2} out the receiving quaternion
  265. * @param {Number} x1 X component
  266. * @param {Number} y1 Y component
  267. * @param {Number} z1 Z component
  268. * @param {Number} w1 W component
  269. * @param {Number} x2 X component
  270. * @param {Number} y2 Y component
  271. * @param {Number} z2 Z component
  272. * @param {Number} w2 W component
  273. * @returns {quat2} out
  274. * @function
  275. */
  276. function set(out, x1, y1, z1, w1, x2, y2, z2, w2) {
  277. out[0] = x1;
  278. out[1] = y1;
  279. out[2] = z1;
  280. out[3] = w1;
  281. out[4] = x2;
  282. out[5] = y2;
  283. out[6] = z2;
  284. out[7] = w2;
  285. return out;
  286. }
  287. /**
  288. * Gets the real part of a dual quat
  289. * @param {quat} out real part
  290. * @param {quat2} a Dual Quaternion
  291. * @return {quat} real part
  292. */
  293. var getReal = exports.getReal = quat.copy;
  294. /**
  295. * Gets the dual part of a dual quat
  296. * @param {quat} out dual part
  297. * @param {quat2} a Dual Quaternion
  298. * @return {quat} dual part
  299. */
  300. function getDual(out, a) {
  301. out[0] = a[4];
  302. out[1] = a[5];
  303. out[2] = a[6];
  304. out[3] = a[7];
  305. return out;
  306. }
  307. /**
  308. * Set the real component of a dual quat to the given quaternion
  309. *
  310. * @param {quat2} out the receiving quaternion
  311. * @param {quat} q a quaternion representing the real part
  312. * @returns {quat2} out
  313. * @function
  314. */
  315. var setReal = exports.setReal = quat.copy;
  316. /**
  317. * Set the dual component of a dual quat to the given quaternion
  318. *
  319. * @param {quat2} out the receiving quaternion
  320. * @param {quat} q a quaternion representing the dual part
  321. * @returns {quat2} out
  322. * @function
  323. */
  324. function setDual(out, q) {
  325. out[4] = q[0];
  326. out[5] = q[1];
  327. out[6] = q[2];
  328. out[7] = q[3];
  329. return out;
  330. }
  331. /**
  332. * Gets the translation of a normalized dual quat
  333. * @param {vec3} out translation
  334. * @param {quat2} a Dual Quaternion to be decomposed
  335. * @return {vec3} translation
  336. */
  337. function getTranslation(out, a) {
  338. var ax = a[4],
  339. ay = a[5],
  340. az = a[6],
  341. aw = a[7],
  342. bx = -a[0],
  343. by = -a[1],
  344. bz = -a[2],
  345. bw = a[3];
  346. out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
  347. out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
  348. out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
  349. return out;
  350. }
  351. /**
  352. * Translates a dual quat by the given vector
  353. *
  354. * @param {quat2} out the receiving dual quaternion
  355. * @param {quat2} a the dual quaternion to translate
  356. * @param {vec3} v vector to translate by
  357. * @returns {quat2} out
  358. */
  359. function translate(out, a, v) {
  360. var ax1 = a[0],
  361. ay1 = a[1],
  362. az1 = a[2],
  363. aw1 = a[3],
  364. bx1 = v[0] * 0.5,
  365. by1 = v[1] * 0.5,
  366. bz1 = v[2] * 0.5,
  367. ax2 = a[4],
  368. ay2 = a[5],
  369. az2 = a[6],
  370. aw2 = a[7];
  371. out[0] = ax1;
  372. out[1] = ay1;
  373. out[2] = az1;
  374. out[3] = aw1;
  375. out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
  376. out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
  377. out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
  378. out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
  379. return out;
  380. }
  381. /**
  382. * Rotates a dual quat around the X axis
  383. *
  384. * @param {quat2} out the receiving dual quaternion
  385. * @param {quat2} a the dual quaternion to rotate
  386. * @param {number} rad how far should the rotation be
  387. * @returns {quat2} out
  388. */
  389. function rotateX(out, a, rad) {
  390. var bx = -a[0],
  391. by = -a[1],
  392. bz = -a[2],
  393. bw = a[3],
  394. ax = a[4],
  395. ay = a[5],
  396. az = a[6],
  397. aw = a[7],
  398. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  399. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  400. az1 = az * bw + aw * bz + ax * by - ay * bx,
  401. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  402. quat.rotateX(out, a, rad);
  403. bx = out[0];
  404. by = out[1];
  405. bz = out[2];
  406. bw = out[3];
  407. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  408. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  409. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  410. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  411. return out;
  412. }
  413. /**
  414. * Rotates a dual quat around the Y axis
  415. *
  416. * @param {quat2} out the receiving dual quaternion
  417. * @param {quat2} a the dual quaternion to rotate
  418. * @param {number} rad how far should the rotation be
  419. * @returns {quat2} out
  420. */
  421. function rotateY(out, a, rad) {
  422. var bx = -a[0],
  423. by = -a[1],
  424. bz = -a[2],
  425. bw = a[3],
  426. ax = a[4],
  427. ay = a[5],
  428. az = a[6],
  429. aw = a[7],
  430. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  431. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  432. az1 = az * bw + aw * bz + ax * by - ay * bx,
  433. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  434. quat.rotateY(out, a, rad);
  435. bx = out[0];
  436. by = out[1];
  437. bz = out[2];
  438. bw = out[3];
  439. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  440. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  441. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  442. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  443. return out;
  444. }
  445. /**
  446. * Rotates a dual quat around the Z axis
  447. *
  448. * @param {quat2} out the receiving dual quaternion
  449. * @param {quat2} a the dual quaternion to rotate
  450. * @param {number} rad how far should the rotation be
  451. * @returns {quat2} out
  452. */
  453. function rotateZ(out, a, rad) {
  454. var bx = -a[0],
  455. by = -a[1],
  456. bz = -a[2],
  457. bw = a[3],
  458. ax = a[4],
  459. ay = a[5],
  460. az = a[6],
  461. aw = a[7],
  462. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  463. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  464. az1 = az * bw + aw * bz + ax * by - ay * bx,
  465. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  466. quat.rotateZ(out, a, rad);
  467. bx = out[0];
  468. by = out[1];
  469. bz = out[2];
  470. bw = out[3];
  471. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  472. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  473. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  474. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  475. return out;
  476. }
  477. /**
  478. * Rotates a dual quat by a given quaternion (a * q)
  479. *
  480. * @param {quat2} out the receiving dual quaternion
  481. * @param {quat2} a the dual quaternion to rotate
  482. * @param {quat} q quaternion to rotate by
  483. * @returns {quat2} out
  484. */
  485. function rotateByQuatAppend(out, a, q) {
  486. var qx = q[0],
  487. qy = q[1],
  488. qz = q[2],
  489. qw = q[3],
  490. ax = a[0],
  491. ay = a[1],
  492. az = a[2],
  493. aw = a[3];
  494. out[0] = ax * qw + aw * qx + ay * qz - az * qy;
  495. out[1] = ay * qw + aw * qy + az * qx - ax * qz;
  496. out[2] = az * qw + aw * qz + ax * qy - ay * qx;
  497. out[3] = aw * qw - ax * qx - ay * qy - az * qz;
  498. ax = a[4];
  499. ay = a[5];
  500. az = a[6];
  501. aw = a[7];
  502. out[4] = ax * qw + aw * qx + ay * qz - az * qy;
  503. out[5] = ay * qw + aw * qy + az * qx - ax * qz;
  504. out[6] = az * qw + aw * qz + ax * qy - ay * qx;
  505. out[7] = aw * qw - ax * qx - ay * qy - az * qz;
  506. return out;
  507. }
  508. /**
  509. * Rotates a dual quat by a given quaternion (q * a)
  510. *
  511. * @param {quat2} out the receiving dual quaternion
  512. * @param {quat} q quaternion to rotate by
  513. * @param {quat2} a the dual quaternion to rotate
  514. * @returns {quat2} out
  515. */
  516. function rotateByQuatPrepend(out, q, a) {
  517. var qx = q[0],
  518. qy = q[1],
  519. qz = q[2],
  520. qw = q[3],
  521. bx = a[0],
  522. by = a[1],
  523. bz = a[2],
  524. bw = a[3];
  525. out[0] = qx * bw + qw * bx + qy * bz - qz * by;
  526. out[1] = qy * bw + qw * by + qz * bx - qx * bz;
  527. out[2] = qz * bw + qw * bz + qx * by - qy * bx;
  528. out[3] = qw * bw - qx * bx - qy * by - qz * bz;
  529. bx = a[4];
  530. by = a[5];
  531. bz = a[6];
  532. bw = a[7];
  533. out[4] = qx * bw + qw * bx + qy * bz - qz * by;
  534. out[5] = qy * bw + qw * by + qz * bx - qx * bz;
  535. out[6] = qz * bw + qw * bz + qx * by - qy * bx;
  536. out[7] = qw * bw - qx * bx - qy * by - qz * bz;
  537. return out;
  538. }
  539. /**
  540. * Rotates a dual quat around a given axis. Does the normalisation automatically
  541. *
  542. * @param {quat2} out the receiving dual quaternion
  543. * @param {quat2} a the dual quaternion to rotate
  544. * @param {vec3} axis the axis to rotate around
  545. * @param {Number} rad how far the rotation should be
  546. * @returns {quat2} out
  547. */
  548. function rotateAroundAxis(out, a, axis, rad) {
  549. //Special case for rad = 0
  550. if (Math.abs(rad) < glMatrix.EPSILON) {
  551. return copy(out, a);
  552. }
  553. var axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
  554. rad = rad * 0.5;
  555. var s = Math.sin(rad);
  556. var bx = s * axis[0] / axisLength;
  557. var by = s * axis[1] / axisLength;
  558. var bz = s * axis[2] / axisLength;
  559. var bw = Math.cos(rad);
  560. var ax1 = a[0],
  561. ay1 = a[1],
  562. az1 = a[2],
  563. aw1 = a[3];
  564. out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  565. out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  566. out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  567. out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  568. var ax = a[4],
  569. ay = a[5],
  570. az = a[6],
  571. aw = a[7];
  572. out[4] = ax * bw + aw * bx + ay * bz - az * by;
  573. out[5] = ay * bw + aw * by + az * bx - ax * bz;
  574. out[6] = az * bw + aw * bz + ax * by - ay * bx;
  575. out[7] = aw * bw - ax * bx - ay * by - az * bz;
  576. return out;
  577. }
  578. /**
  579. * Adds two dual quat's
  580. *
  581. * @param {quat2} out the receiving dual quaternion
  582. * @param {quat2} a the first operand
  583. * @param {quat2} b the second operand
  584. * @returns {quat2} out
  585. * @function
  586. */
  587. function add(out, a, b) {
  588. out[0] = a[0] + b[0];
  589. out[1] = a[1] + b[1];
  590. out[2] = a[2] + b[2];
  591. out[3] = a[3] + b[3];
  592. out[4] = a[4] + b[4];
  593. out[5] = a[5] + b[5];
  594. out[6] = a[6] + b[6];
  595. out[7] = a[7] + b[7];
  596. return out;
  597. }
  598. /**
  599. * Multiplies two dual quat's
  600. *
  601. * @param {quat2} out the receiving dual quaternion
  602. * @param {quat2} a the first operand
  603. * @param {quat2} b the second operand
  604. * @returns {quat2} out
  605. */
  606. function multiply(out, a, b) {
  607. var ax0 = a[0],
  608. ay0 = a[1],
  609. az0 = a[2],
  610. aw0 = a[3],
  611. bx1 = b[4],
  612. by1 = b[5],
  613. bz1 = b[6],
  614. bw1 = b[7],
  615. ax1 = a[4],
  616. ay1 = a[5],
  617. az1 = a[6],
  618. aw1 = a[7],
  619. bx0 = b[0],
  620. by0 = b[1],
  621. bz0 = b[2],
  622. bw0 = b[3];
  623. out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
  624. out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
  625. out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
  626. out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
  627. out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0;
  628. out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0;
  629. out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0;
  630. out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0;
  631. return out;
  632. }
  633. /**
  634. * Alias for {@link quat2.multiply}
  635. * @function
  636. */
  637. var mul = exports.mul = multiply;
  638. /**
  639. * Scales a dual quat by a scalar number
  640. *
  641. * @param {quat2} out the receiving dual quat
  642. * @param {quat2} a the dual quat to scale
  643. * @param {Number} b amount to scale the dual quat by
  644. * @returns {quat2} out
  645. * @function
  646. */
  647. function scale(out, a, b) {
  648. out[0] = a[0] * b;
  649. out[1] = a[1] * b;
  650. out[2] = a[2] * b;
  651. out[3] = a[3] * b;
  652. out[4] = a[4] * b;
  653. out[5] = a[5] * b;
  654. out[6] = a[6] * b;
  655. out[7] = a[7] * b;
  656. return out;
  657. }
  658. /**
  659. * Calculates the dot product of two dual quat's (The dot product of the real parts)
  660. *
  661. * @param {quat2} a the first operand
  662. * @param {quat2} b the second operand
  663. * @returns {Number} dot product of a and b
  664. * @function
  665. */
  666. var dot = exports.dot = quat.dot;
  667. /**
  668. * Performs a linear interpolation between two dual quats's
  669. * NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
  670. *
  671. * @param {quat2} out the receiving dual quat
  672. * @param {quat2} a the first operand
  673. * @param {quat2} b the second operand
  674. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  675. * @returns {quat2} out
  676. */
  677. function lerp(out, a, b, t) {
  678. var mt = 1 - t;
  679. if (dot(a, b) < 0) t = -t;
  680. out[0] = a[0] * mt + b[0] * t;
  681. out[1] = a[1] * mt + b[1] * t;
  682. out[2] = a[2] * mt + b[2] * t;
  683. out[3] = a[3] * mt + b[3] * t;
  684. out[4] = a[4] * mt + b[4] * t;
  685. out[5] = a[5] * mt + b[5] * t;
  686. out[6] = a[6] * mt + b[6] * t;
  687. out[7] = a[7] * mt + b[7] * t;
  688. return out;
  689. }
  690. /**
  691. * Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
  692. *
  693. * @param {quat2} out the receiving dual quaternion
  694. * @param {quat2} a dual quat to calculate inverse of
  695. * @returns {quat2} out
  696. */
  697. function invert(out, a) {
  698. var sqlen = squaredLength(a);
  699. out[0] = -a[0] / sqlen;
  700. out[1] = -a[1] / sqlen;
  701. out[2] = -a[2] / sqlen;
  702. out[3] = a[3] / sqlen;
  703. out[4] = -a[4] / sqlen;
  704. out[5] = -a[5] / sqlen;
  705. out[6] = -a[6] / sqlen;
  706. out[7] = a[7] / sqlen;
  707. return out;
  708. }
  709. /**
  710. * Calculates the conjugate of a dual quat
  711. * If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
  712. *
  713. * @param {quat2} out the receiving quaternion
  714. * @param {quat2} a quat to calculate conjugate of
  715. * @returns {quat2} out
  716. */
  717. function conjugate(out, a) {
  718. out[0] = -a[0];
  719. out[1] = -a[1];
  720. out[2] = -a[2];
  721. out[3] = a[3];
  722. out[4] = -a[4];
  723. out[5] = -a[5];
  724. out[6] = -a[6];
  725. out[7] = a[7];
  726. return out;
  727. }
  728. /**
  729. * Calculates the length of a dual quat
  730. *
  731. * @param {quat2} a dual quat to calculate length of
  732. * @returns {Number} length of a
  733. * @function
  734. */
  735. var length = exports.length = quat.length;
  736. /**
  737. * Alias for {@link quat2.length}
  738. * @function
  739. */
  740. var len = exports.len = length;
  741. /**
  742. * Calculates the squared length of a dual quat
  743. *
  744. * @param {quat2} a dual quat to calculate squared length of
  745. * @returns {Number} squared length of a
  746. * @function
  747. */
  748. var squaredLength = exports.squaredLength = quat.squaredLength;
  749. /**
  750. * Alias for {@link quat2.squaredLength}
  751. * @function
  752. */
  753. var sqrLen = exports.sqrLen = squaredLength;
  754. /**
  755. * Normalize a dual quat
  756. *
  757. * @param {quat2} out the receiving dual quaternion
  758. * @param {quat2} a dual quaternion to normalize
  759. * @returns {quat2} out
  760. * @function
  761. */
  762. function normalize(out, a) {
  763. var magnitude = squaredLength(a);
  764. if (magnitude > 0) {
  765. magnitude = Math.sqrt(magnitude);
  766. var a0 = a[0] / magnitude;
  767. var a1 = a[1] / magnitude;
  768. var a2 = a[2] / magnitude;
  769. var a3 = a[3] / magnitude;
  770. var b0 = a[4];
  771. var b1 = a[5];
  772. var b2 = a[6];
  773. var b3 = a[7];
  774. var a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
  775. out[0] = a0;
  776. out[1] = a1;
  777. out[2] = a2;
  778. out[3] = a3;
  779. out[4] = (b0 - a0 * a_dot_b) / magnitude;
  780. out[5] = (b1 - a1 * a_dot_b) / magnitude;
  781. out[6] = (b2 - a2 * a_dot_b) / magnitude;
  782. out[7] = (b3 - a3 * a_dot_b) / magnitude;
  783. }
  784. return out;
  785. }
  786. /**
  787. * Returns a string representation of a dual quatenion
  788. *
  789. * @param {quat2} a dual quaternion to represent as a string
  790. * @returns {String} string representation of the dual quat
  791. */
  792. function str(a) {
  793. return 'quat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ')';
  794. }
  795. /**
  796. * Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
  797. *
  798. * @param {quat2} a the first dual quaternion.
  799. * @param {quat2} b the second dual quaternion.
  800. * @returns {Boolean} true if the dual quaternions are equal, false otherwise.
  801. */
  802. function exactEquals(a, b) {
  803. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7];
  804. }
  805. /**
  806. * Returns whether or not the dual quaternions have approximately the same elements in the same position.
  807. *
  808. * @param {quat2} a the first dual quat.
  809. * @param {quat2} b the second dual quat.
  810. * @returns {Boolean} true if the dual quats are equal, false otherwise.
  811. */
  812. function equals(a, b) {
  813. var a0 = a[0],
  814. a1 = a[1],
  815. a2 = a[2],
  816. a3 = a[3],
  817. a4 = a[4],
  818. a5 = a[5],
  819. a6 = a[6],
  820. a7 = a[7];
  821. var b0 = b[0],
  822. b1 = b[1],
  823. b2 = b[2],
  824. b3 = b[3],
  825. b4 = b[4],
  826. b5 = b[5],
  827. b6 = b[6],
  828. b7 = b[7];
  829. return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7));
  830. }