Quaternion.js 7.5 KB

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