diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/artifacts/corridor-markings-fixed.svg b/.trellis/tasks/09-08-junction-reference-fidelity/artifacts/corridor-markings-fixed.svg new file mode 100644 index 0000000..934c611 --- /dev/null +++ b/.trellis/tasks/09-08-junction-reference-fidelity/artifacts/corridor-markings-fixed.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/package.json b/package.json index 771735a..ad09bfa 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "road-compiler": "bin/road-compiler.js" }, "scripts": { - "test": "node test/index.js && node test/junction-structure.js && node test/connector-trajectories.js && node test/junction-projection.js && node test/junction-build-plan-counterexamples.js && node test/junction-polygon-ops.js && node test/junction-region-solver.js && node test/junction-facility-composition.js && node test/junction-pavement-region.js && node test/junction-marking-containment.js && node test/native-road-edits.js && node test/road-revisions.js && node test/direct-edit-solver.js && node test/workbench-edit-api.js && node test/workbench-import-compile.js && node test/fixtures.js", + "test": "node test/index.js && node test/junction-structure.js && node test/connector-trajectories.js && node test/junction-projection.js && node test/junction-build-plan-counterexamples.js && node test/junction-polygon-ops.js && node test/junction-region-solver.js && node test/junction-facility-composition.js && node test/junction-pavement-region.js && node test/junction-marking-containment.js && node test/junction-corridor-markings.js && node test/native-road-edits.js && node test/road-revisions.js && node test/direct-edit-solver.js && node test/workbench-edit-api.js && node test/workbench-import-compile.js && node test/fixtures.js", "test:fixtures:update-baseline": "node test/update-fixture-baselines.js", "road:workbench": "node bin/road-workbench.js", "road:export": "node bin/road-compiler.js", diff --git a/src/compile/native-road.js b/src/compile/native-road.js index 2263842..a039f17 100644 --- a/src/compile/native-road.js +++ b/src/compile/native-road.js @@ -1126,11 +1126,13 @@ function compileComplexLaneSeparators(features, controls = []) { const groups = new Map(); for (const feature of features) { if (!feature.properties?.cluster_preview || !feature.properties.road_id) continue; - if (!groups.has(feature.properties.road_id)) groups.set(feature.properties.road_id, []); - groups.get(feature.properties.road_id).push(feature); + const key = JSON.stringify([feature.properties.cluster_id, feature.properties.road_id]); + if (!groups.has(key)) groups.set(key, []); + groups.get(key).push(feature); } const result = []; - for (const [roadId, lanes] of groups) { + for (const lanes of groups.values()) { + const roadId = lanes[0].properties.road_id; lanes.sort((first, second) => first.properties.lane_index - second.properties.lane_index); for (let index = 1; index < lanes.length; index += 1) { const left = lanes[index - 1].geometry.coordinates; @@ -1150,7 +1152,7 @@ function compileComplexLaneSeparators(features, controls = []) { result.push({ type: 'Feature', properties: { - native_id: `complex-lane-separator:${roadId}:${index}-${index + 1}`, + native_id: `complex-lane-separator:${lanes[0].properties.cluster_id}:${roadId}:${index}-${index + 1}`, road_id: roadId, cluster_id: lanes[0].properties.cluster_id, left_lane_index: index, @@ -1192,8 +1194,9 @@ function trimLineBeforeFirstControl(line, controls, width) { if (!overlaps(ring)) continue; let cutoff = Math.max(0, distance - step - 0.12); while (cutoff > 0.5) { - const candidate = roadRing([line[0], pointAlongLine(line, cutoff)], width); - if (candidate && !overlaps(candidate)) return [line[0], pointAlongLine(line, cutoff)]; + const trimmed = trimLineRange(line, 0, total - cutoff); + const candidate = roadRing(trimmed, width); + if (candidate && !overlaps(candidate)) return trimmed; cutoff -= 0.25; } return null; @@ -2357,7 +2360,10 @@ function compileLaneCenterlines( boundaryCluster?.template === 'complex-junction-v1' ? trimLaneOutsideControls( publishedCoordinates, - controlFeatures, + // A road can join two distinct complex junctions. A control at + // the far junction must not truncate the whole inter-junction + // corridor while scanning toward this junction. + controlFeatures.filter((feature) => feature.properties.cluster_id === boundaryCluster.id), LANE_CENTERLINE_PROBE_WIDTH_METERS, clusterCenters.get(boundaryCluster.id), ) diff --git a/test/junction-corridor-markings.js b/test/junction-corridor-markings.js new file mode 100644 index 0000000..de69a31 --- /dev/null +++ b/test/junction-corridor-markings.js @@ -0,0 +1,90 @@ +'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.provenance === 'native-road-complex-lane-separator/v1', + ); + 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');