Quaternion.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  64. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  65. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  66. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  67. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  68. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  69. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  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. slerpSelf: function ( qb, t ) {
  135. var x = this.x, y = this.y, z = this.z, w = this.w;
  136. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  137. var cosHalfTheta = w * qb.w + x * qb.x + y * qb.y + z * qb.z;
  138. if ( cosHalfTheta < 0 ) {
  139. this.w = -qb.w;
  140. this.x = -qb.x;
  141. this.y = -qb.y;
  142. this.z = -qb.z;
  143. cosHalfTheta = -cosHalfTheta;
  144. } else {
  145. this.copy( qb );
  146. }
  147. if ( cosHalfTheta >= 1.0 ) {
  148. this.w = w;
  149. this.x = x;
  150. this.y = y;
  151. this.z = z;
  152. return this;
  153. }
  154. var halfTheta = Math.acos( cosHalfTheta );
  155. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  156. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  157. this.w = 0.5 * ( w + this.w );
  158. this.x = 0.5 * ( x + this.x );
  159. this.y = 0.5 * ( y + this.y );
  160. this.z = 0.5 * ( z + this.z );
  161. return this;
  162. }
  163. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  164. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  165. this.w = ( w * ratioA + this.w * ratioB );
  166. this.x = ( x * ratioA + this.x * ratioB );
  167. this.y = ( y * ratioA + this.y * ratioB );
  168. this.z = ( z * ratioA + this.z * ratioB );
  169. return this;
  170. },
  171. clone: function () {
  172. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  173. }
  174. }
  175. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  176. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  177. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  178. if ( cosHalfTheta < 0 ) {
  179. qm.w = -qb.w;
  180. qm.x = -qb.x;
  181. qm.y = -qb.y;
  182. qm.z = -qb.z;
  183. cosHalfTheta = -cosHalfTheta;
  184. } else {
  185. qm.copy( qb );
  186. }
  187. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  188. qm.w = qa.w;
  189. qm.x = qa.x;
  190. qm.y = qa.y;
  191. qm.z = qa.z;
  192. return qm;
  193. }
  194. var halfTheta = Math.acos( cosHalfTheta );
  195. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  196. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  197. qm.w = 0.5 * ( qa.w + qm.w );
  198. qm.x = 0.5 * ( qa.x + qm.x );
  199. qm.y = 0.5 * ( qa.y + qm.y );
  200. qm.z = 0.5 * ( qa.z + qm.z );
  201. return qm;
  202. }
  203. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta;
  204. var ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  205. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  206. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  207. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  208. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  209. return qm;
  210. }