quaternion.js 24 KB

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