Files
road-compiler/test/junction-facility-composition.js
T

203 lines
7.3 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const { buildPedestrianIsland, validateFacilityComposition } = require('../src/compile/junction-facility-composition');
const ring = [
[0, 0],
[4, 0],
[4, 2],
[0, 2],
[0, 0],
];
const crossings = [1, 2, 3].map((id) => ({
key: `crossing:${id}`,
fromBoundary: `walk:${id}`,
toBoundary: 'island:1',
}));
const island = buildPedestrianIsland({ key: 'island:1', ring, crossingKeys: crossings.map((item) => item.key) });
assert.equal(validateFacilityComposition(island, crossings).valid, true);
assert.equal(buildPedestrianIsland({ key: 'bad', ring, crossingKeys: ['a', 'b'] }), null);
assert.equal(validateFacilityComposition(island, crossings.slice(0, 2)).reason, 'crossing-reference-missing');
console.log('junction facility composition tests passed');
// User's sketch: a horizontal crossing to the right, a vertical crossing down,
// and the island in the upper-left quadrant, bounded by doubled short edges.
const { buildIslandFromCrossingEdges } = require('../src/compile/junction-facility-composition');
const clipping = require('polygon-clipping');
const sketchIsland = buildIslandFromCrossingEdges([0, 0], [0, 6.8], [-6.8, 0]);
assert.deepEqual(sketchIsland[0], sketchIsland.at(-1));
assert.ok(sketchIsland.every(([x, y]) => x <= 0 && y >= 0));
const arcMiddle = sketchIsland[7];
assert.ok(arcMiddle[1] - arcMiddle[0] < 6.8, 'hypotenuse bends toward the right-angle vertex');
const longEdgeStart = sketchIsland[18];
const longEdgeEnd = sketchIsland[34];
const longEdgeMid = sketchIsland[26];
const longEdgeCross =
(longEdgeEnd[0] - longEdgeStart[0]) * (longEdgeMid[1] - longEdgeStart[1]) -
(longEdgeEnd[1] - longEdgeStart[1]) * (longEdgeMid[0] - longEdgeStart[0]);
const cornerSideCross =
(longEdgeEnd[0] - longEdgeStart[0]) * (0 - longEdgeStart[1]) -
(longEdgeEnd[1] - longEdgeStart[1]) * (0 - longEdgeStart[0]);
assert.ok(longEdgeCross * cornerSideCross > 0, 'long edge must bow toward the right-angle vertex');
const islandContains = (point) => {
let inside = false;
for (let i = 0, j = sketchIsland.length - 1; i < sketchIsland.length; j = i++) {
const a = sketchIsland[i],
b = sketchIsland[j];
if (a[1] > point[1] !== b[1] > point[1] && point[0] < ((b[0] - a[0]) * (point[1] - a[1])) / (b[1] - a[1]) + a[0])
inside = !inside;
}
return inside;
};
assert.ok(
sketchIsland
.slice(0, -1)
.every(
(point) => islandContains(point) || sketchIsland.some((item) => item[0] === point[0] && item[1] === point[1]),
),
'green contact points must stay inside island',
);
for (const crossing of [
[
[0, 0],
[12, 0],
[12, 3.4],
[0, 3.4],
[0, 0],
],
[
[0, 0],
[0, -12],
[-3.4, -12],
[-3.4, 0],
[0, 0],
],
])
assert.deepEqual(clipping.intersection([sketchIsland], [crossing]), [], 'short-edge attachment has no area overlap');
const rotate = ([x, y]) => [-y + 20, x - 10];
const rotatedIsland = buildIslandFromCrossingEdges(
...[
[0, 0],
[0, 6.8],
[-6.8, 0],
].map(rotate),
);
rotatedIsland.forEach((p, i) =>
assert.ok(
Math.hypot(...p.map((v, j) => v - rotate(sketchIsland[i])[j])) < 1e-10,
'composition is independent of world orientation',
),
);
const { stripeLayout, orthogonalCrossingHeadings } = require('../src/compile/junction-facility-composition');
for (const span of [3.4, 8.123, 31.7]) {
const layout = stripeLayout(span);
for (let i = 1; i < layout.centers.length; i++)
assert.ok(Math.abs(layout.centers[i] - layout.centers[i - 1] - layout.width - layout.gap) < 1e-10);
assert.ok(layout.centers[0] >= layout.width / 2);
assert.ok(layout.centers.at(-1) + layout.width / 2 <= span + 1e-10);
}
const fitted = orthogonalCrossingHeadings([62.2, 150.4, 240.7, 332]);
for (let i = 1; i < 4; i++) assert.ok(Math.abs(fitted[i] - fitted[i - 1] - 90) < 1e-10);
assert.deepEqual(orthogonalCrossingHeadings([0, 120, 240]), [0, 120, 240]);
assert.deepEqual(orthogonalCrossingHeadings([0, 40, 180, 270]), [0, 40, 180, 270]);
const { crossingArcSupport, solveIslandContacts } = require('../src/compile/junction-facility-composition');
assert.ok(!sketchIsland.some(([x, y]) => x === 0 && y === 0), 'right-angle vertex is rounded');
assert.ok(!sketchIsland.some(([x, y]) => x === 0 && y === 6.8), 'first arc/leg vertex is rounded');
assert.ok(!sketchIsland.some(([x, y]) => x === -6.8 && y === 0), 'second arc/leg vertex is rounded');
// Curve support must consider the ends of a finite-width bar, not only its centre.
const curved = [
[-3, 2],
[-1, 0],
[1, 0],
[3, 2],
[3, -2],
[-3, -2],
[-3, 2],
];
assert.equal(crossingArcSupport(curved, 2), 1);
assert.deepEqual(
clipping.intersection(
[curved],
[
[
[-2, 1],
[2, 1],
[2, 1.42],
[-2, 1.42],
[-2, 1],
],
],
),
[],
);
const solvedContacts = solveIslandContacts(
[
[0, 1.7],
[0, 5.1],
],
[
[-1.7, 0],
[-5.1, 0],
],
3.4,
{ maxLegLength: 20 },
);
assert.equal(solvedContacts.status, 'solved');
assert.equal(solvedContacts.contacts.length, 3);
assert.equal(solvedContacts.greenRing.length, 7);
assert.equal(solvedContacts.legLengths.length, 2);
assert.ok(solvedContacts.legLengths.every((value) => value > 5.1));
assert.equal(new Set(solvedContacts.greenRing.slice(0, -1).map((p) => p.join(','))).size, 6);
// Regression: the saved four-node, 21m cluster previously lost two islands.
{
const fs = require('fs');
const path = require('path');
const compiler = require('../src');
const model = compiler.nativeRoad.compileRoadModel(
fs.readFileSync(path.join(__dirname, '../inputs/osm/枫树二路.osm'), 'utf8'),
{ overrides: [] },
);
const geometry = compiler.nativeRoad.compileGeometry(
model,
{ overrides: [] },
{
edgeLines: false,
junctionTemplates: {
enabled: true,
clusters: [
{
id: 'four-node-contact-regression',
template: 'complex-junction-v1',
coreRadiusMeters: 21,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
},
],
},
},
);
const outer = geometry.sidewalkSurface.features.filter((f) => f.properties.kind === 'island');
const green = geometry.sidewalkSurface.features.filter((f) => f.properties.kind === 'island-green');
const core = geometry.roadSurface.features.find((f) => f.properties.kind === 'complex-core');
assert.ok(core, 'saved four-node 21m configuration must publish center pavement');
assert.equal(core.properties.connected_entry_count, 8);
assert.equal(core.properties.required_pavement_connected, true);
assert.equal(core.properties.shared_seam_error_m, 0);
assert.equal(core.properties.core_ring_self_intersects, false);
assert.equal(outer.length, 4, 'all four corners must publish islands');
assert.equal(green.length, 4, 'all four islands must publish green contact polygons');
assert.equal(new Set(green.map((f) => f.properties.corner_key)).size, 4);
for (const feature of green) {
const points = feature.geometry.coordinates[0].slice(0, -1);
assert.equal(new Set(points.map((p) => p.join(','))).size, 6);
const island = outer.find((f) => f.properties.corner_key === feature.properties.corner_key);
assert.deepEqual(points, island.properties.contact_points);
}
assert.equal(geometry.diagnostics.filter((d) => d.rule === 'complex-junction-island-infeasible').length, 0);
}