Quaternion.js 11 KB

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