From e5d805a369323563368e5699183c37a64b58d2d8 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 9 Sep 2026 11:44:51 +0800 Subject: [PATCH] fix(junction): reject collapsed core boundary edges --- src/compile/junction-boundary-graph.js | 8 +++++++- test/junction-build-plan-counterexamples.js | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/compile/junction-boundary-graph.js b/src/compile/junction-boundary-graph.js index b2e6ffd..baabbf6 100644 --- a/src/compile/junction-boundary-graph.js +++ b/src/compile/junction-boundary-graph.js @@ -174,6 +174,12 @@ function buildCoreClosure(topology, graph, center) { 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 hasZeroLengthEdge = ring.slice(1).some((point, index) => { + const previous = ring[index]; + const dx = point[0] - previous[0]; + const dy = point[1] - previous[1]; + return dx * dx + dy * dy <= 1e-24; + }); const pairedEdges = new Map(); points.forEach((item, index) => { if (!pairedEdges.has(item.edgeRef)) pairedEdges.set(item.edgeRef, []); @@ -183,7 +189,7 @@ function buildCoreClosure(topology, graph, center) { 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)) + if (ring.length < 7 || hasZeroLengthEdge || !paired || Math.abs(signedArea(ring)) < 1e-14 || ringSelfIntersects(ring)) return { status: 'unsupported', reason: 'core-closure-invalid' }; if (graph.exclusionRegions.some((region) => ringsOverlap(region.ring, ring))) return { status: 'unsupported', reason: 'known-exclusion-region' }; diff --git a/test/junction-build-plan-counterexamples.js b/test/junction-build-plan-counterexamples.js index 68df1fd..8cdd38d 100644 --- a/test/junction-build-plan-counterexamples.js +++ b/test/junction-build-plan-counterexamples.js @@ -314,3 +314,10 @@ assert.deepEqual( reorderedTopology.arms.map((arm) => arm.key).sort(), sixArm.topology.arms.map((arm) => arm.key).sort(), ); + +// A collapsed inner seam is a geometric failure even if the remaining ring +// still encloses a positive area. The closure must reject zero-length edges. +const collapsedGraph = JSON.parse(JSON.stringify(teeBoundaryGraph)); +const collapsedSection = collapsedGraph.sections[0]; +collapsedSection.inner.right = collapsedSection.inner.left; +assert.equal(buildCoreClosure(tee.topology, collapsedGraph, point(0, 0)).reason, 'core-closure-invalid');