Skip to content

3D engine that AI agents drive

Harden bevel width and boolean inputs against silent bad results (#2355)

Nguyen Xuan An0d815f23554c
diff --git a/changes/2355.md b/changes/2355.md
new file mode 100644
index 00000000..f509f71f
--- /dev/null
+++ b/changes/2355.md
@@ -0,0 +1 @@
+- Mesh bevel refuses a width of 0, and mesh boolean names a null mesh or cutter or an unusable cut tag, and turns a solver failure on extreme sizes into a clear error (#2355, #2356, #2357).
diff --git a/changes/2355.size.md b/changes/2355.size.md
new file mode 100644
index 00000000..6bc0aef4
--- /dev/null
+++ b/changes/2355.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: 219 bytes gzip for four input guards and one error wrapper that stop a silent degenerate bevel, a null-mesh crash message, a silently ignored cutTag and a raw solver error.
diff --git a/src/mesh-bevel.js b/src/mesh-bevel.js
index 2692ff14..d2c15d0f 100644
--- a/src/mesh-bevel.js
+++ b/src/mesh-bevel.js
@@ -170,6 +170,7 @@ function profileDepthReach(profile) {
*/
export function bevelEdges(mesh, edgeRule, { width, segments = 1, profile: requestedProfile = 0.5, clampOverlap = true, name = 'bevel' } = {}) {
requireFiniteInRange('bevelEdges', 'width', width, 0, MAX_BEVEL_WIDTH);
+ if (width === 0) throw new RangeError('bevelEdges: width was 0; pass a number above 0, because a zero width only leaves degenerate faces.');
requireFiniteInRange('bevelEdges', 'segments', segments, 1, MAX_BEVEL_SEGMENTS, true);
requireFiniteInRange('bevelEdges', 'profile', requestedProfile, 0, 1);
const profile = Math.min(requestedProfile, MAXIMUM_PROFILE);
diff --git a/src/mesh-boolean.js b/src/mesh-boolean.js
index 685398bc..97661e63 100644
--- a/src/mesh-boolean.js
+++ b/src/mesh-boolean.js
@@ -41,6 +41,10 @@ export function meshBooleansLoaded() {
return manifoldModule !== null;
}
+function requireMeshArgument(label, value) {
+ if (!value?.faceOffsets) throw new TypeError(`meshBoolean: ${label} must be mesh data, got ${value}.`);
+}
+
function requireModule() {
if (!manifoldModule) throw new Error('Mesh booleans are not loaded yet. Await loadMeshBooleans() before running a boolean edit.');
return manifoldModule;
@@ -155,6 +159,9 @@ function polygonsFromTriangles(weldedTriangles, propertyTriangles, triangleFaceI
*/
export function meshBoolean(mesh, cutter, operation, { cutTag } = {}) {
if (!BOOLEAN_OPERATIONS.includes(operation)) throw new RangeError(`Boolean operation must be one of ${BOOLEAN_OPERATIONS.join(', ')}.`);
+ requireMeshArgument('mesh', mesh);
+ requireMeshArgument('cutter', cutter);
+ if (cutTag !== undefined && !(typeof cutTag === 'string' && cutTag)) throw new TypeError(`meshBoolean: cutTag was ${JSON.stringify(cutTag)}; pass a non-empty string.`);
const module = requireModule();
for (const [label, input] of [['The mesh', mesh], ['The cutter', cutter]]) {
const report = lintMesh(input);
@@ -182,6 +189,9 @@ export function meshBoolean(mesh, cutter, operation, { cutTag } = {}) {
resources.push(output);
if (!output.triVerts.length) throw new RangeError(`The ${operation} leaves nothing. Check the cutter's position and size against mesh_inspect bounds.`);
return rebuildOutput(mesh, output, propertyCount, cutTag);
+ } catch (error) {
+ if (error?.constructor?.name !== 'ManifoldError') throw error;
+ throw new RangeError(`The ${operation} failed in the solver (${error.message}); rescale extremely small or large parts.`);
} finally {
for (const resource of resources.reverse()) resource.delete?.();
}
diff --git a/tests/mesh-bevel.test.js b/tests/mesh-bevel.test.js
index bc9027e4..2226d48d 100644
--- a/tests/mesh-bevel.test.js
+++ b/tests/mesh-bevel.test.js
@@ -299,3 +299,7 @@ test('the capped profile is clean under default lint up to the highest segment c
assert.deepEqual(lint.problems.filter(problem => problem.severity === 'error'), []);
}
});
+
+test('bevelEdges refuses a width of 0 instead of returning degenerate faces', () => {
+ assert.throws(() => bevelEdges(cube(), { all: true }, { width: 0 }), /bevelEdges: width was 0; pass a number above 0/);
+});
diff --git a/tests/mesh-boolean.test.js b/tests/mesh-boolean.test.js
index ef377800..103b649c 100644
--- a/tests/mesh-boolean.test.js
+++ b/tests/mesh-boolean.test.js
@@ -142,3 +142,16 @@ test('a reused cutTag counts only the faces of the latest cutter', () => {
assert.equal(second.mesh.attributes.face['boolean-cutter-faces'], undefined);
assert.equal(tagCounts(second.mesh).cut, 10);
});
+
+test('meshBoolean names the argument when the mesh, cutter or cutTag is unusable', () => {
+ const cube = () => meshFromGeometry(box(0, 1));
+ assert.throws(() => meshBoolean(cube(), null, 'union'), /meshBoolean: cutter must be mesh data/);
+ assert.throws(() => meshBoolean(undefined, cube(), 'union'), /meshBoolean: mesh must be mesh data/);
+ assert.throws(() => meshBoolean(cube(), cube(), 'union', { cutTag: 5 }), /meshBoolean: cutTag was 5; pass a non-empty string/);
+ assert.throws(() => meshBoolean(cube(), cube(), 'union', { cutTag: '' }), /meshBoolean: cutTag was ""/);
+});
+
+test('meshBoolean turns a solver failure on extreme sizes into an error that says what to check', () => {
+ const tiny = transformMesh(meshFromGeometry(box(0, 1)), { translation: [0.5, 0, 0], rotation: [0, 0, 0, 1], scale: [1e-30, 1e-30, 1e-30] });
+ assert.throws(() => meshBoolean(meshFromGeometry(box(0, 1)), tiny, 'difference'), error => error instanceof RangeError && /The difference failed in the solver.*extremely small or large/.test(error.message));
+});