Merge branch 'master' into refactor/migrate-resample-module

This commit is contained in:
Marc Emmanuel 2026-03-10 11:39:46 +01:00
commit 54ba228fb6
11 changed files with 175 additions and 237 deletions

View file

@ -1,5 +1,5 @@
import Alea from "alea";
import { color } from "d3";
import { color, quadtree } from "d3";
import Delaunator from "delaunator";
import {
type Cells,
@ -267,6 +267,11 @@ export const findGridAll = (
return found;
};
const quadtreeCache = new WeakMap<
object,
ReturnType<typeof quadtree<[number, number, number]>>
>();
/**
* Returns the index of the packed cell containing the given x and y coordinates
* @param {number} x - The x coordinate
@ -278,10 +283,16 @@ export const findClosestCell = (
x: number,
y: number,
radius = Infinity,
packedGraph: any,
pack: { cells: { p: [number, number][] } },
): number | undefined => {
if (!packedGraph.cells?.q) return;
const found = packedGraph.cells.q.find(x, y, radius);
if (!pack.cells?.p) throw new Error("Pack cells not found");
let qTree = quadtreeCache.get(pack.cells.p);
if (!qTree) {
qTree = quadtree(pack.cells.p.map(([px, py], i) => [px, py, i]));
if (!qTree) throw new Error("Failed to create quadtree");
quadtreeCache.set(pack.cells.p, qTree);
}
const found = qTree.find(x, y, radius);
return found ? found[2] : undefined;
};
@ -415,8 +426,13 @@ export const findAllCellsInRadius = (
radius: number,
packedGraph: any,
): number[] => {
// Use findAllInQuadtree directly instead of relying on prototype extension
const found = findAllInQuadtree(x, y, radius, packedGraph.cells.q);
const q = quadtree<[number, number, number]>(
packedGraph.cells.p.map(
([px, py]: [number, number], i: number) =>
[px, py, i] as [number, number, number],
),
);
const found = findAllInQuadtree(x, y, radius, q);
return found.map((r: any) => r[2]);
};