Quaternion.js 12 KB

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