Quaternion.js 7.0 KB

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