feat(junction): establish stable complex junction structure

This commit is contained in:
que01 committed 2026-09-08 11:25:57 +08:00
1 parent bd8b55e58b
commit 0a5a229ce3
22 files changed
+1386 -125

No files matched your search

+114 -27
View File
@@ -1,6 +1,7 @@
'use strict';
const fs = require('fs');
const { topologyIdentity, structureFromTopology } = require('./junction-topology');
const { convertGeoJson, boundsOf } = require('../reference/gaode');
const metricsCache = new WeakMap();
const CORNER_FILLET_SEGMENTS = 12;
@@ -12,6 +13,82 @@ const SIDEWALK_WIDTH_METERS = 2;
// past that end and let the two overlap rather than chase an exact seam.
const SIDEWALK_CORNER_OVERRUN_METERS = 6;
// Geometry and the durable structure use this single external topology.
function resolveComplexJunctionTopology(model, cluster, helpers) {
const nodeIds = new Set(cluster.nodeIds.map(String));
const nodes = [...nodeIds]
.sort()
.map((id) => helpers.junctionPlans.get(id)?.node)
.filter(Boolean);
const carriageways = [];
for (const [nodeId, plan] of helpers.junctionPlans) {
if (!nodeIds.has(String(nodeId))) continue;
for (const approach of plan.approaches) {
const roads = approach.roadIds.map((id) => model.roads.find((road) => road.id === id)).filter(Boolean);
// Source topology is authoritative; geometric proximity is only a fallback
// for legacy plans with no source-node metadata.
const sourced = roads.filter((road) => road.sourceNodeIds?.length >= 2);
const internal = sourced.length
? sourced.every(
(road) => nodeIds.has(String(road.sourceNodeIds[0])) && nodeIds.has(String(road.sourceNodeIds.at(-1))),
)
: nodes.some((node) => node !== plan.node && helpers.distanceMeters(approach.line.at(-1), node) < 4);
if (internal) continue;
carriageways.push({
nodeId: String(nodeId),
approach,
plan,
roads,
heading: helpers.headingAtEndpoint(approach.line),
length: helpers.lineLengthMeters(approach.line),
});
}
}
// Stable source ordering makes heading grouping independent of plan/road order.
carriageways.sort((a, b) => topologyIdentity(a).localeCompare(topologyIdentity(b)));
const duplicateSources = new Set();
const seenSources = new Set();
for (const item of carriageways) {
const identity = topologyIdentity(item);
if (seenSources.has(identity)) duplicateSources.add(identity);
seenSources.add(identity);
}
for (let index = carriageways.length - 1; index > 0; index -= 1)
if (topologyIdentity(carriageways[index]) === topologyIdentity(carriageways[index - 1]))
carriageways.splice(index, 1);
const arms = [];
for (const item of carriageways) {
item.ambiguousSource = duplicateSources.has(topologyIdentity(item));
let arm = arms.find(
(candidate) => Math.abs(normalizeHeading(candidate.representative.heading - item.heading)) < 20,
);
if (!arm) {
arm = { representative: item, members: [] };
arms.push(arm);
}
arm.members.push(item);
}
for (const arm of arms) arm.heading = averageHeading(arm.members.map((item) => item.heading));
for (const item of carriageways)
if (arms.filter((arm) => Math.abs(normalizeHeading(arm.representative.heading - item.heading)) < 20).length > 1)
item.ambiguousSource = true;
arms.sort(
(a, b) =>
a.heading - b.heading || topologyIdentity(a.representative).localeCompare(topologyIdentity(b.representative)),
);
const scope = `cluster:${encodeURIComponent(cluster.id)}`;
for (const arm of arms) {
arm.key = `${scope}:arm:${arm.members.map(topologyIdentity).sort().join('|')}`;
for (const item of arm.members) {
item.key = `${scope}:carriageway:${topologyIdentity(item)}`;
item.armKey = arm.key;
}
}
for (let index = 0; index < arms.length; index += 1)
arms[index].cornerKey = `corner:${arms[index].key}>${arms[(index + 1) % arms.length].key}`;
return { cluster, nodeIds: [...nodeIds].sort(), carriageways, arms };
}
function buildComplexJunctionGeometry(model, cluster, helpers) {
const nodeIds = new Set(cluster.nodeIds.map(String));
const nodes = cluster.nodeIds.map((id) => helpers.junctionPlans.get(String(id))?.node).filter(Boolean);
@@ -33,20 +110,9 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
(sum, point) => [sum[0] + point[0] / nodes.length, sum[1] + point[1] / nodes.length],
[0, 0],
);
const approaches = [];
const carriageways = [];
for (const [nodeId, plan] of helpers.junctionPlans) {
if (!nodeIds.has(String(nodeId))) continue;
for (const approach of plan.approaches) {
const endpoint = approach.line.at(-1);
if (nodes.some((node) => node !== plan.node && helpers.distanceMeters(endpoint, node) < 4)) continue;
const heading = helpers.headingAtEndpoint(approach.line);
const length = helpers.lineLengthMeters(approach.line);
carriageways.push({ nodeId, approach, plan, heading, length });
if (approaches.some((item) => Math.abs(normalizeHeading(item.heading - heading)) < 20)) continue;
approaches.push({ nodeId, approach, plan, heading, length });
}
}
const topology = resolveComplexJunctionTopology(model, cluster, helpers);
const { carriageways, arms } = topology;
const approaches = arms.map((arm) => arm.representative);
if (approaches.length < 3)
return {
features: [],
@@ -62,18 +128,7 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
],
};
const { calibration, coreRadius } = complexJunctionMetrics(cluster);
const sorted = [...approaches].sort((a, b) => a.heading - b.heading);
const arms = sorted.map((representative) => ({
representative,
heading: averageHeading(
carriageways
.filter((candidate) => Math.abs(normalizeHeading(candidate.heading - representative.heading)) < 20)
.map((candidate) => candidate.heading),
),
members: carriageways.filter(
(candidate) => Math.abs(normalizeHeading(candidate.heading - representative.heading)) < 20,
),
}));
const sorted = approaches;
const shape = classifyJunctionShape(arms, center, helpers);
if (shape.kind === 'unsupported')
return {
@@ -141,6 +196,9 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
type: 'Feature',
properties: {
native_id: `complex-stop-line:${cluster.id}:${item.approach.segmentId}`,
object_key: `stop-line:${item.key}`,
arm_key: item.armKey,
carriageway_key: item.key,
cluster_id: cluster.id,
kind: 'complex-stop-line',
road_id: incomingRoad.id,
@@ -223,6 +281,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
type: 'Feature',
properties: {
native_id: `complex-crosswalk:${cluster.id}:${item.nodeId}:${item.approach.segmentId}:${stripe + 1}`,
object_key: `crossing:${arm.key}`,
arm_key: arm.key,
cluster_id: cluster.id,
kind: 'complex-crosswalk',
crossing_node_id: item.nodeId,
@@ -290,6 +350,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
native_id: `complex-corner-island:${cluster.id}:${index + 1}`,
cluster_id: cluster.id,
kind: 'complex-corner-island',
object_key: `island:${first.cornerKey}`,
corner_key: first.cornerKey,
corner_index: index + 1,
from_heading: first.heading,
to_heading: second.heading,
@@ -332,6 +394,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
native_id: `complex-corner-crosswalk:${cluster.id}:${index + 1}:${stripe + 1}`,
cluster_id: cluster.id,
kind: 'complex-corner-crosswalk',
object_key: `crossing:${first.cornerKey}`,
corner_key: first.cornerKey,
corner_index: index + 1,
direction: bisector,
radial_distance_m: stripeRadius,
@@ -394,6 +458,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
native_id: `complex-corner-fillet:${cluster.id}:${index + 1}`,
cluster_id: cluster.id,
kind: 'complex-corner-fillet',
object_key: first.cornerKey,
corner_key: first.cornerKey,
complex_part: 'corner-fillet',
corner_index: index + 1,
from_heading: first.heading,
@@ -453,6 +519,8 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
native_id: `complex-sidewalk-corner:${cluster.id}:${index + 1}`,
cluster_id: cluster.id,
kind: 'complex-sidewalk-corner',
object_key: `sidewalk-turn:${first.cornerKey}`,
corner_key: first.cornerKey,
corner_index: index + 1,
from_heading: first.heading,
to_heading: second.heading,
@@ -530,6 +598,9 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
native_id: `complex-junction:${cluster.id}:carriageway:${item.approach.segmentId}`,
cluster_id: cluster.id,
kind: 'complex-approach',
object_key: item.key,
carriageway_key: item.key,
arm_key: item.armKey,
complex_part: 'carriageway',
heading_deg: item.heading,
lane_count: item.approach.roadIds.reduce(
@@ -562,7 +633,18 @@ function buildComplexJunctionGeometry(model, cluster, helpers) {
),
]
: [];
const allFeatures = [...features, ...islands, ...sidewalkCorners, ...crosswalks, ...stopLines];
const structure = structureFromTopology(topology, shape, center, allFeatures);
for (const feature of allFeatures) {
feature.properties.junction_key = structure.junctionKey;
feature.properties.topology_fingerprint = structure.topologyFingerprint;
}
// Persist the compiler authority with a generated road surface, including the
// supported-shape case where only the central polygon had to be skipped.
const carrier = features.find((feature) => feature.properties.kind === 'complex-core') || features[0];
if (carrier) carrier.properties.junction_structure = structure;
return {
structure,
features,
islands: [...islands, ...sidewalkCorners],
crosswalks,
@@ -907,4 +989,9 @@ function interpolateCoordinate(first, second, ratio) {
return [first[0] + (second[0] - first[0]) * ratio, first[1] + (second[1] - first[1]) * ratio];
}
module.exports = { buildComplexJunctionGeometry, classifyJunctionShape, complexJunctionMetrics };
module.exports = {
resolveComplexJunctionTopology,
buildComplexJunctionGeometry,
classifyJunctionShape,
complexJunctionMetrics,
};