Quaternion.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. _euler: undefined,
  17. _updateEuler: function ( callback ) {
  18. if ( this._euler !== undefined ) {
  19. this._euler.setFromQuaternion( this, undefined, false );
  20. }
  21. },
  22. get x () {
  23. return this._x;
  24. },
  25. set x ( value ) {
  26. this._x = value;
  27. this._updateEuler();
  28. },
  29. get y () {
  30. return this._y;
  31. },
  32. set y ( value ) {
  33. this._y = value;
  34. this._updateEuler();
  35. },
  36. get z () {
  37. return this._z;
  38. },
  39. set z ( value ) {
  40. this._z = value;
  41. this._updateEuler();
  42. },
  43. get w () {
  44. return this._w;
  45. },
  46. set w ( value ) {
  47. this._w = value;
  48. this._updateEuler();
  49. },
  50. set: function ( x, y, z, w ) {
  51. this._x = x;
  52. this._y = y;
  53. this._z = z;
  54. this._w = w;
  55. this._updateEuler();
  56. return this;
  57. },
  58. copy: function ( quaternion ) {
  59. this._x = quaternion._x;
  60. this._y = quaternion._y;
  61. this._z = quaternion._z;
  62. this._w = quaternion._w;
  63. this._updateEuler();
  64. return this;
  65. },
  66. setFromEuler: function ( euler, update ) {
  67. if ( euler instanceof THREE.Euler === false ) {
  68. throw new Error( 'ERROR: Quaternion\'s .setFromEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' );
  69. }
  70. // http://www.mathworks.com/matlabcentral/fileexchange/
  71. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  72. // content/SpinCalc.m
  73. var c1 = Math.cos( euler._x / 2 );
  74. var c2 = Math.cos( euler._y / 2 );
  75. var c3 = Math.cos( euler._z / 2 );
  76. var s1 = Math.sin( euler._x / 2 );
  77. var s2 = Math.sin( euler._y / 2 );
  78. var s3 = Math.sin( euler._z / 2 );
  79. if ( euler.order === 'XYZ' ) {
  80. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  81. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  82. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  83. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  84. } else if ( euler.order === 'YXZ' ) {
  85. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  86. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  87. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  88. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  89. } else if ( euler.order === 'ZXY' ) {
  90. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  91. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  92. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  93. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  94. } else if ( euler.order === 'ZYX' ) {
  95. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  96. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  97. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  98. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  99. } else if ( euler.order === 'YZX' ) {
  100. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  101. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  102. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  103. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  104. } else if ( euler.order === 'XZY' ) {
  105. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  106. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  107. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  108. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  109. }
  110. if ( update !== false ) this._updateEuler();
  111. return this;
  112. },
  113. setFromAxisAngle: function ( axis, angle ) {
  114. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  115. // axis have to be normalized
  116. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  117. this._x = axis.x * s;
  118. this._y = axis.y * s;
  119. this._z = axis.z * s;
  120. this._w = Math.cos( halfAngle );
  121. this._updateEuler();
  122. return this;
  123. },
  124. setFromRotationMatrix: function ( m ) {
  125. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  126. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  127. var te = m.elements,
  128. m11 = te[0], m12 = te[4], m13 = te[8],
  129. m21 = te[1], m22 = te[5], m23 = te[9],
  130. m31 = te[2], m32 = te[6], m33 = te[10],
  131. trace = m11 + m22 + m33,
  132. s;
  133. if ( trace > 0 ) {
  134. s = 0.5 / Math.sqrt( trace + 1.0 );
  135. this._w = 0.25 / s;
  136. this._x = ( m32 - m23 ) * s;
  137. this._y = ( m13 - m31 ) * s;
  138. this._z = ( m21 - m12 ) * s;
  139. } else if ( m11 > m22 && m11 > m33 ) {
  140. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  141. this._w = (m32 - m23 ) / s;
  142. this._x = 0.25 * s;
  143. this._y = (m12 + m21 ) / s;
  144. this._z = (m13 + m31 ) / s;
  145. } else if ( m22 > m33 ) {
  146. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  147. this._w = (m13 - m31 ) / s;
  148. this._x = (m12 + m21 ) / s;
  149. this._y = 0.25 * s;
  150. this._z = (m23 + m32 ) / s;
  151. } else {
  152. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  153. this._w = ( m21 - m12 ) / s;
  154. this._x = ( m13 + m31 ) / s;
  155. this._y = ( m23 + m32 ) / s;
  156. this._z = 0.25 * s;
  157. }
  158. this._updateEuler();
  159. return this;
  160. },
  161. inverse: function () {
  162. this.conjugate().normalize();
  163. return this;
  164. },
  165. conjugate: function () {
  166. this._x *= -1;
  167. this._y *= -1;
  168. this._z *= -1;
  169. this._updateEuler();
  170. return this;
  171. },
  172. lengthSq: function () {
  173. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  174. },
  175. length: function () {
  176. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  177. },
  178. normalize: function () {
  179. var l = this.length();
  180. if ( l === 0 ) {
  181. this._x = 0;
  182. this._y = 0;
  183. this._z = 0;
  184. this._w = 1;
  185. } else {
  186. l = 1 / l;
  187. this._x = this._x * l;
  188. this._y = this._y * l;
  189. this._z = this._z * l;
  190. this._w = this._w * l;
  191. }
  192. return this;
  193. },
  194. multiply: function ( q, p ) {
  195. if ( p !== undefined ) {
  196. console.warn( 'DEPRECATED: Quaternion\'s .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  197. return this.multiplyQuaternions( q, p );
  198. }
  199. return this.multiplyQuaternions( this, q );
  200. },
  201. multiplyQuaternions: function ( a, b ) {
  202. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  203. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  204. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  205. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  206. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  207. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  208. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  209. this._updateEuler();
  210. return this;
  211. },
  212. multiplyVector3: function ( vector ) {
  213. console.warn( 'DEPRECATED: Quaternion\'s .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  214. return vector.applyQuaternion( this );
  215. },
  216. slerp: function ( qb, t ) {
  217. var x = this._x, y = this._y, z = this._z, w = this._w;
  218. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  219. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  220. if ( cosHalfTheta < 0 ) {
  221. this._w = -qb._w;
  222. this._x = -qb._x;
  223. this._y = -qb._y;
  224. this._z = -qb._z;
  225. cosHalfTheta = -cosHalfTheta;
  226. } else {
  227. this.copy( qb );
  228. }
  229. if ( cosHalfTheta >= 1.0 ) {
  230. this._w = w;
  231. this._x = x;
  232. this._y = y;
  233. this._z = z;
  234. return this;
  235. }
  236. var halfTheta = Math.acos( cosHalfTheta );
  237. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  238. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  239. this._w = 0.5 * ( w + this._w );
  240. this._x = 0.5 * ( x + this._x );
  241. this._y = 0.5 * ( y + this._y );
  242. this._z = 0.5 * ( z + this._z );
  243. return this;
  244. }
  245. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  246. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  247. this._w = ( w * ratioA + this._w * ratioB );
  248. this._x = ( x * ratioA + this._x * ratioB );
  249. this._y = ( y * ratioA + this._y * ratioB );
  250. this._z = ( z * ratioA + this._z * ratioB );
  251. this._updateEuler();
  252. return this;
  253. },
  254. equals: function ( quaternion ) {
  255. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  256. },
  257. fromArray: function ( array ) {
  258. this._x = array[ 0 ];
  259. this._y = array[ 1 ];
  260. this._z = array[ 2 ];
  261. this._w = array[ 3 ];
  262. this._updateEuler();
  263. return this;
  264. },
  265. toArray: function () {
  266. return [ this._x, this._y, this._z, this._w ];
  267. },
  268. clone: function () {
  269. return new THREE.Quaternion( this._x, this._y, this._z, this._w );
  270. }
  271. };
  272. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  273. return qm.copy( qa ).slerp( qb, t );
  274. }