quat.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import * as glMatrix from "./common.js";
  2. import * as mat3 from "./mat3.js";
  3. import * as vec3 from "./vec3.js";
  4. import * as vec4 from "./vec4.js";
  5. /**
  6. * Quaternion
  7. * @module quat
  8. */
  9. /**
  10. * Creates a new identity quat
  11. *
  12. * @returns {quat} a new quaternion
  13. */
  14. export function create() {
  15. var out = new glMatrix.ARRAY_TYPE(4);
  16. if (glMatrix.ARRAY_TYPE != Float32Array) {
  17. out[0] = 0;
  18. out[1] = 0;
  19. out[2] = 0;
  20. }
  21. out[3] = 1;
  22. return out;
  23. }
  24. /**
  25. * Set a quat to the identity quaternion
  26. *
  27. * @param {quat} out the receiving quaternion
  28. * @returns {quat} out
  29. */
  30. export function identity(out) {
  31. out[0] = 0;
  32. out[1] = 0;
  33. out[2] = 0;
  34. out[3] = 1;
  35. return out;
  36. }
  37. /**
  38. * Sets a quat from the given angle and rotation axis,
  39. * then returns it.
  40. *
  41. * @param {quat} out the receiving quaternion
  42. * @param {ReadonlyVec3} axis the axis around which to rotate
  43. * @param {Number} rad the angle in radians
  44. * @returns {quat} out
  45. **/
  46. export function setAxisAngle(out, axis, rad) {
  47. rad = rad * 0.5;
  48. var s = Math.sin(rad);
  49. out[0] = s * axis[0];
  50. out[1] = s * axis[1];
  51. out[2] = s * axis[2];
  52. out[3] = Math.cos(rad);
  53. return out;
  54. }
  55. /**
  56. * Gets the rotation axis and angle for a given
  57. * quaternion. If a quaternion is created with
  58. * setAxisAngle, this method will return the same
  59. * values as providied in the original parameter list
  60. * OR functionally equivalent values.
  61. * Example: The quaternion formed by axis [0, 0, 1] and
  62. * angle -90 is the same as the quaternion formed by
  63. * [0, 0, 1] and 270. This method favors the latter.
  64. * @param {vec3} out_axis Vector receiving the axis of rotation
  65. * @param {ReadonlyQuat} q Quaternion to be decomposed
  66. * @return {Number} Angle, in radians, of the rotation
  67. */
  68. export function getAxisAngle(out_axis, q) {
  69. var rad = Math.acos(q[3]) * 2.0;
  70. var s = Math.sin(rad / 2.0);
  71. if (s > glMatrix.EPSILON) {
  72. out_axis[0] = q[0] / s;
  73. out_axis[1] = q[1] / s;
  74. out_axis[2] = q[2] / s;
  75. } else {
  76. // If s is zero, return any axis (no rotation - axis does not matter)
  77. out_axis[0] = 1;
  78. out_axis[1] = 0;
  79. out_axis[2] = 0;
  80. }
  81. return rad;
  82. }
  83. /**
  84. * Gets the angular distance between two unit quaternions
  85. *
  86. * @param {ReadonlyQuat} a Origin unit quaternion
  87. * @param {ReadonlyQuat} b Destination unit quaternion
  88. * @return {Number} Angle, in radians, between the two quaternions
  89. */
  90. export function getAngle(a, b) {
  91. var dotproduct = dot(a, b);
  92. return Math.acos(2 * dotproduct * dotproduct - 1);
  93. }
  94. /**
  95. * Multiplies two quat's
  96. *
  97. * @param {quat} out the receiving quaternion
  98. * @param {ReadonlyQuat} a the first operand
  99. * @param {ReadonlyQuat} b the second operand
  100. * @returns {quat} out
  101. */
  102. export function multiply(out, a, b) {
  103. var ax = a[0],
  104. ay = a[1],
  105. az = a[2],
  106. aw = a[3];
  107. var bx = b[0],
  108. by = b[1],
  109. bz = b[2],
  110. bw = b[3];
  111. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  112. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  113. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  114. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  115. return out;
  116. }
  117. /**
  118. * Rotates a quaternion by the given angle about the X axis
  119. *
  120. * @param {quat} out quat receiving operation result
  121. * @param {ReadonlyQuat} a quat to rotate
  122. * @param {number} rad angle (in radians) to rotate
  123. * @returns {quat} out
  124. */
  125. export function rotateX(out, a, rad) {
  126. rad *= 0.5;
  127. var ax = a[0],
  128. ay = a[1],
  129. az = a[2],
  130. aw = a[3];
  131. var bx = Math.sin(rad),
  132. bw = Math.cos(rad);
  133. out[0] = ax * bw + aw * bx;
  134. out[1] = ay * bw + az * bx;
  135. out[2] = az * bw - ay * bx;
  136. out[3] = aw * bw - ax * bx;
  137. return out;
  138. }
  139. /**
  140. * Rotates a quaternion by the given angle about the Y axis
  141. *
  142. * @param {quat} out quat receiving operation result
  143. * @param {ReadonlyQuat} a quat to rotate
  144. * @param {number} rad angle (in radians) to rotate
  145. * @returns {quat} out
  146. */
  147. export function rotateY(out, a, rad) {
  148. rad *= 0.5;
  149. var ax = a[0],
  150. ay = a[1],
  151. az = a[2],
  152. aw = a[3];
  153. var by = Math.sin(rad),
  154. bw = Math.cos(rad);
  155. out[0] = ax * bw - az * by;
  156. out[1] = ay * bw + aw * by;
  157. out[2] = az * bw + ax * by;
  158. out[3] = aw * bw - ay * by;
  159. return out;
  160. }
  161. /**
  162. * Rotates a quaternion by the given angle about the Z axis
  163. *
  164. * @param {quat} out quat receiving operation result
  165. * @param {ReadonlyQuat} a quat to rotate
  166. * @param {number} rad angle (in radians) to rotate
  167. * @returns {quat} out
  168. */
  169. export function rotateZ(out, a, rad) {
  170. rad *= 0.5;
  171. var ax = a[0],
  172. ay = a[1],
  173. az = a[2],
  174. aw = a[3];
  175. var bz = Math.sin(rad),
  176. bw = Math.cos(rad);
  177. out[0] = ax * bw + ay * bz;
  178. out[1] = ay * bw - ax * bz;
  179. out[2] = az * bw + aw * bz;
  180. out[3] = aw * bw - az * bz;
  181. return out;
  182. }
  183. /**
  184. * Calculates the W component of a quat from the X, Y, and Z components.
  185. * Assumes that quaternion is 1 unit in length.
  186. * Any existing W component will be ignored.
  187. *
  188. * @param {quat} out the receiving quaternion
  189. * @param {ReadonlyQuat} a quat to calculate W component of
  190. * @returns {quat} out
  191. */
  192. export function calculateW(out, a) {
  193. var x = a[0],
  194. y = a[1],
  195. z = a[2];
  196. out[0] = x;
  197. out[1] = y;
  198. out[2] = z;
  199. out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
  200. return out;
  201. }
  202. /**
  203. * Calculate the exponential of a unit quaternion.
  204. *
  205. * @param {quat} out the receiving quaternion
  206. * @param {ReadonlyQuat} a quat to calculate the exponential of
  207. * @returns {quat} out
  208. */
  209. export function exp(out, a) {
  210. var x = a[0],
  211. y = a[1],
  212. z = a[2],
  213. w = a[3];
  214. var r = Math.sqrt(x * x + y * y + z * z);
  215. var et = Math.exp(w);
  216. var s = r > 0 ? et * Math.sin(r) / r : 0;
  217. out[0] = x * s;
  218. out[1] = y * s;
  219. out[2] = z * s;
  220. out[3] = et * Math.cos(r);
  221. return out;
  222. }
  223. /**
  224. * Calculate the natural logarithm of a unit quaternion.
  225. *
  226. * @param {quat} out the receiving quaternion
  227. * @param {ReadonlyQuat} a quat to calculate the exponential of
  228. * @returns {quat} out
  229. */
  230. export function ln(out, a) {
  231. var x = a[0],
  232. y = a[1],
  233. z = a[2],
  234. w = a[3];
  235. var r = Math.sqrt(x * x + y * y + z * z);
  236. var t = r > 0 ? Math.atan2(r, w) / r : 0;
  237. out[0] = x * t;
  238. out[1] = y * t;
  239. out[2] = z * t;
  240. out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
  241. return out;
  242. }
  243. /**
  244. * Calculate the scalar power of a unit quaternion.
  245. *
  246. * @param {quat} out the receiving quaternion
  247. * @param {ReadonlyQuat} a quat to calculate the exponential of
  248. * @param {Number} b amount to scale the quaternion by
  249. * @returns {quat} out
  250. */
  251. export function pow(out, a, b) {
  252. ln(out, a);
  253. scale(out, out, b);
  254. exp(out, out);
  255. return out;
  256. }
  257. /**
  258. * Performs a spherical linear interpolation between two quat
  259. *
  260. * @param {quat} out the receiving quaternion
  261. * @param {ReadonlyQuat} a the first operand
  262. * @param {ReadonlyQuat} b the second operand
  263. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  264. * @returns {quat} out
  265. */
  266. export function slerp(out, a, b, t) {
  267. // benchmarks:
  268. // http://jsperf.com/quaternion-slerp-implementations
  269. var ax = a[0],
  270. ay = a[1],
  271. az = a[2],
  272. aw = a[3];
  273. var bx = b[0],
  274. by = b[1],
  275. bz = b[2],
  276. bw = b[3];
  277. var omega, cosom, sinom, scale0, scale1; // calc cosine
  278. cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
  279. if (cosom < 0.0) {
  280. cosom = -cosom;
  281. bx = -bx;
  282. by = -by;
  283. bz = -bz;
  284. bw = -bw;
  285. } // calculate coefficients
  286. if (1.0 - cosom > glMatrix.EPSILON) {
  287. // standard case (slerp)
  288. omega = Math.acos(cosom);
  289. sinom = Math.sin(omega);
  290. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  291. scale1 = Math.sin(t * omega) / sinom;
  292. } else {
  293. // "from" and "to" quaternions are very close
  294. // ... so we can do a linear interpolation
  295. scale0 = 1.0 - t;
  296. scale1 = t;
  297. } // calculate final values
  298. out[0] = scale0 * ax + scale1 * bx;
  299. out[1] = scale0 * ay + scale1 * by;
  300. out[2] = scale0 * az + scale1 * bz;
  301. out[3] = scale0 * aw + scale1 * bw;
  302. return out;
  303. }
  304. /**
  305. * Generates a random unit quaternion
  306. *
  307. * @param {quat} out the receiving quaternion
  308. * @returns {quat} out
  309. */
  310. export function random(out) {
  311. // Implementation of http://planning.cs.uiuc.edu/node198.html
  312. // TODO: Calling random 3 times is probably not the fastest solution
  313. var u1 = glMatrix.RANDOM();
  314. var u2 = glMatrix.RANDOM();
  315. var u3 = glMatrix.RANDOM();
  316. var sqrt1MinusU1 = Math.sqrt(1 - u1);
  317. var sqrtU1 = Math.sqrt(u1);
  318. out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
  319. out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
  320. out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
  321. out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
  322. return out;
  323. }
  324. /**
  325. * Calculates the inverse of a quat
  326. *
  327. * @param {quat} out the receiving quaternion
  328. * @param {ReadonlyQuat} a quat to calculate inverse of
  329. * @returns {quat} out
  330. */
  331. export function invert(out, a) {
  332. var a0 = a[0],
  333. a1 = a[1],
  334. a2 = a[2],
  335. a3 = a[3];
  336. var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
  337. var invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
  338. out[0] = -a0 * invDot;
  339. out[1] = -a1 * invDot;
  340. out[2] = -a2 * invDot;
  341. out[3] = a3 * invDot;
  342. return out;
  343. }
  344. /**
  345. * Calculates the conjugate of a quat
  346. * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
  347. *
  348. * @param {quat} out the receiving quaternion
  349. * @param {ReadonlyQuat} a quat to calculate conjugate of
  350. * @returns {quat} out
  351. */
  352. export function conjugate(out, a) {
  353. out[0] = -a[0];
  354. out[1] = -a[1];
  355. out[2] = -a[2];
  356. out[3] = a[3];
  357. return out;
  358. }
  359. /**
  360. * Creates a quaternion from the given 3x3 rotation matrix.
  361. *
  362. * NOTE: The resultant quaternion is not normalized, so you should be sure
  363. * to renormalize the quaternion yourself where necessary.
  364. *
  365. * @param {quat} out the receiving quaternion
  366. * @param {ReadonlyMat3} m rotation matrix
  367. * @returns {quat} out
  368. * @function
  369. */
  370. export function fromMat3(out, m) {
  371. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  372. // article "Quaternion Calculus and Fast Animation".
  373. var fTrace = m[0] + m[4] + m[8];
  374. var fRoot;
  375. if (fTrace > 0.0) {
  376. // |w| > 1/2, may as well choose w > 1/2
  377. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  378. out[3] = 0.5 * fRoot;
  379. fRoot = 0.5 / fRoot; // 1/(4w)
  380. out[0] = (m[5] - m[7]) * fRoot;
  381. out[1] = (m[6] - m[2]) * fRoot;
  382. out[2] = (m[1] - m[3]) * fRoot;
  383. } else {
  384. // |w| <= 1/2
  385. var i = 0;
  386. if (m[4] > m[0]) i = 1;
  387. if (m[8] > m[i * 3 + i]) i = 2;
  388. var j = (i + 1) % 3;
  389. var k = (i + 2) % 3;
  390. fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
  391. out[i] = 0.5 * fRoot;
  392. fRoot = 0.5 / fRoot;
  393. out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
  394. out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
  395. out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
  396. }
  397. return out;
  398. }
  399. /**
  400. * Creates a quaternion from the given euler angle x, y, z.
  401. *
  402. * @param {quat} out the receiving quaternion
  403. * @param {x} Angle to rotate around X axis in degrees.
  404. * @param {y} Angle to rotate around Y axis in degrees.
  405. * @param {z} Angle to rotate around Z axis in degrees.
  406. * @returns {quat} out
  407. * @function
  408. */
  409. export function fromEuler(out, x, y, z) {
  410. var halfToRad = 0.5 * Math.PI / 180.0;
  411. x *= halfToRad;
  412. y *= halfToRad;
  413. z *= halfToRad;
  414. var sx = Math.sin(x);
  415. var cx = Math.cos(x);
  416. var sy = Math.sin(y);
  417. var cy = Math.cos(y);
  418. var sz = Math.sin(z);
  419. var cz = Math.cos(z);
  420. out[0] = sx * cy * cz - cx * sy * sz;
  421. out[1] = cx * sy * cz + sx * cy * sz;
  422. out[2] = cx * cy * sz - sx * sy * cz;
  423. out[3] = cx * cy * cz + sx * sy * sz;
  424. return out;
  425. }
  426. /**
  427. * Returns a string representation of a quatenion
  428. *
  429. * @param {ReadonlyQuat} a vector to represent as a string
  430. * @returns {String} string representation of the vector
  431. */
  432. export function str(a) {
  433. return "quat(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  434. }
  435. /**
  436. * Creates a new quat initialized with values from an existing quaternion
  437. *
  438. * @param {ReadonlyQuat} a quaternion to clone
  439. * @returns {quat} a new quaternion
  440. * @function
  441. */
  442. export var clone = vec4.clone;
  443. /**
  444. * Creates a new quat initialized with the given values
  445. *
  446. * @param {Number} x X component
  447. * @param {Number} y Y component
  448. * @param {Number} z Z component
  449. * @param {Number} w W component
  450. * @returns {quat} a new quaternion
  451. * @function
  452. */
  453. export var fromValues = vec4.fromValues;
  454. /**
  455. * Copy the values from one quat to another
  456. *
  457. * @param {quat} out the receiving quaternion
  458. * @param {ReadonlyQuat} a the source quaternion
  459. * @returns {quat} out
  460. * @function
  461. */
  462. export var copy = vec4.copy;
  463. /**
  464. * Set the components of a quat to the given values
  465. *
  466. * @param {quat} out the receiving quaternion
  467. * @param {Number} x X component
  468. * @param {Number} y Y component
  469. * @param {Number} z Z component
  470. * @param {Number} w W component
  471. * @returns {quat} out
  472. * @function
  473. */
  474. export var set = vec4.set;
  475. /**
  476. * Adds two quat's
  477. *
  478. * @param {quat} out the receiving quaternion
  479. * @param {ReadonlyQuat} a the first operand
  480. * @param {ReadonlyQuat} b the second operand
  481. * @returns {quat} out
  482. * @function
  483. */
  484. export var add = vec4.add;
  485. /**
  486. * Alias for {@link quat.multiply}
  487. * @function
  488. */
  489. export var mul = multiply;
  490. /**
  491. * Scales a quat by a scalar number
  492. *
  493. * @param {quat} out the receiving vector
  494. * @param {ReadonlyQuat} a the vector to scale
  495. * @param {Number} b amount to scale the vector by
  496. * @returns {quat} out
  497. * @function
  498. */
  499. export var scale = vec4.scale;
  500. /**
  501. * Calculates the dot product of two quat's
  502. *
  503. * @param {ReadonlyQuat} a the first operand
  504. * @param {ReadonlyQuat} b the second operand
  505. * @returns {Number} dot product of a and b
  506. * @function
  507. */
  508. export var dot = vec4.dot;
  509. /**
  510. * Performs a linear interpolation between two quat's
  511. *
  512. * @param {quat} out the receiving quaternion
  513. * @param {ReadonlyQuat} a the first operand
  514. * @param {ReadonlyQuat} b the second operand
  515. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  516. * @returns {quat} out
  517. * @function
  518. */
  519. export var lerp = vec4.lerp;
  520. /**
  521. * Calculates the length of a quat
  522. *
  523. * @param {ReadonlyQuat} a vector to calculate length of
  524. * @returns {Number} length of a
  525. */
  526. export var length = vec4.length;
  527. /**
  528. * Alias for {@link quat.length}
  529. * @function
  530. */
  531. export var len = length;
  532. /**
  533. * Calculates the squared length of a quat
  534. *
  535. * @param {ReadonlyQuat} a vector to calculate squared length of
  536. * @returns {Number} squared length of a
  537. * @function
  538. */
  539. export var squaredLength = vec4.squaredLength;
  540. /**
  541. * Alias for {@link quat.squaredLength}
  542. * @function
  543. */
  544. export var sqrLen = squaredLength;
  545. /**
  546. * Normalize a quat
  547. *
  548. * @param {quat} out the receiving quaternion
  549. * @param {ReadonlyQuat} a quaternion to normalize
  550. * @returns {quat} out
  551. * @function
  552. */
  553. export var normalize = vec4.normalize;
  554. /**
  555. * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
  556. *
  557. * @param {ReadonlyQuat} a The first quaternion.
  558. * @param {ReadonlyQuat} b The second quaternion.
  559. * @returns {Boolean} True if the vectors are equal, false otherwise.
  560. */
  561. export var exactEquals = vec4.exactEquals;
  562. /**
  563. * Returns whether or not the quaternions have approximately the same elements in the same position.
  564. *
  565. * @param {ReadonlyQuat} a The first vector.
  566. * @param {ReadonlyQuat} b The second vector.
  567. * @returns {Boolean} True if the vectors are equal, false otherwise.
  568. */
  569. export var equals = vec4.equals;
  570. /**
  571. * Sets a quaternion to represent the shortest rotation from one
  572. * vector to another.
  573. *
  574. * Both vectors are assumed to be unit length.
  575. *
  576. * @param {quat} out the receiving quaternion.
  577. * @param {ReadonlyVec3} a the initial vector
  578. * @param {ReadonlyVec3} b the destination vector
  579. * @returns {quat} out
  580. */
  581. export var rotationTo = function () {
  582. var tmpvec3 = vec3.create();
  583. var xUnitVec3 = vec3.fromValues(1, 0, 0);
  584. var yUnitVec3 = vec3.fromValues(0, 1, 0);
  585. return function (out, a, b) {
  586. var dot = vec3.dot(a, b);
  587. if (dot < -0.999999) {
  588. vec3.cross(tmpvec3, xUnitVec3, a);
  589. if (vec3.len(tmpvec3) < 0.000001) vec3.cross(tmpvec3, yUnitVec3, a);
  590. vec3.normalize(tmpvec3, tmpvec3);
  591. setAxisAngle(out, tmpvec3, Math.PI);
  592. return out;
  593. } else if (dot > 0.999999) {
  594. out[0] = 0;
  595. out[1] = 0;
  596. out[2] = 0;
  597. out[3] = 1;
  598. return out;
  599. } else {
  600. vec3.cross(tmpvec3, a, b);
  601. out[0] = tmpvec3[0];
  602. out[1] = tmpvec3[1];
  603. out[2] = tmpvec3[2];
  604. out[3] = 1 + dot;
  605. return normalize(out, out);
  606. }
  607. };
  608. }();
  609. /**
  610. * Performs a spherical linear interpolation with two control points
  611. *
  612. * @param {quat} out the receiving quaternion
  613. * @param {ReadonlyQuat} a the first operand
  614. * @param {ReadonlyQuat} b the second operand
  615. * @param {ReadonlyQuat} c the third operand
  616. * @param {ReadonlyQuat} d the fourth operand
  617. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  618. * @returns {quat} out
  619. */
  620. export var sqlerp = function () {
  621. var temp1 = create();
  622. var temp2 = create();
  623. return function (out, a, b, c, d, t) {
  624. slerp(temp1, a, d, t);
  625. slerp(temp2, b, c, t);
  626. slerp(out, temp1, temp2, 2 * t * (1 - t));
  627. return out;
  628. };
  629. }();
  630. /**
  631. * Sets the specified quaternion with values corresponding to the given
  632. * axes. Each axis is a vec3 and is expected to be unit length and
  633. * perpendicular to all other specified axes.
  634. *
  635. * @param {ReadonlyVec3} view the vector representing the viewing direction
  636. * @param {ReadonlyVec3} right the vector representing the local "right" direction
  637. * @param {ReadonlyVec3} up the vector representing the local "up" direction
  638. * @returns {quat} out
  639. */
  640. export var setAxes = function () {
  641. var matr = mat3.create();
  642. return function (out, view, right, up) {
  643. matr[0] = right[0];
  644. matr[3] = right[1];
  645. matr[6] = right[2];
  646. matr[1] = up[0];
  647. matr[4] = up[1];
  648. matr[7] = up[2];
  649. matr[2] = -view[0];
  650. matr[5] = -view[1];
  651. matr[8] = -view[2];
  652. return normalize(out, fromMat3(out, matr));
  653. };
  654. }();