Vector3.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author *kile / http://kile.stravaganza.org/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author egraether / http://egraether.com/
  7. * @author WestLangley / http://github.com/WestLangley
  8. */
  9. THREE.Vector3 = function ( x, y, z ) {
  10. this.x = x || 0;
  11. this.y = y || 0;
  12. this.z = z || 0;
  13. };
  14. THREE.extend( THREE.Vector3.prototype, {
  15. set: function ( x, y, z ) {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. return this;
  20. },
  21. setX: function ( x ) {
  22. this.x = x;
  23. return this;
  24. },
  25. setY: function ( y ) {
  26. this.y = y;
  27. return this;
  28. },
  29. setZ: function ( z ) {
  30. this.z = z;
  31. return this;
  32. },
  33. setComponent: function ( index, value ) {
  34. switch ( index ) {
  35. case 0: this.x = value; break;
  36. case 1: this.y = value; break;
  37. case 2: this.z = value; break;
  38. default: throw new Error( "index is out of range: " + index );
  39. }
  40. },
  41. getComponent: function ( index ) {
  42. switch ( index ) {
  43. case 0: return this.x;
  44. case 1: return this.y;
  45. case 2: return this.z;
  46. default: throw new Error( "index is out of range: " + index );
  47. }
  48. },
  49. copy: function ( v ) {
  50. this.x = v.x;
  51. this.y = v.y;
  52. this.z = v.z;
  53. return this;
  54. },
  55. add: function ( v, w ) {
  56. if ( w !== undefined ) {
  57. console.warn( 'DEPRECATED: Vector3\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  58. return this.addVectors( v, w );
  59. }
  60. this.x += v.x;
  61. this.y += v.y;
  62. this.z += v.z;
  63. return this;
  64. },
  65. addScalar: function ( s ) {
  66. this.x += s;
  67. this.y += s;
  68. this.z += s;
  69. return this;
  70. },
  71. addVectors: function ( a, b ) {
  72. this.x = a.x + b.x;
  73. this.y = a.y + b.y;
  74. this.z = a.z + b.z;
  75. return this;
  76. },
  77. sub: function ( v, w ) {
  78. if ( w !== undefined ) {
  79. console.warn( 'DEPRECATED: Vector3\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  80. return this.subVectors( v, w );
  81. }
  82. this.x -= v.x;
  83. this.y -= v.y;
  84. this.z -= v.z;
  85. return this;
  86. },
  87. subVectors: function ( a, b ) {
  88. this.x = a.x - b.x;
  89. this.y = a.y - b.y;
  90. this.z = a.z - b.z;
  91. return this;
  92. },
  93. multiply: function ( v, w ) {
  94. if ( w !== undefined ) {
  95. console.warn( 'DEPRECATED: Vector3\'s .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
  96. return this.multiplyVectors( v, w );
  97. }
  98. this.x *= v.x;
  99. this.y *= v.y;
  100. this.z *= v.z;
  101. return this;
  102. },
  103. multiplyScalar: function ( s ) {
  104. this.x *= s;
  105. this.y *= s;
  106. this.z *= s;
  107. return this;
  108. },
  109. multiplyVectors: function ( a, b ) {
  110. this.x = a.x * b.x;
  111. this.y = a.y * b.y;
  112. this.z = a.z * b.z;
  113. return this;
  114. },
  115. applyMatrix3: function ( m ) {
  116. var x = this.x;
  117. var y = this.y;
  118. var z = this.z;
  119. var e = m.elements;
  120. this.x = e[0] * x + e[3] * y + e[6] * z;
  121. this.y = e[1] * x + e[4] * y + e[7] * z;
  122. this.z = e[2] * x + e[5] * y + e[8] * z;
  123. return this;
  124. },
  125. applyMatrix4: function ( m ) {
  126. // input: THREE.Matrix4 affine matrix
  127. var x = this.x, y = this.y, z = this.z;
  128. var e = m.elements;
  129. this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
  130. this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
  131. this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
  132. return this;
  133. },
  134. applyProjection: function ( m ) {
  135. // input: THREE.Matrix4 projection matrix
  136. var x = this.x, y = this.y, z = this.z;
  137. var e = m.elements;
  138. var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide
  139. this.x = ( e[0] * x + e[4] * y + e[8] * z + e[12] ) * d;
  140. this.y = ( e[1] * x + e[5] * y + e[9] * z + e[13] ) * d;
  141. this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;
  142. return this;
  143. },
  144. applyQuaternion: function ( q ) {
  145. var x = this.x;
  146. var y = this.y;
  147. var z = this.z;
  148. var qx = q.x;
  149. var qy = q.y;
  150. var qz = q.z;
  151. var qw = q.w;
  152. // calculate quat * vector
  153. var ix = qw * x + qy * z - qz * y;
  154. var iy = qw * y + qz * x - qx * z;
  155. var iz = qw * z + qx * y - qy * x;
  156. var iw = -qx * x - qy * y - qz * z;
  157. // calculate result * inverse quat
  158. this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  159. this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  160. this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  161. return this;
  162. },
  163. applyEuler: function () {
  164. var q1 = new THREE.Quaternion();
  165. return function ( v, eulerOrder ) {
  166. var quaternion = q1.setFromEuler( v, eulerOrder );
  167. this.applyQuaternion( quaternion );
  168. return this;
  169. };
  170. }(),
  171. applyAxisAngle: function () {
  172. var q1 = new THREE.Quaternion();
  173. return function ( axis, angle ) {
  174. var quaternion = q1.setFromAxisAngle( axis, angle );
  175. this.applyQuaternion( quaternion );
  176. return this;
  177. };
  178. }(),
  179. transformDirection: function ( m ) {
  180. // input: THREE.Matrix4 affine matrix
  181. // vector interpreted as a direction
  182. var x = this.x, y = this.y, z = this.z;
  183. var e = m.elements;
  184. this.x = e[0] * x + e[4] * y + e[8] * z;
  185. this.y = e[1] * x + e[5] * y + e[9] * z;
  186. this.z = e[2] * x + e[6] * y + e[10] * z;
  187. this.normalize();
  188. return this;
  189. },
  190. divide: function ( v ) {
  191. this.x /= v.x;
  192. this.y /= v.y;
  193. this.z /= v.z;
  194. return this;
  195. },
  196. divideScalar: function ( s ) {
  197. if ( s !== 0 ) {
  198. this.x /= s;
  199. this.y /= s;
  200. this.z /= s;
  201. } else {
  202. this.x = 0;
  203. this.y = 0;
  204. this.z = 0;
  205. }
  206. return this;
  207. },
  208. min: function ( v ) {
  209. if ( this.x > v.x ) {
  210. this.x = v.x;
  211. }
  212. if ( this.y > v.y ) {
  213. this.y = v.y;
  214. }
  215. if ( this.z > v.z ) {
  216. this.z = v.z;
  217. }
  218. return this;
  219. },
  220. max: function ( v ) {
  221. if ( this.x < v.x ) {
  222. this.x = v.x;
  223. }
  224. if ( this.y < v.y ) {
  225. this.y = v.y;
  226. }
  227. if ( this.z < v.z ) {
  228. this.z = v.z;
  229. }
  230. return this;
  231. },
  232. clamp: function ( min, max ) {
  233. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  234. if ( this.x < min.x ) {
  235. this.x = min.x;
  236. } else if ( this.x > max.x ) {
  237. this.x = max.x;
  238. }
  239. if ( this.y < min.y ) {
  240. this.y = min.y;
  241. } else if ( this.y > max.y ) {
  242. this.y = max.y;
  243. }
  244. if ( this.z < min.z ) {
  245. this.z = min.z;
  246. } else if ( this.z > max.z ) {
  247. this.z = max.z;
  248. }
  249. return this;
  250. },
  251. negate: function () {
  252. return this.multiplyScalar( - 1 );
  253. },
  254. dot: function ( v ) {
  255. return this.x * v.x + this.y * v.y + this.z * v.z;
  256. },
  257. lengthSq: function () {
  258. return this.x * this.x + this.y * this.y + this.z * this.z;
  259. },
  260. length: function () {
  261. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  262. },
  263. lengthManhattan: function () {
  264. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  265. },
  266. normalize: function () {
  267. return this.divideScalar( this.length() );
  268. },
  269. setLength: function ( l ) {
  270. var oldLength = this.length();
  271. if ( oldLength !== 0 && l !== oldLength ) {
  272. this.multiplyScalar( l / oldLength );
  273. }
  274. return this;
  275. },
  276. lerp: function ( v, alpha ) {
  277. this.x += ( v.x - this.x ) * alpha;
  278. this.y += ( v.y - this.y ) * alpha;
  279. this.z += ( v.z - this.z ) * alpha;
  280. return this;
  281. },
  282. cross: function ( v, w ) {
  283. if ( w !== undefined ) {
  284. console.warn( 'DEPRECATED: Vector3\'s .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  285. return this.crossVectors( v, w );
  286. }
  287. var x = this.x, y = this.y, z = this.z;
  288. this.x = y * v.z - z * v.y;
  289. this.y = z * v.x - x * v.z;
  290. this.z = x * v.y - y * v.x;
  291. return this;
  292. },
  293. crossVectors: function ( a, b ) {
  294. this.x = a.y * b.z - a.z * b.y;
  295. this.y = a.z * b.x - a.x * b.z;
  296. this.z = a.x * b.y - a.y * b.x;
  297. return this;
  298. },
  299. projectOnVector: function () {
  300. var v1 = new THREE.Vector3();
  301. return function( vector ) {
  302. v1.copy( vector ).normalize();
  303. var d = this.dot( v1 );
  304. return this.copy( v1 ).multiplyScalar( d );
  305. };
  306. }(),
  307. projectOnPlane: function () {
  308. var v1 = new THREE.Vector3();
  309. return function( planeNormal ) {
  310. v1.copy( this ).projectOnVector( planeNormal );
  311. return this.sub( v1 );
  312. }
  313. }(),
  314. reflect: function () {
  315. var v1 = new THREE.Vector3();
  316. return function ( vector ) {
  317. v1.copy( this ).projectOnVector( vector ).multiplyScalar( 2 );
  318. return this.subVectors( v1, this );
  319. }
  320. }(),
  321. angleTo: function ( v ) {
  322. var theta = this.dot( v ) / ( this.length() * v.length() );
  323. // clamp, to handle numerical problems
  324. return Math.acos( THREE.Math.clamp ( theta, -1, 1 ) );
  325. },
  326. distanceTo: function ( v ) {
  327. return Math.sqrt( this.distanceToSquared( v ) );
  328. },
  329. distanceToSquared: function ( v ) {
  330. var dx = this.x - v.x;
  331. var dy = this.y - v.y;
  332. var dz = this.z - v.z;
  333. return dx * dx + dy * dy + dz * dz;
  334. },
  335. getPositionFromMatrix: function ( m ) {
  336. this.x = m.elements[12];
  337. this.y = m.elements[13];
  338. this.z = m.elements[14];
  339. return this;
  340. },
  341. setEulerFromRotationMatrix: function ( m, order ) {
  342. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  343. // clamp, to handle numerical problems
  344. function clamp( x ) {
  345. return Math.min( Math.max( x, -1 ), 1 );
  346. }
  347. var te = m.elements;
  348. var m11 = te[0], m12 = te[4], m13 = te[8];
  349. var m21 = te[1], m22 = te[5], m23 = te[9];
  350. var m31 = te[2], m32 = te[6], m33 = te[10];
  351. if ( order === undefined || order === 'XYZ' ) {
  352. this.y = Math.asin( clamp( m13 ) );
  353. if ( Math.abs( m13 ) < 0.99999 ) {
  354. this.x = Math.atan2( - m23, m33 );
  355. this.z = Math.atan2( - m12, m11 );
  356. } else {
  357. this.x = Math.atan2( m32, m22 );
  358. this.z = 0;
  359. }
  360. } else if ( order === 'YXZ' ) {
  361. this.x = Math.asin( - clamp( m23 ) );
  362. if ( Math.abs( m23 ) < 0.99999 ) {
  363. this.y = Math.atan2( m13, m33 );
  364. this.z = Math.atan2( m21, m22 );
  365. } else {
  366. this.y = Math.atan2( - m31, m11 );
  367. this.z = 0;
  368. }
  369. } else if ( order === 'ZXY' ) {
  370. this.x = Math.asin( clamp( m32 ) );
  371. if ( Math.abs( m32 ) < 0.99999 ) {
  372. this.y = Math.atan2( - m31, m33 );
  373. this.z = Math.atan2( - m12, m22 );
  374. } else {
  375. this.y = 0;
  376. this.z = Math.atan2( m21, m11 );
  377. }
  378. } else if ( order === 'ZYX' ) {
  379. this.y = Math.asin( - clamp( m31 ) );
  380. if ( Math.abs( m31 ) < 0.99999 ) {
  381. this.x = Math.atan2( m32, m33 );
  382. this.z = Math.atan2( m21, m11 );
  383. } else {
  384. this.x = 0;
  385. this.z = Math.atan2( - m12, m22 );
  386. }
  387. } else if ( order === 'YZX' ) {
  388. this.z = Math.asin( clamp( m21 ) );
  389. if ( Math.abs( m21 ) < 0.99999 ) {
  390. this.x = Math.atan2( - m23, m22 );
  391. this.y = Math.atan2( - m31, m11 );
  392. } else {
  393. this.x = 0;
  394. this.y = Math.atan2( m13, m33 );
  395. }
  396. } else if ( order === 'XZY' ) {
  397. this.z = Math.asin( - clamp( m12 ) );
  398. if ( Math.abs( m12 ) < 0.99999 ) {
  399. this.x = Math.atan2( m32, m22 );
  400. this.y = Math.atan2( m13, m11 );
  401. } else {
  402. this.x = Math.atan2( - m23, m33 );
  403. this.y = 0;
  404. }
  405. }
  406. return this;
  407. },
  408. setEulerFromQuaternion: function ( q, order ) {
  409. // q is assumed to be normalized
  410. // clamp, to handle numerical problems
  411. function clamp( x ) {
  412. return Math.min( Math.max( x, -1 ), 1 );
  413. }
  414. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  415. var sqx = q.x * q.x;
  416. var sqy = q.y * q.y;
  417. var sqz = q.z * q.z;
  418. var sqw = q.w * q.w;
  419. if ( order === undefined || order === 'XYZ' ) {
  420. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  421. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  422. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  423. } else if ( order === 'YXZ' ) {
  424. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  425. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  426. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  427. } else if ( order === 'ZXY' ) {
  428. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  429. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  430. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  431. } else if ( order === 'ZYX' ) {
  432. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  433. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  434. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  435. } else if ( order === 'YZX' ) {
  436. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  437. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  438. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  439. } else if ( order === 'XZY' ) {
  440. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  441. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  442. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  443. }
  444. return this;
  445. },
  446. getScaleFromMatrix: function ( m ) {
  447. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  448. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  449. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  450. this.x = sx;
  451. this.y = sy;
  452. this.z = sz;
  453. return this;
  454. },
  455. equals: function ( v ) {
  456. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  457. },
  458. toArray: function () {
  459. return [ this.x, this.y, this.z ];
  460. },
  461. clone: function () {
  462. return new THREE.Vector3( this.x, this.y, this.z );
  463. }
  464. } );