Quaternion.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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://exocortex.com
  6. */
  7. THREE.Quaternion = function ( x, y, z, w ) {
  8. this._x = x || 0;
  9. this._y = y || 0;
  10. this._z = z || 0;
  11. this._w = ( w !== undefined ) ? w : 1;
  12. };
  13. THREE.Quaternion.prototype = {
  14. constructor: THREE.Quaternion,
  15. get x () {
  16. return this._x;
  17. },
  18. set x ( value ) {
  19. this._x = value;
  20. this.onChangeCallback();
  21. },
  22. get y () {
  23. return this._y;
  24. },
  25. set y ( value ) {
  26. this._y = value;
  27. this.onChangeCallback();
  28. },
  29. get z () {
  30. return this._z;
  31. },
  32. set z ( value ) {
  33. this._z = value;
  34. this.onChangeCallback();
  35. },
  36. get w () {
  37. return this._w;
  38. },
  39. set w ( value ) {
  40. this._w = value;
  41. this.onChangeCallback();
  42. },
  43. set: function ( x, y, z, w ) {
  44. this._x = x;
  45. this._y = y;
  46. this._z = z;
  47. this._w = w;
  48. this.onChangeCallback();
  49. return this;
  50. },
  51. clone: function () {
  52. return new this.constructor( this._x, this._y, this._z, this._w );
  53. },
  54. copy: function ( quaternion ) {
  55. this._x = quaternion.x;
  56. this._y = quaternion.y;
  57. this._z = quaternion.z;
  58. this._w = quaternion.w;
  59. this.onChangeCallback();
  60. return this;
  61. },
  62. setFromEuler: function ( euler, update ) {
  63. if ( euler instanceof THREE.Euler === false ) {
  64. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  65. }
  66. // http://www.mathworks.com/matlabcentral/fileexchange/
  67. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  68. // content/SpinCalc.m
  69. var c1 = Math.cos( euler._x / 2 );
  70. var c2 = Math.cos( euler._y / 2 );
  71. var c3 = Math.cos( euler._z / 2 );
  72. var s1 = Math.sin( euler._x / 2 );
  73. var s2 = Math.sin( euler._y / 2 );
  74. var s3 = Math.sin( euler._z / 2 );
  75. if ( euler.order === 'XYZ' ) {
  76. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  77. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  78. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  79. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  80. } else if ( euler.order === 'YXZ' ) {
  81. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  82. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  83. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  84. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  85. } else if ( euler.order === 'ZXY' ) {
  86. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  87. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  88. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  89. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  90. } else if ( euler.order === 'ZYX' ) {
  91. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  92. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  93. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  94. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  95. } else if ( euler.order === 'YZX' ) {
  96. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  97. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  98. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  99. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  100. } else if ( euler.order === 'XZY' ) {
  101. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  102. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  103. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  104. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  105. }
  106. if ( update !== false ) this.onChangeCallback();
  107. return this;
  108. },
  109. setFromAxisAngle: function ( axis, angle ) {
  110. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  111. // assumes axis is normalized
  112. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  113. this._x = axis.x * s;
  114. this._y = axis.y * s;
  115. this._z = axis.z * s;
  116. this._w = Math.cos( halfAngle );
  117. this.onChangeCallback();
  118. return this;
  119. },
  120. setFromRotationMatrix: function ( m ) {
  121. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  122. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  123. var te = m.elements,
  124. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  125. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  126. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  127. trace = m11 + m22 + m33,
  128. s;
  129. if ( trace > 0 ) {
  130. s = 0.5 / Math.sqrt( trace + 1.0 );
  131. this._w = 0.25 / s;
  132. this._x = ( m32 - m23 ) * s;
  133. this._y = ( m13 - m31 ) * s;
  134. this._z = ( m21 - m12 ) * s;
  135. } else if ( m11 > m22 && m11 > m33 ) {
  136. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  137. this._w = ( m32 - m23 ) / s;
  138. this._x = 0.25 * s;
  139. this._y = ( m12 + m21 ) / s;
  140. this._z = ( m13 + m31 ) / s;
  141. } else if ( m22 > m33 ) {
  142. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  143. this._w = ( m13 - m31 ) / s;
  144. this._x = ( m12 + m21 ) / s;
  145. this._y = 0.25 * s;
  146. this._z = ( m23 + m32 ) / s;
  147. } else {
  148. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  149. this._w = ( m21 - m12 ) / s;
  150. this._x = ( m13 + m31 ) / s;
  151. this._y = ( m23 + m32 ) / s;
  152. this._z = 0.25 * s;
  153. }
  154. this.onChangeCallback();
  155. return this;
  156. },
  157. setFromUnitVectors: function () {
  158. // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  159. // assumes direction vectors vFrom and vTo are normalized
  160. var v1, r;
  161. var EPS = 0.000001;
  162. return function ( vFrom, vTo ) {
  163. if ( v1 === undefined ) v1 = new THREE.Vector3();
  164. r = vFrom.dot( vTo ) + 1;
  165. if ( r < EPS ) {
  166. r = 0;
  167. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  168. v1.set( - vFrom.y, vFrom.x, 0 );
  169. } else {
  170. v1.set( 0, - vFrom.z, vFrom.y );
  171. }
  172. } else {
  173. v1.crossVectors( vFrom, vTo );
  174. }
  175. this._x = v1.x;
  176. this._y = v1.y;
  177. this._z = v1.z;
  178. this._w = r;
  179. this.normalize();
  180. return this;
  181. }
  182. }(),
  183. inverse: function () {
  184. this.conjugate().normalize();
  185. return this;
  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. multiplyQuaternions: function ( a, b ) {
  228. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  229. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  230. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  231. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  232. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  233. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  234. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  235. this.onChangeCallback();
  236. return this;
  237. },
  238. multiplyVector3: function ( vector ) {
  239. console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  240. return vector.applyQuaternion( this );
  241. },
  242. slerp: function ( qb, t ) {
  243. if ( t === 0 ) return this;
  244. if ( t === 1 ) return this.copy( qb );
  245. var x = this._x, y = this._y, z = this._z, w = this._w;
  246. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  247. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  248. if ( cosHalfTheta < 0 ) {
  249. this._w = - qb._w;
  250. this._x = - qb._x;
  251. this._y = - qb._y;
  252. this._z = - qb._z;
  253. cosHalfTheta = - cosHalfTheta;
  254. } else {
  255. this.copy( qb );
  256. }
  257. if ( cosHalfTheta >= 1.0 ) {
  258. this._w = w;
  259. this._x = x;
  260. this._y = y;
  261. this._z = z;
  262. return this;
  263. }
  264. var halfTheta = Math.acos( cosHalfTheta );
  265. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  266. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  267. this._w = 0.5 * ( w + this._w );
  268. this._x = 0.5 * ( x + this._x );
  269. this._y = 0.5 * ( y + this._y );
  270. this._z = 0.5 * ( z + this._z );
  271. return this;
  272. }
  273. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  274. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  275. this._w = ( w * ratioA + this._w * ratioB );
  276. this._x = ( x * ratioA + this._x * ratioB );
  277. this._y = ( y * ratioA + this._y * ratioB );
  278. this._z = ( z * ratioA + this._z * ratioB );
  279. this.onChangeCallback();
  280. return this;
  281. },
  282. equals: function ( quaternion ) {
  283. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  284. },
  285. fromArray: function ( array, offset ) {
  286. if ( offset === undefined ) offset = 0;
  287. this._x = array[ offset ];
  288. this._y = array[ offset + 1 ];
  289. this._z = array[ offset + 2 ];
  290. this._w = array[ offset + 3 ];
  291. this.onChangeCallback();
  292. return this;
  293. },
  294. toArray: function ( array, offset ) {
  295. if ( array === undefined ) array = [];
  296. if ( offset === undefined ) offset = 0;
  297. array[ offset ] = this._x;
  298. array[ offset + 1 ] = this._y;
  299. array[ offset + 2 ] = this._z;
  300. array[ offset + 3 ] = this._w;
  301. return array;
  302. },
  303. onChange: function ( callback ) {
  304. this.onChangeCallback = callback;
  305. return this;
  306. },
  307. onChangeCallback: function () {}
  308. };
  309. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  310. return qm.copy( qa ).slerp( qb, t );
  311. };
粤ICP备19079148号