Line3.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Vector3 } from './Vector3';
  2. import { _Math } from './Math';
  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 ( optionalTarget ) {
  25. var result = optionalTarget || new Vector3();
  26. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  27. },
  28. delta: function ( optionalTarget ) {
  29. var result = optionalTarget || new Vector3();
  30. return result.subVectors( this.end, this.start );
  31. },
  32. distanceSq: function () {
  33. return this.start.distanceToSquared( this.end );
  34. },
  35. distance: function () {
  36. return this.start.distanceTo( this.end );
  37. },
  38. at: function ( t, optionalTarget ) {
  39. var result = optionalTarget || new Vector3();
  40. return this.delta( result ).multiplyScalar( t ).add( this.start );
  41. },
  42. closestPointToPointParameter: function () {
  43. var startP = new Vector3();
  44. var startEnd = new Vector3();
  45. return function closestPointToPointParameter( point, clampToLine ) {
  46. startP.subVectors( point, this.start );
  47. startEnd.subVectors( this.end, this.start );
  48. var startEnd2 = startEnd.dot( startEnd );
  49. var startEnd_startP = startEnd.dot( startP );
  50. var t = startEnd_startP / startEnd2;
  51. if ( clampToLine ) {
  52. t = _Math.clamp( t, 0, 1 );
  53. }
  54. return t;
  55. };
  56. }(),
  57. closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
  58. var t = this.closestPointToPointParameter( point, clampToLine );
  59. var result = optionalTarget || new Vector3();
  60. return this.delta( result ).multiplyScalar( t ).add( this.start );
  61. },
  62. applyMatrix4: function ( matrix ) {
  63. this.start.applyMatrix4( matrix );
  64. this.end.applyMatrix4( matrix );
  65. return this;
  66. },
  67. equals: function ( line ) {
  68. return line.start.equals( this.start ) && line.end.equals( this.end );
  69. }
  70. } );
  71. export { Line3 };