Quaternion.js 11 KB

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