all files / src/ Line3.js

33.33% Statements 17/51
25% Branches 6/24
35.71% Functions 5/14
33.33% Lines 17/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                                                                                                                                                                                                                                                                           
import Vector3 from './vector3.js';
import { clamp, approximatelyEquals } from './math.js';
 
// Copied from THREE.JS
/**
 * @author bhouston / http://exocortex.com
 */
 
 
class Line3 {
  constructor (start, end) {
 
    this.start = (start !== undefined) ? start : new Vector3();
    this.end = (end !== undefined) ? end : new Vector3();
 
  }
 
  set (start, end) {
 
    this.start.copy(start);
    this.end.copy(end);
 
    return this;
 
  }
 
  copy (line) {
 
    this.start.copy(line.start);
    this.end.copy(line.end);
 
    return this;
 
  }
 
  center (optionalTarget) {
 
    const result = optionalTarget || new Vector3();
 
 
    return result.addVectors(this.start, this.end).multiplyScalar(0.5);
 
  }
 
  delta (optionalTarget) {
 
    const result = optionalTarget || new Vector3();
 
 
    return result.subVectors(this.end, this.start);
 
  }
 
  distanceSq () {
 
    return this.start.distanceToSquared(this.end);
 
  }
 
  distance () {
 
    return this.start.distanceTo(this.end);
 
  }
 
  at (t, optionalTarget) {
 
    const result = optionalTarget || new Vector3();
 
    return this.delta(result).multiplyScalar(t).add(this.start);
 
  }
 
  closestPointToPointParameter (point, clampToLine) {
 
    const startP = new Vector3();
    const startEnd = new Vector3();
 
    startP.subVectors(point, this.start);
    startEnd.subVectors(this.end, this.start);
 
    const startEnd2 = startEnd.dot(startEnd);
    const startEnd_startP = startEnd.dot(startP);
 
    let t = startEnd_startP / startEnd2;
 
    if (clampToLine) {
      t = clamp(t, 0, 1);
    }
 
    return t;
 
  }
 
  closestPointToPoint (point, clampToLine, optionalTarget) {
 
    const t = this.closestPointToPointParameter(point, clampToLine);
 
    const result = optionalTarget || new Vector3();
 
    return this.delta(result).multiplyScalar(t).add(this.start);
 
  }
 
  applyMatrix4 (matrix) {
 
    this.start.applyMatrix4(matrix);
    this.end.applyMatrix4(matrix);
 
    return this;
 
  }
 
  equals (line) {
 
    return line.start.equals(this.start) && line.end.equals(this.end);
 
  }
 
  clone () {
 
    return new Line3().copy(this);
 
  }
 
  intersectLine (line) {
    // http://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment/10288710#10288710
    // Consider two lines r1 and r2, represented by the following parametric equations:A + vt and B + us, respectively.
    // Where A is a point of r1 and v a vector parallel to line.
    // And B is a point of r2 and u a vector parallel to line.
    // 'this' represents r2 and 'line' represents r1
    const da = this.end.clone().sub(this.start); //u
    const db = line.end.clone().sub(line.start); //v
    const dc = line.start.clone().sub(this.start); // AB
 
    const daCrossDb = da.clone().cross(db);
    const dcCrossDb = dc.clone().cross(db);
 
    // Lines are not coplanar, stop here
    // Coplanar only if the vectors AB, u, v are linearly dependent, i.e AB . (u × v) = 0
    const coplanarResult = dc.dot(daCrossDb);
    const normalizedCoplanarResult =
      coplanarResult / (dc.lengthSq() * daCrossDb.lengthSq());
    if (!approximatelyEquals(normalizedCoplanarResult, 0)) {
      return;
    }
 
    const s = dcCrossDb.dot(daCrossDb) / daCrossDb.lengthSq();
 
    // Make sure we have an intersection
    if (s > 1.0 || isNaN(s)) {
      return;
    }
 
    const intersection = this.start.clone().add(da.clone().multiplyScalar(s));
    const distanceTest = intersection.clone().sub(line.start).lengthSq() + intersection.clone().sub(line.end).lengthSq();
 
    if (distanceTest <= line.distanceSq()) {
      return intersection;
    }
 
    return;
  }
}
 
export default Line3;