Vector4.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author philogb / http://blog.thejit.org/
  4. * @author mikael emtinger / http://gomo.se/
  5. * @author egraether / http://egraether.com/
  6. * @author WestLangley / http://github.com/WestLangley
  7. */
  8. function Vector4( x = 0, y = 0, z = 0, w = 1 ) {
  9. this.x = x;
  10. this.y = y;
  11. this.z = z;
  12. this.w = w;
  13. }
  14. Object.defineProperties( Vector4.prototype, {
  15. "width": {
  16. get: function () {
  17. return this.z;
  18. },
  19. set: function ( value ) {
  20. this.z = value;
  21. }
  22. },
  23. "height": {
  24. get: function () {
  25. return this.w;
  26. },
  27. set: function ( value ) {
  28. this.w = value;
  29. }
  30. }
  31. } );
  32. Object.assign( Vector4.prototype, {
  33. isVector4: true,
  34. set: function ( x, y, z, w ) {
  35. this.x = x;
  36. this.y = y;
  37. this.z = z;
  38. this.w = w;
  39. return this;
  40. },
  41. setScalar: function ( scalar ) {
  42. this.x = scalar;
  43. this.y = scalar;
  44. this.z = scalar;
  45. this.w = scalar;
  46. return this;
  47. },
  48. setX: function ( x ) {
  49. this.x = x;
  50. return this;
  51. },
  52. setY: function ( y ) {
  53. this.y = y;
  54. return this;
  55. },
  56. setZ: function ( z ) {
  57. this.z = z;
  58. return this;
  59. },
  60. setW: function ( w ) {
  61. this.w = w;
  62. return this;
  63. },
  64. setComponent: function ( index, value ) {
  65. switch ( index ) {
  66. case 0: this.x = value; break;
  67. case 1: this.y = value; break;
  68. case 2: this.z = value; break;
  69. case 3: this.w = value; break;
  70. default: throw new Error( 'index is out of range: ' + index );
  71. }
  72. return this;
  73. },
  74. getComponent: function ( index ) {
  75. switch ( index ) {
  76. case 0: return this.x;
  77. case 1: return this.y;
  78. case 2: return this.z;
  79. case 3: return this.w;
  80. default: throw new Error( 'index is out of range: ' + index );
  81. }
  82. },
  83. clone: function () {
  84. return new this.constructor( this.x, this.y, this.z, this.w );
  85. },
  86. copy: function ( v ) {
  87. this.x = v.x;
  88. this.y = v.y;
  89. this.z = v.z;
  90. this.w = ( v.w !== undefined ) ? v.w : 1;
  91. return this;
  92. },
  93. add: function ( v, w ) {
  94. if ( w !== undefined ) {
  95. console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  96. return this.addVectors( v, w );
  97. }
  98. this.x += v.x;
  99. this.y += v.y;
  100. this.z += v.z;
  101. this.w += v.w;
  102. return this;
  103. },
  104. addScalar: function ( s ) {
  105. this.x += s;
  106. this.y += s;
  107. this.z += s;
  108. this.w += s;
  109. return this;
  110. },
  111. addVectors: function ( a, b ) {
  112. this.x = a.x + b.x;
  113. this.y = a.y + b.y;
  114. this.z = a.z + b.z;
  115. this.w = a.w + b.w;
  116. return this;
  117. },
  118. addScaledVector: function ( v, s ) {
  119. this.x += v.x * s;
  120. this.y += v.y * s;
  121. this.z += v.z * s;
  122. this.w += v.w * s;
  123. return this;
  124. },
  125. sub: function ( v, w ) {
  126. if ( w !== undefined ) {
  127. console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  128. return this.subVectors( v, w );
  129. }
  130. this.x -= v.x;
  131. this.y -= v.y;
  132. this.z -= v.z;
  133. this.w -= v.w;
  134. return this;
  135. },
  136. subScalar: function ( s ) {
  137. this.x -= s;
  138. this.y -= s;
  139. this.z -= s;
  140. this.w -= s;
  141. return this;
  142. },
  143. subVectors: function ( a, b ) {
  144. this.x = a.x - b.x;
  145. this.y = a.y - b.y;
  146. this.z = a.z - b.z;
  147. this.w = a.w - b.w;
  148. return this;
  149. },
  150. multiplyScalar: function ( scalar ) {
  151. this.x *= scalar;
  152. this.y *= scalar;
  153. this.z *= scalar;
  154. this.w *= scalar;
  155. return this;
  156. },
  157. applyMatrix4: function ( m ) {
  158. const x = this.x, y = this.y, z = this.z, w = this.w;
  159. const e = m.elements;
  160. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  161. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  162. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  163. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  164. return this;
  165. },
  166. divideScalar: function ( scalar ) {
  167. return this.multiplyScalar( 1 / scalar );
  168. },
  169. setAxisAngleFromQuaternion: function ( q ) {
  170. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  171. // q is assumed to be normalized
  172. this.w = 2 * Math.acos( q.w );
  173. const s = Math.sqrt( 1 - q.w * q.w );
  174. if ( s < 0.0001 ) {
  175. this.x = 1;
  176. this.y = 0;
  177. this.z = 0;
  178. } else {
  179. this.x = q.x / s;
  180. this.y = q.y / s;
  181. this.z = q.z / s;
  182. }
  183. return this;
  184. },
  185. setAxisAngleFromRotationMatrix: function ( m ) {
  186. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  187. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  188. let angle, x, y, z; // variables for result
  189. const epsilon = 0.01, // margin to allow for rounding errors
  190. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  191. te = m.elements,
  192. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  193. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  194. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  195. if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
  196. ( Math.abs( m13 - m31 ) < epsilon ) &&
  197. ( Math.abs( m23 - m32 ) < epsilon ) ) {
  198. // singularity found
  199. // first check for identity matrix which must have +1 for all terms
  200. // in leading diagonal and zero in other terms
  201. if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
  202. ( Math.abs( m13 + m31 ) < epsilon2 ) &&
  203. ( Math.abs( m23 + m32 ) < epsilon2 ) &&
  204. ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  205. // this singularity is identity matrix so angle = 0
  206. this.set( 1, 0, 0, 0 );
  207. return this; // zero angle, arbitrary axis
  208. }
  209. // otherwise this singularity is angle = 180
  210. angle = Math.PI;
  211. const xx = ( m11 + 1 ) / 2;
  212. const yy = ( m22 + 1 ) / 2;
  213. const zz = ( m33 + 1 ) / 2;
  214. const xy = ( m12 + m21 ) / 4;
  215. const xz = ( m13 + m31 ) / 4;
  216. const yz = ( m23 + m32 ) / 4;
  217. if ( ( xx > yy ) && ( xx > zz ) ) {
  218. // m11 is the largest diagonal term
  219. if ( xx < epsilon ) {
  220. x = 0;
  221. y = 0.707106781;
  222. z = 0.707106781;
  223. } else {
  224. x = Math.sqrt( xx );
  225. y = xy / x;
  226. z = xz / x;
  227. }
  228. } else if ( yy > zz ) {
  229. // m22 is the largest diagonal term
  230. if ( yy < epsilon ) {
  231. x = 0.707106781;
  232. y = 0;
  233. z = 0.707106781;
  234. } else {
  235. y = Math.sqrt( yy );
  236. x = xy / y;
  237. z = yz / y;
  238. }
  239. } else {
  240. // m33 is the largest diagonal term so base result on this
  241. if ( zz < epsilon ) {
  242. x = 0.707106781;
  243. y = 0.707106781;
  244. z = 0;
  245. } else {
  246. z = Math.sqrt( zz );
  247. x = xz / z;
  248. y = yz / z;
  249. }
  250. }
  251. this.set( x, y, z, angle );
  252. return this; // return 180 deg rotation
  253. }
  254. // as we have reached here there are no singularities so we can handle normally
  255. let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
  256. ( m13 - m31 ) * ( m13 - m31 ) +
  257. ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  258. if ( Math.abs( s ) < 0.001 ) s = 1;
  259. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  260. // caught by singularity test above, but I've left it in just in case
  261. this.x = ( m32 - m23 ) / s;
  262. this.y = ( m13 - m31 ) / s;
  263. this.z = ( m21 - m12 ) / s;
  264. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  265. return this;
  266. },
  267. min: function ( v ) {
  268. this.x = Math.min( this.x, v.x );
  269. this.y = Math.min( this.y, v.y );
  270. this.z = Math.min( this.z, v.z );
  271. this.w = Math.min( this.w, v.w );
  272. return this;
  273. },
  274. max: function ( v ) {
  275. this.x = Math.max( this.x, v.x );
  276. this.y = Math.max( this.y, v.y );
  277. this.z = Math.max( this.z, v.z );
  278. this.w = Math.max( this.w, v.w );
  279. return this;
  280. },
  281. clamp: function ( min, max ) {
  282. // assumes min < max, componentwise
  283. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  284. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  285. this.z = Math.max( min.z, Math.min( max.z, this.z ) );
  286. this.w = Math.max( min.w, Math.min( max.w, this.w ) );
  287. return this;
  288. },
  289. clampScalar: function ( minVal, maxVal ) {
  290. this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
  291. this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
  292. this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
  293. this.w = Math.max( minVal, Math.min( maxVal, this.w ) );
  294. return this;
  295. },
  296. clampLength: function ( min, max ) {
  297. const length = this.length();
  298. return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
  299. },
  300. floor: function () {
  301. this.x = Math.floor( this.x );
  302. this.y = Math.floor( this.y );
  303. this.z = Math.floor( this.z );
  304. this.w = Math.floor( this.w );
  305. return this;
  306. },
  307. ceil: function () {
  308. this.x = Math.ceil( this.x );
  309. this.y = Math.ceil( this.y );
  310. this.z = Math.ceil( this.z );
  311. this.w = Math.ceil( this.w );
  312. return this;
  313. },
  314. round: function () {
  315. this.x = Math.round( this.x );
  316. this.y = Math.round( this.y );
  317. this.z = Math.round( this.z );
  318. this.w = Math.round( this.w );
  319. return this;
  320. },
  321. roundToZero: function () {
  322. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  323. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  324. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  325. this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
  326. return this;
  327. },
  328. negate: function () {
  329. this.x = - this.x;
  330. this.y = - this.y;
  331. this.z = - this.z;
  332. this.w = - this.w;
  333. return this;
  334. },
  335. dot: function ( v ) {
  336. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  337. },
  338. lengthSq: function () {
  339. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  340. },
  341. length: function () {
  342. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  343. },
  344. manhattanLength: function () {
  345. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  346. },
  347. normalize: function () {
  348. return this.divideScalar( this.length() || 1 );
  349. },
  350. setLength: function ( length ) {
  351. return this.normalize().multiplyScalar( length );
  352. },
  353. lerp: function ( v, alpha ) {
  354. this.x += ( v.x - this.x ) * alpha;
  355. this.y += ( v.y - this.y ) * alpha;
  356. this.z += ( v.z - this.z ) * alpha;
  357. this.w += ( v.w - this.w ) * alpha;
  358. return this;
  359. },
  360. lerpVectors: function ( v1, v2, alpha ) {
  361. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  362. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  363. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  364. this.w = v1.w + ( v2.w - v1.w ) * alpha;
  365. return this;
  366. },
  367. equals: function ( v ) {
  368. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  369. },
  370. fromArray: function ( array, offset ) {
  371. if ( offset === undefined ) offset = 0;
  372. this.x = array[ offset ];
  373. this.y = array[ offset + 1 ];
  374. this.z = array[ offset + 2 ];
  375. this.w = array[ offset + 3 ];
  376. return this;
  377. },
  378. toArray: function ( array, offset ) {
  379. if ( array === undefined ) array = [];
  380. if ( offset === undefined ) offset = 0;
  381. array[ offset ] = this.x;
  382. array[ offset + 1 ] = this.y;
  383. array[ offset + 2 ] = this.z;
  384. array[ offset + 3 ] = this.w;
  385. return array;
  386. },
  387. fromBufferAttribute: function ( attribute, index, offset ) {
  388. if ( offset !== undefined ) {
  389. console.warn( 'THREE.Vector4: offset has been removed from .fromBufferAttribute().' );
  390. }
  391. this.x = attribute.getX( index );
  392. this.y = attribute.getY( index );
  393. this.z = attribute.getZ( index );
  394. this.w = attribute.getW( index );
  395. return this;
  396. },
  397. random: function () {
  398. this.x = Math.random();
  399. this.y = Math.random();
  400. this.z = Math.random();
  401. this.w = Math.random();
  402. return this;
  403. }
  404. } );
  405. export { Vector4 };