mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
fix(radio): a cluster kept from the last pass names the same station first
Points too close together on screen are grouped by geo/screenCluster.js as the default renderer's entity clustering grouped them. Its range queries ran on a uniform grid sorted by index, while the reference queries a KDBush tree, so a cluster kept as the eye came lower could list its stations in another order, and a click on it played a different station. The index is now KDBush, built as the reference builds it, and kdbush is a declared dependency (it already came with the globe). The package boundaries list kdbush as external wherever screenCluster is owned, and own src/viewSky.js beside src/viewWeather.js.
This commit is contained in:
1 parent
14e406002c
commit
c57eb74506
6 files changed
+109
-70
No files matched your search
+13
-39
@@ -17,6 +17,8 @@
|
||||
* right and down.
|
||||
*/
|
||||
|
||||
import KDBush from 'kdbush';
|
||||
|
||||
/**
|
||||
* @typedef {object} ScreenClusterPoint
|
||||
* @property {string} id
|
||||
@@ -71,7 +73,7 @@ export function clusterScreenPoints({
|
||||
ys[i] = viewHeight - points[i].screen.y;
|
||||
}
|
||||
const clustered = new Uint8Array(n);
|
||||
const index = createGridIndex(xs, ys, Math.max(1, pixelRange * 2));
|
||||
const index = createIndex(xs, ys);
|
||||
const clusters = [];
|
||||
const kept = [];
|
||||
|
||||
@@ -164,43 +166,15 @@ function pointBox(x, y, size, pixelRange) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A uniform grid over packed coordinates, answering which points lie in a
|
||||
* box (bounds included), in ascending index order.
|
||||
* The points' index: a KDBush tree built as the reference renderer's entity
|
||||
* clustering builds it (64 per node, 64-bit coordinates), so a range query
|
||||
* lists the same points in the same order and a cluster kept from the last
|
||||
* pass names the same station first.
|
||||
*/
|
||||
function createGridIndex(xs, ys, cell) {
|
||||
const buckets = new Map();
|
||||
const key = (cx, cy) => `${cx}:${cy}`;
|
||||
for (let i = 0; i < xs.length; i++) {
|
||||
const k = key(Math.floor(xs[i] / cell), Math.floor(ys[i] / cell));
|
||||
let bucket = buckets.get(k);
|
||||
if (!bucket) buckets.set(k, (bucket = []));
|
||||
bucket.push(i);
|
||||
}
|
||||
return {
|
||||
range(minX, minY, maxX, maxY) {
|
||||
const found = [];
|
||||
if (!(maxX >= minX && maxY >= minY)) return found;
|
||||
const x0 = Math.floor(minX / cell);
|
||||
const x1 = Math.floor(maxX / cell);
|
||||
const y0 = Math.floor(minY / cell);
|
||||
const y1 = Math.floor(maxY / cell);
|
||||
if ((x1 - x0 + 1) * (y1 - y0 + 1) > buckets.size) {
|
||||
for (let i = 0; i < xs.length; i++)
|
||||
if (xs[i] >= minX && xs[i] <= maxX && ys[i] >= minY && ys[i] <= maxY)
|
||||
found.push(i);
|
||||
return found;
|
||||
}
|
||||
for (let cx = x0; cx <= x1; cx++)
|
||||
for (let cy = y0; cy <= y1; cy++)
|
||||
for (const i of buckets.get(key(cx, cy)) ?? [])
|
||||
if (
|
||||
xs[i] >= minX &&
|
||||
xs[i] <= maxX &&
|
||||
ys[i] >= minY &&
|
||||
ys[i] <= maxY
|
||||
)
|
||||
found.push(i);
|
||||
return found.sort((a, b) => a - b);
|
||||
},
|
||||
};
|
||||
function createIndex(xs, ys) {
|
||||
if (!xs.length) return { range: () => [] };
|
||||
const index = new KDBush(xs.length, 64, Float64Array);
|
||||
for (let i = 0; i < xs.length; i++) index.add(xs[i], ys[i]);
|
||||
index.finish();
|
||||
return index;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import KDBush from 'kdbush';
|
||||
import { clusterScreenPoints } from './screenCluster.js';
|
||||
|
||||
/** A point at screen `x, y` whose world position is `x, y, 0` too. */
|
||||
@@ -129,8 +130,51 @@ test('many points cluster as a scan of every point would', () => {
|
||||
ids.forEach((id) => done.add(id));
|
||||
}
|
||||
}
|
||||
// The same seeds take the same points; the order after the seed is the
|
||||
// index's, checked below.
|
||||
const seedThenSorted = (ids) => [ids[0], ...ids.slice(1).sort()];
|
||||
assert.deepEqual(
|
||||
result.clusters.map((c) => c.ids),
|
||||
expected,
|
||||
result.clusters.map((c) => seedThenSorted(c.ids)),
|
||||
expected.map(seedThenSorted),
|
||||
);
|
||||
});
|
||||
|
||||
test('a cluster lists its points in the reference renderer’s KDBush order, so a kept one names the same point first', () => {
|
||||
// The reference indexes the points with KDBush (64 per node, 64-bit) and
|
||||
// lists a cluster's points in the order a range query returns them.
|
||||
let seed = 11;
|
||||
const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
|
||||
const points = Array.from({ length: 400 }, (_, i) =>
|
||||
at(`p${i}`, random() * 400, random() * 400),
|
||||
);
|
||||
const index = new KDBush(points.length, 64, Float64Array);
|
||||
for (const p of points) index.add(p.screen.x, 1000 - p.screen.y);
|
||||
index.finish();
|
||||
const first = run(points);
|
||||
const [kept] = first.state.clusters;
|
||||
// Closer, at once: the first kept cluster takes every unclustered point in
|
||||
// its box, in the index's order.
|
||||
// (Its box is measured from the top, the index from the bottom.)
|
||||
const coord = { x: 200, y: 800 };
|
||||
const again = run(points, {
|
||||
eyeHeight: 999_999,
|
||||
previous: first.state,
|
||||
project: () => coord,
|
||||
});
|
||||
const width = Math.max(kept.width * 1e-6, kept.minimumWidth);
|
||||
const height = Math.max(kept.height * 1e-6, kept.minimumHeight);
|
||||
const expected = index
|
||||
.range(
|
||||
coord.x - width * 0.5,
|
||||
coord.y - height * 0.5,
|
||||
coord.x + width,
|
||||
coord.y + height,
|
||||
)
|
||||
.map((i) => points[i].id);
|
||||
assert.ok(expected.length >= 3, 'the kept cluster holds');
|
||||
assert.deepEqual(again.clusters[0].ids, expected);
|
||||
const ascending = [...expected].sort(
|
||||
(a, b) => Number(a.slice(1)) - Number(b.slice(1)),
|
||||
);
|
||||
assert.notDeepEqual(expected, ascending, 'the order is not the input order');
|
||||
});
|
||||
Reference in new issue
Block a user