Vector4.js 10 KB

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