Line3.js 2.5 KB

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