Quaternion.js 4.3 KB

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