Quaternion.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. */
  6. THREE.Quaternion = function( x, y, z, w ) {
  7. this.x = x || 0;
  8. this.y = y || 0;
  9. this.z = z || 0;
  10. this.w = ( w !== undefined ) ? w : 1;
  11. };
  12. THREE.Quaternion.prototype = {
  13. constructor: THREE.Quaternion,
  14. set: function ( x, y, z, w ) {
  15. this.x = x;
  16. this.y = y;
  17. this.z = z;
  18. this.w = w;
  19. return this;
  20. },
  21. copy: function ( q ) {
  22. this.x = q.x;
  23. this.y = q.y;
  24. this.z = q.z;
  25. this.w = q.w;
  26. return this;
  27. },
  28. setFromEuler: function ( v, order ) {
  29. // http://www.mathworks.com/matlabcentral/fileexchange/
  30. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  31. // content/SpinCalc.m
  32. var c1 = Math.cos( v.x / 2 );
  33. var c2 = Math.cos( v.y / 2 );
  34. var c3 = Math.cos( v.z / 2 );
  35. var s1 = Math.sin( v.x / 2 );
  36. var s2 = Math.sin( v.y / 2 );
  37. var s3 = Math.sin( v.z / 2 );
  38. if ( order === undefined || order === 'XYZ' ) {
  39. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  40. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  41. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  42. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  43. } else if ( order === 'YXZ' ) {
  44. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  45. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  46. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  47. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  48. } else if ( order === 'ZXY' ) {
  49. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  50. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  51. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  52. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  53. } else if ( order === 'ZYX' ) {
  54. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  55. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  56. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  57. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  58. } else if ( order === 'YZX' ) {
  59. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  60. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  61. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  62. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  63. } else if ( order === 'XZY' ) {
  64. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  65. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  66. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  67. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  68. }
  69. return this;
  70. },
  71. setFromAxisAngle: function ( axis, angle ) {
  72. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  73. // axis have to be normalized
  74. var halfAngle = angle / 2,
  75. s = Math.sin( halfAngle );
  76. this.x = axis.x * s;
  77. this.y = axis.y * s;
  78. this.z = axis.z * s;
  79. this.w = Math.cos( halfAngle );
  80. return this;
  81. },
  82. setFromRotationMatrix: function ( m ) {
  83. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  84. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  85. var te = m.elements,
  86. m11 = te[0], m12 = te[4], m13 = te[8],
  87. m21 = te[1], m22 = te[5], m23 = te[9],
  88. m31 = te[2], m32 = te[6], m33 = te[10],
  89. trace = m11 + m22 + m33,
  90. s;
  91. if( trace > 0 ) {
  92. s = 0.5 / Math.sqrt( trace + 1.0 );
  93. this.w = 0.25 / s;
  94. this.x = ( m32 - m23 ) * s;
  95. this.y = ( m13 - m31 ) * s;
  96. this.z = ( m21 - m12 ) * s;
  97. } else if ( m11 > m22 && m11 > m33 ) {
  98. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  99. this.w = (m32 - m23 ) / s;
  100. this.x = 0.25 * s;
  101. this.y = (m12 + m21 ) / s;
  102. this.z = (m13 + m31 ) / s;
  103. } else if (m22 > m33) {
  104. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  105. this.w = (m13 - m31 ) / s;
  106. this.x = (m12 + m21 ) / s;
  107. this.y = 0.25 * s;
  108. this.z = (m23 + m32 ) / s;
  109. } else {
  110. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  111. this.w = ( m21 - m12 ) / s;
  112. this.x = ( m13 + m31 ) / s;
  113. this.y = ( m23 + m32 ) / s;
  114. this.z = 0.25 * s;
  115. }
  116. return this;
  117. },
  118. inverse: function () {
  119. this.conjugate().normalize();
  120. return this;
  121. },
  122. conjugate: function () {
  123. this.x *= -1;
  124. this.y *= -1;
  125. this.z *= -1;
  126. return this;
  127. },
  128. length: function () {
  129. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  130. },
  131. normalize: function () {
  132. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  133. if ( l === 0 ) {
  134. this.x = 0;
  135. this.y = 0;
  136. this.z = 0;
  137. this.w = 1;
  138. } else {
  139. l = 1 / l;
  140. this.x = this.x * l;
  141. this.y = this.y * l;
  142. this.z = this.z * l;
  143. this.w = this.w * l;
  144. }
  145. return this;
  146. },
  147. multiply: function ( a, b ) {
  148. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  149. var qax = a.x, qay = a.y, qaz = a.z, qaw = a.w,
  150. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  151. this.x = qax * qbw + qay * qbz - qaz * qby + qaw * qbx;
  152. this.y = -qax * qbz + qay * qbw + qaz * qbx + qaw * qby;
  153. this.z = qax * qby - qay * qbx + qaz * qbw + qaw * qbz;
  154. this.w = -qax * qbx - qay * qby - qaz * qbz + qaw * qbw;
  155. return this;
  156. },
  157. multiplySelf: function ( b ) {
  158. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  159. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  160. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  161. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  162. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  163. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  164. return this;
  165. },
  166. multiplyVector3: function ( vector, dest ) {
  167. if ( !dest ) { dest = vector; }
  168. var x = vector.x, y = vector.y, z = vector.z,
  169. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  170. // calculate quat * vector
  171. var ix = qw * x + qy * z - qz * y,
  172. iy = qw * y + qz * x - qx * z,
  173. iz = qw * z + qx * y - qy * x,
  174. iw = -qx * x - qy * y - qz * z;
  175. // calculate result * inverse quat
  176. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  177. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  178. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  179. return dest;
  180. },
  181. slerpSelf: function ( qb, t ) {
  182. var x = this.x, y = this.y, z = this.z, w = this.w;
  183. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  184. var cosHalfTheta = w * qb.w + x * qb.x + y * qb.y + z * qb.z;
  185. if ( cosHalfTheta < 0 ) {
  186. this.w = -qb.w;
  187. this.x = -qb.x;
  188. this.y = -qb.y;
  189. this.z = -qb.z;
  190. cosHalfTheta = -cosHalfTheta;
  191. } else {
  192. this.copy( qb );
  193. }
  194. if ( cosHalfTheta >= 1.0 ) {
  195. this.w = w;
  196. this.x = x;
  197. this.y = y;
  198. this.z = z;
  199. return this;
  200. }
  201. var halfTheta = Math.acos( cosHalfTheta );
  202. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  203. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  204. this.w = 0.5 * ( w + this.w );
  205. this.x = 0.5 * ( x + this.x );
  206. this.y = 0.5 * ( y + this.y );
  207. this.z = 0.5 * ( z + this.z );
  208. return this;
  209. }
  210. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  211. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  212. this.w = ( w * ratioA + this.w * ratioB );
  213. this.x = ( x * ratioA + this.x * ratioB );
  214. this.y = ( y * ratioA + this.y * ratioB );
  215. this.z = ( z * ratioA + this.z * ratioB );
  216. return this;
  217. },
  218. clone: function () {
  219. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  220. }
  221. }
  222. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  223. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  224. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  225. if ( cosHalfTheta < 0 ) {
  226. qm.w = -qb.w;
  227. qm.x = -qb.x;
  228. qm.y = -qb.y;
  229. qm.z = -qb.z;
  230. cosHalfTheta = -cosHalfTheta;
  231. } else {
  232. qm.copy( qb );
  233. }
  234. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  235. qm.w = qa.w;
  236. qm.x = qa.x;
  237. qm.y = qa.y;
  238. qm.z = qa.z;
  239. return qm;
  240. }
  241. var halfTheta = Math.acos( cosHalfTheta );
  242. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  243. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  244. qm.w = 0.5 * ( qa.w + qm.w );
  245. qm.x = 0.5 * ( qa.x + qm.x );
  246. qm.y = 0.5 * ( qa.y + qm.y );
  247. qm.z = 0.5 * ( qa.z + qm.z );
  248. return qm;
  249. }
  250. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta;
  251. var ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  252. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  253. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  254. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  255. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  256. return qm;
  257. }