feat(junction): share complex topology build plan
This commit is contained in:
1 parent
855400fba2
commit
a12e2ac823
20 files changed
+712
-147
No files matched your search
@@ -0,0 +1,123 @@
|
||||
'use strict';
|
||||
|
||||
// Source-independent counterexamples for the JunctionBuildPlan contract.
|
||||
|
||||
const assert = require('assert/strict');
|
||||
const { resolveJunctionBuildPlan } = require('../src/compile/junction-build-plan');
|
||||
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 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],
|
||||
),
|
||||
);
|
||||
|
||||
// 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,
|
||||
);
|
||||
|
||||
console.log('junction build-plan counterexample tests passed');
|
||||
Reference in new issue
Block a user