quaternion.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview Implements quaternions and their conversion functions. In this
  8. * implementation, quaternions are represented as 4 element vectors with the
  9. * first 3 elements holding the imaginary components and the 4th element holding
  10. * the real component.
  11. */
  12. goog.provide('goog.vec.Quaternion');
  13. goog.provide('goog.vec.Quaternion.AnyType');
  14. goog.require('goog.vec');
  15. goog.require('goog.vec.Vec3');
  16. goog.require('goog.vec.Vec4');
  17. /** @typedef {!goog.vec.Float32} */ goog.vec.Quaternion.Float32;
  18. /** @typedef {!goog.vec.Float64} */ goog.vec.Quaternion.Float64;
  19. /** @typedef {!goog.vec.Number} */ goog.vec.Quaternion.Number;
  20. /** @typedef {!goog.vec.AnyType} */ goog.vec.Quaternion.AnyType;
  21. /**
  22. * Creates a Float32 quaternion, initialized to zero.
  23. *
  24. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  25. */
  26. goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32;
  27. /**
  28. * Creates a Float64 quaternion, initialized to zero.
  29. *
  30. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  31. */
  32. goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64;
  33. /**
  34. * Creates a Number quaternion, initialized to zero.
  35. *
  36. * @return {goog.vec.Quaternion.Number} The new quaternion.
  37. */
  38. goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber;
  39. /**
  40. * Creates a new Float32 quaternion initialized with the values from the
  41. * supplied array.
  42. *
  43. * @param {!goog.vec.AnyType} vec The source 4 element array.
  44. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  45. */
  46. goog.vec.Quaternion.createFloat32FromArray =
  47. goog.vec.Vec4.createFloat32FromArray;
  48. /**
  49. * Creates a new Float64 quaternion initialized with the values from the
  50. * supplied array.
  51. *
  52. * @param {!goog.vec.AnyType} vec The source 4 element array.
  53. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  54. */
  55. goog.vec.Quaternion.createFloat64FromArray =
  56. goog.vec.Vec4.createFloat64FromArray;
  57. /**
  58. * Creates a new Float32 quaternion initialized with the supplied values.
  59. *
  60. * @param {number} v0 The value for element at index 0.
  61. * @param {number} v1 The value for element at index 1.
  62. * @param {number} v2 The value for element at index 2.
  63. * @param {number} v3 The value for element at index 3.
  64. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  65. */
  66. goog.vec.Quaternion.createFloat32FromValues =
  67. goog.vec.Vec4.createFloat32FromValues;
  68. /**
  69. * Creates a new Float64 quaternion initialized with the supplied values.
  70. *
  71. * @param {number} v0 The value for element at index 0.
  72. * @param {number} v1 The value for element at index 1.
  73. * @param {number} v2 The value for element at index 2.
  74. * @param {number} v3 The value for element at index 3.
  75. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  76. */
  77. goog.vec.Quaternion.createFloat64FromValues =
  78. goog.vec.Vec4.createFloat64FromValues;
  79. /**
  80. * Creates a clone of the given Float32 quaternion.
  81. *
  82. * @param {!goog.vec.Quaternion.Float32} q The source quaternion.
  83. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  84. */
  85. goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32;
  86. /**
  87. * Creates a clone of the given Float64 quaternion.
  88. *
  89. * @param {!goog.vec.Quaternion.Float64} q The source quaternion.
  90. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  91. */
  92. goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64;
  93. /**
  94. * Creates a Float32 quaternion, initialized to the identity.
  95. *
  96. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  97. */
  98. goog.vec.Quaternion.createIdentityFloat32 = function() {
  99. var quat = goog.vec.Quaternion.createFloat32();
  100. goog.vec.Quaternion.makeIdentity(quat);
  101. return quat;
  102. };
  103. /**
  104. * Creates a Float64 quaternion, initialized to the identity.
  105. *
  106. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  107. */
  108. goog.vec.Quaternion.createIdentityFloat64 = function() {
  109. var quat = goog.vec.Quaternion.createFloat64();
  110. goog.vec.Quaternion.makeIdentity(quat);
  111. return quat;
  112. };
  113. /**
  114. * Initializes the quaternion with the given values.
  115. *
  116. * @param {!goog.vec.Quaternion.AnyType} q The quaternion to receive
  117. * the values.
  118. * @param {number} v0 The value for element at index 0.
  119. * @param {number} v1 The value for element at index 1.
  120. * @param {number} v2 The value for element at index 2.
  121. * @param {number} v3 The value for element at index 3.
  122. * @return {!goog.vec.Vec4.AnyType} return q so that operations can be
  123. * chained together.
  124. */
  125. goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues;
  126. /**
  127. * Initializes the quaternion with the given array of values.
  128. *
  129. * @param {!goog.vec.Quaternion.AnyType} q The quaternion to receive
  130. * the values.
  131. * @param {!goog.vec.AnyType} values The array of values.
  132. * @return {!goog.vec.Quaternion.AnyType} return q so that operations can be
  133. * chained together.
  134. */
  135. goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray;
  136. /**
  137. * Adds the two quaternions.
  138. *
  139. * @param {!goog.vec.Quaternion.AnyType} quat0 The first addend.
  140. * @param {!goog.vec.Quaternion.AnyType} quat1 The second addend.
  141. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  142. * receive the result. May be quat0 or quat1.
  143. */
  144. goog.vec.Quaternion.add = goog.vec.Vec4.add;
  145. /**
  146. * Negates a quaternion, storing the result into resultQuat.
  147. *
  148. * @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion to negate.
  149. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  150. * receive the result. May be quat0.
  151. */
  152. goog.vec.Quaternion.negate = goog.vec.Vec4.negate;
  153. /**
  154. * Multiplies each component of quat0 with scalar storing the product into
  155. * resultVec.
  156. *
  157. * @param {!goog.vec.Quaternion.AnyType} quat0 The source quaternion.
  158. * @param {number} scalar The value to multiply with each component of quat0.
  159. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  160. * receive the result. May be quat0.
  161. */
  162. goog.vec.Quaternion.scale = goog.vec.Vec4.scale;
  163. /**
  164. * Returns the square magnitude of the given quaternion.
  165. *
  166. * @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion.
  167. * @return {number} The magnitude of the quaternion.
  168. */
  169. goog.vec.Quaternion.magnitudeSquared = goog.vec.Vec4.magnitudeSquared;
  170. /**
  171. * Returns the magnitude of the given quaternion.
  172. *
  173. * @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion.
  174. * @return {number} The magnitude of the quaternion.
  175. */
  176. goog.vec.Quaternion.magnitude = goog.vec.Vec4.magnitude;
  177. /**
  178. * Normalizes the given quaternion storing the result into resultVec.
  179. *
  180. * @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion to
  181. * normalize.
  182. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  183. * receive the result. May be quat0.
  184. */
  185. goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize;
  186. /**
  187. * Computes the dot (scalar) product of two quaternions.
  188. *
  189. * @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion.
  190. * @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion.
  191. * @return {number} The scalar product.
  192. */
  193. goog.vec.Quaternion.dot = goog.vec.Vec4.dot;
  194. /**
  195. * Computes the inverse of the quaternion in quat, storing the result into
  196. * resultQuat.
  197. *
  198. * If the quaternion is already normalized, goog.vec.Quaternion.conjugate
  199. * is faster than this function and produces the same result.
  200. *
  201. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion to invert.
  202. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to receive
  203. * the result.
  204. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  205. * operations can be chained together.
  206. */
  207. goog.vec.Quaternion.invert = function(quat, resultQuat) {
  208. var a0 = quat[0], a1 = quat[1], a2 = quat[2], a3 = quat[3];
  209. var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
  210. var invDot = dot ? 1.0 / dot : 0;
  211. resultQuat[0] = -a0 * invDot;
  212. resultQuat[1] = -a1 * invDot;
  213. resultQuat[2] = -a2 * invDot;
  214. resultQuat[3] = a3 * invDot;
  215. return resultQuat;
  216. };
  217. /**
  218. * Computes the conjugate of the quaternion in quat, storing the result into
  219. * resultQuat.
  220. *
  221. * If the quaternion is normalized already, this function is faster than
  222. * goog.Quaternion.inverse and produces the same result.
  223. *
  224. * @param {!goog.vec.Quaternion.AnyType} quat The source quaternion.
  225. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  226. * receive the result.
  227. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  228. * operations can be chained together.
  229. */
  230. goog.vec.Quaternion.conjugate = function(quat, resultQuat) {
  231. resultQuat[0] = -quat[0];
  232. resultQuat[1] = -quat[1];
  233. resultQuat[2] = -quat[2];
  234. resultQuat[3] = quat[3];
  235. return resultQuat;
  236. };
  237. /**
  238. * Concatenates the two quaternions storing the result into resultQuat.
  239. *
  240. * @param {!goog.vec.Quaternion.AnyType} quat0 The first quaternion.
  241. * @param {!goog.vec.Quaternion.AnyType} quat1 The second quaternion.
  242. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  243. * receive the result.
  244. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  245. * operations can be chained together.
  246. */
  247. goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) {
  248. var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3];
  249. var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3];
  250. resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
  251. resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1;
  252. resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1;
  253. resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  254. return resultQuat;
  255. };
  256. /**
  257. * Makes the given quaternion the identity quaternion (0, 0, 0, 1).
  258. *
  259. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
  260. * @return {!goog.vec.Quaternion.AnyType} Return quat so that
  261. * operations can be chained together.
  262. */
  263. goog.vec.Quaternion.makeIdentity = function(quat) {
  264. quat[0] = 0;
  265. quat[1] = 0;
  266. quat[2] = 0;
  267. quat[3] = 1;
  268. return quat;
  269. };
  270. /**
  271. * Generates a unit quaternion from the given angle-axis rotation pair.
  272. * The rotation axis is not required to be a unit vector, but should
  273. * have non-zero length. The angle should be specified in radians.
  274. *
  275. * @param {number} angle The angle (in radians) to rotate about the axis.
  276. * @param {!goog.vec.Quaternion.AnyType} axis Unit vector specifying the
  277. * axis of rotation.
  278. * @param {!goog.vec.Quaternion.AnyType} quat Unit quaternion to store the
  279. * result.
  280. * @return {!goog.vec.Quaternion.AnyType} Return quat so that
  281. * operations can be chained together.
  282. */
  283. goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) {
  284. // Normalize the axis of rotation.
  285. goog.vec.Vec3.normalize(axis, axis);
  286. var halfAngle = 0.5 * angle;
  287. var sin = Math.sin(halfAngle);
  288. goog.vec.Quaternion.setFromValues(
  289. quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle));
  290. // Normalize the resulting quaternion.
  291. goog.vec.Quaternion.normalize(quat, quat);
  292. return quat;
  293. };
  294. /**
  295. * Generates an angle-axis rotation pair from a unit quaternion.
  296. * The quaternion is assumed to be of unit length. The calculated
  297. * values are returned via the passed 'axis' object and the 'angle'
  298. * number returned by the function itself. The returned rotation axis
  299. * is a non-zero length unit vector, and the returned angle is in
  300. * radians in the range of [-PI, +PI].
  301. *
  302. * @param {!goog.vec.Quaternion.AnyType} quat Unit quaternion to convert.
  303. * @param {!goog.vec.Quaternion.AnyType} axis Vector to store the returned
  304. * rotation axis.
  305. * @return {number} angle Angle (in radians) to rotate about 'axis'.
  306. * The range of the returned angle is [-PI, +PI].
  307. */
  308. goog.vec.Quaternion.toAngleAxis = function(quat, axis) {
  309. var angle = 2 * Math.acos(quat[3]);
  310. var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1);
  311. if (magnitude < goog.vec.EPSILON) {
  312. // This is nearly an identity rotation, so just use a fixed +X axis.
  313. goog.vec.Vec3.setFromValues(axis, 1, 0, 0);
  314. } else {
  315. // Compute the proper rotation axis.
  316. goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]);
  317. // Make sure the rotation axis is of unit length.
  318. goog.vec.Vec3.normalize(axis, axis);
  319. }
  320. // Adjust the range of the returned angle to [-PI, +PI].
  321. if (angle > Math.PI) {
  322. angle -= 2 * Math.PI;
  323. }
  324. return angle;
  325. };
  326. /**
  327. * Generates the quaternion from the given 3x3 rotation matrix.
  328. *
  329. * Perf: http://jsperf.com/conversion-of-3x3-matrix-to-quaternion
  330. * http://jsperf.com/goog-vec-fromrotationmatrix3-a
  331. *
  332. * @param {!goog.vec.AnyType} matrix The source matrix.
  333. * @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion.
  334. * @return {!goog.vec.Quaternion.AnyType} Return quat so that
  335. * operations can be chained together.
  336. */
  337. goog.vec.Quaternion.fromRotationMatrix3 = function(matrix, quat) {
  338. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  339. // article "Quaternion Calculus and Fast Animation".
  340. var fTrace = matrix[0] + matrix[4] + matrix[8];
  341. var fRoot;
  342. if (fTrace > 0.0) {
  343. // |w| > 1/2, may as well choose w > 1/2
  344. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  345. quat[3] = 0.5 * fRoot;
  346. fRoot = 0.5 / fRoot; // 1 / (4w)
  347. quat[0] = (matrix[5] - matrix[7]) * fRoot;
  348. quat[1] = (matrix[6] - matrix[2]) * fRoot;
  349. quat[2] = (matrix[1] - matrix[3]) * fRoot;
  350. } else {
  351. // |w| <= 1/2
  352. var i = 0;
  353. if (matrix[4] > matrix[0]) i = 1;
  354. if (matrix[8] > matrix[i * 3 + i]) i = 2;
  355. var j = (i + 1) % 3;
  356. var k = (i + 2) % 3;
  357. fRoot = Math.sqrt(
  358. matrix[i * 3 + i] - matrix[j * 3 + j] - matrix[k * 3 + k] + 1.0);
  359. quat[i] = 0.5 * fRoot;
  360. fRoot = 0.5 / fRoot;
  361. quat[3] = (matrix[j * 3 + k] - matrix[k * 3 + j]) * fRoot;
  362. quat[j] = (matrix[j * 3 + i] + matrix[i * 3 + j]) * fRoot;
  363. quat[k] = (matrix[k * 3 + i] + matrix[i * 3 + k]) * fRoot;
  364. // Flip all signs if w is negative.
  365. if (quat[3] < 0) {
  366. quat[0] = -quat[0];
  367. quat[1] = -quat[1];
  368. quat[2] = -quat[2];
  369. quat[3] = -quat[3];
  370. }
  371. }
  372. return quat;
  373. };
  374. /**
  375. * Generates the quaternion from the given 4x4 rotation matrix.
  376. *
  377. * Perf: http://jsperf.com/goog-vec-fromrotationmatrix4
  378. *
  379. * Implementation is the same as fromRotationMatrix3 but using indices from
  380. * the top left 3x3 in a 4x4 matrix.
  381. *
  382. * @param {!goog.vec.AnyType} matrix The source matrix.
  383. * @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion.
  384. * @return {!goog.vec.Quaternion.AnyType} Return quat so that
  385. * operations can be chained together.
  386. */
  387. goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
  388. var fTrace = matrix[0] + matrix[5] + matrix[10];
  389. var fRoot;
  390. if (fTrace > 0.0) {
  391. // |w| > 1/2, may as well choose w > 1/2
  392. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  393. quat[3] = 0.5 * fRoot;
  394. fRoot = 0.5 / fRoot; // 1 / (4w)
  395. quat[0] = (matrix[6] - matrix[9]) * fRoot;
  396. quat[1] = (matrix[8] - matrix[2]) * fRoot;
  397. quat[2] = (matrix[1] - matrix[4]) * fRoot;
  398. } else {
  399. // |w| <= 1/2
  400. var i = 0;
  401. if (matrix[5] > matrix[0]) i = 1;
  402. if (matrix[10] > matrix[i * 4 + i]) i = 2;
  403. var j = (i + 1) % 3;
  404. var k = (i + 2) % 3;
  405. fRoot = Math.sqrt(
  406. matrix[i * 4 + i] - matrix[j * 4 + j] - matrix[k * 4 + k] + 1.0);
  407. quat[i] = 0.5 * fRoot;
  408. fRoot = 0.5 / fRoot;
  409. quat[3] = (matrix[j * 4 + k] - matrix[k * 4 + j]) * fRoot;
  410. quat[j] = (matrix[j * 4 + i] + matrix[i * 4 + j]) * fRoot;
  411. quat[k] = (matrix[k * 4 + i] + matrix[i * 4 + k]) * fRoot;
  412. // Flip all signs if w is negative.
  413. if (quat[3] < 0) {
  414. quat[0] = -quat[0];
  415. quat[1] = -quat[1];
  416. quat[2] = -quat[2];
  417. quat[3] = -quat[3];
  418. }
  419. }
  420. return quat;
  421. };
  422. /**
  423. * Generates the 3x3 rotation matrix from the given quaternion.
  424. *
  425. * @param {!goog.vec.Quaternion.AnyType} quat The source quaternion.
  426. * @param {!goog.vec.AnyType} matrix The resulting matrix.
  427. * @return {!goog.vec.AnyType} Return resulting matrix so that
  428. * operations can be chained together.
  429. */
  430. goog.vec.Quaternion.toRotationMatrix3 = function(quat, matrix) {
  431. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  432. var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
  433. var wx = x2 * w;
  434. var wy = y2 * w;
  435. var wz = z2 * w;
  436. var xx = x2 * x;
  437. var xy = y2 * x;
  438. var xz = z2 * x;
  439. var yy = y2 * y;
  440. var yz = z2 * y;
  441. var zz = z2 * z;
  442. matrix[0] = 1 - (yy + zz);
  443. matrix[1] = xy + wz;
  444. matrix[2] = xz - wy;
  445. matrix[3] = xy - wz;
  446. matrix[4] = 1 - (xx + zz);
  447. matrix[5] = yz + wx;
  448. matrix[6] = xz + wy;
  449. matrix[7] = yz - wx;
  450. matrix[8] = 1 - (xx + yy);
  451. return matrix;
  452. };
  453. /**
  454. * Generates the 4x4 rotation matrix from the given quaternion.
  455. *
  456. * @param {!goog.vec.Quaternion.AnyType} quat The source quaternion.
  457. * @param {!goog.vec.AnyType} matrix The resulting matrix.
  458. * @return {!goog.vec.AnyType} Return resulting matrix so that
  459. * operations can be chained together.
  460. */
  461. goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) {
  462. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  463. var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
  464. var wx = x2 * w;
  465. var wy = y2 * w;
  466. var wz = z2 * w;
  467. var xx = x2 * x;
  468. var xy = y2 * x;
  469. var xz = z2 * x;
  470. var yy = y2 * y;
  471. var yz = z2 * y;
  472. var zz = z2 * z;
  473. matrix[0] = 1 - (yy + zz);
  474. matrix[1] = xy + wz;
  475. matrix[2] = xz - wy;
  476. matrix[3] = 0;
  477. matrix[4] = xy - wz;
  478. matrix[5] = 1 - (xx + zz);
  479. matrix[6] = yz + wx;
  480. matrix[7] = 0;
  481. matrix[8] = xz + wy;
  482. matrix[9] = yz - wx;
  483. matrix[10] = 1 - (xx + yy);
  484. matrix[11] = 0;
  485. matrix[12] = 0;
  486. matrix[13] = 0;
  487. matrix[14] = 0;
  488. matrix[15] = 1;
  489. return matrix;
  490. };
  491. /**
  492. * Rotates a quaternion by the given angle about the X axis.
  493. *
  494. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
  495. * @param {number} angle The angle in radians.
  496. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  497. * receive the result.
  498. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  499. * operations can be chained together.
  500. */
  501. goog.vec.Quaternion.rotateX = function(quat, angle, resultQuat) {
  502. angle *= 0.5;
  503. var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
  504. var bx = Math.sin(angle), bw = Math.cos(angle);
  505. resultQuat[0] = ax * bw + aw * bx;
  506. resultQuat[1] = ay * bw + az * bx;
  507. resultQuat[2] = az * bw - ay * bx;
  508. resultQuat[3] = aw * bw - ax * bx;
  509. return resultQuat;
  510. };
  511. /**
  512. * Rotates a quaternion by the given angle about the Y axis.
  513. *
  514. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
  515. * @param {number} angle The angle in radians.
  516. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  517. * receive the result.
  518. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  519. * operations can be chained together.
  520. */
  521. goog.vec.Quaternion.rotateY = function(quat, angle, resultQuat) {
  522. angle *= 0.5;
  523. var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
  524. var by = Math.sin(angle), bw = Math.cos(angle);
  525. resultQuat[0] = ax * bw - az * by;
  526. resultQuat[1] = ay * bw + aw * by;
  527. resultQuat[2] = az * bw + ax * by;
  528. resultQuat[3] = aw * bw - ay * by;
  529. return resultQuat;
  530. };
  531. /**
  532. * Rotates a quaternion by the given angle about the Z axis.
  533. *
  534. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
  535. * @param {number} angle The angle in radians.
  536. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  537. * receive the result.
  538. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  539. * operations can be chained together.
  540. */
  541. goog.vec.Quaternion.rotateZ = function(quat, angle, resultQuat) {
  542. angle *= 0.5;
  543. var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
  544. var bz = Math.sin(angle), bw = Math.cos(angle);
  545. resultQuat[0] = ax * bw + ay * bz;
  546. resultQuat[1] = ay * bw - ax * bz;
  547. resultQuat[2] = az * bw + aw * bz;
  548. resultQuat[3] = aw * bw - az * bz;
  549. return resultQuat;
  550. };
  551. /**
  552. * Transforms a vec with a quaternion. Works on both vec3s and vec4s.
  553. *
  554. * @param {!goog.vec.AnyType} vec The vec to transform.
  555. * @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
  556. * @param {!goog.vec.AnyType} resultVec The vec to receive the result.
  557. * @return {!goog.vec.AnyType} Return resultVec so that operations can be
  558. * chained together. Note that the caller is responsible for type-casting.
  559. */
  560. goog.vec.Quaternion.transformVec = function(vec, quat, resultVec) {
  561. var x = vec[0], y = vec[1], z = vec[2];
  562. var qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3];
  563. // Calculate quat * vec.
  564. var ix = qw * x + qy * z - qz * y;
  565. var iy = qw * y + qz * x - qx * z;
  566. var iz = qw * z + qx * y - qy * x;
  567. var iw = -qx * x - qy * y - qz * z;
  568. // Calculate result * inverse quat.
  569. resultVec[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  570. resultVec[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  571. resultVec[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  572. return resultVec;
  573. };
  574. /**
  575. * Computes the spherical linear interpolated value from the given quaternions
  576. * q0 and q1 according to the coefficient t. The resulting quaternion is stored
  577. * in resultQuat.
  578. *
  579. * @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion.
  580. * @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion.
  581. * @param {number} t The interpolating coefficient.
  582. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  583. * receive the result.
  584. * @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
  585. * operations can be chained together.
  586. */
  587. goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) {
  588. // Compute the dot product between q0 and q1 (cos of the angle between q0 and
  589. // q1). If it's outside the interval [-1,1], then the arccos is not defined.
  590. // The usual reason for this is that q0 and q1 are colinear. In this case
  591. // the angle between the two is zero, so just return q1.
  592. var cosVal = goog.vec.Quaternion.dot(q0, q1);
  593. if (cosVal > 1 || cosVal < -1) {
  594. goog.vec.Vec4.setFromArray(resultQuat, q1);
  595. return resultQuat;
  596. }
  597. // Quaternions are a double cover on the space of rotations. That is, q and -q
  598. // represent the same rotation. Thus we have two possibilities when
  599. // interpolating between q0 and q1: going the short way or the long way. We
  600. // prefer the short way since that is the likely expectation from users.
  601. var factor = 1;
  602. if (cosVal < 0) {
  603. factor = -1;
  604. cosVal = -cosVal;
  605. }
  606. // Compute the angle between q0 and q1. If it's very small, then just return
  607. // q1 to avoid a very large denominator below.
  608. var angle = Math.acos(cosVal);
  609. if (angle <= goog.vec.EPSILON) {
  610. goog.vec.Vec4.setFromArray(resultQuat, q1);
  611. return resultQuat;
  612. }
  613. // Compute the coefficients and interpolate.
  614. var invSinVal = 1 / Math.sin(angle);
  615. var c0 = Math.sin((1 - t) * angle) * invSinVal;
  616. var c1 = factor * Math.sin(t * angle) * invSinVal;
  617. resultQuat[0] = q0[0] * c0 + q1[0] * c1;
  618. resultQuat[1] = q0[1] * c0 + q1[1] * c1;
  619. resultQuat[2] = q0[2] * c0 + q1[2] * c1;
  620. resultQuat[3] = q0[3] * c0 + q1[3] * c1;
  621. return resultQuat;
  622. };
  623. /**
  624. * Compute the simple linear interpolation of the two quaternions q0 and q1
  625. * according to the coefficient t. The resulting quaternion is stored in
  626. * resultVec.
  627. *
  628. * @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion.
  629. * @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion.
  630. * @param {number} t The interpolation factor.
  631. * @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  632. * receive the results (may be q0 or q1).
  633. */
  634. goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp;