Quaternion.js 8.6 KB

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