Line3.js 2.3 KB

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