Quaternion.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Quaternion = function( x, y, z, w ) {
  6. this.set(
  7. x || 0,
  8. y || 0,
  9. z || 0,
  10. w !== undefined ? w : 1
  11. );
  12. };
  13. THREE.Quaternion.prototype = {
  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 ( vec3 ) {
  29. var c = 0.5 * Math.PI / 360, // 0.5 is an optimization
  30. x = vec3.x * c,
  31. y = vec3.y * c,
  32. z = vec3.z * c,
  33. c1 = Math.cos( y ),
  34. s1 = Math.sin( y ),
  35. c2 = Math.cos( -z ),
  36. s2 = Math.sin( -z ),
  37. c3 = Math.cos( x ),
  38. s3 = Math.sin( x ),
  39. c1c2 = c1 * c2,
  40. s1s2 = s1 * s2;
  41. this.w = c1c2 * c3 - s1s2 * s3;
  42. this.x = c1c2 * s3 + s1s2 * c3;
  43. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  44. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  45. return this;
  46. },
  47. setFromAxisAngle: function ( axis, angle ) {
  48. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  49. // axis have to be normalized
  50. var halfAngle = angle / 2,
  51. s = Math.sin( halfAngle );
  52. this.x = axis.x * s;
  53. this.y = axis.y * s;
  54. this.z = axis.z * s;
  55. this.w = Math.cos( halfAngle );
  56. return this;
  57. },
  58. calculateW : function () {
  59. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  60. return this;
  61. },
  62. inverse : function () {
  63. this.x *= -1;
  64. this.y *= -1;
  65. this.z *= -1;
  66. return this;
  67. },
  68. length : function () {
  69. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  70. },
  71. normalize : function () {
  72. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  73. if ( l == 0 ) {
  74. this.x = 0;
  75. this.y = 0;
  76. this.z = 0;
  77. this.w = 0;
  78. } else {
  79. l = 1 / l;
  80. this.x = this.x * l;
  81. this.y = this.y * l;
  82. this.z = this.z * l;
  83. this.w = this.w * l;
  84. }
  85. return this;
  86. },
  87. multiplySelf : function ( quat2 ) {
  88. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  89. qbx = quat2.x, qby = quat2.y, qbz = quat2.z, qbw = quat2.w;
  90. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  91. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  92. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  93. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  94. return this;
  95. },
  96. multiply: function ( q1, q2 ) {
  97. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  98. this.x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
  99. this.y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
  100. this.z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
  101. this.w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
  102. return this;
  103. },
  104. multiplyVector3 : function ( vec, dest ) {
  105. if( !dest ) { dest = vec; }
  106. var x = vec.x, y = vec.y, z = vec.z,
  107. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  108. // calculate quat * vec
  109. var ix = qw * x + qy * z - qz * y,
  110. iy = qw * y + qz * x - qx * z,
  111. iz = qw * z + qx * y - qy * x,
  112. iw = -qx * x - qy * y - qz * z;
  113. // calculate result * inverse quat
  114. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  115. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  116. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  117. return dest;
  118. }
  119. }
  120. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  121. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  122. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  123. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  124. return qm;
  125. }
  126. var halfTheta = Math.acos( cosHalfTheta ),
  127. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  128. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  129. qm.w = 0.5 * ( qa.w + qb.w );
  130. qm.x = 0.5 * ( qa.x + qb.x );
  131. qm.y = 0.5 * ( qa.y + qb.y );
  132. qm.z = 0.5 * ( qa.z + qb.z );
  133. return qm;
  134. }
  135. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  136. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  137. qm.w = ( qa.w * ratioA + qb.w * ratioB );
  138. qm.x = ( qa.x * ratioA + qb.x * ratioB );
  139. qm.y = ( qa.y * ratioA + qb.y * ratioB );
  140. qm.z = ( qa.z * ratioA + qb.z * ratioB );
  141. return qm;
  142. }