Quaternion.js 11 KB

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