Files
road-compiler/test/junction-corridor-markings.js
T

118 lines
5.7 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const fs = require('fs');
const path = require('path');
const { compileRoadModel, compileGeometry } = require('../src/compile/native-road');
const model = compileRoadModel(fs.readFileSync(path.join(__dirname, '../inputs/osm/枫树二路.osm'), 'utf8'), {
overrides: [],
});
const clusters = [
{
id: 'west',
template: 'complex-junction-v1',
coreRadiusMeters: 21,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
},
{ id: 'east', template: 'complex-junction-v1', coreRadiusMeters: 18, nodeIds: ['1590698046', '6538106539'] },
];
const meters = (a, b) => Math.hypot((a[0] - b[0]) * 111320 * Math.cos((a[1] * Math.PI) / 180), (a[1] - b[1]) * 111320);
for (const ordered of [clusters, [...clusters].reverse()]) {
const geometry = compileGeometry(
model,
{ overrides: [] },
{ edgeLines: false, junctionTemplates: { enabled: true, clusters: ordered } },
);
for (const roadId of ['road:way/858770823:segment/2:forward', 'road:way/859509947:segment/5:forward']) {
const lanes = geometry.laneCenterlines.features.filter(
(f) => f.properties.road_id === roadId && !f.properties.cluster_preview,
);
assert.equal(lanes.length, 3);
for (const lane of lanes) {
const coordinates = lane.geometry.coordinates;
const length = coordinates.slice(1).reduce((sum, point, i) => sum + meters(coordinates[i], point), 0);
assert.ok(length > 500, `${roadId}: external lane truncated to ${length}m by the other junction`);
}
const road = model.roads.find((r) => r.id === roadId);
const midpoint = road.centerline[0].map((v, i) => (v + road.centerline.at(-1)[i]) / 2);
const complexSeparators = geometry.laneSeparators.features.filter(
(f) =>
f.properties.road_id === roadId &&
f.properties.cluster_id === 'west' &&
f.properties.provenance === 'native-road-complex-lane-separator/v1',
);
if (roadId === 'road:way/858770823:segment/2:forward') {
const core = geometry.roadSurface.features.find(
(feature) => feature.properties?.kind === 'complex-core' && feature.properties?.cluster_id === 'west',
);
if (core) {
const clusterCenter = core.properties.center;
const coreRadius = Number(core.properties.radius_m);
for (const separator of complexSeparators)
assert.ok(
separator.geometry.coordinates[0].every((point) => meters(point, clusterCenter) >= coreRadius - 0.05),
'complex separator must stop outside the calibrated west-cluster core',
);
const endpointDistances = complexSeparators
.filter((separator) => separator.properties.cluster_id === 'west')
.map((separator) => {
const ring = separator.geometry.coordinates[0];
return Math.min(...ring.map((point) => meters(point, clusterCenter)));
});
assert.ok(
Math.max(...endpointDistances) - Math.min(...endpointDistances) < 0.2,
'adjacent complex separators must share a stable core-side endpoint',
);
}
}
for (const separator of complexSeparators) {
assert.ok(
separator.geometry.coordinates[0].every((p) => meters(p, midpoint) > 180),
'junction approach separators must not be synthesized in the middle of the road',
);
}
// Separator bars must bisect adjacent published lane paths, including
// source bends; clipping at a stop must not replace that path by a chord.
const segmentDistance = (p, a, b) => {
const xy = (q) => [(q[0] - p[0]) * Math.cos((p[1] * Math.PI) / 180) * 111320, (q[1] - p[1]) * 111320];
const x = xy(a),
y = xy(b),
v = y.map((n, i) => n - x[i]);
const t = Math.max(0, Math.min(1, -(x[0] * v[0] + x[1] * v[1]) / (v[0] ** 2 + v[1] ** 2)));
return Math.hypot(x[0] + t * v[0], x[1] + t * v[1]);
};
for (const feature of geometry.laneSeparators.features.filter(
(f) => f.properties.road_id === roadId && f.properties.pattern === 'dashed',
)) {
const ring = feature.geometry.coordinates[0].slice(0, 4);
const p = [0, 1].map((axis) => ring.reduce((sum, q) => sum + q[axis], 0) / 4);
if (meters(p, midpoint) > 180) continue;
for (const index of [feature.properties.left_lane_index, feature.properties.right_lane_index]) {
const line = lanes.find((l) => l.properties.lane_index === index).geometry.coordinates;
const distance = Math.min(...line.slice(1).map((q, i) => segmentDistance(p, line[i], q)));
assert.ok(
Math.abs(distance - 1.625) < 0.12,
`separator is ${distance}m from lane ${index}, expected half lane width`,
);
}
}
const arrows = geometry.directionArrows.features.filter(
(f) => f.properties.road_id === roadId && !f.properties.cluster_preview,
);
assert.ok(arrows.length > 0);
for (const arrow of arrows) {
const line = lanes.find((l) => l.properties.lane_index === arrow.properties.lane_index).geometry.coordinates;
// The symmetric through-arrow template has its tip on the lane axis.
const tip = arrow.geometry.coordinates[0][0];
if (meters(tip, midpoint) > 180) continue;
const distance = Math.min(...line.slice(1).map((q, i) => segmentDistance(tip, line[i], q)));
assert.ok(distance < 0.05, `arrow tip is ${distance}m away from its lane centre`);
}
const centralSeparators = geometry.laneSeparators.features.filter(
(f) => f.properties.road_id === roadId && f.geometry.coordinates[0].some((p) => meters(p, midpoint) < 100),
);
assert.ok(centralSeparators.length > 20, `${roadId}: dashed separators missing in the middle of the corridor`);
}
}
console.log('junction corridor marking tests passed');