Quaternion.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. _x: 0,_y: 0, _z: 0, _w: 0,
  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. copy: function ( quaternion ) {
  53. this._x = quaternion.x;
  54. this._y = quaternion.y;
  55. this._z = quaternion.z;
  56. this._w = quaternion.w;
  57. this.onChangeCallback();
  58. return this;
  59. },
  60. setFromEuler: function ( euler, update ) {
  61. if ( euler instanceof THREE.Euler === false ) {
  62. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  63. }
  64. // http://www.mathworks.com/matlabcentral/fileexchange/
  65. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  66. // content/SpinCalc.m
  67. var c1 = Math.cos( euler._x / 2 );
  68. var c2 = Math.cos( euler._y / 2 );
  69. var c3 = Math.cos( euler._z / 2 );
  70. var s1 = Math.sin( euler._x / 2 );
  71. var s2 = Math.sin( euler._y / 2 );
  72. var s3 = Math.sin( euler._z / 2 );
  73. if ( euler.order === 'XYZ' ) {
  74. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  75. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  76. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  77. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  78. } else if ( euler.order === 'YXZ' ) {
  79. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  80. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  81. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  82. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  83. } else if ( euler.order === 'ZXY' ) {
  84. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  85. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  86. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  87. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  88. } else if ( euler.order === 'ZYX' ) {
  89. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  90. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  91. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  92. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  93. } else if ( euler.order === 'YZX' ) {
  94. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  95. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  96. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  97. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  98. } else if ( euler.order === 'XZY' ) {
  99. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  100. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  101. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  102. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  103. }
  104. if ( update !== false ) this.onChangeCallback();
  105. return this;
  106. },
  107. setFromAxisAngle: function ( axis, angle ) {
  108. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  109. // assumes axis is normalized
  110. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  111. this._x = axis.x * s;
  112. this._y = axis.y * s;
  113. this._z = axis.z * s;
  114. this._w = Math.cos( halfAngle );
  115. this.onChangeCallback();
  116. return this;
  117. },
  118. setFromRotationMatrix: function ( m ) {
  119. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  120. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  121. var te = m.elements,
  122. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  123. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  124. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  125. trace = m11 + m22 + m33,
  126. s;
  127. if ( trace > 0 ) {
  128. s = 0.5 / Math.sqrt( trace + 1.0 );
  129. this._w = 0.25 / s;
  130. this._x = ( m32 - m23 ) * s;
  131. this._y = ( m13 - m31 ) * s;
  132. this._z = ( m21 - m12 ) * s;
  133. } else if ( m11 > m22 && m11 > m33 ) {
  134. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  135. this._w = ( m32 - m23 ) / s;
  136. this._x = 0.25 * s;
  137. this._y = ( m12 + m21 ) / s;
  138. this._z = ( m13 + m31 ) / s;
  139. } else if ( m22 > m33 ) {
  140. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  141. this._w = ( m13 - m31 ) / s;
  142. this._x = ( m12 + m21 ) / s;
  143. this._y = 0.25 * s;
  144. this._z = ( m23 + m32 ) / s;
  145. } else {
  146. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  147. this._w = ( m21 - m12 ) / s;
  148. this._x = ( m13 + m31 ) / s;
  149. this._y = ( m23 + m32 ) / s;
  150. this._z = 0.25 * s;
  151. }
  152. this.onChangeCallback();
  153. return this;
  154. },
  155. setFromUnitVectors: function () {
  156. // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  157. // assumes direction vectors vFrom and vTo are normalized
  158. var v1, r;
  159. var EPS = 0.000001;
  160. return function ( vFrom, vTo ) {
  161. if ( v1 === undefined ) v1 = new THREE.Vector3();
  162. r = vFrom.dot( vTo ) + 1;
  163. if ( r < EPS ) {
  164. r = 0;
  165. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  166. v1.set( - vFrom.y, vFrom.x, 0 );
  167. } else {
  168. v1.set( 0, - vFrom.z, vFrom.y );
  169. }
  170. } else {
  171. v1.crossVectors( vFrom, vTo );
  172. }
  173. this._x = v1.x;
  174. this._y = v1.y;
  175. this._z = v1.z;
  176. this._w = r;
  177. this.normalize();
  178. return this;
  179. }
  180. }(),
  181. inverse: function () {
  182. this.conjugate().normalize();
  183. return this;
  184. },
  185. conjugate: function () {
  186. this._x *= - 1;
  187. this._y *= - 1;
  188. this._z *= - 1;
  189. this.onChangeCallback();
  190. return this;
  191. },
  192. dot: function ( v ) {
  193. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  194. },
  195. lengthSq: function () {
  196. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  197. },
  198. length: function () {
  199. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  200. },
  201. normalize: function () {
  202. var l = this.length();
  203. if ( l === 0 ) {
  204. this._x = 0;
  205. this._y = 0;
  206. this._z = 0;
  207. this._w = 1;
  208. } else {
  209. l = 1 / l;
  210. this._x = this._x * l;
  211. this._y = this._y * l;
  212. this._z = this._z * l;
  213. this._w = this._w * l;
  214. }
  215. this.onChangeCallback();
  216. return this;
  217. },
  218. multiply: function ( q, p ) {
  219. if ( p !== undefined ) {
  220. console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  221. return this.multiplyQuaternions( q, p );
  222. }
  223. return this.multiplyQuaternions( this, q );
  224. },
  225. multiplyQuaternions: function ( a, b ) {
  226. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  227. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  228. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  229. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  230. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  231. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  232. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  233. this.onChangeCallback();
  234. return this;
  235. },
  236. multiplyVector3: function ( vector ) {
  237. console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  238. return vector.applyQuaternion( this );
  239. },
  240. slerp: function ( qb, t ) {
  241. if ( t === 0 ) return this;
  242. if ( t === 1 ) return this.copy( qb );
  243. var x = this._x, y = this._y, z = this._z, w = this._w;
  244. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  245. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  246. if ( cosHalfTheta < 0 ) {
  247. this._w = - qb._w;
  248. this._x = - qb._x;
  249. this._y = - qb._y;
  250. this._z = - qb._z;
  251. cosHalfTheta = - cosHalfTheta;
  252. } else {
  253. this.copy( qb );
  254. }
  255. if ( cosHalfTheta >= 1.0 ) {
  256. this._w = w;
  257. this._x = x;
  258. this._y = y;
  259. this._z = z;
  260. return this;
  261. }
  262. var halfTheta = Math.acos( cosHalfTheta );
  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 ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  272. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  273. this._w = ( w * ratioA + this._w * ratioB );
  274. this._x = ( x * ratioA + this._x * ratioB );
  275. this._y = ( y * ratioA + this._y * ratioB );
  276. this._z = ( z * ratioA + this._z * ratioB );
  277. this.onChangeCallback();
  278. return this;
  279. },
  280. equals: function ( quaternion ) {
  281. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  282. },
  283. fromArray: function ( array ) {
  284. this._x = array[ 0 ];
  285. this._y = array[ 1 ];
  286. this._z = array[ 2 ];
  287. this._w = array[ 3 ];
  288. this.onChangeCallback();
  289. return this;
  290. },
  291. toArray: function () {
  292. return [ this._x, this._y, this._z, this._w ];
  293. },
  294. onChange: function ( callback ) {
  295. this.onChangeCallback = callback;
  296. return this;
  297. },
  298. onChangeCallback: function () {},
  299. clone: function () {
  300. return new THREE.Quaternion( this._x, this._y, this._z, this._w );
  301. }
  302. };
  303. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  304. return qm.copy( qa ).slerp( qb, t );
  305. }