Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified archify.zip
Binary file not shown.
77 changes: 76 additions & 1 deletion archify/renderers/shared/geometry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,82 @@ export function segmentRectIntersectionLength(segment, rect) {
return length * Math.max(0, leave - enter);
}

const ROUTE_INDEX_CELL_SIZE = 128;
const ROUTE_INDEX_MAX_CELLS_PER_ROUTE = 256;

function routeBounds(route) {
let x1 = Infinity;
let y1 = Infinity;
let x2 = -Infinity;
let y2 = -Infinity;
for (const [x, y] of route.points) {
x1 = Math.min(x1, x);
y1 = Math.min(y1, y);
x2 = Math.max(x2, x);
y2 = Math.max(y2, y);
}
return { x1, y1, x2, y2 };
}

function createRouteCandidateIndex(routes) {
const cells = new Map();
const globalRouteIndexes = [];

routes.forEach((route, routeIndex) => {
const bounds = routeBounds(route);
const x1 = Math.floor(bounds.x1 / ROUTE_INDEX_CELL_SIZE);
const y1 = Math.floor(bounds.y1 / ROUTE_INDEX_CELL_SIZE);
const x2 = Math.floor(bounds.x2 / ROUTE_INDEX_CELL_SIZE);
const y2 = Math.floor(bounds.y2 / ROUTE_INDEX_CELL_SIZE);
if (![x1, y1, x2, y2].every(Number.isSafeInteger)) {
globalRouteIndexes.push(routeIndex);
return;
}
const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);

// Very long routes are cheaper to test once per label than to duplicate
// across a large part of the index.
if (!Number.isSafeInteger(cellCount) || cellCount > ROUTE_INDEX_MAX_CELLS_PER_ROUTE) {
Comment thread
tt-a1i marked this conversation as resolved.
globalRouteIndexes.push(routeIndex);
return;
}
for (let y = y1; y <= y2; y += 1) {
for (let x = x1; x <= x2; x += 1) {
const key = `${x}:${y}`;
const bucket = cells.get(key);
if (bucket) bucket.push(routeIndex);
else cells.set(key, [routeIndex]);
}
}
});

return function candidates(rect, threshold) {
const x1 = Math.floor((rect.x - threshold) / ROUTE_INDEX_CELL_SIZE);
const y1 = Math.floor((rect.y - threshold) / ROUTE_INDEX_CELL_SIZE);
const x2 = Math.floor((rect.x + rect.width + threshold) / ROUTE_INDEX_CELL_SIZE);
const y2 = Math.floor((rect.y + rect.height + threshold) / ROUTE_INDEX_CELL_SIZE);
const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);

// A huge threshold offers no selectivity and can overflow cell iteration.
if (![x1, y1, x2, y2].every(Number.isSafeInteger)
|| !Number.isSafeInteger(cellCount)
|| cellCount > Math.max(64, cells.size * 4)) return routes;

const routeIndexes = new Set(globalRouteIndexes);
for (let y = y1; y <= y2; y += 1) {
for (let x = x1; x <= x2; x += 1) {
for (const routeIndex of cells.get(`${x}:${y}`) || []) routeIndexes.add(routeIndex);
}
}
return [...routeIndexes]
.sort((left, right) => left - right)
.map((routeIndex) => routes[routeIndex]);
};
}

export function collectLabelRouteClearance({ labels, routedRelations, threshold }) {
if (!Number.isFinite(threshold) || threshold < 0) return [];
if (threshold === 0) return [];
const routeCandidates = asArray(routedRelations).map((entry, fallbackIndex) => {
const relation = entry?.relation || entry;
const points = normalizeRoutePoints(entry?.points || relation?.routePoints);
Expand All @@ -125,6 +199,7 @@ export function collectLabelRouteClearance({ labels, routedRelations, threshold
});
const hits = [];
const seenLabels = new Set();
const candidateRoutes = createRouteCandidateIndex(routes);

for (const [fallbackIndex, label] of asArray(labels).entries()) {
const rect = label?.rect || label;
Expand All @@ -133,7 +208,7 @@ export function collectLabelRouteClearance({ labels, routedRelations, threshold
const labelIdentity = relationshipIdentity(label?.relation, relationIndex);
if (seenLabels.has(labelIdentity)) continue;
seenLabels.add(labelIdentity);
for (const route of routes) {
for (const route of candidateRoutes(rect, threshold)) {
if (relationIndex === route.relationIndex || sameRelationship(label?.relation, route.relation)) continue;
let nearest = null;
for (let segmentIndex = 0; segmentIndex < route.points.length - 1; segmentIndex += 1) {
Expand Down
78 changes: 78 additions & 0 deletions archify/test/geometry.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,84 @@ test('minimumLabelRouteClearance handles receipt sets above the argument limit',
assert.equal(minimumLabelRouteClearance([]), null);
});

test('collectLabelRouteClearance indexes sparse routes without changing hit order', () => {
const owner = { id: 'owner', from: 'owner-a', to: 'owner-b' };
const routes = Array.from({ length: 500 }, (_, index) => ({
relation: { id: `route-${index}`, from: `a-${index}`, to: `b-${index}` },
relationIndex: index + 1,
points: [[0, index * 256], [100, index * 256]],
}));
const nearFirst = { relation: routes[12].relation, relationIndex: 13, points: [[0, 101], [100, 101]] };
const nearSecond = { relation: routes[4].relation, relationIndex: 5, points: [[0, 103], [100, 103]] };
routes[12] = nearFirst;
routes[4] = nearSecond;
const labels = [{ relation: owner, relationIndex: 0, label: 'indexed', ...rect(20, 100, 20, 1) }];

const hits = collectLabelRouteClearance({ labels, routedRelations: routes, threshold: 4 });
assert.deepEqual(hits.map((hit) => hit.otherRelationIndex), [5, 13]);
assert.deepEqual(hits.map((hit) => hit.clearance), [2, 0]);
});

test('collectLabelRouteClearance keeps very long routes in the global candidate set', () => {
const owner = { id: 'owner', from: 'a', to: 'b' };
const long = { id: 'long', from: 'c', to: 'd' };
const hits = collectLabelRouteClearance({
labels: [{ relation: owner, relationIndex: 0, label: 'long route', ...rect(50000, 10, 20, 10) }],
routedRelations: [{ relation: long, relationIndex: 1, points: [[0, 15], [100000, 15]] }],
threshold: 4,
});
assert.equal(hits.length, 1);
assert.equal(hits[0].otherRelation, long);
assert.equal(hits[0].clearance, 0);
});

test('collectLabelRouteClearance falls back for unsafe finite grid coordinates', () => {
const owner = { id: 'owner', from: 'a', to: 'b' };
const unsafe = { id: 'unsafe', from: 'c', to: 'd' };
const hits = collectLabelRouteClearance({
labels: [{ relation: owner, relationIndex: 0, label: 'unsafe coordinate', ...rect(1e20, 1e20, 1e7, 1e7) }],
routedRelations: [{ relation: unsafe, relationIndex: 1, points: [[1e20 - 1e8, 1e20 + 5e6], [1e20 + 1e8, 1e20 + 5e6]] }],
threshold: 4,
});
assert.equal(hits.length, 1);
assert.equal(hits[0].otherRelation, unsafe);
assert.equal(hits[0].clearance, 0);
});

test('collectLabelRouteClearance falls back when threshold query is unselective', () => {
const owner = { id: 'owner', from: 'a', to: 'b' };
const other = { id: 'other', from: 'c', to: 'd' };
const hits = collectLabelRouteClearance({
labels: [{ relation: owner, relationIndex: 0, label: 'wide threshold', ...rect(100, 100, 10, 10) }],
routedRelations: [{ relation: other, relationIndex: 1, points: [[0, 105], [200, 105]] }],
threshold: 1e9,
});
assert.equal(hits.length, 1);
assert.equal(hits[0].otherRelation, other);
assert.equal(hits[0].clearance, 0);
});

test('collectLabelRouteClearance stays bounded for thousands of sparse routes', () => {
const count = 3000;
const routedRelations = Array.from({ length: count }, (_, index) => ({
relation: { id: `route-${index}`, from: `a-${index}`, to: `b-${index}` },
relationIndex: index,
points: [[0, index * 20], [100, index * 20]],
}));
const labels = routedRelations.map((route, index) => ({
relation: route.relation,
relationIndex: index,
label: `label-${index}`,
...rect(20, index * 20 - 2, 20, 4),
}));
const started = performance.now();
const hits = collectLabelRouteClearance({ labels, routedRelations, threshold: 2 });
const elapsed = performance.now() - started;

assert.deepEqual(hits, []);
assert.ok(elapsed < 1500, `expected sparse clearance scan under 1500ms, received ${elapsed.toFixed(1)}ms`);
});

test('endpoint-side direction distinguishes perpendicular entry from a tangent border run', () => {
const clean = [[350, 160], [350, 200], [150, 200], [150, 240]];
const tangent = [[350, 160], [350, 200], [100, 200], [100, 240], [150, 240]];
Expand Down