2
0

Quaternion.js 12 KB

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