fix: align junction crossings and round pedestrian islands

This commit is contained in:
que01 committed 2026-09-11 17:35:32 +08:00
1 parent 76f81d024a
commit 7a7912f302
18 files changed
+491 -96

No files matched your search

+110 -89
View File
@@ -1,10 +1,16 @@
'use strict';
const fs = require('fs');
const { COMPLEX_CROSSWALK_DEPTH_METERS } = require('./junction-metrics');
const { structureFromTopology } = require('./junction-topology');
const { resolveJunctionBuildPlan, stationAtDistanceFrom } = require('./junction-build-plan');
const { buildJunctionBoundaryGraph, buildCoreClosure, ringsOverlap } = require('./junction-boundary-graph');
const { solveCornerFeasibleRegion } = require('./junction-region-solver');
const {
buildIslandFromCrossingEdges,
orthogonalCrossingHeadings,
stripeLayout,
crossingArcSupport,
} = require('./junction-facility-composition');
const { complexJunctionMetrics: calculateComplexJunctionMetrics } = require('./junction-metrics');
const { convertGeoJson, boundsOf } = require('../reference/gaode');
const metricsCache = new WeakMap();
@@ -157,41 +163,37 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
.map((roadId) => model.roads.find((road) => road.id === roadId))
.find((road) => String(road?.sourceNodeIds.at(-1)) === String(item.nodeId));
if (incomingRoad) {
// Keep the stop bar just outside the road crosswalk. The previous fixed
// core-radius offset placed it nearly ten metres beyond the crossing.
// Offset is clear space beyond the relocated crossing outer edge; include
// half the stop bar thickness so the minimum offset cannot cut into it.
const arm = arms.find((candidate) => candidate.members.includes(item));
const crossingKey = `crossing:${arm.key}`;
const crossingRadius = armCrosswalkRadiusFor(arm);
const stopOffset = crossingControls.get(crossingKey)?.stopLineOffsetMeters || 3;
const stopCenter = pointOnCarriagewayRadius(item, center, crossingRadius + stopOffset, helpers);
const stopHeading = orthogonalCrossingHeadings(arms.map((candidate) => candidate.heading))[arms.indexOf(arm)];
const envelope = armEnvelopeAtRadius(arm, crossingRadius, center, helpers);
const basePoint = pointOnCarriagewayRadius(item, center, crossingRadius, helpers);
const lateral = signedLateralMeters(envelope.center, basePoint, stopHeading);
const stopCenter = helpers.offsetCoordinate(
helpers.offsetCoordinate(envelope.center, stopHeading + 90, lateral),
stopHeading,
COMPLEX_CROSSWALK_DEPTH_METERS * 1.5 + 0.24 + stopOffset,
);
const crossingEvidence = crossingPlans.get(arm.key)?.evidence || {
kind: 'unknown-no-crossing-source',
crossingNodeIds: [],
};
const ring = [
helpers.offsetCoordinate(
helpers.offsetCoordinate(stopCenter, item.heading + 90, -outerHalf),
item.heading,
helpers.offsetCoordinate(stopCenter, stopHeading + 90, -outerHalf),
stopHeading,
-0.24,
),
helpers.offsetCoordinate(helpers.offsetCoordinate(stopCenter, stopHeading + 90, outerHalf), stopHeading, -0.24),
helpers.offsetCoordinate(helpers.offsetCoordinate(stopCenter, stopHeading + 90, outerHalf), stopHeading, 0.24),
helpers.offsetCoordinate(helpers.offsetCoordinate(stopCenter, stopHeading + 90, -outerHalf), stopHeading, 0.24),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stopCenter, item.heading + 90, outerHalf),
item.heading,
-0.24,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stopCenter, item.heading + 90, outerHalf),
item.heading,
0.24,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stopCenter, item.heading + 90, -outerHalf),
item.heading,
0.24,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stopCenter, item.heading + 90, -outerHalf),
item.heading,
helpers.offsetCoordinate(stopCenter, stopHeading + 90, -outerHalf),
stopHeading,
-0.24,
),
];
@@ -258,57 +260,68 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
}
arm.crosswalkFrame = {
center: envelope.center,
groupDepth: 3.4,
groupDepth: COMPLEX_CROSSWALK_DEPTH_METERS,
supportHeading: normalizeHeading(arm.heading + 90),
envelopeWidthMeters: envelope.widthMeters,
};
arm.crosswalkEvidence = evidence;
}
const crossingHeadings = orthogonalCrossingHeadings(arms.map((arm) => arm.heading));
arms.forEach((arm, index) => {
if (arm.crosswalkFrame) {
arm.crosswalkFrame.heading = crossingHeadings[index];
arm.crosswalkFrame.supportHeading = normalizeHeading(crossingHeadings[index] + 90);
}
});
for (let index = 0; index < arms.length; index += 1) {
const arm = arms[index];
if (!arm.crosswalkFrame) continue;
const item = arm.representative;
const crossingHeading = arm.crosswalkFrame.heading;
const roadEdgeInset = 0.35;
const usableSpan = Math.max(0.42, arm.crosswalkFrame.envelopeWidthMeters - roadEdgeInset * 2);
const endpoints = [
helpers.offsetCoordinate(arm.crosswalkFrame.center, arm.heading + 90, -usableSpan / 2),
helpers.offsetCoordinate(arm.crosswalkFrame.center, arm.heading + 90, usableSpan / 2),
];
const groupDepth = arm.crosswalkFrame.groupDepth;
arm.crosswalkFrame.center = helpers.offsetCoordinate(arm.crosswalkFrame.center, crossingHeading, groupDepth);
arm.crosswalkRadius += groupDepth;
const endpoints = [
helpers.offsetCoordinate(arm.crosswalkFrame.center, crossingHeading + 90, -usableSpan / 2),
helpers.offsetCoordinate(arm.crosswalkFrame.center, crossingHeading + 90, usableSpan / 2),
];
const stripeWidth = 0.42;
const stripeCount = Math.max(6, Math.floor((usableSpan - stripeWidth) / 0.82) + 1);
const stripeSpacing = stripeCount > 1 ? (usableSpan - stripeWidth) / (stripeCount - 1) : 0;
const layout = stripeLayout(usableSpan);
const stripeCount = layout.centers.length;
const stripeSpacing = layout.pitch;
arm.crosswalkFrame.center = midpoint(...endpoints);
arm.crosswalkFrame.endpoints = endpoints;
arm.crosswalkFrame.spanMeters = usableSpan;
arm.crosswalkFrame.roadEdgeInsetMeters = roadEdgeInset;
for (let stripe = 0; stripe < stripeCount; stripe += 1) {
const along = stripeWidth / 2 + stripe * stripeSpacing;
const along = layout.centers[stripe];
const centerPoint = helpers.offsetCoordinate(endpoints[0], bearing(...endpoints), along);
const ring = [
helpers.offsetCoordinate(
helpers.offsetCoordinate(centerPoint, arm.heading, -groupDepth / 2),
arm.heading + 90,
helpers.offsetCoordinate(centerPoint, crossingHeading, -groupDepth / 2),
crossingHeading + 90,
-stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(centerPoint, arm.heading, groupDepth / 2),
arm.heading + 90,
helpers.offsetCoordinate(centerPoint, crossingHeading, groupDepth / 2),
crossingHeading + 90,
-stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(centerPoint, arm.heading, groupDepth / 2),
arm.heading + 90,
helpers.offsetCoordinate(centerPoint, crossingHeading, groupDepth / 2),
crossingHeading + 90,
stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(centerPoint, arm.heading, -groupDepth / 2),
arm.heading + 90,
helpers.offsetCoordinate(centerPoint, crossingHeading, -groupDepth / 2),
crossingHeading + 90,
stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(centerPoint, arm.heading, -groupDepth / 2),
arm.heading + 90,
helpers.offsetCoordinate(centerPoint, crossingHeading, -groupDepth / 2),
crossingHeading + 90,
-stripeWidth / 2,
),
];
@@ -323,7 +336,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
kind: 'complex-crosswalk',
crossing_node_id: item.nodeId,
road_id: item.approach.roadIds[0],
direction: arm.heading,
direction: crossingHeading,
radial_distance_m: arm.crosswalkRadius,
span_m: usableSpan,
road_envelope_span_m: arm.crosswalkFrame.envelopeWidthMeters,
@@ -448,27 +461,29 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
},
geometry: { type: 'Polygon', coordinates: [ring] },
});
const islandDepth = Math.min(7, Math.max(3, apexReach * 0.35));
const islandHalfWidth = Math.min(
2.2,
Math.max(1.2, Math.min(first.widthMeters || 4, second.widthMeters || 4) * 0.28),
if (!first.crosswalkFrame?.endpoints || !second.crosswalkFrame?.endpoints) continue;
const endpointPairs = first.crosswalkFrame.endpoints.flatMap((a) =>
second.crosswalkFrame.endpoints.map((b) => ({ a, b, distance: helpers.distanceMeters(a, b) })),
);
const islandCenter = helpers.offsetCoordinate(apex, bisector, -islandDepth * 0.5);
const islandRing = [
helpers.offsetCoordinate(helpers.offsetCoordinate(apex, bisector, 0.6), bisector + 90, -islandHalfWidth),
helpers.offsetCoordinate(
helpers.offsetCoordinate(islandCenter, bisector, islandDepth * 0.5),
bisector + 90,
-islandHalfWidth * 0.72,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(islandCenter, bisector, islandDepth * 0.5),
bisector + 90,
islandHalfWidth * 0.72,
),
helpers.offsetCoordinate(helpers.offsetCoordinate(apex, bisector, 0.6), bisector + 90, islandHalfWidth),
helpers.offsetCoordinate(helpers.offsetCoordinate(apex, bisector, 0.6), bisector + 90, -islandHalfWidth),
];
const pair = endpointPairs.sort((a, b) => a.distance - b.distance)[0];
const crossingCorner = rayIntersection(
{ point: pair.a, heading: first.crosswalkFrame.heading },
{ point: pair.b, heading: second.crosswalkFrame.heading },
center,
);
if (!crossingCorner) continue;
const stripeWidth = 0.42;
const outerFirst = helpers.offsetCoordinate(
crossingCorner,
first.crosswalkFrame.heading,
first.crosswalkFrame.groupDepth * 2,
);
const outerSecond = helpers.offsetCoordinate(
crossingCorner,
second.crosswalkFrame.heading,
second.crosswalkFrame.groupDepth * 2,
);
const islandRing = buildIslandFromCrossingEdges(crossingCorner, outerFirst, outerSecond);
const publishedIslandRing = islandRing;
if (!cornerOverlapsExclusion(publishedIslandRing, first.cornerKey, '安全岛'))
islands.push({
@@ -493,48 +508,51 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
// Complex junctions carry a default diagonal crossing across each safe
// corner. Source crossing nodes refine provenance/inset values, but are
// not required for the default visual treatment.
const islandMidpoint = publishedIslandRing
.slice(0, -1)
.reduce(
(sum, point) => [
sum[0] + point[0] / (publishedIslandRing.length - 1),
sum[1] + point[1] / (publishedIslandRing.length - 1),
],
[0, 0],
);
const diagonalCenter = helpers.offsetCoordinate(islandMidpoint, bisector, islandHalfWidth + 1.2);
const diagonalSpan = Math.min(6, Math.max(2.4, helpers.distanceMeters(tangents[0], tangents[1]) * 0.35));
const diagonalDepth = 2.4;
const stripeCount = 7;
const stripeWidth = 0.32;
const stripeSpacing = diagonalDepth / stripeCount;
const outerMidpoint = [(outerFirst[0] + outerSecond[0]) / 2, (outerFirst[1] + outerSecond[1]) / 2];
const diagonalDepth = COMPLEX_CROSSWALK_DEPTH_METERS;
const diagonalHeading = bearing(outerFirst, outerSecond) - 90;
const outwardHeading = bearing(crossingCorner, outerMidpoint);
const theta = (outwardHeading * Math.PI) / 180;
const lonScale = 111320 * Math.cos((outerMidpoint[1] * Math.PI) / 180);
const projectedIsland = publishedIslandRing.map((p) => {
const east = (p[0] - outerMidpoint[0]) * lonScale;
const north = (p[1] - outerMidpoint[1]) * 111320;
return [east * Math.cos(theta) - north * Math.sin(theta), east * Math.sin(theta) + north * Math.cos(theta)];
});
const support = crossingArcSupport(projectedIsland, COMPLEX_CROSSWALK_DEPTH_METERS / 2);
if (support === null) continue;
const diagonalCenter = helpers.offsetCoordinate(outerMidpoint, outwardHeading, support + diagonalDepth / 2);
const diagonalSpan = COMPLEX_CROSSWALK_DEPTH_METERS;
const layout = stripeLayout(diagonalDepth);
const stripeCount = layout.centers.length;
const stripeSpacing = layout.pitch;
for (let stripe = 0; stripe < stripeCount; stripe += 1) {
const along = -diagonalDepth / 2 + (stripe + 0.5) * stripeSpacing;
const stripeCenter = helpers.offsetCoordinate(diagonalCenter, bisector, along);
const along = -diagonalDepth / 2 + layout.centers[stripe] - (layout.centers[0] - stripeWidth / 2);
const stripeCenter = helpers.offsetCoordinate(diagonalCenter, diagonalHeading, along);
const ring = [
helpers.offsetCoordinate(
helpers.offsetCoordinate(stripeCenter, bisector + 90, -diagonalSpan / 2),
bisector,
helpers.offsetCoordinate(stripeCenter, diagonalHeading + 90, -diagonalSpan / 2),
diagonalHeading,
-stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stripeCenter, bisector + 90, diagonalSpan / 2),
bisector,
helpers.offsetCoordinate(stripeCenter, diagonalHeading + 90, diagonalSpan / 2),
diagonalHeading,
-stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stripeCenter, bisector + 90, diagonalSpan / 2),
bisector,
helpers.offsetCoordinate(stripeCenter, diagonalHeading + 90, diagonalSpan / 2),
diagonalHeading,
stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stripeCenter, bisector + 90, -diagonalSpan / 2),
bisector,
helpers.offsetCoordinate(stripeCenter, diagonalHeading + 90, -diagonalSpan / 2),
diagonalHeading,
stripeWidth / 2,
),
helpers.offsetCoordinate(
helpers.offsetCoordinate(stripeCenter, bisector + 90, -diagonalSpan / 2),
bisector,
helpers.offsetCoordinate(stripeCenter, diagonalHeading + 90, -diagonalSpan / 2),
diagonalHeading,
-stripeWidth / 2,
),
];
@@ -549,6 +567,9 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
to_boundary: `island:inferred/${first.cornerKey}`,
cluster_id: cluster.id,
kind: 'complex-corner-crosswalk',
stripe_width_m: stripeWidth,
stripe_spacing_m: stripeSpacing,
group_depth_m: COMPLEX_CROSSWALK_DEPTH_METERS,
corner_key: first.cornerKey,
facility_evidence: 'default-complex-junction',
provenance: 'native-road-complex-junction-corner-crosswalk/v1',
+17 -3
View File
@@ -1,5 +1,9 @@
'use strict';
const { orthogonalCrossingHeadings } = require('./junction-facility-composition');
const { COMPLEX_CROSSWALK_DEPTH_METERS } = require('./junction-metrics');
const { diagnostic } = require('./diagnostics');
const { approachForCrossing, arrivalEndpointIds, effectiveCrosswalkInsetMax } = require('./crossing-approach');
const { haversineMeters, metersAt, polylineLength } = require('../geometry/lane-geometry');
@@ -1798,7 +1802,9 @@ function makeClusterCrossingHandles(model, junctionData, constraints, 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 crossingArms = clusterArms(clusterId, itemsByCluster.get(clusterId) || []);
const crossingHeadings = orthogonalCrossingHeadings(crossingArms.map((arm) => arm.heading));
for (const [armIndex, arm] of crossingArms.entries()) {
const item = arm.representative;
const line = clusterApproachLine(item);
const length = polylineLength(line);
@@ -1831,7 +1837,11 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters)
handleId: `handle:cluster-crossing-inset:${crossingKey}`,
kind: 'cluster-crossing-inset',
anchor,
position: pointAt(baseCrosswalkDistance + inset),
position: offsetCoordinate(
pointAt(baseCrosswalkDistance + inset),
crossingHeadings[armIndex],
COMPLEX_CROSSWALK_DEPTH_METERS,
),
axisAzimuth: arm.heading,
value: { current: inset, min: 0, max: MAX_CROSSWALK_INSET_METERS, unit: 'meter' },
...(insetConstraint ? { constraintId: insetConstraint.id } : {}),
@@ -1844,7 +1854,11 @@ function makeClusterCrossingHandles(model, junctionData, constraints, clusters)
handleId: `handle:cluster-stop-line-offset:${crossingKey}`,
kind: 'cluster-stop-line-offset',
anchor,
position: pointAt(baseCrosswalkDistance + (Number.isFinite(sourceCrossingDistance) ? inset : 0) + offset),
position: offsetCoordinate(
pointAt(baseCrosswalkDistance + (Number.isFinite(sourceCrossingDistance) ? inset : 0)),
crossingHeadings[armIndex],
COMPLEX_CROSSWALK_DEPTH_METERS * 1.5 + 0.24 + offset,
),
axisAzimuth: arm.heading,
value: { current: offset, min: MIN_STOP_LINE_OFFSET_METERS, max: 12, unit: 'meter' },
...(stopConstraint ? { constraintId: stopConstraint.id } : {}),
+87 -1
View File
@@ -17,4 +17,90 @@ function validateFacilityComposition(island, crossings) {
return { valid: true, reason: null };
}
module.exports = { buildPedestrianIsland, validateFacilityComposition };
// The two legs are extensions of crossing short edges, in the same coordinate frame.
function buildIslandFromCrossingEdges(corner, firstEnd, secondEnd, segments = 12) {
const control = corner.map((value, axis) => (value + (firstEnd[axis] + secondEnd[axis]) / 2) / 2);
const mix = (a, b, t) => a.map((v, axis) => v * (1 - t) + b[axis] * t);
const evaluate = (t) =>
corner.map((_, axis) => (1 - t) ** 2 * firstEnd[axis] + 2 * t * (1 - t) * control[axis] + t * t * secondEnd[axis]);
const derivative = (t) =>
corner.map((_, axis) => 2 * (1 - t) * (control[axis] - firstEnd[axis]) + 2 * t * (secondEnd[axis] - control[axis]));
const cubic = (a, b, c, d) =>
Array.from({ length: 9 }, (_, i) => {
const t = i / 8,
u = 1 - t;
return a.map((v, axis) => u ** 3 * v + 3 * u * u * t * b[axis] + 3 * u * t * t * c[axis] + t ** 3 * d[axis]);
});
const trim = 0.2;
const left = mix(corner, firstEnd, 1 - trim),
right = mix(corner, secondEnd, 1 - trim);
const arcStart = evaluate(trim),
arcEnd = evaluate(1 - trim);
const leftCurve = cubic(
left,
mix(left, firstEnd, 0.65),
arcStart.map((v, i) => v - (derivative(trim)[i] * trim) / 3),
arcStart,
);
const rightCurve = cubic(
arcEnd,
arcEnd.map((v, i) => v + (derivative(1 - trim)[i] * trim) / 3),
mix(right, secondEnd, 0.65),
right,
);
const nearFirst = mix(corner, firstEnd, trim),
nearSecond = mix(corner, secondEnd, trim);
const nose = Array.from({ length: 9 }, (_, i) => {
const t = i / 8;
return corner.map((v, axis) => (1 - t) ** 2 * nearSecond[axis] + 2 * t * (1 - t) * v + t * t * nearFirst[axis]);
});
const middle = Array.from({ length: segments + 1 }, (_, i) => evaluate(trim + ((1 - 2 * trim) * i) / segments));
const points = [nearFirst, ...leftCurve, ...middle.slice(1), ...rightCurve.slice(1), ...nose.slice(0, -1)];
return [...points, points[0]];
}
module.exports = { buildPedestrianIsland, validateFacilityComposition, buildIslandFromCrossingEdges };
// Fit a common orthogonal frame only for a near-orthogonal four-arm crossing.
function orthogonalCrossingHeadings(headings) {
if (headings.length !== 4) return headings;
const radians = Math.PI / 180;
const axis =
Math.atan2(
headings.reduce((sum, h) => sum + Math.sin(h * radians * 4), 0),
headings.reduce((sum, h) => sum + Math.cos(h * radians * 4), 0),
) /
(4 * radians);
const fitted = headings.map((h) => axis + Math.round((h - axis) / 90) * 90);
if (fitted.some((h, i) => Math.abs(h - headings[i]) > 15)) return headings;
if (new Set(fitted.map((h) => ((h % 360) + 360) % 360)).size !== 4) return headings;
return fitted;
}
function stripeLayout(span, width = 0.42, gap = 0.4) {
const pitch = width + gap;
const count = Math.max(0, Math.floor((span + gap) / pitch));
const margin = (span - (count * width + Math.max(0, count - 1) * gap)) / 2;
return { width, gap, pitch, centers: Array.from({ length: count }, (_, i) => margin + width / 2 + i * pitch) };
}
module.exports.orthogonalCrossingHeadings = orthogonalCrossingHeadings;
module.exports.stripeLayout = stripeLayout;
// Support of the curved island inside the white bar's lateral footprint.
// Coordinates are local metres: x along the bar, y toward the outside.
function crossingArcSupport(ring, halfWidth) {
const candidates = [];
for (let i = 1; i < ring.length; i++) {
const a = ring[i - 1],
b = ring[i];
if (Math.abs(a[0]) <= halfWidth) candidates.push(a[1]);
for (const x of [-halfWidth, halfWidth]) {
if ((a[0] < x && b[0] > x) || (b[0] < x && a[0] > x)) {
const t = (x - a[0]) / (b[0] - a[0]);
candidates.push(a[1] + t * (b[1] - a[1]));
}
}
}
return candidates.length ? Math.max(...candidates) : null;
}
module.exports.crossingArcSupport = crossingArcSupport;
+1 -1
View File
@@ -28,4 +28,4 @@ function complexJunctionMetrics(cluster, calibration = null) {
};
}
module.exports = { complexJunctionMetrics, transitionLengthForCutback };
module.exports = { complexJunctionMetrics, transitionLengthForCutback, COMPLEX_CROSSWALK_DEPTH_METERS: 3.4 };