Files
road-compiler/test/junction-pavement-region.js

188 lines
8.3 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const fs = require('fs');
const clipping = require('polygon-clipping');
const { compileRoadModel, compileGeometry } = require('../src/compile/native-road');
const { solveComplexPavementRegion, segmentCoveredByPolygon } = require('../src/compile/junction-pavement-region');
const rectangle = (x1, y1, x2, y2) => [
[x1, y1],
[x2, y1],
[x2, y2],
[x1, y2],
[x1, y1],
];
const ringArea = (ring) =>
Math.abs(
ring.slice(1).reduce((sum, point, index) => sum + ring[index][0] * point[1] - point[0] * ring[index][1], 0) / 2,
);
const multiArea = (multiPolygon) =>
multiPolygon.reduce(
(sum, polygon) => sum + ringArea(polygon[0]) - polygon.slice(1).reduce((holes, hole) => holes + ringArea(hole), 0),
0,
);
const model = compileRoadModel(fs.readFileSync('./inputs/osm/枫树二路.osm', 'utf8'), { overrides: [] });
for (const [name, nodeIds] of Object.entries({
cross: ['8005332807', '8024512135', '8024512145', '8024512147'],
tee: ['8005332807', '8024512135', '8024512145'],
dividedTee: ['1590698046', '6538106539'],
})) {
const geometry = compileGeometry(
model,
{ overrides: [] },
{
edgeLines: false,
junctionTemplates: {
enabled: true,
clusters: [
{
id: name,
template: 'complex-junction-v1',
coreRadiusMeters: name === 'dividedTee' ? 18 : 21,
nodeIds,
},
],
},
},
);
const pavement = geometry.roadSurface.features.find(
(feature) => feature.properties.kind === 'complex-pavement-region',
);
assert.ok(pavement, `${name} must publish an authoritative pavement region`);
assert.equal(pavement.geometry.coordinates.length, 1, `${name} pavement must be one connected region`);
assert.equal(pavement.geometry.coordinates[0].length, 1, `${name} pavement must not expose internal holes`);
assert.equal(
geometry.diagnostics.some((item) => item.rule === 'complex-junction-pavement-region-disconnected'),
false,
);
if (name === 'dividedTee')
assert.ok(
geometry.roadSurface.features.some((feature) => feature.properties.kind === 'complex-core'),
'dividedTee must publish a closed complex core',
);
// The region owns road-arm crossings. The island-hypotenuse crossing is a
// pedestrian facility above the road layer and does not expand pavement.
for (const stripe of geometry.crosswalks.features.filter(
(feature) => feature.properties.cluster_id === name && feature.properties.kind === 'complex-crosswalk',
))
assert.ok(
multiArea(clipping.difference(stripe.geometry.coordinates, pavement.geometry.coordinates)) < 1e-10,
`${name} ${stripe.properties.native_id} must lie on the unified pavement`,
);
}
// A valid five-arm corner-radius edit used to let polygon-clipping throw while
// assembling connector pavement, aborting the entire preview. It must instead
// retain the changed structural pavement and defer only unverifiable routes.
{
const cluster = {
id: 'five-arm-pavement-assembly',
template: 'complex-junction-v1',
coreRadiusMeters: 32,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147', '8613058676'],
};
const options = { edgeLines: false, junctionTemplates: { enabled: true, clusters: [cluster] } };
const baseline = compileGeometry(model, { overrides: [] }, options);
const cornerKeys = baseline.roadSurface.features
.filter((feature) => feature.properties.kind === 'complex-corner-fillet')
.map((feature) => feature.properties.corner_key);
const edited = compileGeometry(
model,
{ overrides: [] },
{
...options,
directEdit: { clusterCorners: new Map([[cluster.id, new Map(cornerKeys.map((key) => [key, 14]))]]) },
},
);
assert.ok(edited.roadSurface.features.some((feature) => feature.properties.kind === 'complex-corner-fillet'));
}
// Construction gaps are filled; explicit exclusions are subtracted afterwards,
// including when a crossing footprint originally covers the same obstacle.
{
const parts = [rectangle(0, 0, 10, 2), rectangle(0, 8, 10, 10), rectangle(0, 0, 2, 10), rectangle(8, 0, 10, 10)];
assert.equal(multiArea(solveComplexPavementRegion(parts).regions), 100);
const holes = [rectangle(3, 3, 4, 4), rectangle(6, 6, 7, 7)];
const result = solveComplexPavementRegion(parts, [rectangle(2, 2, 8, 8)], holes);
assert.equal(result.status, 'supported');
assert.equal(result.regions[0].length, 3);
assert.equal(multiArea(result.regions), 98);
const overlapping = solveComplexPavementRegion(parts, [], [holes[0], rectangle(3.5, 3, 4.5, 4)]);
assert.equal(multiArea(overlapping.regions), 98.5);
assert.equal(solveComplexPavementRegion(parts, [], [rectangle(-1, -1, 11, 11)]).status, 'empty');
assert.equal(solveComplexPavementRegion(parts, [], [rectangle(4, -1, 6, 11)]).status, 'disconnected');
const notch = solveComplexPavementRegion(parts, [], [rectangle(4, -1, 6, 1)]);
assert.equal(notch.status, 'supported');
assert.equal(multiArea(notch.regions), 98);
assert.equal(
segmentCoveredByPolygon([2, 3.5], [8, 3.5], result.regions[0]),
false,
'interior hole must break seam coverage',
);
assert.equal(segmentCoveredByPolygon([0, 0], [10, 0], result.regions[0]), true);
assert.equal(
segmentCoveredByPolygon([0, 0], [10, 0], notch.regions[0]),
false,
'interior notch must break full seam coverage',
);
}
// Measure final published OSM pavement in local metres, independently of the
// construction solver. Core holes alone did not catch the original defect.
{
const cluster = {
id: 'exclusion-pavement',
template: 'complex-junction-v1',
coreRadiusMeters: 21,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
};
const options = { junctionTemplates: { enabled: true, clusters: [cluster] } };
const compile = (exclusionRegions = []) =>
compileGeometry({ ...model, exclusionRegions }, { overrides: [] }, options);
const pavementOf = (geometry) =>
geometry.roadSurface.features.find((feature) => feature.properties.kind === 'complex-pavement-region');
const baseline = compile();
const origin = pavementOf(baseline).properties.center;
const scale = 111320 * Math.cos((origin[1] * Math.PI) / 180);
const ll = ([x, y]) => [origin[0] + x / scale, origin[1] + y / 111320];
const xy = ([x, y]) => [(x - origin[0]) * scale, (y - origin[1]) * 111320];
const mask = (id, bounds) => ({ id, isHole: true, ring: rectangle(...bounds).map(ll) });
for (const exclusions of [
[mask('one-square-metre', [-0.5, -0.5, 0.5, 0.5])],
[mask('first-hole', [-1.5, -0.5, -0.5, 0.5]), mask('second-hole', [0.5, -0.5, 1.5, 0.5])],
[mask('overlap-a', [-0.5, -0.5, 0.5, 0.5]), mask('overlap-b', [0, -0.5, 1, 0.5])],
]) {
const published = pavementOf(compile(exclusions));
assert.ok(published, 'safe interior exclusions must retain a canonical pavement');
assert.ok(published.geometry.coordinates[0].length > 1, 'canonical pavement must retain explicit holes');
const pavementMeters = published.geometry.coordinates.map((polygon) => polygon.map((ring) => ring.map(xy)));
for (const exclusion of exclusions)
assert.ok(
multiArea(clipping.intersection(pavementMeters, [[exclusion.ring.map(xy)]])) < 1e-6,
`${exclusion.id} must remain unpaved`,
);
}
const approach = baseline.roadSurface.features.find((feature) => feature.properties.kind === 'complex-approach');
for (const exclusion of [
mask('fully-covered', [-200, -200, 200, 200]),
mask('split-region', [-0.5, -100, 0.5, 100]),
{ id: 'blocked-entry', ring: approach.geometry.coordinates[0] },
]) {
const result = compile([exclusion]);
assert.equal(pavementOf(result), undefined, `${exclusion.id} must refuse unified publication`);
const diagnostic = result.diagnostics.find((entry) => entry.rule.startsWith('complex-junction-pavement-region-'));
assert.ok(diagnostic, `${exclusion.id} requires a pavement rejection diagnostic`);
assert.deepEqual(diagnostic.exclusion_region_ids, [exclusion.id]);
assert.equal(diagnostic.subjectId, `junction-cluster:${cluster.id}`);
assert.equal(
result.roadSurface.features.some((feature) => feature.properties.pavement_input),
false,
);
if (exclusion.id === 'blocked-entry')
assert.ok(diagnostic.carriageway_keys.length, 'omitted required entry must be identified');
}
}
console.log('junction pavement region tests passed');