159 lines
6.9 KiB
JavaScript
159 lines
6.9 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]]) },
|
|
},
|
|
);
|
|
// Check the complete crossing envelope at intermediate positions, not only
|
|
// stripe coverage at the final position. This catches the exposed road gap.
|
|
const clipping = require('polygon-clipping');
|
|
const toMeters = ([x, y]) => [(x - 114) * 111320 * Math.cos((30 * Math.PI) / 180), (y - 30) * 111320];
|
|
const area = (regions) =>
|
|
regions.reduce(
|
|
(total, polygon) =>
|
|
total +
|
|
polygon.reduce((sum, ring, index) => {
|
|
const value =
|
|
Math.abs(ring.slice(1).reduce((acc, p, i) => acc + ring[i][0] * p[1] - p[0] * ring[i][1], 0)) / 2;
|
|
return sum + (index ? -value : value);
|
|
}, 0),
|
|
0,
|
|
);
|
|
const groups = (geometry) => {
|
|
const result = new Map();
|
|
for (const feature of geometry.crosswalks.features) {
|
|
if (feature.properties.kind !== 'complex-crosswalk') continue;
|
|
const key = feature.properties.arm_key;
|
|
const stripes = result.get(key) || [];
|
|
stripes.push(feature.geometry.coordinates[0]);
|
|
result.set(key, stripes);
|
|
}
|
|
return new Map(
|
|
[...result].map(([key, stripes]) => {
|
|
const first = stripes[0],
|
|
last = stripes.at(-1);
|
|
return [key, [first[0], first[1], last[2], last[3], first[0]].map(toMeters)];
|
|
}),
|
|
);
|
|
};
|
|
const originalGroups = groups(base);
|
|
for (const offset of [3.3, 8]) {
|
|
const moved = compileGeometry(
|
|
model,
|
|
{ overrides: [] },
|
|
{
|
|
...options,
|
|
directEdit: { clusterIslandOffsets: new Map([[cluster.id, offset]]) },
|
|
},
|
|
);
|
|
const pavement = moved.roadSurface.features.find((f) => f.properties.kind === 'complex-pavement-region');
|
|
assert.ok(pavement, `offset ${offset}: must publish unified pavement`);
|
|
const regions = pavement.geometry.coordinates.map((polygon) => polygon.map((ring) => ring.map(toMeters)));
|
|
for (const [key, end] of groups(moved)) {
|
|
const start = originalGroups.get(key);
|
|
for (const ratio of [0, 0.25, 0.5, 0.75, 1]) {
|
|
const envelope = start.map((point, i) => point.map((v, axis) => v + (end[i][axis] - v) * ratio));
|
|
const missing = area(clipping.difference([envelope], regions));
|
|
assert.ok(missing < 0.001, `offset ${offset}, arm ${key}, sweep ${ratio}: ${missing} m² unpaved`);
|
|
}
|
|
}
|
|
}
|
|
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');
|