Vector4.js 10 KB

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