/js/Three/math/Quaternion.js

https://gitlab.com/mitch10e-byu/dight495 · JavaScript · 523 lines · 295 code · 211 blank · 17 comment · 43 complexity · c20e30301f20ee100a049c66c1528434 MD5 · raw file

  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. get x () {
  16. return this._x;
  17. },
  18. set x ( value ) {
  19. this._x = value;
  20. this.onChangeCallback();
  21. },
  22. get y () {
  23. return this._y;
  24. },
  25. set y ( value ) {
  26. this._y = value;
  27. this.onChangeCallback();
  28. },
  29. get z () {
  30. return this._z;
  31. },
  32. set z ( value ) {
  33. this._z = value;
  34. this.onChangeCallback();
  35. },
  36. get w () {
  37. return this._w;
  38. },
  39. set w ( value ) {
  40. this._w = value;
  41. this.onChangeCallback();
  42. },
  43. set: function ( x, y, z, w ) {
  44. this._x = x;
  45. this._y = y;
  46. this._z = z;
  47. this._w = w;
  48. this.onChangeCallback();
  49. return this;
  50. },
  51. clone: function () {
  52. return new this.constructor( this._x, this._y, this._z, this._w );
  53. },
  54. copy: function ( quaternion ) {
  55. this._x = quaternion.x;
  56. this._y = quaternion.y;
  57. this._z = quaternion.z;
  58. this._w = quaternion.w;
  59. this.onChangeCallback();
  60. return this;
  61. },
  62. setFromEuler: function ( euler, update ) {
  63. if ( euler instanceof THREE.Euler === false ) {
  64. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  65. }
  66. // http://www.mathworks.com/matlabcentral/fileexchange/
  67. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  68. // content/SpinCalc.m
  69. var c1 = Math.cos( euler._x / 2 );
  70. var c2 = Math.cos( euler._y / 2 );
  71. var c3 = Math.cos( euler._z / 2 );
  72. var s1 = Math.sin( euler._x / 2 );
  73. var s2 = Math.sin( euler._y / 2 );
  74. var s3 = Math.sin( euler._z / 2 );
  75. var order = euler.order;
  76. if ( order === 'XYZ' ) {
  77. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  78. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  79. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  80. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  81. } else if ( order === 'YXZ' ) {
  82. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  83. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  84. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  85. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  86. } else if ( order === 'ZXY' ) {
  87. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  88. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  89. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  90. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  91. } else if ( order === 'ZYX' ) {
  92. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  93. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  94. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  95. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  96. } else if ( order === 'YZX' ) {
  97. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  98. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  99. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  100. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  101. } else if ( order === 'XZY' ) {
  102. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  103. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  104. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  105. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  106. }
  107. if ( update !== false ) this.onChangeCallback();
  108. return this;
  109. },
  110. setFromAxisAngle: function ( axis, angle ) {
  111. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  112. // assumes axis is normalized
  113. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  114. this._x = axis.x * s;
  115. this._y = axis.y * s;
  116. this._z = axis.z * s;
  117. this._w = Math.cos( halfAngle );
  118. this.onChangeCallback();
  119. return this;
  120. },
  121. setFromRotationMatrix: function ( m ) {
  122. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  123. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  124. var te = m.elements,
  125. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  126. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  127. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  128. trace = m11 + m22 + m33,
  129. s;
  130. if ( trace > 0 ) {
  131. s = 0.5 / Math.sqrt( trace + 1.0 );
  132. this._w = 0.25 / s;
  133. this._x = ( m32 - m23 ) * s;
  134. this._y = ( m13 - m31 ) * s;
  135. this._z = ( m21 - m12 ) * s;
  136. } else if ( m11 > m22 && m11 > m33 ) {
  137. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  138. this._w = ( m32 - m23 ) / s;
  139. this._x = 0.25 * s;
  140. this._y = ( m12 + m21 ) / s;
  141. this._z = ( m13 + m31 ) / s;
  142. } else if ( m22 > m33 ) {
  143. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  144. this._w = ( m13 - m31 ) / s;
  145. this._x = ( m12 + m21 ) / s;
  146. this._y = 0.25 * s;
  147. this._z = ( m23 + m32 ) / s;
  148. } else {
  149. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  150. this._w = ( m21 - m12 ) / s;
  151. this._x = ( m13 + m31 ) / s;
  152. this._y = ( m23 + m32 ) / s;
  153. this._z = 0.25 * s;
  154. }
  155. this.onChangeCallback();
  156. return this;
  157. },
  158. setFromUnitVectors: function () {
  159. // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  160. // assumes direction vectors vFrom and vTo are normalized
  161. var v1, r;
  162. var EPS = 0.000001;
  163. return function ( vFrom, vTo ) {
  164. if ( v1 === undefined ) v1 = new THREE.Vector3();
  165. r = vFrom.dot( vTo ) + 1;
  166. if ( r < EPS ) {
  167. r = 0;
  168. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  169. v1.set( - vFrom.y, vFrom.x, 0 );
  170. } else {
  171. v1.set( 0, - vFrom.z, vFrom.y );
  172. }
  173. } else {
  174. v1.crossVectors( vFrom, vTo );
  175. }
  176. this._x = v1.x;
  177. this._y = v1.y;
  178. this._z = v1.z;
  179. this._w = r;
  180. this.normalize();
  181. return this;
  182. }
  183. }(),
  184. inverse: function () {
  185. this.conjugate().normalize();
  186. return this;
  187. },
  188. conjugate: function () {
  189. this._x *= - 1;
  190. this._y *= - 1;
  191. this._z *= - 1;
  192. this.onChangeCallback();
  193. return this;
  194. },
  195. dot: function ( v ) {
  196. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  197. },
  198. lengthSq: function () {
  199. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  200. },
  201. length: function () {
  202. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  203. },
  204. normalize: function () {
  205. var l = this.length();
  206. if ( l === 0 ) {
  207. this._x = 0;
  208. this._y = 0;
  209. this._z = 0;
  210. this._w = 1;
  211. } else {
  212. l = 1 / l;
  213. this._x = this._x * l;
  214. this._y = this._y * l;
  215. this._z = this._z * l;
  216. this._w = this._w * l;
  217. }
  218. this.onChangeCallback();
  219. return this;
  220. },
  221. multiply: function ( q, p ) {
  222. if ( p !== undefined ) {
  223. console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  224. return this.multiplyQuaternions( q, p );
  225. }
  226. return this.multiplyQuaternions( this, q );
  227. },
  228. multiplyQuaternions: function ( a, b ) {
  229. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  230. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  231. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  232. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  233. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  234. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  235. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  236. this.onChangeCallback();
  237. return this;
  238. },
  239. multiplyVector3: function ( vector ) {
  240. console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  241. return vector.applyQuaternion( this );
  242. },
  243. slerp: function ( qb, t ) {
  244. if ( t === 0 ) return this;
  245. if ( t === 1 ) return this.copy( qb );
  246. var x = this._x, y = this._y, z = this._z, w = this._w;
  247. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  248. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  249. if ( cosHalfTheta < 0 ) {
  250. this._w = - qb._w;
  251. this._x = - qb._x;
  252. this._y = - qb._y;
  253. this._z = - qb._z;
  254. cosHalfTheta = - cosHalfTheta;
  255. } else {
  256. this.copy( qb );
  257. }
  258. if ( cosHalfTheta >= 1.0 ) {
  259. this._w = w;
  260. this._x = x;
  261. this._y = y;
  262. this._z = z;
  263. return this;
  264. }
  265. var halfTheta = Math.acos( cosHalfTheta );
  266. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  267. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  268. this._w = 0.5 * ( w + this._w );
  269. this._x = 0.5 * ( x + this._x );
  270. this._y = 0.5 * ( y + this._y );
  271. this._z = 0.5 * ( z + this._z );
  272. return this;
  273. }
  274. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  275. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  276. this._w = ( w * ratioA + this._w * ratioB );
  277. this._x = ( x * ratioA + this._x * ratioB );
  278. this._y = ( y * ratioA + this._y * ratioB );
  279. this._z = ( z * ratioA + this._z * ratioB );
  280. this.onChangeCallback();
  281. return this;
  282. },
  283. equals: function ( quaternion ) {
  284. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  285. },
  286. fromArray: function ( array, offset ) {
  287. if ( offset === undefined ) offset = 0;
  288. this._x = array[ offset ];
  289. this._y = array[ offset + 1 ];
  290. this._z = array[ offset + 2 ];
  291. this._w = array[ offset + 3 ];
  292. this.onChangeCallback();
  293. return this;
  294. },
  295. toArray: function ( array, offset ) {
  296. if ( array === undefined ) array = [];
  297. if ( offset === undefined ) offset = 0;
  298. array[ offset ] = this._x;
  299. array[ offset + 1 ] = this._y;
  300. array[ offset + 2 ] = this._z;
  301. array[ offset + 3 ] = this._w;
  302. return array;
  303. },
  304. onChange: function ( callback ) {
  305. this.onChangeCallback = callback;
  306. return this;
  307. },
  308. onChangeCallback: function () {}
  309. };
  310. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  311. return qm.copy( qa ).slerp( qb, t );
  312. };