Line3.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author bhouston / http://clara.io
  3. */
  4. THREE.Line3 = function ( start, end ) {
  5. this.start = ( start !== undefined ) ? start : new THREE.Vector3();
  6. this.end = ( end !== undefined ) ? end : new THREE.Vector3();
  7. };
  8. THREE.Line3.prototype = {
  9. constructor: THREE.Line3,
  10. set: function ( start, end ) {
  11. this.start.copy( start );
  12. this.end.copy( end );
  13. return this;
  14. },
  15. clone: function () {
  16. return new this.constructor().copy( this );
  17. },
  18. copy: function ( line ) {
  19. this.start.copy( line.start );
  20. this.end.copy( line.end );
  21. return this;
  22. },
  23. center: function ( optionalTarget ) {
  24. var result = optionalTarget || new THREE.Vector3();
  25. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  26. },
  27. delta: function ( optionalTarget ) {
  28. var result = optionalTarget || new THREE.Vector3();
  29. return result.subVectors( this.end, this.start );
  30. },
  31. distanceSq: function () {
  32. return this.start.distanceToSquared( this.end );
  33. },
  34. distance: function () {
  35. return this.start.distanceTo( this.end );
  36. },
  37. at: function ( t, optionalTarget ) {
  38. var result = optionalTarget || new THREE.Vector3();
  39. return this.delta( result ).multiplyScalar( t ).add( this.start );
  40. },
  41. closestPointToPointParameter: function () {
  42. var startP = new THREE.Vector3();
  43. var startEnd = new THREE.Vector3();
  44. return function ( point, clampToLine ) {
  45. startP.subVectors( point, this.start );
  46. startEnd.subVectors( this.end, this.start );
  47. var startEnd2 = startEnd.dot( startEnd );
  48. var startEnd_startP = startEnd.dot( startP );
  49. var t = startEnd_startP / startEnd2;
  50. if ( clampToLine ) {
  51. t = THREE.Math.clamp( t, 0, 1 );
  52. }
  53. return t;
  54. };
  55. }(),
  56. closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
  57. var t = this.closestPointToPointParameter( point, clampToLine );
  58. var result = optionalTarget || new THREE.Vector3();
  59. return this.delta( result ).multiplyScalar( t ).add( this.start );
  60. },
  61. applyMatrix4: function ( matrix ) {
  62. this.start.applyMatrix4( matrix );
  63. this.end.applyMatrix4( matrix );
  64. return this;
  65. },
  66. equals: function ( line ) {
  67. return line.start.equals( this.start ) && line.end.equals( this.end );
  68. }
  69. };