all files / src/ plane.js

10% Statements 7/70
0% Branches 0/26
9.52% Functions 2/21
10% Lines 7/70
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import Vector3 from './vector3.js';
 
// Copied from Three.JS
/**
 * @author bhouston / http://exocortex.com
 */
 
class Plane {
  constructor (normal, constant) {
 
    this.normal = (normal !== undefined) ? normal : new Vector3(1, 0, 0);
    this.constant = (constant !== undefined) ? constant : 0;
 
  }
 
  set (normal, constant) {
 
    this.normal.copy(normal);
    this.constant = constant;
 
    return this;
 
  }
 
  setComponents (x, y, z, w) {
 
    this.normal.set(x, y, z);
    this.constant = w;
 
    return this;
 
  }
 
  setFromNormalAndCoplanarPoint (normal, point) {
 
    this.normal.copy(normal);
    // Must be this.normal, not normal, as this.normal is normalized
    this.constant = -point.dot(this.normal);
 
    return this;
 
  }
 
  copy (plane) {
 
    this.normal.copy(plane.normal);
    this.constant = plane.constant;
 
    return this;
 
  }
 
  normalize () {
 
    // Note: will lead to a divide by zero if the plane is invalid.
 
    const inverseNormalLength = 1.0 / this.normal.length();
 
    this.normal.multiplyScalar(inverseNormalLength);
    this.constant *= inverseNormalLength;
 
    return this;
 
  }
 
  negate () {
 
    this.constant *= -1;
    this.normal.negate();
 
    return this;
 
  }
 
  distanceToPoint (point) {
 
    return this.normal.dot(point) + this.constant;
 
  }
 
  distanceToSphere (sphere) {
 
    return this.distanceToPoint(sphere.center) - sphere.radius;
 
  }
 
  projectPoint (point, optionalTarget) {
 
    return this.orthoPoint(point, optionalTarget).sub(point).negate();
 
  }
 
  orthoPoint (point, optionalTarget) {
 
    const perpendicularMagnitude = this.distanceToPoint(point);
 
    const result = optionalTarget || new Vector3();
 
 
    return result.copy(this.normal).multiplyScalar(perpendicularMagnitude);
 
  }
 
  isIntersectionLine (line) {
 
    // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
 
    const startSign = this.distanceToPoint(line.start);
    const endSign = this.distanceToPoint(line.end);
 
    return (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
 
  }
 
  intersectPlane (targetPlane) {
    // Returns the intersection line between two planes
    const direction = this.normal.clone().cross(targetPlane.normal);
    const origin = new Vector3();
    const intersectionData = {
      origin,
      direction
    };
 
    // If the planes are parallel, return an empty vector for the intersection line
    if (this.normal.clone().cross(targetPlane.normal).length < 1e-10) {
      intersectionData.direction = new Vector3();
 
      return intersectionData;
    }
 
    const h1 = this.constant;
    const h2 = targetPlane.constant;
    const n1dotn2 = this.normal.clone().dot(targetPlane.normal);
 
    const c1 = -(h1 - h2 * n1dotn2) / (1 - n1dotn2 * n1dotn2);
    const c2 = -(h2 - h1 * n1dotn2) / (1 - n1dotn2 * n1dotn2);
 
    intersectionData.origin = this.normal.clone().multiplyScalar(c1).add(targetPlane.normal.clone().multiplyScalar(c2));
 
    return intersectionData;
  }
 
  coplanarPoint (optionalTarget) {
 
    const result = optionalTarget || new Vector3();
 
 
    return result.copy(this.normal).multiplyScalar(-this.constant);
 
  }
 
  translate (offset) {
 
    this.constant = this.constant - offset.dot(this.normal);
 
    return this;
 
  }
 
  equals (plane) {
 
    return plane.normal.equals(this.normal) && (plane.constant === this.constant);
 
  }
 
  clone () {
 
    return new Plane().copy(this);
 
  }
}
 
Plane.prototype.setFromCoplanarPoints = (function () {
 
  const v1 = new Vector3();
  const v2 = new Vector3();
 
  return function (a, b, c) {
 
    const normal = v1.subVectors(c, b).cross(v2.subVectors(a, b)).normalize();
 
    // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
 
    this.setFromNormalAndCoplanarPoint(normal, a);
 
    return this;
 
  };
 
})();
 
Plane.prototype.intersectLine = (function () {
 
  const v1 = new Vector3();
 
  return function (line, optionalTarget) {
 
    const result = optionalTarget || new Vector3();
 
    const direction = line.delta(v1);
 
    const denominator = this.normal.dot(direction);
 
    if (denominator === 0) {
 
      // Line is coplanar, return origin
      if (this.distanceToPoint(line.start) === 0) {
 
        return result.copy(line.start);
 
      }
 
      // Unsure if this is the correct method to handle this case.
      return undefined;
 
    }
 
    const t = -(line.start.dot(this.normal) + this.constant) / denominator;
 
    if (t < 0 || t > 1) {
 
      return undefined;
 
    }
 
    return result.copy(direction).multiplyScalar(t).add(line.start);
 
  };
 
})();
 
export default Plane;