344 lines
12 KiB
JavaScript
344 lines
12 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 { auditJunctionSeams, LAYERS, measureSegment } = require('./helpers/junction-seams');
|
|
|
|
const overrides = { overrides: [] };
|
|
function compile(name, model, clusters, extra = {}, styles = overrides) {
|
|
const geometry = compileGeometry(model, styles, { junctionTemplates: { enabled: true, clusters }, ...extra });
|
|
const structures = geometry.roadSurface.features.map((f) => f.properties.junction_structure).filter(Boolean);
|
|
assert.equal(structures.length, clusters.length, `${name}: missing junction structure`);
|
|
const report = auditJunctionSeams(model, geometry, structures);
|
|
return { name, model, clusters, geometry, structures, report };
|
|
}
|
|
|
|
function synthetic(curved = false, twoWay = false, adjacent = false) {
|
|
const nodes = [
|
|
[-5, 0],
|
|
[240, 0],
|
|
[5, 240],
|
|
[-240, 0],
|
|
[-5, -240],
|
|
[100, curved ? 12 : 0],
|
|
[5, 0],
|
|
];
|
|
if (adjacent) nodes.push([250, 0], [240, 240], [500, 0], [250, -240]);
|
|
const ways = [2, 3, 4, 5].map((n) => [n, [n <= 3 ? 7 : 1, ...(n === 2 ? [6] : []), n]]);
|
|
if (adjacent) ways.push([9, [2, 9]], [10, [8, 10]], [11, [8, 11]]);
|
|
const internal = [[1, 7], ...(adjacent ? [[2, 8]] : [])];
|
|
const osm = `<osm>${nodes.map(([x, y], i) => `<node id="${i + 1}" lon="${120 + x / (111320 * Math.cos(Math.PI / 6))}" lat="${30 + y / 111320}"/>`).join('')}${ways.map(([id, refs]) => `<way id="${id}">${refs.map((ref) => `<nd ref="${ref}"/>`).join('')}<tag k="highway" v="primary"/><tag k="lanes" v="${twoWay ? 4 : 3}"/>${twoWay ? '' : '<tag k="oneway" v="yes"/>'}<tag k="sidewalk" v="both"/></way>`).join('')}${internal.map((refs, i) => `<way id="internal${i}">${refs.map((ref) => `<nd ref="${ref}"/>`).join('')}<tag k="highway" v="primary"/></way>`).join('')}</osm>`;
|
|
return compileRoadModel(osm, overrides);
|
|
}
|
|
|
|
const cluster = { id: 'synthetic', template: 'complex-junction-v1', coreRadiusMeters: 21, nodeIds: ['1', '7'] };
|
|
const realModel = compileRoadModel(
|
|
fs.readFileSync(path.join(__dirname, '../inputs/osm/枫树二路.osm'), 'utf8'),
|
|
overrides,
|
|
);
|
|
const west = {
|
|
id: 'west',
|
|
template: 'complex-junction-v1',
|
|
coreRadiusMeters: 21,
|
|
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
|
|
};
|
|
const east = {
|
|
id: 'east',
|
|
template: 'complex-junction-v1',
|
|
coreRadiusMeters: 18,
|
|
nodeIds: ['1590698046', '6538106539'],
|
|
};
|
|
const scenarios = [
|
|
compile('straight', synthetic(), [cluster]),
|
|
compile('curved', synthetic(true), [cluster]),
|
|
compile('two-way', synthetic(false, true), [cluster]),
|
|
compile('two-way-curved', synthetic(true, true), [cluster]),
|
|
compile('real-osm', realModel, [west]),
|
|
compile('adjacent', realModel, [west, east]),
|
|
compile(
|
|
'sidewalks',
|
|
{ ...realModel, roads: realModel.roads.map((r) => ({ ...r, sidewalkLeft: true, sidewalkRight: true })) },
|
|
[west],
|
|
),
|
|
];
|
|
scenarios.push(
|
|
compile('two-way-adjacent', synthetic(true, true, true), [
|
|
cluster,
|
|
{ ...cluster, id: 'synthetic-east', nodeIds: ['2', '8'] },
|
|
]),
|
|
);
|
|
const curvedTwoWay = scenarios.find((s) => s.name === 'two-way-curved');
|
|
const twoWayApproach = curvedTwoWay.geometry.roadSurface.features.find((f) => f.properties.kind === 'complex-approach');
|
|
scenarios.push(
|
|
compile('two-way-width-edit', curvedTwoWay.model, [cluster], {
|
|
directEdit: {
|
|
clusterPlans: new Map([
|
|
[cluster.id, new Map([[twoWayApproach.properties.carriageway_key, twoWayApproach.properties.width_m * 1.2]])],
|
|
]),
|
|
},
|
|
}),
|
|
);
|
|
|
|
const curved = scenarios[1];
|
|
const approach = curved.geometry.roadSurface.features.find((f) => f.properties.kind === 'complex-approach');
|
|
scenarios.push(
|
|
compile('width-edit', curved.model, [cluster], {
|
|
directEdit: {
|
|
clusterPlans: new Map([
|
|
[cluster.id, new Map([[approach.properties.carriageway_key, approach.properties.width_m * 1.2]])],
|
|
]),
|
|
},
|
|
}),
|
|
);
|
|
scenarios.push(
|
|
compile('island-edit', realModel, [west], { directEdit: { clusterIslandOffsets: new Map([[west.id, 3.3]]) } }),
|
|
);
|
|
scenarios.push(compile('adjacent-reordered', { ...realModel, roads: [...realModel.roads].reverse() }, [east, west]));
|
|
scenarios.push(
|
|
compile(
|
|
'dashed-edges',
|
|
synthetic(true, true),
|
|
[cluster],
|
|
{},
|
|
{
|
|
overrides: synthetic(true, true).roads.flatMap((r) =>
|
|
['left', 'right'].map((side) => ({
|
|
kind: 'edge-line-style',
|
|
roadId: r.id,
|
|
side,
|
|
color: 'white',
|
|
pattern: 'dashed',
|
|
})),
|
|
),
|
|
},
|
|
),
|
|
);
|
|
|
|
const expectedCoverage = {
|
|
straight: [8, 4, 8, 0, 24, 16, 4],
|
|
curved: [8, 4, 8, 0, 24, 16, 4],
|
|
'two-way': [8, 4, 8, 4, 32, 20, 8],
|
|
'two-way-curved': [8, 4, 8, 4, 32, 20, 8],
|
|
'real-osm': [16, 8, 16, 0, 48, 32, 0],
|
|
adjacent: [26, 13, 26, 1, 76, 49, 0],
|
|
sidewalks: [16, 8, 16, 0, 48, 32, 8],
|
|
'width-edit': [8, 4, 8, 0, 24, 16, 4],
|
|
'island-edit': [16, 8, 16, 0, 48, 32, 0],
|
|
'adjacent-reordered': [26, 13, 26, 1, 76, 49, 0],
|
|
'dashed-edges': [8, 4, 8, 4, 32, 20, 8],
|
|
'two-way-adjacent': [16, 8, 16, 8, 64, 40, 16],
|
|
'two-way-width-edit': [8, 4, 8, 4, 32, 20, 8],
|
|
};
|
|
for (const { name, report } of scenarios) {
|
|
assert.deepEqual(
|
|
LAYERS.map((layer) => report[layer].checked),
|
|
expectedCoverage[name],
|
|
`${name}: coverage changed`,
|
|
);
|
|
for (const layer of LAYERS) {
|
|
assert.equal(
|
|
report[layer].failed,
|
|
0,
|
|
`${name}/${layer}: ${JSON.stringify(report[layer].records.filter((r) => r.status === 'failed'))}`,
|
|
);
|
|
assert.ok(report[layer].records.every((r) => r.status !== 'not-applicable' || r.reason));
|
|
}
|
|
}
|
|
for (const layer of LAYERS)
|
|
assert.ok(
|
|
scenarios.some((s) => s.report[layer].checked > 0),
|
|
`${layer}: empty audit`,
|
|
);
|
|
|
|
// Reordering must preserve expected source identities, not just pass counts.
|
|
function identities(report) {
|
|
return Object.fromEntries(
|
|
LAYERS.map((layer) => [
|
|
layer,
|
|
report[layer].records
|
|
.map((r) =>
|
|
JSON.stringify({
|
|
...r,
|
|
featureIds: undefined,
|
|
errorMeters: undefined,
|
|
uncoveredMeters: undefined,
|
|
gapMeters: undefined,
|
|
roadIds: [...r.roadIds].sort(),
|
|
}),
|
|
)
|
|
.sort(),
|
|
]),
|
|
);
|
|
}
|
|
assert.deepEqual(
|
|
identities(scenarios.find((s) => s.name === 'adjacent').report),
|
|
identities(scenarios.find((s) => s.name === 'adjacent-reordered').report),
|
|
);
|
|
|
|
// Corrupt published outputs while retaining the independent expected topology.
|
|
const base = scenarios.find((s) => s.name === 'two-way');
|
|
function detects(name, layer, mutate, scenario = base) {
|
|
const geometry = structuredClone(scenario.geometry);
|
|
mutate(geometry);
|
|
const result = auditJunctionSeams(scenario.model, geometry, scenario.structures);
|
|
assert.ok(result[layer].failed > 0, `${name}: audit silently accepted damaged ${layer}`);
|
|
}
|
|
const shift = (coordinates, dx, dy) =>
|
|
typeof coordinates[0] === 'number'
|
|
? [coordinates[0] + dx / (111320 * Math.cos((coordinates[1] * Math.PI) / 180)), coordinates[1] + dy / 111320]
|
|
: coordinates.map((value) => shift(value, dx, dy));
|
|
const move = (feature) => {
|
|
feature.geometry.coordinates = shift(feature.geometry.coordinates, 0.02, 0);
|
|
};
|
|
for (const [layer, predicate, auditLayer = layer] of [
|
|
['roadSurface', (f) => Boolean(f.properties.directional_road_ids)],
|
|
['roadSurface', (f) => f.properties.kind === 'complex-core', 'coreApproach'],
|
|
['roadSurface', (f) => f.properties.kind === 'complex-approach'],
|
|
['edgeLines', () => true],
|
|
['centerLines', () => true],
|
|
['laneCenterlines', (f) => !f.properties.cluster_preview],
|
|
['laneSeparators', (f) => f.properties.provenance === 'native-road-complex-lane-separator/v1'],
|
|
['sidewalkSurface', (f) => f.properties.kind === 'complex-sidewalk-corner'],
|
|
]) {
|
|
detects('missing counterpart', auditLayer, (g) => {
|
|
g[layer].features = g[layer].features.filter((f) => !predicate(f));
|
|
});
|
|
detects('one missing counterpart', auditLayer, (g) => {
|
|
const index = g[layer].features.findIndex(predicate);
|
|
assert.ok(index >= 0);
|
|
g[layer].features.splice(index, 1);
|
|
});
|
|
detects('2 cm displacement', auditLayer, (g) => g[layer].features.filter(predicate).forEach(move));
|
|
}
|
|
detects('wrong preview source', 'laneCenterlines', (g) => {
|
|
g.laneCenterlines.features.find((f) => f.properties.cluster_preview).properties.road_id = 'wrong-road';
|
|
});
|
|
detects('wrong centre primary source', 'centerLines', (g) => {
|
|
for (const f of g.centerLines.features) f.properties.road_id = 'wrong-road';
|
|
});
|
|
detects('partial centre source match', 'centerLines', (g) => {
|
|
for (const f of g.centerLines.features) f.properties.directional_road_ids = `${f.properties.road_id},wrong-road`;
|
|
});
|
|
for (const layer of ['edgeLines', 'centerLines', 'laneCenterlines', 'laneSeparators']) {
|
|
detects('wrong source', layer, (g) => {
|
|
for (const f of g[layer].features) {
|
|
f.properties.road_id = 'wrong-road';
|
|
if (f.properties.directional_road_ids) f.properties.directional_road_ids = 'wrong-road';
|
|
}
|
|
});
|
|
}
|
|
detects('swapped lane indices', 'laneCenterlines', (g) => {
|
|
for (const f of g.laneCenterlines.features.filter((f) => !f.properties.cluster_preview))
|
|
f.properties.lane_index = 3 - f.properties.lane_index;
|
|
});
|
|
detects('wrong surface source', 'roadSurface', (g) => {
|
|
for (const f of g.roadSurface.features.filter((f) => f.properties.directional_road_ids))
|
|
f.properties.directional_road_ids = 'wrong-road';
|
|
});
|
|
detects('missing sidewalk strip', 'sidewalkSurface', (g) => {
|
|
g.sidewalkSurface.features = g.sidewalkSurface.features.filter(
|
|
(f) => f.properties.provenance !== 'native-road-sidewalk/v1',
|
|
);
|
|
});
|
|
detects('wrong sidewalk source', 'sidewalkSurface', (g) => {
|
|
for (const f of g.sidewalkSurface.features.filter((f) => f.properties.provenance === 'native-road-sidewalk/v1'))
|
|
f.properties.directional_road_ids = 'wrong-road';
|
|
});
|
|
detects(
|
|
'displaced edge dashes',
|
|
'edgeLines',
|
|
(g) => g.edgeLines.features.forEach(move),
|
|
scenarios.find((s) => s.name === 'dashed-edges'),
|
|
);
|
|
detects(
|
|
'missing anchored edge dash',
|
|
'edgeLines',
|
|
(g) => g.edgeLines.features.shift(),
|
|
scenarios.find((s) => s.name === 'dashed-edges'),
|
|
);
|
|
detects('missing exterior separators', 'laneSeparators', (g) => {
|
|
g.laneSeparators.features = g.laneSeparators.features.filter(
|
|
(f) => f.properties.provenance !== 'native-road-lane-separator/v1',
|
|
);
|
|
});
|
|
detects('displaced exterior separators', 'laneSeparators', (g) =>
|
|
g.laneSeparators.features.filter((f) => f.properties.provenance === 'native-road-lane-separator/v1').forEach(move),
|
|
);
|
|
|
|
// A 2 cm notch away from all endpoint/quarter/midpoint probes must fail.
|
|
const metric = ([x, y]) => [120 + x / (111320 * Math.cos(Math.PI / 6)), 30 + y / 111320];
|
|
const polygon = (ring) => ({ geometry: { type: 'Polygon', coordinates: [ring.map(metric)] } });
|
|
const full = polygon([
|
|
[0, 0],
|
|
[10, 0],
|
|
[10, 2],
|
|
[0, 2],
|
|
[0, 0],
|
|
]);
|
|
const notch = polygon([
|
|
[0, 0],
|
|
[3.11, 0],
|
|
[3.11, 0.1],
|
|
[3.13, 0.1],
|
|
[3.13, 0],
|
|
[10, 0],
|
|
[10, 2],
|
|
[0, 2],
|
|
[0, 0],
|
|
]);
|
|
for (const boundaryOnly of [true, false]) {
|
|
assert.ok(measureSegment(metric([0, 0]), metric([10, 0]), full, boundaryOnly).uncoveredMeters < 0.01);
|
|
assert.ok(measureSegment(metric([0, 0]), metric([10, 0]), notch, boundaryOnly).uncoveredMeters > 0.019);
|
|
}
|
|
const holed = polygon([
|
|
[0, -1],
|
|
[10, -1],
|
|
[10, 2],
|
|
[0, 2],
|
|
[0, -1],
|
|
]);
|
|
holed.geometry.coordinates.push(
|
|
[
|
|
[3.11, -0.1],
|
|
[3.13, -0.1],
|
|
[3.13, 0.1],
|
|
[3.11, 0.1],
|
|
[3.11, -0.1],
|
|
].map(metric),
|
|
);
|
|
assert.ok(measureSegment(metric([0, 0]), metric([10, 0]), holed, false).uncoveredMeters > 0.019);
|
|
|
|
const summary = scenarios.map(({ name, report }) => ({
|
|
name,
|
|
layers: Object.fromEntries(
|
|
LAYERS.map((layer) => {
|
|
const { checked, failed, notApplicable, records } = report[layer];
|
|
return [
|
|
layer,
|
|
{
|
|
checked,
|
|
failed,
|
|
notApplicable,
|
|
maxErrorMeters: Math.max(0, ...records.map((r) => r.errorMeters || 0)),
|
|
maxUncoveredMeters: Math.max(0, ...records.map((r) => r.uncoveredMeters || 0)),
|
|
},
|
|
];
|
|
}),
|
|
),
|
|
}));
|
|
const reportIndex = process.argv.indexOf('--report');
|
|
if (reportIndex >= 0)
|
|
fs.writeFileSync(
|
|
process.argv[reportIndex + 1],
|
|
JSON.stringify(
|
|
{ toleranceMeters: 0.01, summary, scenarios: scenarios.map(({ name, report }) => ({ name, report })) },
|
|
null,
|
|
2,
|
|
) + '\n',
|
|
);
|
|
console.log(
|
|
`junction layer seam tests passed (${scenarios.length} scenarios, ${summary.reduce((sum, s) => sum + Object.values(s.layers).reduce((n, l) => n + l.checked, 0), 0)} measured joins)`,
|
|
);
|