Files
road-compiler/test/junction-island-offset.js
T

105 lines
4.7 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const { radialShift } = require('../src/compile/junction-island-offset');
const center = [114, 30];
const islandCenter = [114.0001, 30.0001];
const shift = radialShift(center, islandCenter, 2);
assert.ok(Math.hypot(shift[0] * 111320 * Math.cos((center[1] * Math.PI) / 180), shift[1] * 111320) > 1.99);
// Real four-arm integration: the compiler receives one cluster value but moves
// all four islands and their linked arm crossings in the same compilation pass.
{
const fs = require('fs');
const path = require('path');
const { compileRoadModel, compileGeometry } = require('../src/compile/native-road');
const cluster = {
id: 'offset-four-arm',
template: 'complex-junction-v1',
coreRadiusMeters: 21,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
};
const model = compileRoadModel(fs.readFileSync(path.join(__dirname, '../inputs/osm/枫树二路.osm'), 'utf8'), {
overrides: [],
});
const options = { edgeLines: false, junctionTemplates: { enabled: true, clusters: [cluster] } };
const base = compileGeometry(model, { overrides: [] }, options);
const shifted = compileGeometry(
model,
{ overrides: [] },
{
...options,
directEdit: { clusterIslandOffsets: new Map([[cluster.id, 2]]) },
},
);
const byKind = (geometry, kind) =>
geometry.sidewalkSurface.features.filter((feature) => feature.properties.kind === kind);
const baseIslands = byKind(base, 'island');
const shiftedIslands = byKind(shifted, 'island');
assert.equal(baseIslands.length, 4);
assert.equal(shiftedIslands.length, 4);
for (const island of shiftedIslands) {
const original = baseIslands.find((feature) => feature.properties.corner_key === island.properties.corner_key);
assert.notDeepEqual(island.geometry.coordinates, original.geometry.coordinates, 'each island must move');
}
const baseCrosswalks = base.crosswalks.features.filter((feature) => feature.properties.kind === 'complex-crosswalk');
const shiftedCrosswalks = shifted.crosswalks.features.filter(
(feature) => feature.properties.kind === 'complex-crosswalk',
);
assert.equal(shiftedCrosswalks.length, baseCrosswalks.length);
assert.ok(
shiftedCrosswalks.some(
(feature, index) =>
JSON.stringify(feature.geometry.coordinates) !== JSON.stringify(baseCrosswalks[index].geometry.coordinates),
),
'linked arm crossings must be rebuilt with the islands',
);
const baseStops = base.vehicleStopLines.features.filter((feature) => feature.properties.kind === 'complex-stop-line');
const shiftedStops = shifted.vehicleStopLines.features.filter(
(feature) => feature.properties.kind === 'complex-stop-line',
);
assert.ok(
shiftedStops.some(
(feature, index) =>
JSON.stringify(feature.geometry.coordinates) !== JSON.stringify(baseStops[index].geometry.coordinates),
),
'stop lines must retain their spacing from moved arm crossings',
);
const allStripeVertices = shifted.crosswalks.features.flatMap((feature) => feature.geometry.coordinates[0]);
const metersBetween = (a, b) =>
Math.hypot((a[0] - b[0]) * 111320 * Math.cos((a[1] * Math.PI) / 180), (a[1] - b[1]) * 111320);
for (const green of byKind(shifted, 'island-green')) {
for (const contactPoint of green.geometry.coordinates[0].slice(0, -1))
assert.ok(
Math.min(...allStripeVertices.map((vertex) => metersBetween(contactPoint, vertex))) < 0.01,
'each green contact endpoint must remain on a linked crossing after the offset',
);
}
const local = (point) => [
(point[0] - 114.1229) * 111320 * Math.cos((point[1] * Math.PI) / 180),
(point[1] - 30.4629) * 111320,
];
const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
const properIntersection = (a, b, c, d) => {
const ab = cross(a, b, c) * cross(a, b, d);
const cd = cross(c, d, a) * cross(c, d, b);
return ab < -1e-8 && cd < -1e-8;
};
const islandEdges = shiftedIslands.flatMap((island) =>
island.geometry.coordinates[0]
.slice(1)
.map((point, index) => [local(island.geometry.coordinates[0][index]), local(point)]),
);
const laneSegments = shifted.laneCenterlines.features.flatMap((feature) => {
const coordinates = feature.geometry.type === 'LineString' ? feature.geometry.coordinates : [];
return coordinates.slice(1).map((point, index) => [local(coordinates[index]), local(point)]);
});
assert.equal(
laneSegments.some(([a, b]) => islandEdges.some(([c, d]) => properIntersection(a, b, c, d))),
false,
'the shifted island boundary must stay outside vehicle lane centre lines',
);
}
console.log('junction island offset tests passed');