Line3.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Vector3 } from './Vector3.js';
  2. import * as MathUtils from './MathUtils.js';
  3. const _startP = /*@__PURE__*/ new Vector3();
  4. const _startEnd = /*@__PURE__*/ new Vector3();
  5. class Line3 {
  6. constructor( start = new Vector3(), end = new Vector3() ) {
  7. this.start = start;
  8. this.end = end;
  9. }
  10. set( start, end ) {
  11. this.start.copy( start );
  12. this.end.copy( end );
  13. return this;
  14. }
  15. copy( line ) {
  16. this.start.copy( line.start );
  17. this.end.copy( line.end );
  18. return this;
  19. }
  20. getCenter( target ) {
  21. return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  22. }
  23. delta( target ) {
  24. return target.subVectors( this.end, this.start );
  25. }
  26. distanceSq() {
  27. return this.start.distanceToSquared( this.end );
  28. }
  29. distance() {
  30. return this.start.distanceTo( this.end );
  31. }
  32. at( t, target ) {
  33. return this.delta( target ).multiplyScalar( t ).add( this.start );
  34. }
  35. closestPointToPointParameter( point, clampToLine ) {
  36. _startP.subVectors( point, this.start );
  37. _startEnd.subVectors( this.end, this.start );
  38. const startEnd2 = _startEnd.dot( _startEnd );
  39. const startEnd_startP = _startEnd.dot( _startP );
  40. let t = startEnd_startP / startEnd2;
  41. if ( clampToLine ) {
  42. t = MathUtils.clamp( t, 0, 1 );
  43. }
  44. return t;
  45. }
  46. closestPointToPoint( point, clampToLine, target ) {
  47. const t = this.closestPointToPointParameter( point, clampToLine );
  48. return this.delta( target ).multiplyScalar( t ).add( this.start );
  49. }
  50. applyMatrix4( matrix ) {
  51. this.start.applyMatrix4( matrix );
  52. this.end.applyMatrix4( matrix );
  53. return this;
  54. }
  55. equals( line ) {
  56. return line.start.equals( this.start ) && line.end.equals( this.end );
  57. }
  58. clone() {
  59. return new this.constructor().copy( this );
  60. }
  61. }
  62. export { Line3 };