From a98d170757fe98bbfdcfbacc69cdca8fdb564740 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 9 Sep 2026 10:20:01 +0800 Subject: [PATCH] fix(junction): align complex crossing handles --- src/compile/direct-edit-solver.js | 61 +++++++++++++++++++++++++++++-- test/workbench-edit-api.js | 12 ++++++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/compile/direct-edit-solver.js b/src/compile/direct-edit-solver.js index 1ce943b..63f047f 100644 --- a/src/compile/direct-edit-solver.js +++ b/src/compile/direct-edit-solver.js @@ -14,7 +14,7 @@ const { connectionConnectorId, } = require('./native-ids'); const { complexJunctionMetrics } = require('./junction-metrics'); -const { resolveJunctionBuildPlan } = require('./junction-build-plan'); +const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('./junction-build-plan'); const RESOLUTION_SCHEMA = 'road-edit-resolution/v1'; const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1'; @@ -255,6 +255,52 @@ function clusterApproachLine(item) { return item.approach.line || []; } +function clusterCenter(model, cluster) { + const endpointsByNode = new Map(); + for (const endpoint of model.endpoints || []) { + const nodeId = String(endpoint.nodeId); + if (!endpointsByNode.has(nodeId) && Array.isArray(endpoint.coordinate)) + endpointsByNode.set(nodeId, endpoint.coordinate); + } + const points = (cluster.nodeIds || []).map((nodeId) => endpointsByNode.get(String(nodeId))).filter(Boolean); + if (!points.length) return null; + return points.reduce((sum, point) => [sum[0] + point[0] / points.length, sum[1] + point[1] / points.length], [0, 0]); +} + +function clusterRadiusPoint(line, center, radius) { + if (!center || !line.length) return null; + const length = polylineLength(line); + if (!Number.isFinite(length) || length <= 0) return line[0] || null; + const station = stationAtDistanceFrom(line, center, radius, { distanceMeters: haversineMeters }); + return coordinateAt(line, Math.max(0, Math.min(1, station / length))); +} + +function signedLateralMeters(origin, point, heading) { + const east = (point[0] - origin[0]) * 111320 * Math.cos((origin[1] * Math.PI) / 180); + const north = (point[1] - origin[1]) * 111320; + const radians = ((heading + 90) * Math.PI) / 180; + return east * Math.sin(radians) + north * Math.cos(radians); +} + +function clusterArmRadiusPoint(arm, center, radius) { + const points = arm.members + .map((member) => ({ member, point: clusterRadiusPoint(clusterApproachLine(member), center, radius) })) + .filter((item) => item.point); + if (!points.length) return null; + const reference = points[0].point; + let minimum = Infinity; + let maximum = -Infinity; + for (const { member, point } of points) { + const lateral = signedLateralMeters(reference, point, arm.heading); + const halfWidth = (Number(member.approach.widthMeters) || 0) / 2; + minimum = Math.min(minimum, lateral - halfWidth); + maximum = Math.max(maximum, lateral + halfWidth); + } + return Number.isFinite(minimum) && Number.isFinite(maximum) + ? offsetCoordinate(reference, arm.heading + 90, (minimum + maximum) / 2) + : reference; +} + function clusterArms(clusterId, items) { const byArm = new Map(); for (const item of items) { @@ -1443,6 +1489,7 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters) for (const cluster of clusters) { if (cluster.template !== 'complex-junction-v1') continue; const clusterId = String(cluster.id); + const center = clusterCenter(model, cluster); for (const arm of clusterArms(clusterId, itemsByCluster.get(clusterId) || [])) { const item = arm.representative; const line = clusterApproachLine(item); @@ -1455,14 +1502,20 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters) crossingKey, carriagewayKeys: arm.members.map((member) => member.key).sort(), }; - const pointAt = (distance) => coordinateAt(line, Math.min(0.9, Math.max(0.05, distance / length))); + // Complex crossing geometry is anchored to the core radius. Using the + // carriageway cutback here put the editor knob on a different station + // from the zebra it controls, especially for multi-node clusters. + const pointAt = (distance) => + clusterArmRadiusPoint(arm, center, distance) || + coordinateAt(line, Math.min(0.9, Math.max(0.05, distance / length))); + const baseCrosswalkDistance = Math.max(2, complexJunctionMetrics(cluster).coreRadius * 0.68); const insetConstraint = constraintFor(constraints, 'cluster-crossing-inset', anchor); const inset = numericValue(insetConstraint, 'insetMeters', 0); handles.push({ handleId: `handle:cluster-crossing-inset:${crossingKey}`, kind: 'cluster-crossing-inset', anchor, - position: pointAt(Math.max(2, item.approach.cutback * 0.68) + inset), + position: pointAt(baseCrosswalkDistance + inset), axisAzimuth: arm.heading, value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' }, ...(insetConstraint ? { constraintId: insetConstraint.id } : {}), @@ -1475,7 +1528,7 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters) handleId: `handle:cluster-stop-line-offset:${crossingKey}`, kind: 'cluster-stop-line-offset', anchor, - position: pointAt(Math.max(2, item.approach.cutback * 0.68) + inset + offset), + position: pointAt(baseCrosswalkDistance + inset + offset), axisAzimuth: arm.heading, value: { current: offset, min: MIN_STOP_LINE_OFFSET_METERS, max: 12, unit: 'meter' }, ...(stopConstraint ? { constraintId: stopConstraint.id } : {}), diff --git a/test/workbench-edit-api.js b/test/workbench-edit-api.js index 7ed9439..f4ee3eb 100644 --- a/test/workbench-edit-api.js +++ b/test/workbench-edit-api.js @@ -330,6 +330,18 @@ assert.notDeepEqual( ); const complexCrossingHandle = clusterState.handles.handles.find((handle) => handle.kind === 'cluster-crossing-inset'); assert.ok(complexCrossingHandle, 'the complex fixture must publish grouped crossing handles'); +const renderedComplexCrosswalk = clusterPreview.layers.crosswalks.features.find( + (feature) => feature.properties.crossing_key === complexCrossingHandle.anchor.crossingKey, +); +const crossingHandleLatitudeScale = 111320 * Math.cos((complexCrossingHandle.position[1] * Math.PI) / 180); +assert.ok( + Math.hypot( + (complexCrossingHandle.position[0] - renderedComplexCrosswalk.properties.frame_center[0]) * + crossingHandleLatitudeScale, + (complexCrossingHandle.position[1] - renderedComplexCrosswalk.properties.frame_center[1]) * 111320, + ) < 2, + 'the complex crossing handle must remain on the rendered crossing frame', +); const complexCrossingConstraint = { id: 'constraint:complex-crossing', kind: 'cluster-crossing-inset',