Quaternion.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. * @author bhouston / http://exocortex.com
  6. */
  7. THREE.Quaternion = function( x, y, z, w ) {
  8. this.x = x || 0;
  9. this.y = y || 0;
  10. this.z = z || 0;
  11. this.w = ( w !== undefined ) ? w : 1;
  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 ( v, order ) {
  30. // http://www.mathworks.com/matlabcentral/fileexchange/
  31. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  32. // content/SpinCalc.m
  33. var c1 = Math.cos( v.x / 2 );
  34. var c2 = Math.cos( v.y / 2 );
  35. var c3 = Math.cos( v.z / 2 );
  36. var s1 = Math.sin( v.x / 2 );
  37. var s2 = Math.sin( v.y / 2 );
  38. var s3 = Math.sin( v.z / 2 );
  39. if ( order === undefined || order === 'XYZ' ) {
  40. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  41. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  42. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  43. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  44. } else if ( order === 'YXZ' ) {
  45. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  46. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  47. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  48. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  49. } else if ( order === 'ZXY' ) {
  50. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  51. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  52. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  53. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  54. } else if ( order === 'ZYX' ) {
  55. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  56. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  57. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  58. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  59. } else if ( order === 'YZX' ) {
  60. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  61. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  62. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  63. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  64. } else if ( order === 'XZY' ) {
  65. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  66. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  67. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  68. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  69. }
  70. return this;
  71. },
  72. setFromAxisAngle: function ( axis, angle ) {
  73. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  74. // axis have to be normalized
  75. var halfAngle = angle / 2,
  76. s = Math.sin( halfAngle );
  77. this.x = axis.x * s;
  78. this.y = axis.y * s;
  79. this.z = axis.z * s;
  80. this.w = Math.cos( halfAngle );
  81. return this;
  82. },
  83. setFromRotationMatrix: function ( m ) {
  84. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  85. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  86. var te = m.elements,
  87. m11 = te[0], m12 = te[4], m13 = te[8],
  88. m21 = te[1], m22 = te[5], m23 = te[9],
  89. m31 = te[2], m32 = te[6], m33 = te[10],
  90. trace = m11 + m22 + m33,
  91. s;
  92. if( trace > 0 ) {
  93. s = 0.5 / Math.sqrt( trace + 1.0 );
  94. this.w = 0.25 / s;
  95. this.x = ( m32 - m23 ) * s;
  96. this.y = ( m13 - m31 ) * s;
  97. this.z = ( m21 - m12 ) * s;
  98. } else if ( m11 > m22 && m11 > m33 ) {
  99. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  100. this.w = (m32 - m23 ) / s;
  101. this.x = 0.25 * s;
  102. this.y = (m12 + m21 ) / s;
  103. this.z = (m13 + m31 ) / s;
  104. } else if (m22 > m33) {
  105. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  106. this.w = (m13 - m31 ) / s;
  107. this.x = (m12 + m21 ) / s;
  108. this.y = 0.25 * s;
  109. this.z = (m23 + m32 ) / s;
  110. } else {
  111. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  112. this.w = ( m21 - m12 ) / s;
  113. this.x = ( m13 + m31 ) / s;
  114. this.y = ( m23 + m32 ) / s;
  115. this.z = 0.25 * s;
  116. }
  117. return this;
  118. },
  119. inverse: function () {
  120. this.conjugate().normalize();
  121. return this;
  122. },
  123. conjugate: function () {
  124. this.x *= -1;
  125. this.y *= -1;
  126. this.z *= -1;
  127. return this;
  128. },
  129. lengthSq: function () {
  130. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  131. },
  132. length: function () {
  133. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  134. },
  135. normalize: function () {
  136. var l = this.length();
  137. if ( l === 0 ) {
  138. this.x = 0;
  139. this.y = 0;
  140. this.z = 0;
  141. this.w = 1;
  142. } else {
  143. l = 1 / l;
  144. this.x = this.x * l;
  145. this.y = this.y * l;
  146. this.z = this.z * l;
  147. this.w = this.w * l;
  148. }
  149. return this;
  150. },
  151. multiply: function ( a, b ) {
  152. this.copy( a );
  153. return this.multiplySelf( b );
  154. },
  155. multiplySelf: function ( b ) {
  156. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  157. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  158. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  159. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  160. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  161. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  162. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  163. return this;
  164. },
  165. slerpSelf: function ( qb, t ) {
  166. var x = this.x, y = this.y, z = this.z, w = this.w;
  167. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  168. var cosHalfTheta = w * qb.w + x * qb.x + y * qb.y + z * qb.z;
  169. if ( cosHalfTheta < 0 ) {
  170. this.w = -qb.w;
  171. this.x = -qb.x;
  172. this.y = -qb.y;
  173. this.z = -qb.z;
  174. cosHalfTheta = -cosHalfTheta;
  175. } else {
  176. this.copy( qb );
  177. }
  178. if ( cosHalfTheta >= 1.0 ) {
  179. this.w = w;
  180. this.x = x;
  181. this.y = y;
  182. this.z = z;
  183. return this;
  184. }
  185. var halfTheta = Math.acos( cosHalfTheta );
  186. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  187. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  188. this.w = 0.5 * ( w + this.w );
  189. this.x = 0.5 * ( x + this.x );
  190. this.y = 0.5 * ( y + this.y );
  191. this.z = 0.5 * ( z + this.z );
  192. return this;
  193. }
  194. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  195. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  196. this.w = ( w * ratioA + this.w * ratioB );
  197. this.x = ( x * ratioA + this.x * ratioB );
  198. this.y = ( y * ratioA + this.y * ratioB );
  199. this.z = ( z * ratioA + this.z * ratioB );
  200. return this;
  201. },
  202. equals: function ( v ) {
  203. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  204. },
  205. clone: function () {
  206. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  207. }
  208. }
  209. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  210. return qm.copy( qa ).slerpSelf( qb, t );
  211. }