quaternion.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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.require('goog.vec');
  23. goog.require('goog.vec.Vec3');
  24. goog.require('goog.vec.Vec4');
  25. /** @typedef {goog.vec.Float32} */ goog.vec.Quaternion.Float32;
  26. /** @typedef {goog.vec.Float64} */ goog.vec.Quaternion.Float64;
  27. /** @typedef {goog.vec.Number} */ goog.vec.Quaternion.Number;
  28. /** @typedef {goog.vec.AnyType} */ goog.vec.Quaternion.AnyType;
  29. //The following type are deprecated - use the above types instead.
  30. /** @typedef {goog.vec.Vec4.Type} */ goog.vec.Quaternion.Type;
  31. /** @typedef {goog.vec.ArrayType} */ goog.vec.Quaternion.QuaternionLike;
  32. /**
  33. * @typedef {goog.vec.Vec4.Type}
  34. */
  35. goog.vec.Quaternion.Type;
  36. /**
  37. * Creates a Float32 quaternion, initialized to zero.
  38. *
  39. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  40. */
  41. goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32;
  42. /**
  43. * Creates a Float64 quaternion, initialized to zero.
  44. *
  45. * @return {goog.vec.Quaternion.Float64} The new quaternion.
  46. */
  47. goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64;
  48. /**
  49. * Creates a Number quaternion, initialized to zero.
  50. *
  51. * @return {goog.vec.Quaternion.Number} The new quaternion.
  52. */
  53. goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber;
  54. /**
  55. * Creates a quaternion, initialized to zero.
  56. *
  57. * @deprecated Use createFloat32.
  58. * @return {!goog.vec.Quaternion.Type} The new quaternion.
  59. */
  60. goog.vec.Quaternion.create = goog.vec.Vec4.create;
  61. /**
  62. * Creates a new Float32 quaternion initialized with the values from the
  63. * supplied array.
  64. *
  65. * @param {goog.vec.AnyType} vec The source 4 element array.
  66. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  67. */
  68. goog.vec.Quaternion.createFloat32FromArray =
  69. goog.vec.Vec4.createFloat32FromArray;
  70. /**
  71. * Creates a new Float64 quaternion initialized with the values from the
  72. * supplied array.
  73. *
  74. * @param {goog.vec.AnyType} vec The source 4 element array.
  75. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  76. */
  77. goog.vec.Quaternion.createFloat64FromArray =
  78. goog.vec.Vec4.createFloat64FromArray;
  79. /**
  80. * Creates a new quaternion initialized with the values from the supplied
  81. * array.
  82. *
  83. * @deprecated Use createFloat32FromArray.
  84. * @param {!goog.vec.Quaternion.QuaternionLike} vec The source 4 element array.
  85. * @return {!goog.vec.Quaternion.Type} The new quaternion.
  86. */
  87. goog.vec.Quaternion.createFromArray =
  88. goog.vec.Vec4.createFromArray;
  89. /**
  90. * Creates a new Float32 quaternion initialized with the supplied values.
  91. *
  92. * @param {number} v0 The value for element at index 0.
  93. * @param {number} v1 The value for element at index 1.
  94. * @param {number} v2 The value for element at index 2.
  95. * @param {number} v3 The value for element at index 3.
  96. * @return {!goog.vec.Quaternion.Float32} The new quaternion.
  97. */
  98. goog.vec.Quaternion.createFloat32FromValues =
  99. goog.vec.Vec4.createFloat32FromValues;
  100. /**
  101. * Creates a new Float64 quaternion initialized with the supplied values.
  102. *
  103. * @param {number} v0 The value for element at index 0.
  104. * @param {number} v1 The value for element at index 1.
  105. * @param {number} v2 The value for element at index 2.
  106. * @param {number} v3 The value for element at index 3.
  107. * @return {!goog.vec.Quaternion.Float64} The new quaternion.
  108. */
  109. goog.vec.Quaternion.createFloat64FromValues =
  110. goog.vec.Vec4.createFloat64FromValues;
  111. /**
  112. * Creates a new quaternion initialized with the supplied values.
  113. *
  114. * @deprecated Use createFloat32FromValues.
  115. * @param {number} v0 The value for element at index 0.
  116. * @param {number} v1 The value for element at index 1.
  117. * @param {number} v2 The value for element at index 2.
  118. * @param {number} v3 The value for element at index 3.
  119. * @return {!goog.vec.Quaternion.Type} The new quaternion.
  120. */
  121. goog.vec.Quaternion.createFromValues =
  122. goog.vec.Vec4.createFromValues;
  123. /**
  124. * Creates a clone of the given Float32 quaternion.
  125. *
  126. * @param {goog.vec.Quaternion.Float32} q The source quaternion.
  127. * @return {goog.vec.Quaternion.Float32} The new quaternion.
  128. */
  129. goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32;
  130. /**
  131. * Creates a clone of the given Float64 quaternion.
  132. *
  133. * @param {goog.vec.Quaternion.Float64} q The source quaternion.
  134. * @return {goog.vec.Quaternion.Float64} The new quaternion.
  135. */
  136. goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64;
  137. /**
  138. * Creates a clone of the given quaternion.
  139. *
  140. * @deprecated Use cloneFloat32.
  141. * @param {goog.vec.Quaternion.QuaternionLike} q The source quaternion.
  142. * @return {!goog.vec.Quaternion.Type} The new quaternion.
  143. */
  144. goog.vec.Quaternion.clone = goog.vec.Vec4.clone;
  145. /**
  146. * Initializes the quaternion with the given values.
  147. *
  148. * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
  149. * the values.
  150. * @param {number} v0 The value for element at index 0.
  151. * @param {number} v1 The value for element at index 1.
  152. * @param {number} v2 The value for element at index 2.
  153. * @param {number} v3 The value for element at index 3.
  154. * @return {!goog.vec.Vec4.AnyType} return q so that operations can be
  155. * chained together.
  156. */
  157. goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues;
  158. /**
  159. * Initializes the quaternion with the given array of values.
  160. *
  161. * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
  162. * the values.
  163. * @param {goog.vec.AnyType} values The array of values.
  164. * @return {!goog.vec.Quaternion.AnyType} return q so that operations can be
  165. * chained together.
  166. */
  167. goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray;
  168. /**
  169. * Adds the two quaternions.
  170. *
  171. * @param {goog.vec.Quaternion.AnyType} quat0 The first addend.
  172. * @param {goog.vec.Quaternion.AnyType} quat1 The second addend.
  173. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  174. * receive the result. May be quat0 or quat1.
  175. */
  176. goog.vec.Quaternion.add = goog.vec.Vec4.add;
  177. /**
  178. * Negates a quaternion, storing the result into resultQuat.
  179. *
  180. * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to negate.
  181. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  182. * receive the result. May be quat0.
  183. */
  184. goog.vec.Quaternion.negate = goog.vec.Vec4.negate;
  185. /**
  186. * Multiplies each component of quat0 with scalar storing the product into
  187. * resultVec.
  188. *
  189. * @param {goog.vec.Quaternion.AnyType} quat0 The source quaternion.
  190. * @param {number} scalar The value to multiply with each component of quat0.
  191. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  192. * receive the result. May be quat0.
  193. */
  194. goog.vec.Quaternion.scale = goog.vec.Vec4.scale;
  195. /**
  196. * Returns the square magnitude of the given quaternion.
  197. *
  198. * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
  199. * @return {number} The magnitude of the quaternion.
  200. */
  201. goog.vec.Quaternion.magnitudeSquared =
  202. goog.vec.Vec4.magnitudeSquared;
  203. /**
  204. * Returns the magnitude of the given quaternion.
  205. *
  206. * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
  207. * @return {number} The magnitude of the quaternion.
  208. */
  209. goog.vec.Quaternion.magnitude =
  210. goog.vec.Vec4.magnitude;
  211. /**
  212. * Normalizes the given quaternion storing the result into resultVec.
  213. *
  214. * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to
  215. * normalize.
  216. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  217. * receive the result. May be quat0.
  218. */
  219. goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize;
  220. /**
  221. * Computes the dot (scalar) product of two quaternions.
  222. *
  223. * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
  224. * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
  225. * @return {number} The scalar product.
  226. */
  227. goog.vec.Quaternion.dot = goog.vec.Vec4.dot;
  228. /**
  229. * Computes the conjugate of the quaternion in quat storing the result into
  230. * resultQuat.
  231. *
  232. * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
  233. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  234. * receive the result.
  235. * @return {!goog.vec.Quaternion.AnyType} Return q so that
  236. * operations can be chained together.
  237. */
  238. goog.vec.Quaternion.conjugate = function(quat, resultQuat) {
  239. resultQuat[0] = -quat[0];
  240. resultQuat[1] = -quat[1];
  241. resultQuat[2] = -quat[2];
  242. resultQuat[3] = quat[3];
  243. return resultQuat;
  244. };
  245. /**
  246. * Concatenates the two quaternions storing the result into resultQuat.
  247. *
  248. * @param {goog.vec.Quaternion.AnyType} quat0 The first quaternion.
  249. * @param {goog.vec.Quaternion.AnyType} quat1 The second quaternion.
  250. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  251. * receive the result.
  252. * @return {!goog.vec.Quaternion.AnyType} Return q so that
  253. * operations can be chained together.
  254. */
  255. goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) {
  256. var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3];
  257. var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3];
  258. resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
  259. resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1;
  260. resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1;
  261. resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  262. return resultQuat;
  263. };
  264. /**
  265. * Generates a unit quaternion from the given angle-axis rotation pair.
  266. * The rotation axis is not required to be a unit vector, but should
  267. * have non-zero length. The angle should be specified in radians.
  268. *
  269. * @param {number} angle The angle (in radians) to rotate about the axis.
  270. * @param {goog.vec.Quaternion.AnyType} axis Unit vector specifying the
  271. * axis of rotation.
  272. * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to store the
  273. * result.
  274. * @return {goog.vec.Quaternion.AnyType} Return q so that
  275. * operations can be chained together.
  276. */
  277. goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) {
  278. // Normalize the axis of rotation.
  279. goog.vec.Vec3.normalize(axis, axis);
  280. var halfAngle = 0.5 * angle;
  281. var sin = Math.sin(halfAngle);
  282. goog.vec.Quaternion.setFromValues(
  283. quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle));
  284. // Normalize the resulting quaternion.
  285. goog.vec.Quaternion.normalize(quat, quat);
  286. return quat;
  287. };
  288. /**
  289. * Generates an angle-axis rotation pair from a unit quaternion.
  290. * The quaternion is assumed to be of unit length. The calculated
  291. * values are returned via the passed 'axis' object and the 'angle'
  292. * number returned by the function itself. The returned rotation axis
  293. * is a non-zero length unit vector, and the returned angle is in
  294. * radians in the range of [-PI, +PI].
  295. *
  296. * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to convert.
  297. * @param {goog.vec.Quaternion.AnyType} axis Vector to store the returned
  298. * rotation axis.
  299. * @return {number} angle Angle (in radians) to rotate about 'axis'.
  300. * The range of the returned angle is [-PI, +PI].
  301. */
  302. goog.vec.Quaternion.toAngleAxis = function(quat, axis) {
  303. var angle = 2 * Math.acos(quat[3]);
  304. var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1);
  305. if (magnitude < goog.vec.EPSILON) {
  306. // This is nearly an identity rotation, so just use a fixed +X axis.
  307. goog.vec.Vec3.setFromValues(axis, 1, 0, 0);
  308. } else {
  309. // Compute the proper rotation axis.
  310. goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]);
  311. // Make sure the rotation axis is of unit length.
  312. goog.vec.Vec3.normalize(axis, axis);
  313. }
  314. // Adjust the range of the returned angle to [-PI, +PI].
  315. if (angle > Math.PI) {
  316. angle -= 2 * Math.PI;
  317. }
  318. return angle;
  319. };
  320. /**
  321. * Generates the quaternion from the given rotation matrix.
  322. *
  323. * @param {goog.vec.Quaternion.AnyType} matrix The source matrix.
  324. * @param {goog.vec.Quaternion.AnyType} quat The resulting quaternion.
  325. * @return {!goog.vec.Quaternion.AnyType} Return q so that
  326. * operations can be chained together.
  327. */
  328. goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
  329. var sx = matrix[0], sy = matrix[5], sz = matrix[10];
  330. quat[3] = Math.sqrt(Math.max(0, 1 + sx + sy + sz)) / 2;
  331. quat[0] = Math.sqrt(Math.max(0, 1 + sx - sy - sz)) / 2;
  332. quat[1] = Math.sqrt(Math.max(0, 1 - sx + sy - sz)) / 2;
  333. quat[2] = Math.sqrt(Math.max(0, 1 - sx - sy + sz)) / 2;
  334. quat[0] = (matrix[6] - matrix[9] < 0) != (quat[0] < 0) ? -quat[0] : quat[0];
  335. quat[1] = (matrix[8] - matrix[2] < 0) != (quat[1] < 0) ? -quat[1] : quat[1];
  336. quat[2] = (matrix[1] - matrix[4] < 0) != (quat[2] < 0) ? -quat[2] : quat[2];
  337. return quat;
  338. };
  339. /**
  340. * Generates the rotation matrix from the given quaternion.
  341. *
  342. * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
  343. * @param {goog.vec.AnyType} matrix The resulting matrix.
  344. * @return {!goog.vec.AnyType} Return resulting matrix so that
  345. * operations can be chained together.
  346. */
  347. goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) {
  348. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  349. var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
  350. var wx = x2 * w;
  351. var wy = y2 * w;
  352. var wz = z2 * w;
  353. var xx = x2 * x;
  354. var xy = y2 * x;
  355. var xz = z2 * x;
  356. var yy = y2 * y;
  357. var yz = z2 * y;
  358. var zz = z2 * z;
  359. matrix[0] = 1 - (yy + zz);
  360. matrix[1] = xy + wz;
  361. matrix[2] = xz - wy;
  362. matrix[3] = 0;
  363. matrix[4] = xy - wz;
  364. matrix[5] = 1 - (xx + zz);
  365. matrix[6] = yz + wx;
  366. matrix[7] = 0;
  367. matrix[8] = xz + wy;
  368. matrix[9] = yz - wx;
  369. matrix[10] = 1 - (xx + yy);
  370. matrix[11] = 0;
  371. matrix[12] = 0;
  372. matrix[13] = 0;
  373. matrix[14] = 0;
  374. matrix[15] = 1;
  375. return matrix;
  376. };
  377. /**
  378. * Computes the spherical linear interpolated value from the given quaternions
  379. * q0 and q1 according to the coefficient t. The resulting quaternion is stored
  380. * in resultQuat.
  381. *
  382. * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
  383. * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
  384. * @param {number} t The interpolating coefficient.
  385. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  386. * receive the result.
  387. * @return {goog.vec.Quaternion.AnyType} Return q so that
  388. * operations can be chained together.
  389. */
  390. goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) {
  391. // Compute the dot product between q0 and q1 (cos of the angle between q0 and
  392. // q1). If it's outside the interval [-1,1], then the arccos is not defined.
  393. // The usual reason for this is that q0 and q1 are colinear. In this case
  394. // the angle between the two is zero, so just return q1.
  395. var cosVal = goog.vec.Quaternion.dot(q0, q1);
  396. if (cosVal > 1 || cosVal < -1) {
  397. goog.vec.Vec4.setFromArray(resultQuat, q1);
  398. return resultQuat;
  399. }
  400. // Quaternions are a double cover on the space of rotations. That is, q and -q
  401. // represent the same rotation. Thus we have two possibilities when
  402. // interpolating between q0 and q1: going the short way or the long way. We
  403. // prefer the short way since that is the likely expectation from users.
  404. var factor = 1;
  405. if (cosVal < 0) {
  406. factor = -1;
  407. cosVal = -cosVal;
  408. }
  409. // Compute the angle between q0 and q1. If it's very small, then just return
  410. // q1 to avoid a very large denominator below.
  411. var angle = Math.acos(cosVal);
  412. if (angle <= goog.vec.EPSILON) {
  413. goog.vec.Vec4.setFromArray(resultQuat, q1);
  414. return resultQuat;
  415. }
  416. // Compute the coefficients and interpolate.
  417. var invSinVal = 1 / Math.sin(angle);
  418. var c0 = Math.sin((1 - t) * angle) * invSinVal;
  419. var c1 = factor * Math.sin(t * angle) * invSinVal;
  420. resultQuat[0] = q0[0] * c0 + q1[0] * c1;
  421. resultQuat[1] = q0[1] * c0 + q1[1] * c1;
  422. resultQuat[2] = q0[2] * c0 + q1[2] * c1;
  423. resultQuat[3] = q0[3] * c0 + q1[3] * c1;
  424. return resultQuat;
  425. };
  426. /**
  427. * Compute the simple linear interpolation of the two quaternions q0 and q1
  428. * according to the coefficient t. The resulting quaternion is stored in
  429. * resultVec.
  430. *
  431. * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
  432. * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
  433. * @param {number} t The interpolation factor.
  434. * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
  435. * receive the results (may be q0 or q1).
  436. */
  437. goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp;