From 3bf2edaf993761f44377d87b190f09274680fdf4 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 9 Sep 2026 10:00:32 +0800 Subject: [PATCH] fix(junction): reject overlapping exclusion cores --- src/compile/junction-boundary-graph.js | 26 ++++++++++++++++++++- test/junction-build-plan-counterexamples.js | 9 +++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/compile/junction-boundary-graph.js b/src/compile/junction-boundary-graph.js index 83812d7..5216e91 100644 --- a/src/compile/junction-boundary-graph.js +++ b/src/compile/junction-boundary-graph.js @@ -126,6 +126,30 @@ function pointInRing(point, ring) { }, false); } +function segmentsIntersect(first, second, third, fourth) { + const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); + const firstSide = cross(first, second, third); + const secondSide = cross(first, second, fourth); + const thirdSide = cross(third, fourth, first); + const fourthSide = cross(third, fourth, second); + return ( + ((firstSide >= 0 && secondSide <= 0) || (firstSide <= 0 && secondSide >= 0)) && + ((thirdSide >= 0 && fourthSide <= 0) || (thirdSide <= 0 && fourthSide >= 0)) + ); +} + +function ringsOverlap(first, second) { + return ( + first.slice(0, -1).some((point) => pointInRing(point, second)) || + second.slice(0, -1).some((point) => pointInRing(point, first)) || + first + .slice(1) + .some((point, index) => + second.slice(1).some((other, otherIndex) => segmentsIntersect(first[index], point, second[otherIndex], other)), + ) + ); +} + // 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. @@ -157,7 +181,7 @@ function buildCoreClosure(topology, graph, center) { }); if (ring.length < 7 || !paired || Math.abs(signedArea(ring)) < 1e-14 || ringSelfIntersects(ring)) return { status: 'unsupported', reason: 'core-closure-invalid' }; - if (graph.exclusionRegions.some((region) => region.ring.some((point) => pointInRing(point, ring)))) + if (graph.exclusionRegions.some((region) => ringsOverlap(region.ring, ring))) return { status: 'unsupported', reason: 'known-exclusion-region' }; return { status: 'supported', diff --git a/test/junction-build-plan-counterexamples.js b/test/junction-build-plan-counterexamples.js index 94cc3f0..99c3d55 100644 --- a/test/junction-build-plan-counterexamples.js +++ b/test/junction-build-plan-counterexamples.js @@ -135,6 +135,15 @@ const teeWithIsland = buildJunctionBoundaryGraph( [{ id: 'island:known', ring: exclusionRing }], ); assert.equal(buildCoreClosure(tee.topology, teeWithIsland, point(0, 0)).reason, 'known-exclusion-region'); +const enclosingExclusion = [point(-30, -30), point(30, -30), point(30, 30), point(-30, 30), point(-30, -30)]; +const teeInsideExclusion = buildJunctionBoundaryGraph( + tee.topology, + point(0, 0), + { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, + tee.helpers, + [{ id: 'island:enclosing', ring: enclosingExclusion }], +); +assert.equal(buildCoreClosure(tee.topology, teeInsideExclusion, point(0, 0)).reason, 'known-exclusion-region'); // Offset and unequal-width approaches preserve individual source sections and // local section tangents rather than a single arm envelope.