Quaternion.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. * @author bhouston / http://clara.io
  6. */
  7. import { MathUtils } from './MathUtils.js';
  8. function Quaternion( x = 0, y = 0, z = 0, w = 1 ) {
  9. this._x = x;
  10. this._y = y;
  11. this._z = z;
  12. this._w = w;
  13. }
  14. Object.assign( Quaternion, {
  15. slerp: function ( qa, qb, qm, t ) {
  16. return qm.copy( qa ).slerp( qb, t );
  17. },
  18. slerpFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) {
  19. // fuzz-free, array-based Quaternion SLERP operation
  20. let x0 = src0[ srcOffset0 + 0 ],
  21. y0 = src0[ srcOffset0 + 1 ],
  22. z0 = src0[ srcOffset0 + 2 ],
  23. w0 = src0[ srcOffset0 + 3 ];
  24. const x1 = src1[ srcOffset1 + 0 ],
  25. y1 = src1[ srcOffset1 + 1 ],
  26. z1 = src1[ srcOffset1 + 2 ],
  27. w1 = src1[ srcOffset1 + 3 ];
  28. if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) {
  29. let s = 1 - t,
  30. cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1,
  31. dir = ( cos >= 0 ? 1 : - 1 ),
  32. sqrSin = 1 - cos * cos;
  33. // Skip the Slerp for tiny steps to avoid numeric problems:
  34. if ( sqrSin > Number.EPSILON ) {
  35. const sin = Math.sqrt( sqrSin ),
  36. len = Math.atan2( sin, cos * dir );
  37. s = Math.sin( s * len ) / sin;
  38. t = Math.sin( t * len ) / sin;
  39. }
  40. const tDir = t * dir;
  41. x0 = x0 * s + x1 * tDir;
  42. y0 = y0 * s + y1 * tDir;
  43. z0 = z0 * s + z1 * tDir;
  44. w0 = w0 * s + w1 * tDir;
  45. // Normalize in case we just did a lerp:
  46. if ( s === 1 - t ) {
  47. const f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 );
  48. x0 *= f;
  49. y0 *= f;
  50. z0 *= f;
  51. w0 *= f;
  52. }
  53. }
  54. dst[ dstOffset ] = x0;
  55. dst[ dstOffset + 1 ] = y0;
  56. dst[ dstOffset + 2 ] = z0;
  57. dst[ dstOffset + 3 ] = w0;
  58. },
  59. multiplyQuaternionsFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) {
  60. const x0 = src0[ srcOffset0 ];
  61. const y0 = src0[ srcOffset0 + 1 ];
  62. const z0 = src0[ srcOffset0 + 2 ];
  63. const w0 = src0[ srcOffset0 + 3 ];
  64. const x1 = src1[ srcOffset1 ];
  65. const y1 = src1[ srcOffset1 + 1 ];
  66. const z1 = src1[ srcOffset1 + 2 ];
  67. const w1 = src1[ srcOffset1 + 3 ];
  68. dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
  69. dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
  70. dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
  71. dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  72. return dst;
  73. }
  74. } );
  75. Object.defineProperties( Quaternion.prototype, {
  76. x: {
  77. get: function () {
  78. return this._x;
  79. },
  80. set: function ( value ) {
  81. this._x = value;
  82. this._onChangeCallback();
  83. }
  84. },
  85. y: {
  86. get: function () {
  87. return this._y;
  88. },
  89. set: function ( value ) {
  90. this._y = value;
  91. this._onChangeCallback();
  92. }
  93. },
  94. z: {
  95. get: function () {
  96. return this._z;
  97. },
  98. set: function ( value ) {
  99. this._z = value;
  100. this._onChangeCallback();
  101. }
  102. },
  103. w: {
  104. get: function () {
  105. return this._w;
  106. },
  107. set: function ( value ) {
  108. this._w = value;
  109. this._onChangeCallback();
  110. }
  111. }
  112. } );
  113. Object.assign( Quaternion.prototype, {
  114. isQuaternion: true,
  115. set: function ( x, y, z, w ) {
  116. this._x = x;
  117. this._y = y;
  118. this._z = z;
  119. this._w = w;
  120. this._onChangeCallback();
  121. return this;
  122. },
  123. clone: function () {
  124. return new this.constructor( this._x, this._y, this._z, this._w );
  125. },
  126. copy: function ( quaternion ) {
  127. this._x = quaternion.x;
  128. this._y = quaternion.y;
  129. this._z = quaternion.z;
  130. this._w = quaternion.w;
  131. this._onChangeCallback();
  132. return this;
  133. },
  134. setFromEuler: function ( euler, update ) {
  135. if ( ! ( euler && euler.isEuler ) ) {
  136. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects an Euler rotation rather than a Vector3 and order.' );
  137. }
  138. const x = euler._x, y = euler._y, z = euler._z, order = euler.order;
  139. // http://www.mathworks.com/matlabcentral/fileexchange/
  140. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  141. // content/SpinCalc.m
  142. const cos = Math.cos;
  143. const sin = Math.sin;
  144. const c1 = cos( x / 2 );
  145. const c2 = cos( y / 2 );
  146. const c3 = cos( z / 2 );
  147. const s1 = sin( x / 2 );
  148. const s2 = sin( y / 2 );
  149. const s3 = sin( z / 2 );
  150. switch ( order ) {
  151. case 'XYZ':
  152. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  153. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  154. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  155. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  156. break;
  157. case 'YXZ':
  158. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  159. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  160. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  161. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  162. break;
  163. case 'ZXY':
  164. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  165. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  166. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  167. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  168. break;
  169. case 'ZYX':
  170. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  171. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  172. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  173. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  174. break;
  175. case 'YZX':
  176. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  177. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  178. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  179. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  180. break;
  181. case 'XZY':
  182. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  183. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  184. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  185. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  186. break;
  187. default:
  188. console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order );
  189. }
  190. if ( update !== false ) this._onChangeCallback();
  191. return this;
  192. },
  193. setFromAxisAngle: function ( axis, angle ) {
  194. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  195. // assumes axis is normalized
  196. const halfAngle = angle / 2, s = Math.sin( halfAngle );
  197. this._x = axis.x * s;
  198. this._y = axis.y * s;
  199. this._z = axis.z * s;
  200. this._w = Math.cos( halfAngle );
  201. this._onChangeCallback();
  202. return this;
  203. },
  204. setFromRotationMatrix: function ( m ) {
  205. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  206. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  207. const te = m.elements,
  208. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  209. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  210. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  211. trace = m11 + m22 + m33;
  212. if ( trace > 0 ) {
  213. const s = 0.5 / Math.sqrt( trace + 1.0 );
  214. this._w = 0.25 / s;
  215. this._x = ( m32 - m23 ) * s;
  216. this._y = ( m13 - m31 ) * s;
  217. this._z = ( m21 - m12 ) * s;
  218. } else if ( m11 > m22 && m11 > m33 ) {
  219. const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  220. this._w = ( m32 - m23 ) / s;
  221. this._x = 0.25 * s;
  222. this._y = ( m12 + m21 ) / s;
  223. this._z = ( m13 + m31 ) / s;
  224. } else if ( m22 > m33 ) {
  225. const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  226. this._w = ( m13 - m31 ) / s;
  227. this._x = ( m12 + m21 ) / s;
  228. this._y = 0.25 * s;
  229. this._z = ( m23 + m32 ) / s;
  230. } else {
  231. const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  232. this._w = ( m21 - m12 ) / s;
  233. this._x = ( m13 + m31 ) / s;
  234. this._y = ( m23 + m32 ) / s;
  235. this._z = 0.25 * s;
  236. }
  237. this._onChangeCallback();
  238. return this;
  239. },
  240. setFromUnitVectors: function ( vFrom, vTo ) {
  241. // assumes direction vectors vFrom and vTo are normalized
  242. const EPS = 0.000001;
  243. let r = vFrom.dot( vTo ) + 1;
  244. if ( r < EPS ) {
  245. r = 0;
  246. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  247. this._x = - vFrom.y;
  248. this._y = vFrom.x;
  249. this._z = 0;
  250. this._w = r;
  251. } else {
  252. this._x = 0;
  253. this._y = - vFrom.z;
  254. this._z = vFrom.y;
  255. this._w = r;
  256. }
  257. } else {
  258. // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
  259. this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
  260. this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
  261. this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
  262. this._w = r;
  263. }
  264. return this.normalize();
  265. },
  266. angleTo: function ( q ) {
  267. return 2 * Math.acos( Math.abs( MathUtils.clamp( this.dot( q ), - 1, 1 ) ) );
  268. },
  269. rotateTowards: function ( q, step ) {
  270. const angle = this.angleTo( q );
  271. if ( angle === 0 ) return this;
  272. const t = Math.min( 1, step / angle );
  273. this.slerp( q, t );
  274. return this;
  275. },
  276. inverse: function () {
  277. // quaternion is assumed to have unit length
  278. return this.conjugate();
  279. },
  280. conjugate: function () {
  281. this._x *= - 1;
  282. this._y *= - 1;
  283. this._z *= - 1;
  284. this._onChangeCallback();
  285. return this;
  286. },
  287. dot: function ( v ) {
  288. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  289. },
  290. lengthSq: function () {
  291. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  292. },
  293. length: function () {
  294. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  295. },
  296. normalize: function () {
  297. let l = this.length();
  298. if ( l === 0 ) {
  299. this._x = 0;
  300. this._y = 0;
  301. this._z = 0;
  302. this._w = 1;
  303. } else {
  304. l = 1 / l;
  305. this._x = this._x * l;
  306. this._y = this._y * l;
  307. this._z = this._z * l;
  308. this._w = this._w * l;
  309. }
  310. this._onChangeCallback();
  311. return this;
  312. },
  313. multiply: function ( q, p ) {
  314. if ( p !== undefined ) {
  315. console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  316. return this.multiplyQuaternions( q, p );
  317. }
  318. return this.multiplyQuaternions( this, q );
  319. },
  320. premultiply: function ( q ) {
  321. return this.multiplyQuaternions( q, this );
  322. },
  323. multiplyQuaternions: function ( a, b ) {
  324. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  325. const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  326. const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  327. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  328. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  329. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  330. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  331. this._onChangeCallback();
  332. return this;
  333. },
  334. slerp: function ( qb, t ) {
  335. if ( t === 0 ) return this;
  336. if ( t === 1 ) return this.copy( qb );
  337. const x = this._x, y = this._y, z = this._z, w = this._w;
  338. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  339. let cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  340. if ( cosHalfTheta < 0 ) {
  341. this._w = - qb._w;
  342. this._x = - qb._x;
  343. this._y = - qb._y;
  344. this._z = - qb._z;
  345. cosHalfTheta = - cosHalfTheta;
  346. } else {
  347. this.copy( qb );
  348. }
  349. if ( cosHalfTheta >= 1.0 ) {
  350. this._w = w;
  351. this._x = x;
  352. this._y = y;
  353. this._z = z;
  354. return this;
  355. }
  356. const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
  357. if ( sqrSinHalfTheta <= Number.EPSILON ) {
  358. const s = 1 - t;
  359. this._w = s * w + t * this._w;
  360. this._x = s * x + t * this._x;
  361. this._y = s * y + t * this._y;
  362. this._z = s * z + t * this._z;
  363. this.normalize();
  364. this._onChangeCallback();
  365. return this;
  366. }
  367. const sinHalfTheta = Math.sqrt( sqrSinHalfTheta );
  368. const halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta );
  369. const ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  370. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  371. this._w = ( w * ratioA + this._w * ratioB );
  372. this._x = ( x * ratioA + this._x * ratioB );
  373. this._y = ( y * ratioA + this._y * ratioB );
  374. this._z = ( z * ratioA + this._z * ratioB );
  375. this._onChangeCallback();
  376. return this;
  377. },
  378. equals: function ( quaternion ) {
  379. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  380. },
  381. fromArray: function ( array, offset ) {
  382. if ( offset === undefined ) offset = 0;
  383. this._x = array[ offset ];
  384. this._y = array[ offset + 1 ];
  385. this._z = array[ offset + 2 ];
  386. this._w = array[ offset + 3 ];
  387. this._onChangeCallback();
  388. return this;
  389. },
  390. toArray: function ( array, offset ) {
  391. if ( array === undefined ) array = [];
  392. if ( offset === undefined ) offset = 0;
  393. array[ offset ] = this._x;
  394. array[ offset + 1 ] = this._y;
  395. array[ offset + 2 ] = this._z;
  396. array[ offset + 3 ] = this._w;
  397. return array;
  398. },
  399. fromBufferAttribute: function ( attribute, index ) {
  400. this._x = attribute.getX( index );
  401. this._y = attribute.getY( index );
  402. this._z = attribute.getZ( index );
  403. this._w = attribute.getW( index );
  404. return this;
  405. },
  406. _onChange: function ( callback ) {
  407. this._onChangeCallback = callback;
  408. return this;
  409. },
  410. _onChangeCallback: function () {}
  411. } );
  412. export { Quaternion };