3D engine that AI agents drive
Leave parts with non-finite bounds out of the coplanar search (#839)
Nguyen Xuan An4b12fe673fec
diff --git a/changes/839-coplanar-push.md b/changes/839-coplanar-push.mdnew file mode 100644index 00000000..bcbdb1e3--- /dev/null+++ b/changes/839-coplanar-push.md@@ -0,0 +1 @@+- The coplanar diagnostics no longer throw "Cannot read properties of undefined (reading 'push')" mid-render when parts with non-finite bounds (a NaN vertex, an overflowed pose) share a plane; such parts are left out of the search (#839).diff --git a/src/coplanar-overlap.js b/src/coplanar-overlap.jsindex 84059ce3..ca6571db 100644--- a/src/coplanar-overlap.js+++ b/src/coplanar-overlap.js@@ -343,11 +343,15 @@ export function findCoplanarOverlaps(parts, { tolerance = COPLANAR_TOLERANCE, wo// report as a pair, and the rest are left out, so a stack of n costs 2 parts and not n * n / 2 pairs.const stack = stacks.get(part.geometry) ?? new Map();stacks.set(part.geometry, stack);+ // A NaN or infinite bound (a stray vertex, a pose that overflowed) cannot be placed in space; its box would turn the+ // priority bin index into NaN. Such a part has no place to fight in, so it is left out.+ const bounds = worldBounds(part);+ if (!bounds.every(Number.isFinite)) continue;const pose = Array.prototype.join.call(part.matrix, ',');const copies = stack.get(pose) ?? 0;if (copies >= 2) continue;stack.set(pose, copies + 1);- entries.push({ part, bounds: worldBounds(part) });+ entries.push({ part, bounds });}const budget = { work: 0, limit: workBudget };const scanBudget = { triangles: triangleBudget, exhausted: false };diff --git a/tests/coplanar-overlap.test.js b/tests/coplanar-overlap.test.jsindex 2909da7b..2db64ea6 100644--- a/tests/coplanar-overlap.test.js+++ b/tests/coplanar-overlap.test.js@@ -274,3 +274,28 @@ test('ranks settle in chains, and a stack deeper than the ranks is reported instconst onOnePlane = Array.from({ length: 20 }, (unused, index) => gridPart(`plane${index}`, 20 - index, 1, 0));assert.deepEqual(resolveFlushParts(onOnePlane).leftovers, [], 'on one plane the hidden faces are cut away, however deep the stack');});++// #839: a part whose geometry holds a NaN vertex that no triangle uses has NaN bounds but real triangles. Two of them landed in+// one plane bucket, the priority bin index became NaN and the bin lookup threw "Cannot read properties of undefined (reading+// 'push')" partway through a clip, once enough instances had spawned.+function particleWithStrayVertex(name, x) {+ const part = gridPart(name, 0.2, 1, 0);+ const positions = new Float32Array(part.geometry.positions.length + 3);+ positions.set(part.geometry.positions);+ positions.fill(Number.NaN, part.geometry.positions.length);+ return movedPart({ ...part, geometry: { ...part.geometry, positions } }, name, x, 3);+}++test('parts with non-finite bounds are skipped, and the finite parts report as before at every instance count', () => {+ const floor = gridPart('floor', 20, 4, 0);+ const marking = gridPart('marking', 2, 1, 0.0004);+ const particle = gridPart('particle', 0.2, 1, 0);+ for (const count of [10, 94, 400]) {+ const finite = Array.from({ length: count }, (unused, index) => movedPart(particle, `particle${index}`, index * 0.3, 3));+ const broken = [particleWithStrayVertex('brokenOne', 0), particleWithStrayVertex('brokenTwo', 0.1)];+ const withBroken = findCoplanarOverlaps([floor, marking, ...finite, ...broken]);+ const without = findCoplanarOverlaps([floor, marking, ...finite]);+ assert.deepEqual(withBroken.overlaps.map(overlap => overlap.smaller.name), without.overlaps.map(overlap => overlap.smaller.name));+ assert.ok(withBroken.overlaps.some(overlap => overlap.smaller.name === 'marking'));+ }+});