Quaternion.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.x = x || 0;
  7. this.y = y || 0;
  8. this.z = z || 0;
  9. this.w = ( w !== undefined ) ? w : 1;
  10. };
  11. THREE.Quaternion.prototype = {
  12. constructor: THREE.Quaternion,
  13. set: function ( x, y, z, w ) {
  14. this.x = x;
  15. this.y = y;
  16. this.z = z;
  17. this.w = w;
  18. return this;
  19. },
  20. copy: function ( q ) {
  21. this.x = q.x;
  22. this.y = q.y;
  23. this.z = q.z;
  24. this.w = q.w;
  25. return this;
  26. },
  27. clone: function () {
  28. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  29. },
  30. setFromEuler: function ( vec3 ) {
  31. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  32. x = vec3.x * c,
  33. y = vec3.y * c,
  34. z = vec3.z * c,
  35. c1 = Math.cos( y ),
  36. s1 = Math.sin( y ),
  37. c2 = Math.cos( -z ),
  38. s2 = Math.sin( -z ),
  39. c3 = Math.cos( x ),
  40. s3 = Math.sin( x ),
  41. c1c2 = c1 * c2,
  42. s1s2 = s1 * s2;
  43. this.w = c1c2 * c3 - s1s2 * s3;
  44. this.x = c1c2 * s3 + s1s2 * c3;
  45. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  46. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  47. return this;
  48. },
  49. setFromAxisAngle: function ( axis, angle ) {
  50. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  51. // axis have to be normalized
  52. var halfAngle = angle / 2,
  53. s = Math.sin( halfAngle );
  54. this.x = axis.x * s;
  55. this.y = axis.y * s;
  56. this.z = axis.z * s;
  57. this.w = Math.cos( halfAngle );
  58. return this;
  59. },
  60. setFromRotationMatrix: function ( m ) {
  61. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  62. function copySign( a, b ) {
  63. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  64. }
  65. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  66. this.w = Math.sqrt( Math.max( 0, absQ + m.n11 + m.n22 + m.n33 ) ) / 2;
  67. this.x = Math.sqrt( Math.max( 0, absQ + m.n11 - m.n22 - m.n33 ) ) / 2;
  68. this.y = Math.sqrt( Math.max( 0, absQ - m.n11 + m.n22 - m.n33 ) ) / 2;
  69. this.z = Math.sqrt( Math.max( 0, absQ - m.n11 - m.n22 + m.n33 ) ) / 2;
  70. this.x = copySign( this.x, ( m.n32 - m.n23 ) );
  71. this.y = copySign( this.y, ( m.n13 - m.n31 ) );
  72. this.z = copySign( this.z, ( m.n21 - m.n12 ) );
  73. this.normalize();
  74. return this;
  75. },
  76. calculateW : function () {
  77. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  78. return this;
  79. },
  80. inverse: function () {
  81. this.x *= -1;
  82. this.y *= -1;
  83. this.z *= -1;
  84. return this;
  85. },
  86. length: function () {
  87. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  88. },
  89. normalize: function () {
  90. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  91. if ( l === 0 ) {
  92. this.x = 0;
  93. this.y = 0;
  94. this.z = 0;
  95. this.w = 0;
  96. } else {
  97. l = 1 / l;
  98. this.x = this.x * l;
  99. this.y = this.y * l;
  100. this.z = this.z * l;
  101. this.w = this.w * l;
  102. }
  103. return this;
  104. },
  105. multiplySelf: function ( quat2 ) {
  106. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  107. qbx = quat2.x, qby = quat2.y, qbz = quat2.z, qbw = quat2.w;
  108. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  109. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  110. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  111. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  112. return this;
  113. },
  114. multiply: function ( q1, q2 ) {
  115. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  116. this.x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
  117. this.y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
  118. this.z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
  119. this.w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
  120. return this;
  121. },
  122. multiplyVector3: function ( vec, dest ) {
  123. if ( !dest ) { dest = vec; }
  124. var x = vec.x, y = vec.y, z = vec.z,
  125. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  126. // calculate quat * vec
  127. var ix = qw * x + qy * z - qz * y,
  128. iy = qw * y + qz * x - qx * z,
  129. iz = qw * z + qx * y - qy * x,
  130. iw = -qx * x - qy * y - qz * z;
  131. // calculate result * inverse quat
  132. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  133. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  134. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  135. return dest;
  136. }
  137. }
  138. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  139. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  140. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  141. if (cosHalfTheta < 0) {
  142. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  143. cosHalfTheta = -cosHalfTheta;
  144. } else {
  145. qm.copy(qb);
  146. }
  147. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  148. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  149. return qm;
  150. }
  151. var halfTheta = Math.acos( cosHalfTheta ),
  152. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  153. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  154. qm.w = 0.5 * ( qa.w + qb.w );
  155. qm.x = 0.5 * ( qa.x + qb.x );
  156. qm.y = 0.5 * ( qa.y + qb.y );
  157. qm.z = 0.5 * ( qa.z + qb.z );
  158. return qm;
  159. }
  160. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  161. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  162. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  163. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  164. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  165. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  166. return qm;
  167. }