feat(junction): share complex topology build plan

This commit is contained in:
que01 committed 2026-09-08 17:47:35 +08:00
1 parent 855400fba2
commit a12e2ac823
20 files changed
+712 -147

No files matched your search

+41 -70
View File
@@ -13,8 +13,8 @@ const {
junctionSurfaceId,
connectionConnectorId,
} = require('./native-ids');
const { topologyIdentity } = require('./junction-topology');
const { complexJunctionMetrics } = require('./complex-junction');
const { resolveJunctionBuildPlan } = require('./junction-build-plan');
const RESOLUTION_SCHEMA = 'road-edit-resolution/v1';
const HANDLE_MANIFEST_SCHEMA = 'road-edit-handles/v1';
@@ -211,89 +211,60 @@ function clusterCarriageways(model, junctionData, clusters) {
const result = new Map();
for (const cluster of Array.isArray(clusters) ? clusters : []) {
if (!cluster?.id || !Array.isArray(cluster.nodeIds)) continue;
const nodeIds = new Set(cluster.nodeIds.map(String));
const items = [];
for (const nodeId of nodeIds) {
for (const approach of junctionData.approaches.get(nodeId) || []) {
const roads = approach.items.map((item) => item.road);
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))),
);
if (internal || !roads.length) continue;
const source = {
nodeId: String(nodeId),
approach: { segmentId: approach.segmentId, roadIds: roads.map((road) => road.id) },
roads,
const plans = new Map();
for (const nodeId of cluster.nodeIds.map(String)) {
const approaches = (junctionData.approaches.get(nodeId) || []).map((source) => {
const roads = source.items.map((item) => item.road);
const first = source.items[0];
const line = first?.endpoint.side === 'end' ? [...roads[0].centerline].reverse() : roads[0]?.centerline || [];
return {
...source,
roadIds: roads.map((road) => road.id),
widthMeters: source.items.reduce((sum, item) => sum + item.road.widthMeters, 0),
line,
};
items.push({
clusterId: String(cluster.id),
nodeId: String(nodeId),
approach,
roads,
identity: topologyIdentity(source),
key: `cluster:${encodeURIComponent(cluster.id)}:carriageway:${topologyIdentity(source)}`,
sourceNodeIds: [...new Set(roads.flatMap((road) => (road.sourceNodeIds || []).map(String)))].sort(),
sourceRoadIds: roads.map((road) => String(road.id)).sort(),
widthMeters: approach.items.reduce((sum, item) => sum + item.road.widthMeters, 0),
laneCount: roads.reduce((sum, road) => sum + (Number(road.laneCount) || 0), 0),
});
}
});
const node = approaches[0]?.line?.[0] || [0, 0];
plans.set(nodeId, { node, approaches });
}
const seen = new Set();
const topology = resolveJunctionBuildPlan(model, cluster, {
junctionPlans: plans,
distanceMeters: haversineMeters,
lineLengthMeters: polylineLength,
headingAtEndpoint: (line) => tangentAzimuth(line, 0),
});
const armByKey = new Map(topology.arms.flatMap((arm) => arm.members.map((member) => [member.key, arm])));
result.set(
String(cluster.id),
items.filter((item) => !seen.has(item.key) && seen.add(item.key)),
topology.carriageways.map((item) => ({
...item,
clusterId: String(cluster.id),
identity: item.key.split(':carriageway:').at(-1),
sourceNodeIds: [...new Set(item.roads.flatMap((road) => (road.sourceNodeIds || []).map(String)))].sort(),
sourceRoadIds: item.roads.map((road) => String(road.id)).sort(),
widthMeters: item.approach.widthMeters,
laneCount: item.roads.reduce((sum, road) => sum + (Number(road.laneCount) || 0), 0),
arm: armByKey.get(item.key),
})),
);
}
return result;
}
function clusterApproachLine(item) {
const road = item.approach.items[0]?.road;
if (!road) return [];
return item.approach.endpoint.side === 'end' ? [...road.centerline].reverse() : road.centerline;
return item.approach.line || [];
}
function headingDifference(first, second) {
return Math.abs(((first - second + 540) % 360) - 180);
}
function averageHeading(headings) {
const vector = headings.reduce(
(sum, heading) => {
const radians = (heading * Math.PI) / 180;
return [sum[0] + Math.sin(radians), sum[1] + Math.cos(radians)];
},
[0, 0],
);
return ((Math.atan2(vector[0], vector[1]) * 180) / Math.PI + 360) % 360;
}
// Mirrors resolveComplexJunctionTopology(): corner identity belongs to adjacent
// external arms, not whichever individual carriageways happened to be listed.
function clusterArms(clusterId, items) {
const arms = [];
for (const item of [...items].sort((a, b) => a.identity.localeCompare(b.identity))) {
item.heading = tangentAzimuth(clusterApproachLine(item), 0.05);
let arm = arms.find((candidate) => headingDifference(candidate.representative.heading, item.heading) < 20);
if (!arm) {
arm = { representative: item, members: [] };
arms.push(arm);
}
arm.members.push(item);
const byArm = new Map();
for (const item of items) {
if (!item.arm || byArm.has(item.arm.key)) continue;
byArm.set(item.arm.key, {
...item.arm,
members: item.arm.members.map((member) => items.find((candidate) => candidate.key === member.key) || member),
});
}
for (const arm of arms) {
arm.heading = averageHeading(arm.members.map((member) => member.heading));
arm.key = `cluster:${encodeURIComponent(clusterId)}:arm:${arm.members
.map((member) => member.identity)
.sort()
.join('|')}`;
}
arms.sort((a, b) => a.heading - b.heading || a.representative.identity.localeCompare(b.representative.identity));
return arms;
return [...byArm.values()];
}
function numericValue(constraint, key, fallback) {