Line3.js 2.5 KB

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