All files DPlane.ts

100% Statements 67/67
100% Branches 28/28
100% Functions 13/13
100% Lines 64/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 20211x 11x   11x   11x     31x 31x 31x 31x 31x 31x 31x       20x 2x   18x 2x   16x 2x   14x 14x         14x           6x           5x       5x 5x 5x   4x           6x           5x       5x 5x 5x   4x           6x           5x       5x 5x 5x   4x                       1x 1x           2x         1x           1x 1x             1x 1x 1x                 2x           2x 2x                       2x           2x 2x 2x 2x 2x 2x                       2x           2x 2x 2x 2x 2x 2x            
import {DPoint} from './DPoint';
import {gaussianElimination} from './utils';
import {DPolygon} from './DPolygon';
import {DNumbers} from './DNumbers';
 
export class DPlane {
  // eslint-disable-next-line max-params
  constructor(
    public a: number,
    public b: number,
    public c: number,
    public d: number,
    public p1: DPoint = DPoint.zero(),
    public p2: DPoint = DPoint.zero(),
    public p3: DPoint = DPoint.zero()
  ) {}
 
  static find(p1: DPoint, p2: DPoint, p3: DPoint): DPlane {
    if (p1.x === p2.x && p2.x === p3.x) {
      return new DPlane(1, 0, 0, -p1.x, p1, p2, p3);
    }
    if (p1.y === p2.y && p2.y === p3.y) {
      return new DPlane(0, 1, 0, -p1.y, p1, p2, p3);
    }
    if (p1.z === p2.z && p2.z === p3.z) {
      return new DPlane(0, 0, 1, -p1.z!, p1, p2, p3);
    }
    const d = 1;
    const [a, b, c] = gaussianElimination([
      [p1.x, p1.y, p1.z!, -d],
      [p2.x, p2.y, p2.z!, -d],
      [p3.x, p3.y, p3.z!, -d]
    ]);
    return new DPlane(a, b, c, d, p1, p2, p3);
  }
 
  x(p: DPoint): DPoint;
  x(p: DPolygon): DPolygon;
  x(p: DPoint | DPolygon): DPoint | DPolygon {
    if (p instanceof DPoint) {
      const {
        a,
        b,
        c,
        d
      } = this;
      const {
        y,
        z
      } = p;
      p.x = -(b * y + c * z! + d) / a;
      return p;
    }
    return (p as DPolygon).map((t: DPoint) => this.x(t));
  }
 
  y(p: DPoint): DPoint;
  y(p: DPolygon): DPolygon;
  y(p: DPoint | DPolygon): DPoint | DPolygon {
    if (p instanceof DPoint) {
      const {
        a,
        b,
        c,
        d
      } = this;
      const {
        x,
        z
      } = p;
      p.y = -(a * x + c * z! + d) / b;
      return p;
    }
    return (p as DPolygon).map((t: DPoint) => this.y(t));
  }
 
  z(p: DPoint): DPoint;
  z(p: DPolygon): DPolygon;
  z(p: DPoint | DPolygon): DPoint | DPolygon {
    if (p instanceof DPoint) {
      const {
        a,
        b,
        c,
        d
      } = this;
      const {
        x,
        y
      } = p;
      p.z = -(a * x + b * y + d) / c;
      return p;
    }
    return (p as DPolygon).map((t: DPoint) => this.z(t));
  }
 
  clone(): DPlane {
    const {
      a,
      b,
      c,
      d,
      p1,
      p2,
      p3
    } = this;
    return new DPlane(a, b, c, d, p1, p2, p3);
  }
 
  distance(p: DPoint): number;
  distance(p: DPlane): number;
  distance(p: DPoint | DPlane): number {
    if (p instanceof DPoint) {
      const {
        x,
        y,
        z
      } = p;
      const {
        a,
        b,
        c,
        d
      } = this;
      return Math.abs(a * x + b * y + c * z! + d) / Math.sqrt(a * a + b * b + c * c);
    }
    const {
      a,
      b,
      c,
      d
    } = p as DPlane;
    const {d: r} = this;
    return Math.abs(d - r) / Math.sqrt(a * a + b * b + c * c);
  }
 
  equal(p: DPlane): boolean {
    const {
      a,
      b,
      c,
      d
    } = p;
    const {
      a: q,
      b: w,
      c: e,
      d: r
    } = this;
    return DNumbers.like(a, q) &&
      DNumbers.like(b, w) &&
      DNumbers.like(c, e) &&
      DNumbers.like(d, r);
  }
 
  same(p: DPlane): boolean {
    const {
      a,
      b,
      c,
      d
    } = p;
    const {
      a: q,
      b: w,
      c: e,
      d: r
    } = this;
    const t = a / q;
    const y = b / w;
    const u = c / e;
    const i = d / r;
    return DNumbers.like(t, y) &&
      DNumbers.like(t, u) &&
      DNumbers.like(t, c) &&
      DNumbers.like(t, i);
  }
 
  parallel(p: DPlane): boolean {
    const {
      a,
      b,
      c,
      d
    } = p;
    const {
      a: q,
      b: w,
      c: e,
      d: r
    } = this;
    const t = a / q;
    const y = b / w;
    const u = c / e;
    const i = d / r;
    return DNumbers.like(t, y) &&
      DNumbers.like(t, u) &&
      DNumbers.like(t, c) &&
      !DNumbers.like(t, i);
  }
}