All files / src/core/grid triangle-grid.ts

70% Statements 98/140
100% Branches 18/18
70% Functions 7/10
70% Lines 98/140

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 1981x           1x 4x   4x   4x 4x   4x 4x   4x 1003x   1003x   1003x   1003x   1003x   1003x   1003x 602x 401x 401x 1003x 401x 301x 301x 401x   1003x 1003x   4x               4x     2x 2x   2x   2x     1x 1x   1x   1x 1x 1x     1x 1x   1x   1x 1x 1x 2x   4x 2020x   2020x   2020x   2020x   2020x   2020x   2020x 1010x 1010x   1010x   1010x 1010x 1010x 1010x 1010x   1010x   1010x 1010x 1010x 2020x   4x 2020x   2020x 6060x   6060x 2020x 2020x   4x                                           4x                                           4x 101x 101x 101x 101x 101x   101x   101x   101x   101x 2020x   2020x 6060x 6060x 5454x 6060x 1212x 1212x 6060x 101x   101x 101x 4x  
import { Point, EdgeInfo } from "../../types";
import { pointsClose, removeDuplicateEdges } from "../utils/geometry";
import { Grid } from "./grid";
import { scanCellWindow } from "./grid-scan";
import { findClosestEdgeInWindow, findClosestVertexInWindow } from "./grid-search";
 
export class TriangleGrid implements Grid {
  private triWidth: number;
 
  private triHeight: number;
 
  constructor(private scale: number) {
    this.triWidth = scale;
 
    this.triHeight = (scale * Math.sqrt(3)) / 2;
  }
 
  pixelToCell(pixel: Point): { col: number; row: number } {
    const q = pixel.x / (this.triWidth / 2);
 
    const r = pixel.y / this.triHeight;
 
    let col = Math.floor(q);
 
    const row = Math.floor(r);
 
    const u = q - col;
 
    const v = r - row;
 
    if ((row + col) % 2 === 0) {
      if (u + v < 1) {
        col--;
      }
    } else {
      if (u < v) {
        col--;
      }
    }
 
    return { col, row };
  }
 
  cellToPixel(cell: { col: number; row: number }): Point {
    const x = cell.col * this.triWidth * 0.5;
 
    const y = cell.row * this.triHeight;
 
    return { x: x + this.triWidth / 2, y: y + this.triHeight / 2 };
  }
 
  getNeighbors(cell: {
    col: number;
    row: number;
  }): { col: number; row: number }[] {
    const { col, row } = cell;
 
    const isUpward = (row + col) % 2 === 0; // Determines if triangle is pointing up
 
    if (isUpward) {
      // Upward triangle neighbors: shares edges with two side triangles and one bottom triangle
 
      return [
        { col: col - 1, row }, // Left
 
        { col: col + 1, row }, // Right
 
        { col, row: row + 1 }, // Bottom
      ];
    } else {
      // Downward triangle neighbors: shares edges with two side triangles and one top triangle
 
      return [
        { col: col - 1, row }, // Left
 
        { col: col + 1, row }, // Right
 
        { col, row: row - 1 }, // Top
      ];
    }
  }
 
  getCellPolygon(cell: { col: number; row: number }): Point[] {
    const triangleHeight = (this.scale * Math.sqrt(3)) / 2;
 
    const triX = cell.col * this.scale * 0.5;
 
    const triY = cell.row * triangleHeight;
 
    const isUpward = (cell.row + cell.col) % 2 === 0;
 
    const centerX = triX + this.scale / 2;
 
    const centerY = triY + triangleHeight / 2;
 
    if (isUpward) {
      return [
        { x: centerX, y: centerY - triangleHeight / 2 },
 
        { x: centerX - this.scale / 2, y: centerY + triangleHeight / 2 },
 
        { x: centerX + this.scale / 2, y: centerY + triangleHeight / 2 },
      ];
    } else {
      return [
        { x: centerX - this.scale / 2, y: centerY - triangleHeight / 2 },
 
        { x: centerX + this.scale / 2, y: centerY - triangleHeight / 2 },
 
        { x: centerX, y: centerY + triangleHeight / 2 },
      ];
    }
  }
 
  getCellEdges(cell: { col: number; row: number }): EdgeInfo[] {
    const poly = this.getCellPolygon(cell);
 
    return poly.map((p, i) => ({
      type: "edge",
 
      points: [p, poly[(i + 1) % poly.length]],
    }));
  }
 
  getEdgeAt(
    pixel: Point,
    threshold: number,
    gridWidth: number,
    gridHeight: number
  ): EdgeInfo | null {
    const approxRow = Math.floor(pixel.y / this.triHeight);
 
    const approxCol = Math.floor(pixel.x / (this.triWidth * 0.5));
    return findClosestEdgeInWindow(
      pixel,
      threshold,
      gridWidth,
      gridHeight,
      approxCol,
      approxRow,
      2,
      2,
      (cell) => this.getCellEdges(cell)
    );
  }
 
  getVertexAt(
    pixel: Point,
    threshold: number,
    gridWidth: number,
    gridHeight: number
  ): Point | null {
    const approxRow = Math.floor(pixel.y / this.triHeight);
 
    const approxCol = Math.floor(pixel.x / (this.triWidth * 0.5));
    return findClosestVertexInWindow(
      pixel,
      threshold,
      gridWidth,
      gridHeight,
      approxCol,
      approxRow,
      2,
      2,
      (cell) => this.getCellPolygon(cell)
    );
  }
 
  getEdgesAtVertex(
    vertex: Point,
    gridWidth: number,
    gridHeight: number
  ): EdgeInfo[] {
    const edges: EdgeInfo[] = [];
 
    const epsilon = 0.1;
 
    const approxRow = Math.floor(vertex.y / this.triHeight);
 
    const approxCol = Math.floor(vertex.x / (this.triWidth * 0.5));
 
    scanCellWindow(gridWidth, gridHeight, approxCol, approxRow, 2, 2, (c, r) => {
      const cellEdges = this.getCellEdges({ col: c, row: r });
 
      for (const edge of cellEdges) {
        if (
          pointsClose(vertex, edge.points[0], epsilon) ||
          pointsClose(vertex, edge.points[1], epsilon)
        ) {
          edges.push(edge);
        }
      }
    });
 
    return removeDuplicateEdges(edges);
  }
}