Line3.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @author bhouston / http://exocortex.com
  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. var line = new this.constructor();
  17. return line.copy( this );
  18. },
  19. copy: function ( line ) {
  20. this.start.copy( line.start );
  21. this.end.copy( line.end );
  22. return this;
  23. },
  24. center: function ( optionalTarget ) {
  25. var result = optionalTarget || new THREE.Vector3();
  26. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  27. },
  28. delta: function ( optionalTarget ) {
  29. var result = optionalTarget || new THREE.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 THREE.Vector3();
  40. return this.delta( result ).multiplyScalar( t ).add( this.start );
  41. },
  42. closestPointToPointParameter: function () {
  43. var startP = new THREE.Vector3();
  44. var startEnd = new THREE.Vector3();
  45. return function ( 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 = THREE.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 THREE.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. };