fix: fill junction core and connect cluster lane trajectories

This commit is contained in:
que01 committed 2026-09-14 10:47:25 +08:00
1 parent 695bf21a84
commit 6a8f026d12
12 files changed
+527 -83

No files matched your search

+69 -37
View File
@@ -1,5 +1,7 @@
'use strict';
const { union } = require('./junction-polygon-ops');
const { solvePavedRegion } = require('./junction-region-solver');
const { stationAtDistanceFrom } = require('./junction-build-plan');
@@ -219,41 +221,70 @@ function coordinateDistanceMeters(first, second) {
// A central paved region is safe to infer only when every arm has exactly one
// source carriageway. Its shared entry seams are the sections' `inner` edges;
// every remaining edge is an explicit closure between adjacent arms.
function buildCoreClosure(topology, graph, center) {
let coreSections = graph.sections;
if (topology.arms.some((arm) => arm.members.length !== 1)) {
if (topology.arms.length < 4 || topology.arms.length > 6)
return { status: 'unsupported', reason: 'parallel-carriageway-closure-unresolved' };
coreSections = topology.arms.map(
(arm) =>
graph.sections
.filter((section) => section.armKey === arm.key && section.ringStatus === 'supported')
.sort((a, b) => {
const distance = (section) => {
const x = (section.inner.left[0] + section.inner.right[0]) / 2 - center[0];
const y = (section.inner.left[1] + section.inner.right[1]) / 2 - center[1];
return x * x + y * y;
};
return distance(a) - distance(b) || a.carriagewayKey.localeCompare(b.carriagewayKey);
})[0],
);
if (coreSections.some((section) => !section))
return { status: 'unsupported', reason: 'parallel-carriageway-closure-unresolved' };
// Split at boundary crossings to check complete seam intervals, not samples.
function segmentCoveredByRing(start, end, ring) {
const scale = Math.cos((start[1] * Math.PI) / 180);
const xy = (p) => [(p[0] - start[0]) * 111320 * scale, (p[1] - start[1]) * 111320];
const d = xy(end),
boundary = ring.map(xy);
const cross = (a, b) => a[0] * b[1] - a[1] * b[0];
const parameters = [0, 1];
for (let i = 1; i < boundary.length; i++) {
const a = boundary[i - 1],
b = boundary[i],
edge = [b[0] - a[0], b[1] - a[1]];
const denominator = cross(d, edge);
if (Math.abs(denominator) < 1e-10) continue;
const t = cross(a, edge) / denominator,
u = cross(a, d) / denominator;
if (t > 0 && t < 1 && u >= 0 && u <= 1) parameters.push(t);
}
if (coreSections.length < 3 || coreSections.length > 6)
parameters.sort((a, b) => a - b);
const covered = (t) => {
const p = d.map((v) => v * t);
if (pointInRing(p, boundary)) return true;
return boundary.slice(1).some((b, i) => {
const a = boundary[i],
dx = b[0] - a[0],
dy = b[1] - a[1];
const length = dx * dx + dy * dy;
const q = length ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / length)) : 0;
return Math.hypot(p[0] - a[0] - q * dx, p[1] - a[1] - q * dy) <= 0.0001;
});
};
return parameters.every(covered) && parameters.slice(1).every((t, i) => covered((t + parameters[i]) / 2));
}
function buildCoreClosure(topology, graph, center) {
const coreSections = graph.sections.filter((section) => section.ringStatus === 'supported');
if (topology.arms.length < 3 || topology.arms.length > 6)
return { status: 'unsupported', reason: 'unsupported-arm-count' };
const points = coreSections
.flatMap((section) => [
{ point: section.inner.left, edgeRef: section.edgeRefs.inner },
{ point: section.inner.right, edgeRef: section.edgeRefs.inner },
])
.filter((item) => item.point?.every(Number.isFinite))
.sort(
(first, second) =>
Math.atan2(first.point[1] - center[1], first.point[0] - center[0]) -
Math.atan2(second.point[1] - center[1], second.point[0] - center[0]),
);
const ring = [...points.map((item) => item.point), points[0]?.point].filter(Boolean);
if (topology.arms.length === 3 && topology.arms.some((arm) => arm.members.length !== 1))
return { status: 'unsupported', reason: 'parallel-carriageway-closure-unresolved' };
// Sort whole entry edges, never their individual endpoints. Each valid
// carriageway contributes its full inner seam, including divided arms.
const angle = (point) => Math.atan2(point[1] - center[1], point[0] - center[0]);
const points = [...coreSections]
.sort((a, b) => {
const middle = (section) => section.inner.left.map((v, i) => (v + section.inner.right[i]) / 2);
return angle(middle(a)) - angle(middle(b));
})
.flatMap((section) => {
const ends = [section.inner.left, section.inner.right];
const delta = Math.atan2(Math.sin(angle(ends[1]) - angle(ends[0])), Math.cos(angle(ends[1]) - angle(ends[0])));
if (delta < 0) ends.reverse();
return ends.map((point) => ({ point, edgeRef: section.edgeRefs.inner }));
});
let ring = [...points.map((item) => item.point), points[0]?.point].filter(Boolean);
let mergedEntryCells = false;
if (topology.arms.some((arm) => arm.members.length > 1) && ring.length >= 7 && ringSelfIntersects(ring)) {
const cells = ring.slice(1).map((point, index) => [[center, ring[index], point, center]]);
const region = union(...cells);
if (region.length === 1 && region[0].length === 1) {
ring = region[0][0];
mergedEntryCells = true;
}
}
const pavedRegion = solvePavedRegion(
ring,
graph.exclusionRegions.filter((region) => !region.isHole).map((region) => region.ring),
@@ -298,9 +329,10 @@ function buildCoreClosure(topology, graph, center) {
rightIndex >= 0 &&
(Math.abs(leftIndex - rightIndex) === 1 || Math.abs(leftIndex - rightIndex) === ring.length - 2);
const edge = graph.edges.find((candidate) => candidate.key === section.edgeRefs.inner);
if (!contiguous || !edge) return { carriagewayKey: section.carriagewayKey, errorMeters: Infinity };
const ringLeft = ring[leftIndex];
const ringRight = ring[rightIndex];
const covered = mergedEntryCells && segmentCoveredByRing(section.inner.left, section.inner.right, ring);
if ((!contiguous && !covered) || !edge) return { carriagewayKey: section.carriagewayKey, errorMeters: Infinity };
const ringLeft = contiguous ? ring[leftIndex] : section.inner.left;
const ringRight = contiguous ? ring[rightIndex] : section.inner.right;
const forward = Math.max(
coordinateDistanceMeters(ringLeft, edge.from),
coordinateDistanceMeters(ringRight, edge.to),
@@ -328,4 +360,4 @@ function buildCoreClosure(topology, graph, center) {
};
}
module.exports = { buildJunctionBoundaryGraph, buildCoreClosure, ringsOverlap };
module.exports = { buildJunctionBoundaryGraph, buildCoreClosure, ringsOverlap, segmentCoveredByRing };