Quaternion.js 11 KB

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