Files
road-compiler/test/junction-build-plan-counterexamples.js
T

167 lines
6.9 KiB
JavaScript

'use strict';
// Source-independent counterexamples for the JunctionBuildPlan contract.
const assert = require('assert/strict');
const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('../src/compile/junction-build-plan');
const { buildJunctionBoundaryGraph } = require('../src/compile/junction-boundary-graph');
const { classifyJunctionShape } = require('../src/compile/complex-junction');
const metersPerDegree = 111320;
const point = (east, north) => [east / metersPerDegree, north / metersPerDegree];
function heading(line) {
const [from, to] = line;
return (Math.atan2(to[0] - from[0], to[1] - from[1]) * 180) / Math.PI;
}
function distance(first, second) {
return Math.hypot((first[0] - second[0]) * metersPerDegree, (first[1] - second[1]) * metersPerDegree);
}
function length(line) {
return line.slice(1).reduce((total, item, index) => total + distance(line[index], item), 0);
}
function pointAtStation(line, station) {
let remaining = station;
for (let index = 1; index < line.length; index += 1) {
const segmentLength = distance(line[index - 1], line[index]);
if (remaining <= segmentLength) {
const ratio = remaining / segmentLength;
return [
line[index - 1][0] + (line[index][0] - line[index - 1][0]) * ratio,
line[index - 1][1] + (line[index][1] - line[index - 1][1]) * ratio,
];
}
remaining -= segmentLength;
}
return line.at(-1);
}
function fixture(name, entries) {
const nodeId = `${name}:node`;
const approaches = entries.map((entry, index) => ({
segmentId: `${name}:segment:${index + 1}`,
roadIds: [`${name}:road:${index + 1}`],
widthMeters: entry.widthMeters,
line: entry.line,
}));
return {
model: {
roads: entries.map((entry, index) => ({
id: `${name}:road:${index + 1}`,
segmentId: `${name}:segment:${index + 1}`,
sourceNodeIds: [nodeId, `${name}:outer:${index + 1}`],
laneCount: 1,
})),
},
cluster: { id: name, nodeIds: [nodeId, `${name}:internal`] },
helpers: {
junctionPlans: new Map([
[nodeId, { node: point(0, 0), approaches }],
[`${name}:internal`, { node: point(1, 1), approaches: [] }],
]),
headingAtEndpoint: heading,
lineLengthMeters: length,
distanceMeters: distance,
},
};
}
function topology(name, entries) {
const input = fixture(name, entries);
return { ...input, topology: resolveJunctionBuildPlan(input.model, input.cluster, input.helpers) };
}
// A divided approach keeps independently addressable entry sections even when
// both carriageways share an arm.
const divided = topology('divided', [
{ widthMeters: 4, line: [point(-5, 0), point(-5, 50)] },
{ widthMeters: 5, line: [point(5, 0), point(5, 50)] },
{ widthMeters: 7, line: [point(0, 0), point(50, 0)] },
{ widthMeters: 7, line: [point(0, 0), point(0, -50)] },
]);
assert.equal(divided.topology.carriageways.length, 4);
assert.equal(divided.topology.arms.length, 3);
assert.equal(divided.topology.arms.find((arm) => arm.members.length === 2)?.members.length, 2);
assert.equal(new Set(divided.topology.carriageways.map((item) => item.entrySection.carriagewayKey)).size, 4);
assert.ok(
divided.topology.carriageways.every(
(item) => item.entrySection.boundaryEndpoints.near.left[0] !== item.entrySection.boundaryEndpoints.near.right[0],
),
);
const dividedBoundaryGraph = buildJunctionBoundaryGraph(
divided.topology,
point(0, 0),
{ outerRadiusFor: () => 25, innerRadiusFor: () => 10 },
divided.helpers,
);
assert.equal(dividedBoundaryGraph.status, 'entry-sections-only');
assert.equal(dividedBoundaryGraph.sections.length, divided.topology.carriageways.length);
assert.equal(new Set(dividedBoundaryGraph.sections.map((section) => section.carriagewayKey)).size, 4);
assert.equal(dividedBoundaryGraph.edges.length, dividedBoundaryGraph.sections.length * 4);
// A T junction has explicit, source-stable entry sections. Closure edges are a
// later BoundaryGraph concern and must not be fabricated by topology parsing.
const tee = topology('tee', [
{ widthMeters: 8, line: [point(0, 0), point(0, 45)] },
{ widthMeters: 4, line: [point(0, 0), point(45, 0)] },
{ widthMeters: 4, line: [point(0, 0), point(-45, 0)] },
]);
assert.equal(tee.topology.arms.length, 3);
assert.equal(classifyJunctionShape(tee.topology.arms, point(0, 0), tee.helpers).kind, 't-junction');
assert.ok(tee.topology.carriageways.every((item) => item.entrySection.feasibility === 'supported'));
// Offset and unequal-width approaches preserve individual source sections and
// local section tangents rather than a single arm envelope.
const offsetUnequal = topology('offset-unequal', [
{ widthMeters: 12, line: [point(-8, 0), point(-8, 48)] },
{ widthMeters: 4, line: [point(0, 18), point(48, 18)] },
{ widthMeters: 6, line: [point(9, 0), point(9, -28)] },
{ widthMeters: 9, line: [point(0, -12), point(-48, -12)] },
]);
assert.equal(offsetUnequal.topology.arms.length, 4);
assert.equal(
classifyJunctionShape(offsetUnequal.topology.arms, point(0, 0), offsetUnequal.helpers).kind,
'offset-cross',
);
assert.deepEqual(
offsetUnequal.topology.carriageways.map((item) => item.entrySection.farStationMeters),
offsetUnequal.topology.carriageways.map((item) => item.length),
);
// A short curved approach reports bounded feasibility while retaining distinct
// local tangents at each end of its source polyline.
const curvedShort = topology('curved-short', [
{ widthMeters: 6, line: [point(0, 0), point(0, 3), point(7, 7)] },
{ widthMeters: 6, line: [point(0, 0), point(35, 0)] },
{ widthMeters: 6, line: [point(0, 0), point(0, -35)] },
]);
assert.equal(curvedShort.topology.carriageways[0].heading, 0, 'current heading samples only the first segment');
assert.ok(curvedShort.topology.carriageways[0].length < 12, 'fixture is shorter than the minimum complex core radius');
assert.equal(curvedShort.topology.carriageways[0].entrySection.feasibility, 'short-approach');
assert.equal(curvedShort.topology.carriageways[0].entrySection.reason, 'entry-section-too-short');
assert.notEqual(
curvedShort.topology.carriageways[0].entrySection.nearTangent,
curvedShort.topology.carriageways[0].entrySection.farTangent,
);
const curvedBoundaryGraph = buildJunctionBoundaryGraph(
curvedShort.topology,
point(0, 0),
{ outerRadiusFor: () => 8, innerRadiusFor: () => 2 },
curvedShort.helpers,
);
const curvedSection = curvedBoundaryGraph.sections.find(
(section) => section.carriagewayKey === curvedShort.topology.carriageways[0].key,
);
assert.notEqual(curvedSection.outer.tangent, curvedSection.inner.tangent);
const curvedBoundaryLine = [point(0, 0), point(0, 5), point(10, 10)];
const curvedBoundaryStation = stationAtDistanceFrom(curvedBoundaryLine, point(0, 0), 9, {
distanceMeters: distance,
});
assert.ok(Math.abs(distance(pointAtStation(curvedBoundaryLine, curvedBoundaryStation), point(0, 0)) - 9) < 0.01);
console.log('junction build-plan counterexample tests passed');