Line3.js 2.5 KB

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