3D engine that AI agents drive
Give the opt-out pair report the flush search's reach (#943)
Nguyen Xuan An1afd9d8e274d
diff --git a/changes/943-optout-coplanar-reach.md b/changes/943-optout-coplanar-reach.mdnew file mode 100644index 00000000..6c40975b--- /dev/null+++ b/changes/943-optout-coplanar-reach.md@@ -0,0 +1 @@+- With `resolveCoplanar: false`, the near-flush pair report now reaches as far as the flush search (2 mm or more by scene size, never under 1 mm), so a 1.5 mm pair is reported; the pair count in the report stays bounded as before (#943).diff --git a/src/coplanar-overlap.js b/src/coplanar-overlap.jsindex 965841b7..0cab35d4 100644--- a/src/coplanar-overlap.js+++ b/src/coplanar-overlap.js@@ -1,6 +1,6 @@import { depthLayerOf, flushOrderOf, setFlushOrder } from './depth-layer.js';import { resolveFlush } from './flush-resolve.js';-import { boundsDiagonal as diagonal, determinantSign, worldBounds } from './flush-search.js';+import { boundsDiagonal as diagonal, determinantSign, nearTolerance, worldBounds } from './flush-search.js';// Two jobs. resolveFlushParts ranks near-coplanar triangles so they stop z-fighting (flush-search.js, flush-resolve.js); the// renderer runs it once on the parts it is built with, and the agent scene whenever its parts change. findCoplanarOverlaps is@@ -333,8 +333,10 @@ function forEachCandidatePair(entries, tolerance, scanBudget, test) {* material?, decal?, depthLayer?, skin?, morph? }`; skinned and morphed parts are skipped. Returns* `{ overlaps, truncated, work }` with each overlap as `{ smaller, larger, area, distance, normal }` (areas in square* metres, the larger part by bounds diagonal). `work` counts bounds and triangle tests, capped by `workBudget`.+ * Unless `tolerance` is given, the reach is the flush search's (nearTolerance of the scene bounds, never below+ * COPLANAR_TOLERANCE), so a scene that opted out hears about every pair the ranking would have settled.*/-export function findCoplanarOverlaps(parts, { tolerance = COPLANAR_TOLERANCE, workBudget = DEFAULT_WORK_BUDGET, triangleBudget = DEFAULT_TRIANGLE_BUDGET } = {}) {+export function findCoplanarOverlaps(parts, { tolerance: requestedTolerance = null, workBudget = DEFAULT_WORK_BUDGET, triangleBudget = DEFAULT_TRIANGLE_BUDGET } = {}) {const entries = [];const stacks = new Map();for (const part of parts) {@@ -353,6 +355,7 @@ export function findCoplanarOverlaps(parts, { tolerance = COPLANAR_TOLERANCE, wostack.set(pose, copies + 1);entries.push({ part, bounds });}+ const tolerance = requestedTolerance ?? Math.max(COPLANAR_TOLERANCE, nearTolerance(diagonal(sceneBoundsOf(entries))));const budget = { work: 0, limit: workBudget };const scanBudget = { triangles: triangleBudget, exhausted: false };const overlaps = [];@@ -389,6 +392,17 @@ export function findCoplanarOverlaps(parts, { tolerance = COPLANAR_TOLERANCE, woreturn { overlaps, truncated, work: budget.work };}+function sceneBoundsOf(entries) {+ const scene = [Infinity, Infinity, Infinity, -Infinity, -Infinity, -Infinity];+ for (const { bounds } of entries) {+ for (let axis = 0; axis < 3; axis++) {+ scene[axis] = Math.min(scene[axis], bounds[axis]);+ scene[3 + axis] = Math.max(scene[3 + axis], bounds[3 + axis]);+ }+ }+ return scene;+}+function metres(value) {return `${Math.round(value * 1000) / 1000}`;}diff --git a/tests/coplanar-overlap.test.js b/tests/coplanar-overlap.test.jsindex 6525c519..8d29c33c 100644--- a/tests/coplanar-overlap.test.js+++ b/tests/coplanar-overlap.test.js@@ -1,7 +1,7 @@import test from 'node:test';import assert from 'node:assert/strict';import { createAgentScene } from '../src/agent-scene.js';-import { clearFlushParts, findCoplanarOverlaps, coplanarOverlapDiagnostics, flushLeftoverDiagnostics, installCoplanarDiagnostics, resolveFlushParts } from '../src/coplanar-overlap.js';+import { COPLANAR_TOLERANCE, clearFlushParts, findCoplanarOverlaps, coplanarOverlapDiagnostics, flushLeftoverDiagnostics, installCoplanarDiagnostics, resolveFlushParts } from '../src/coplanar-overlap.js';import { depthLayerOf, flushOrderOf } from '../src/depth-layer.js';import { loadAgentSceneModules } from '../src/agent-scene-modules.js';import { frameDiagnostics } from '../src/frame-diagnostics.js';@@ -259,7 +259,7 @@ test('ranks settle in chains, and a stack deeper than the ranks is reported instconst big = gridPart('chainBig', 1, 1, 0);const middle = gridPart('chainMiddle', 0.5, 1, step);const small = gridPart('chainSmall', 0.2, 1, step * 2);- assert.equal(findCoplanarOverlaps([small, middle, big]).overlaps.length, 0, 'wider apart than the overlap check looks');+ assert.equal(findCoplanarOverlaps([small, middle, big], { tolerance: COPLANAR_TOLERANCE }).overlaps.length, 0, 'wider apart than the 1 mm check');const rankOf = part => flushOrderOf(part)?.ranges.at(-1).rank ?? 0;assert.deepEqual(resolveFlushParts([small, middle, big]), { ranked: 2, leftovers: [] });assert.deepEqual([big, middle, small].map(rankOf), [0, 1, 2]);@@ -313,3 +313,20 @@ test('a touching pair whose shared box overflows is searched without throwing',const strips = [overflowingStrip('stripOne', 0), overflowingStrip('stripTwo', 1)];assert.doesNotThrow(() => findCoplanarOverlaps(strips));});++test('the pair report reaches as far as the flush search, so a 1.5 mm pair is reported when the ranking is off', () => {+ const base = gridPart('base', 4, 4, 0);+ const nearFlush = gridPart('nearFlush', 2, 2, 0.0015);+ const { overlaps } = findCoplanarOverlaps([base, nearFlush]);+ assert.equal(overlaps.length, 1);+ assert.ok(Math.abs(overlaps[0].distance - 0.0015) < 1e-6);+ assert.equal(resolveFlushParts([base, nearFlush]).ranked, 1);+ const farApart = gridPart('farApart', 2, 2, 0.01);+ assert.deepEqual(findCoplanarOverlaps([base, farApart]).overlaps, []);+});++test('with resolveCoplanar false, a 1.5 mm near-flush pair of nodes is reported by scene_inspect', () => {+ const warnings = coplanarWarnings([wall, node('plate', [0, 0, 0.0015], [1, 0.5, 0.2])]);+ assert.equal(warnings.length, 1);+ assert.deepEqual([...warnings[0].parts].sort(), ['plate', 'wall']);+});