fix(junction): align complex crossing handles

This commit is contained in:
que01 committed 2026-09-09 10:20:01 +08:00
1 parent 48fd432266
commit a98d170757
2 files changed
+69 -4

No files matched your search

+57 -4
View File
@@ -14,7 +14,7 @@ const {
connectionConnectorId, connectionConnectorId,
} = require('./native-ids'); } = require('./native-ids');
const { complexJunctionMetrics } = require('./junction-metrics'); 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 RESOLUTION_SCHEMA = 'road-edit-resolution/v1';
const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1'; const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1';
@@ -255,6 +255,52 @@ function clusterApproachLine(item) {
return item.approach.line || []; 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) { function clusterArms(clusterId, items) {
const byArm = new Map(); const byArm = new Map();
for (const item of items) { for (const item of items) {
@@ -1443,6 +1489,7 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters)
for (const cluster of clusters) { for (const cluster of clusters) {
if (cluster.template !== 'complex-junction-v1') continue; if (cluster.template !== 'complex-junction-v1') continue;
const clusterId = String(cluster.id); const clusterId = String(cluster.id);
const center = clusterCenter(model, cluster);
for (const arm of clusterArms(clusterId, itemsByCluster.get(clusterId) || [])) { for (const arm of clusterArms(clusterId, itemsByCluster.get(clusterId) || [])) {
const item = arm.representative; const item = arm.representative;
const line = clusterApproachLine(item); const line = clusterApproachLine(item);
@@ -1455,14 +1502,20 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters)
crossingKey, crossingKey,
carriagewayKeys: arm.members.map((member) => member.key).sort(), 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 insetConstraint = constraintFor(constraints, 'cluster-crossing-inset', anchor);
const inset = numericValue(insetConstraint, 'insetMeters', 0); const inset = numericValue(insetConstraint, 'insetMeters', 0);
handles.push({ handles.push({
handleId: `handle:cluster-crossing-inset:${crossingKey}`, handleId: `handle:cluster-crossing-inset:${crossingKey}`,
kind: 'cluster-crossing-inset', kind: 'cluster-crossing-inset',
anchor, anchor,
position: pointAt(Math.max(2, item.approach.cutback * 0.68) + inset), position: pointAt(baseCrosswalkDistance + inset),
axisAzimuth: arm.heading, axisAzimuth: arm.heading,
value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' }, value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' },
...(insetConstraint ? { constraintId: insetConstraint.id } : {}), ...(insetConstraint ? { constraintId: insetConstraint.id } : {}),
@@ -1475,7 +1528,7 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters)
handleId: `handle:cluster-stop-line-offset:${crossingKey}`, handleId: `handle:cluster-stop-line-offset:${crossingKey}`,
kind: 'cluster-stop-line-offset', kind: 'cluster-stop-line-offset',
anchor, anchor,
position: pointAt(Math.max(2, item.approach.cutback * 0.68) + inset + offset), position: pointAt(baseCrosswalkDistance + inset + offset),
axisAzimuth: arm.heading, axisAzimuth: arm.heading,
value: { current: offset, min: MIN_STOP_LINE_OFFSET_METERS, max: 12, unit: 'meter' }, value: { current: offset, min: MIN_STOP_LINE_OFFSET_METERS, max: 12, unit: 'meter' },
...(stopConstraint ? { constraintId: stopConstraint.id } : {}), ...(stopConstraint ? { constraintId: stopConstraint.id } : {}),
+12
View File
@@ -330,6 +330,18 @@ assert.notDeepEqual(
); );
const complexCrossingHandle = clusterState.handles.handles.find((handle) => handle.kind === 'cluster-crossing-inset'); const complexCrossingHandle = clusterState.handles.handles.find((handle) => handle.kind === 'cluster-crossing-inset');
assert.ok(complexCrossingHandle, 'the complex fixture must publish grouped crossing handles'); 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 = { const complexCrossingConstraint = {
id: 'constraint:complex-crossing', id: 'constraint:complex-crossing',
kind: 'cluster-crossing-inset', kind: 'cluster-crossing-inset',