Line3.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if ( target === undefined ) {
  22. console.warn( 'THREE.Line3: .getCenter() target is now required' );
  23. target = new Vector3();
  24. }
  25. return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  26. }
  27. delta( target ) {
  28. if ( target === undefined ) {
  29. console.warn( 'THREE.Line3: .delta() target is now required' );
  30. target = new Vector3();
  31. }
  32. return target.subVectors( this.end, this.start );
  33. }
  34. distanceSq() {
  35. return this.start.distanceToSquared( this.end );
  36. }
  37. distance() {
  38. return this.start.distanceTo( this.end );
  39. }
  40. at( t, target ) {
  41. if ( target === undefined ) {
  42. console.warn( 'THREE.Line3: .at() target is now required' );
  43. target = new Vector3();
  44. }
  45. return this.delta( target ).multiplyScalar( t ).add( this.start );
  46. }
  47. closestPointToPointParameter( point, clampToLine ) {
  48. _startP.subVectors( point, this.start );
  49. _startEnd.subVectors( this.end, this.start );
  50. const startEnd2 = _startEnd.dot( _startEnd );
  51. const startEnd_startP = _startEnd.dot( _startP );
  52. let t = startEnd_startP / startEnd2;
  53. if ( clampToLine ) {
  54. t = MathUtils.clamp( t, 0, 1 );
  55. }
  56. return t;
  57. }
  58. closestPointToPoint( point, clampToLine, target ) {
  59. const t = this.closestPointToPointParameter( point, clampToLine );
  60. if ( target === undefined ) {
  61. console.warn( 'THREE.Line3: .closestPointToPoint() target is now required' );
  62. target = new Vector3();
  63. }
  64. return this.delta( target ).multiplyScalar( t ).add( this.start );
  65. }
  66. applyMatrix4( matrix ) {
  67. this.start.applyMatrix4( matrix );
  68. this.end.applyMatrix4( matrix );
  69. return this;
  70. }
  71. equals( line ) {
  72. return line.start.equals( this.start ) && line.end.equals( this.end );
  73. }
  74. clone() {
  75. return new this.constructor().copy( this );
  76. }
  77. }
  78. export { Line3 };
粤ICP备19079148号