feat(junction): close verified complex cores

This commit is contained in:
que01 committed 2026-09-09 09:15:58 +08:00
1 parent b4abfdb870
commit 88d8b841e5
5 files changed
+97 -25

No files matched your search

+62 -1
View File
@@ -92,4 +92,65 @@ function buildJunctionBoundaryGraph(topology, center, radii, helpers) {
};
}
module.exports = { buildJunctionBoundaryGraph };
function ringSelfIntersects(ring) {
const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
const straddles = (p1, p2, p3, p4) => {
const d1 = cross(p3, p4, p1);
const d2 = cross(p3, p4, p2);
const d3 = cross(p1, p2, p3);
const d4 = cross(p1, p2, p4);
return ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0));
};
for (let first = 0; first < ring.length - 1; first += 1) {
for (let second = first + 2; second < ring.length - 1; second += 1) {
if (first === 0 && second === ring.length - 2) continue;
if (straddles(ring[first], ring[first + 1], ring[second], ring[second + 1])) return true;
}
}
return false;
}
function signedArea(ring) {
return ring.slice(1).reduce((area, point, index) => area + ring[index][0] * point[1] - point[0] * ring[index][1], 0);
}
// 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) {
if (topology.arms.some((arm) => arm.members.length !== 1))
return { status: 'unsupported', reason: 'parallel-carriageway-closure-unresolved' };
if (graph.sections.length < 3 || graph.sections.length > 6)
return { status: 'unsupported', reason: 'unsupported-arm-count' };
const points = graph.sections
.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);
const pairedEdges = new Map();
points.forEach((item, index) => {
if (!pairedEdges.has(item.edgeRef)) pairedEdges.set(item.edgeRef, []);
pairedEdges.get(item.edgeRef).push(index);
});
const paired = [...pairedEdges.values()].every((indexes) => {
if (indexes.length !== 2) return false;
return Math.abs(indexes[0] - indexes[1]) === 1 || (indexes[0] === 0 && indexes[1] === points.length - 1);
});
if (ring.length < 7 || !paired || Math.abs(signedArea(ring)) < 1e-14 || ringSelfIntersects(ring))
return { status: 'unsupported', reason: 'core-closure-invalid' };
return {
status: 'supported',
ring,
sharedEdgeRefs: [...pairedEdges.keys()].sort(),
closureCount: graph.sections.length,
};
}
module.exports = { buildJunctionBoundaryGraph, buildCoreClosure };