Vector4.js 11 KB

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