Vector3.js 14 KB

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