Files
road-compiler/test/connector-trajectories.js
T

123 lines
4.3 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const fs = require('fs');
const { compileRoadModel, compileGeometry } = require('../src/compile/native-road');
function vector(a, b) {
return [(b[0] - a[0]) * Math.cos((a[1] * Math.PI) / 180), b[1] - a[1]];
}
function angle(a, b) {
return (
(Math.acos(Math.max(-1, Math.min(1, (a[0] * b[0] + a[1] * b[1]) / (Math.hypot(...a) * Math.hypot(...b))))) * 180) /
Math.PI
);
}
function check(geometry, label, clipped = false) {
const lanes = new Map(
geometry.laneCenterlines.features.map((f) => [
f.properties.native_id,
f.properties.cluster_preview && f.properties.incoming === false
? [...f.geometry.coordinates].reverse()
: f.geometry.coordinates,
]),
);
let maximum = 0;
let checked = 0;
let unavailable = 0;
for (const feature of geometry.connectors.features) {
const line = feature.geometry.coordinates;
const incoming = lanes.get(feature.properties.from_lane_id);
const outgoing = lanes.get(feature.properties.to_lane_id);
if (clipped && (!incoming || !outgoing)) {
unavailable += 1;
continue;
}
if (
clipped &&
(JSON.stringify(line[0]) !== JSON.stringify(incoming.at(-1)) ||
JSON.stringify(line.at(-1)) !== JSON.stringify(outgoing[0]))
) {
unavailable += 1;
continue;
}
checked += 1;
assert.deepEqual(line[0], incoming.at(-1));
assert.deepEqual(line.at(-1), outgoing[0]);
const error = Math.max(
angle(vector(incoming.at(-2), incoming.at(-1)), vector(line[0], line[1])),
angle(vector(outgoing[0], outgoing[1]), vector(line.at(-2), line.at(-1))),
);
maximum = Math.max(maximum, error);
assert.ok(error <= 1.01, `${label} ${feature.properties.native_id}: tangent error ${error}`);
assert.ok(line.every((p) => p.every(Number.isFinite)));
}
assert.ok(checked > 0);
console.log(
`${label}: ${checked} checked, ${unavailable} clipped/missing endpoint references, max tangent error ${maximum.toFixed(4)} deg`,
);
}
// Oblique four-arm and T nodes exercise the former quadratic and straight
// special cases using public compilation, not a duplicate curve implementation.
for (const count of [3, 4]) {
const coords = [
[0, 0],
[0, 0.001],
[0.001, 0.0001],
[0, -0.001],
[-0.001, 0],
];
const osm = `<osm>${coords.map((p, i) => `<node id="${i}" lon="${p[0]}" lat="${p[1]}"/>`).join('')}${coords
.slice(1, count + 1)
.map(
(_, i) =>
`<way id="${i + 10}"><nd ref="0"/><nd ref="${i + 1}"/><tag k="highway" v="primary"/><tag k="lanes" v="4"/></way>`,
)
.join('')}</osm>`;
check(compileGeometry(compileRoadModel(osm, { overrides: [] }), { overrides: [] }), `${count}-arm`);
}
if (process.argv[2]) {
const root = process.argv[2];
const overrides = JSON.parse(fs.readFileSync(`${root}/native-road-overrides.json`));
const options = JSON.parse(fs.readFileSync(`${root}/active/area-config.snapshot.json`)).options;
check(
compileGeometry(compileRoadModel(fs.readFileSync(`${root}/source.osm`, 'utf8'), overrides), overrides, options),
'saved workbench',
true,
);
}
// The four-node configuration must publish whole cluster movements whose
// endpoints refer to visible approach lanes, not invisible internal OSM lanes.
{
const model = compileRoadModel(
fs.readFileSync(require('path').join(__dirname, '../inputs/osm/枫树二路.osm'), 'utf8'),
{ overrides: [] },
);
const geometry = compileGeometry(
model,
{ overrides: [] },
{
edgeLines: false,
junctionTemplates: {
enabled: true,
clusters: [
{
id: 'cluster-path-regression',
template: 'complex-junction-v1',
coreRadiusMeters: 21,
nodeIds: ['8005332807', '8024512135', '8024512145', '8024512147'],
},
],
},
},
);
const paths = geometry.connectors.features.filter((f) => f.properties.cluster_id === 'cluster-path-regression');
assert.ok(paths.length > 0);
check({ ...geometry, connectors: { ...geometry.connectors, features: paths } }, 'whole cluster');
for (const path of paths) {
assert.equal(path.properties.cluster_internal, false);
assert.ok(path.properties.source_movement_ids.length > 0);
assert.ok(!path.properties.from_lane_id.includes('undefined'));
}
}