Quaternion.js 5.3 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. setFromEuler: function ( vector ) {
  28. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  29. x = vector.x * c,
  30. y = vector.y * c,
  31. z = vector.z * c,
  32. c1 = Math.cos( y ),
  33. s1 = Math.sin( y ),
  34. c2 = Math.cos( -z ),
  35. s2 = Math.sin( -z ),
  36. c3 = Math.cos( x ),
  37. s3 = Math.sin( x ),
  38. c1c2 = c1 * c2,
  39. s1s2 = s1 * s2;
  40. this.w = c1c2 * c3 - s1s2 * s3;
  41. this.x = c1c2 * s3 + s1s2 * c3;
  42. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  43. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  44. return this;
  45. },
  46. setFromAxisAngle: function ( axis, angle ) {
  47. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  48. // axis have to be normalized
  49. var halfAngle = angle / 2,
  50. s = Math.sin( halfAngle );
  51. this.x = axis.x * s;
  52. this.y = axis.y * s;
  53. this.z = axis.z * s;
  54. this.w = Math.cos( halfAngle );
  55. return this;
  56. },
  57. setFromRotationMatrix: function ( m ) {
  58. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  59. function copySign( a, b ) {
  60. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  61. }
  62. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  63. this.w = Math.sqrt( Math.max( 0, absQ + m.n11 + m.n22 + m.n33 ) ) / 2;
  64. this.x = Math.sqrt( Math.max( 0, absQ + m.n11 - m.n22 - m.n33 ) ) / 2;
  65. this.y = Math.sqrt( Math.max( 0, absQ - m.n11 + m.n22 - m.n33 ) ) / 2;
  66. this.z = Math.sqrt( Math.max( 0, absQ - m.n11 - m.n22 + m.n33 ) ) / 2;
  67. this.x = copySign( this.x, ( m.n32 - m.n23 ) );
  68. this.y = copySign( this.y, ( m.n13 - m.n31 ) );
  69. this.z = copySign( this.z, ( m.n21 - m.n12 ) );
  70. this.normalize();
  71. return this;
  72. },
  73. calculateW : function () {
  74. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  75. return this;
  76. },
  77. inverse: function () {
  78. this.x *= -1;
  79. this.y *= -1;
  80. this.z *= -1;
  81. return this;
  82. },
  83. length: function () {
  84. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  85. },
  86. normalize: function () {
  87. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  88. if ( l === 0 ) {
  89. this.x = 0;
  90. this.y = 0;
  91. this.z = 0;
  92. this.w = 0;
  93. } else {
  94. l = 1 / l;
  95. this.x = this.x * l;
  96. this.y = this.y * l;
  97. this.z = this.z * l;
  98. this.w = this.w * l;
  99. }
  100. return this;
  101. },
  102. multiply: function ( a, b ) {
  103. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  104. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  105. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  106. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  107. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  108. return this;
  109. },
  110. multiplySelf: function ( b ) {
  111. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  112. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  113. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  114. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  115. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  116. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  117. return this;
  118. },
  119. multiplyVector3: function ( vector, dest ) {
  120. if ( !dest ) { dest = vector; }
  121. var x = vector.x, y = vector.y, z = vector.z,
  122. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  123. // calculate quat * vector
  124. var ix = qw * x + qy * z - qz * y,
  125. iy = qw * y + qz * x - qx * z,
  126. iz = qw * z + qx * y - qy * x,
  127. iw = -qx * x - qy * y - qz * z;
  128. // calculate result * inverse quat
  129. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  130. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  131. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  132. return dest;
  133. },
  134. clone: function () {
  135. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  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. }