3D engine that AI agents drive
Cap the coplanar face area so an overflowing shared box cannot index past the bins (#839)
Nguyen Xuan An1034a98970ec
diff --git a/changes/839-coplanar-overflow.md b/changes/839-coplanar-overflow.mdnew file mode 100644index 00000000..333dece9--- /dev/null+++ b/changes/839-coplanar-overflow.md@@ -0,0 +1 @@+- The coplanar search no longer throws "reading 'push'" for a touching pair whose shared box overflows to Infinity (a second route to a NaN priority bin, after non-finite bounds).diff --git a/src/coplanar-overlap.js b/src/coplanar-overlap.jsindex ca6571db..965841b7 100644--- a/src/coplanar-overlap.js+++ b/src/coplanar-overlap.js@@ -365,7 +365,9 @@ export function findCoplanarOverlaps(parts, { tolerance = COPLANAR_TOLERANCE, woconst region = boundsOverlapRegion(first.bounds, second.bounds, tolerance);if (!region || resolvedByLayer(first.part, second.part) || listed >= MAXIMUM_LISTED_PAIRS) return;const extents = [region[3] - region[0], region[4] - region[1], region[5] - region[2]].sort((smallest, largest) => smallest - largest);- const faceArea = extents[1] * extents[2];+ // Finite bounds can still overflow when subtracted (a flat box 1e308 long): 0 * Infinity is NaN, and a NaN rank would index+ // past the bins. The face area is capped so the rank stays a number.+ const faceArea = Math.min(extents[1] * extents[2] || 0, Number.MAX_VALUE);const flatBonus = extents[0] <= tolerance ? PRIORITY_BINS / 2 : 0;const sizeRank = Math.max(0, Math.min(PRIORITY_BINS / 2 - 1, Math.floor(Math.log10(Math.max(faceArea, 1e-6)) + 6)));bins[PRIORITY_BINS - 1 - (flatBonus + sizeRank)].push(first, second, region);diff --git a/tests/coplanar-overlap.test.js b/tests/coplanar-overlap.test.jsindex 2db64ea6..6525c519 100644--- a/tests/coplanar-overlap.test.js+++ b/tests/coplanar-overlap.test.js@@ -299,3 +299,17 @@ test('parts with non-finite bounds are skipped, and the finite parts report as bassert.ok(withBroken.overlaps.some(overlap => overlap.smaller.name === 'marking'));}});++// #839: bounds can be finite while the shared box of two parts overflows. A flat touching pair 1e308 long gave a face area of+// 0 * Infinity, so the priority bin index was NaN and the bin lookup threw "reading 'push'".+function overflowingStrip(name, firstZ) {+ const reach = 1e308;+ const positions = new Float64Array([-reach, 0, firstZ, reach, 0, firstZ, reach, 0, firstZ + 1, -reach, 0, firstZ + 1]);+ const matrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);+ return { name, geometry: { positions, indices: new Uint32Array([0, 1, 2, 0, 2, 3]) }, matrix, material: {} };+}++test('a touching pair whose shared box overflows is searched without throwing', () => {+ const strips = [overflowingStrip('stripOne', 0), overflowingStrip('stripTwo', 1)];+ assert.doesNotThrow(() => findCoplanarOverlaps(strips));+});