All files InterpolationMatrix.ts

100% Statements 75/75
100% Branches 21/21
100% Functions 24/24
100% Lines 66/66

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 14711x 11x 11x   11x     3x 3x 3x                     3x   3x   3x 3x 3x 3x     3x 3x       3x 12x   16x 16x   12x   16x 16x   3x       24x             3x 3x 3x       403x 403x 401x 1x 1x 1x     400x   2x           1x     121x 121x 121x 121x 121x     121x         242x       12x 3x 363x 1452x 363x 484x 15x   469x 1876x 1758x 1758x   1876x   469x           3x 12x 12x 16x           3x 33x 33x 363x                 363x 363x          
import {DPolygon} from './DPolygon';
import {DPoint} from './DPoint';
import {isDefAndNotNull} from './utils';
 
export class InterpolationMatrix {
  readonly minPoint: DPoint;
  readonly maxPoint: DPoint;
  private points: DPoint[] = [];
  private cells: Record<number, Record<number, DPolygon>> = {};
  private allCells: DPolygon[] = [];
  private readonly sizePoly: DPolygon;
  private readonly keys: string[];
  readonly size: DPoint;
 
  /**
   * (Inverse distance weighting)[https://en.wikipedia.org/wiki/Inverse_distance_weighting]
   * `bboxLike`, `stepSize` and `points` should be in same unit.
   */
  constructor(
    bboxLike: DPolygon,
    private readonly stepSize: number,
    keys: string[] | string,
    private readonly p: number = 2
  ) {
    this.minPoint = bboxLike.minPoint;
    this.maxPoint = bboxLike.maxPoint;
    this.sizePoly = DPolygon.createSquareBySize(new DPoint(this.stepSize));
    this.size = this.maxPoint.clone().move(this.minPoint.clone().minus())
      .divide(this.stepSize)
      .ceil();
    this.keys = Array.isArray(keys) ? keys : [keys];
    this.generateCells();
  }
 
  setKnownPoints(points: DPoint[] | DPolygon): InterpolationMatrix {
    this.points = points instanceof DPolygon ? points.points : points;
    this.minPoint.setProperties(this.points.reduce((a: Record<string, number>, v: DPoint) => this.keys
      .reduce((aa: Record<string, number>, k: string) => {
        aa[k] = Math.min(a[k] ?? Infinity, v.properties[k]);
        return aa;
      }, a), {}));
    this.maxPoint.setProperties(this.points.reduce((a: Record<string, number>, v: DPoint) => this.keys
      .reduce((aa: Record<string, number>, k: string) => {
        aa[k] = Math.max(a[k] ?? -Infinity, v.properties[k]);
        return aa;
      }, a), {}));
    return this;
  }
 
  positionToCellCoords(d: DPoint): DPoint {
    return d.clone()
      .move(this.minPoint.clone().minus())
      .divide(this.stepSize)
      .floor();
  }
 
  calculate(): InterpolationMatrix {
    this.setKnownValues();
    this.interpolateValues();
    return this;
  }
 
  getCellValue({x, y}: DPoint, key?: string | string[]): (number | Record<string, number>) {
    const cell = this.cells[x][y];
    if (key) {
      if (Array.isArray(key)) {
        return key.reduce((a: Record<string, number>, k: string) => {
          a[k] = cell.properties[k];
          return a;
        }, {});
      }
      return cell.properties[key];
    }
    return {
      ...cell.properties
    };
  }
 
  get getCellData(): Record<number, Record<number, Record<string, number>>> {
    return this.allCells.reduce((a: Record<number, Record<number, Record<string, number>>>, c) => {
      const {
        x,
        y,
        ...props
      } = c.properties;
      a[x] = a[x] || {};
      a[x][y] = {
        ...props
      };
      return a;
    }, {});
  }
 
  get allCellsClone(): DPolygon[] {
    return this.allCells.map((p: DPolygon) => p.clone());
  }
 
  private interpolateValues() {
    const points = this.points.map((p: DPoint) => this.positionToCellCoords(p));
    this.allCells.forEach((cell: DPolygon) => {
      const t = new DPoint(cell.properties.x, cell.properties.y);
      const distances = points.map((p) => t.distance(p) ** this.p);
      this.keys.forEach((k) => {
        if (isDefAndNotNull(cell.properties[k])) {
          return;
        }
        const [valueSum, oneSum] = distances.reduce((a, d, i) => {
          if (isDefAndNotNull(this.points[i].properties[k])) {
            a[0] += this.points[i].properties[k] / d;
            a[1] += 1 / d;
          }
          return a;
        }, [0, 0]);
        cell.properties[k] = valueSum / oneSum;
      });
    });
  }
 
  private setKnownValues() {
    this.points.forEach((p: DPoint) => {
      const {x, y} = this.positionToCellCoords(p);
      this.keys.forEach((k) => {
        this.cells[x][y].properties[k] = p.properties[k];
      });
    });
  }
 
  private generateCells() {
    for (let i = this.minPoint.x, x = 0; i < this.maxPoint.x; i += this.stepSize, x++) {
      this.cells[x] = this.cells[x] || {};
      for (let j = this.minPoint.y, y = 0; j < this.maxPoint.y; j += this.stepSize, y++) {
        const t = this.sizePoly
          .clone()
          .loop()
          .move(i, j)
          .run()
          .setProperties({
            x,
            y
          });
        this.cells[x][y] = t;
        this.allCells.push(t);
      }
    }
  }
}