3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Name the field in curve radius and closed errors (#2089)
Nguyen Xuan An892511b65cbe
diff --git a/changes/2089.md b/changes/2089.mdnew file mode 100644index 00000000..241019c5--- /dev/null+++ b/changes/2089.md@@ -0,0 +1 @@+- Fix: `curve` and `curveSet` errors for a bad `radius` (0 or less) or a `closed` flag that is not true or false now name only that field (`radius`, `closed`, `splines[1].closed`; `geometries[i]` first in a scene recipe), the value received and an example, instead of one shared sentence that blamed both (#2089)diff --git a/docs/modeling.md b/docs/modeling.mdindex b1ee141d..6eb8042e 100644--- a/docs/modeling.md+++ b/docs/modeling.md@@ -62,6 +62,7 @@ sweeps a cross-section along a chain of cubic Bezier spans, the way a Blender cu- `profile` is 3 to 256 `[x, y]` points of a closed cross-section in units of `radius`, either winding. Without it thesection is a circle of `radialSegments` sides (3 to 128). Corners sharper than 40 degrees stay flat shaded.- Open curves get flat caps with their own vertices, so the surface is closed and works with booleans.+- A `radius` of 0 or less, or a `closed` flag that is not true or false, throws an error that names that one field (`radius`, `closed` or `splines[1].closed`), the value received and an example; in a scene recipe the field starts with `geometries[i]`.Volume, area and bounds match Blender 5.2 for an open and a closed Bezier curve with a round bevel to 2e-5(`tests/blender-curve-text.test.js`). A Blender bevel resolution of `n` is `radialSegments: 4 + 2n`.diff --git a/src/agent-scene.js b/src/agent-scene.jsindex 3276a50b..b92f18b8 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -617,6 +617,9 @@ function geometryEstimate(value, scope = PENDING_CUTTER_SCOPE) {if (!Array.isArray(knots) || knots.length < 2 || knots.length > CURVE_TEXT_LIMITS.maxCurvePoints) {throw invalidField('points', Array.isArray(knots) ? `${knots.length} points` : knots, `a list of 2 to ${CURVE_TEXT_LIMITS.maxCurvePoints} points, such as [[0, 0, 0], [0, 1, 0]]`);}+ const curveChecks = agentSceneModule('modeling');+ curveChecks.checkCurveRadius(value.radius ?? 0.05);+ curveChecks.checkClosedFlag(value.closed ?? false);const sides = value.profile === undefined ? detail('radialSegments', 8, 3, 128) : points(value.profile, 2, 3, 'profile');const rings = (value.closed ? knots.length : knots.length - 1) * detail('resolution', 12, 1, 64) + 1;return rings * sides * 2 + 2 * (sides + 1);diff --git a/src/modeling.js b/src/modeling.jsindex f2a1c60a..5c511362 100644--- a/src/modeling.js+++ b/src/modeling.js@@ -355,6 +355,17 @@ function nurbsBasisFrom(input, count, closed, label) {return { order, knots: knots && [...knots], knotMode };}+/** Check a curve's closed flag. A single curve (label points) names the field closed, a spline in a list names splines[i].closed. */+export function checkClosedFlag(closed, label = 'points') {+ const field = label === 'points' ? 'closed' : `${label}.closed`;+ if (typeof closed !== 'boolean') throw invalidField(field, closed, 'true or false, such as false', TypeError);+}++/** Check a curve's tube radius. Errors name the field, so a scene recipe adds geometries[i]. */+export function checkCurveRadius(radius) {+ if (!(radius > 0)) throw invalidField('radius', radius, 'a number above 0, such as 0.05');+}+// One spline: points or a primitive shape, a type (bezier, poly, nurbs), closed, NURBS weights and a trim.function splineFrom(input, label) {if (!input || typeof input !== 'object' || Array.isArray(input)) throw new TypeError(`${label} must be an object with points or shape.`);@@ -367,7 +378,7 @@ function splineFrom(input, label) {if (input.closed !== undefined || input.kind !== undefined) throw new TypeError(`${label}.closed and ${label}.kind come from shape. Remove them.`);({ points, closed, type: kind } = shapeToSpline(input.shape));}- if (typeof closed !== 'boolean') throw new RangeError('radius must be positive and closed must be boolean.');+ checkClosedFlag(closed, label);if (!SPLINE_TYPES.includes(kind)) throw new RangeError(`${label}.kind must be one of ${SPLINE_TYPES.join(', ')}.`);if (!Array.isArray(points) || points.length < (closed ? 3 : 2) || points.length > MAX_CURVE_POINTS) {throw new RangeError(`${label === 'points' ? 'points' : `${label}.points`} needs ${closed ? 3 : 2} to ${MAX_CURVE_POINTS} entries.`);@@ -437,7 +448,7 @@ function planCurveSet(splines, {radius = 0.05, taper, profile, resolution = 12, radialSegments = 8,}, labelFirst, growing = false) {radius = finite(radius, 'radius');- if (radius <= 0) throw new RangeError('radius must be positive and closed must be boolean.');+ checkCurveRadius(radius);integer(resolution, 1, 64, 'resolution');integer(radialSegments, 3, 128, 'radialSegments');if (!Array.isArray(splines) || splines.length < 1 || splines.length > MAX_SPLINES) throw new RangeError(`splines needs 1 to ${MAX_SPLINES} entries.`);diff --git a/tests/curve-error-fields.test.js b/tests/curve-error-fields.test.jsnew file mode 100644index 00000000..7e5678d6--- /dev/null+++ b/tests/curve-error-fields.test.js@@ -0,0 +1,45 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { createAgentScene, loadAllAgentSceneModules } from '../src/agent-scene.js';+import { curve, curveSet } from '../src/modeling.js';++await loadAllAgentSceneModules();++const POINTS = [[0, 0, 0], [1, 0, 0], [1, 1, 0]];++function recipeWith(geometry) {+ return {+ format: 'crucible.scene', schemaVersion: 1,+ geometries: [{ id: 'box', type: 'box', bevel: 0 }, { id: 'part', type: 'curve', ...geometry }],+ materials: [{ id: 'paint', color: [0.5, 0.5, 0.5] }],+ nodes: [{ id: 'part', geometry: 'part', material: 'paint' }],+ };+}++// Each row: the geometry, the field path the agent must see, what it received, the example and the field that must not be blamed.+const CASES = [+ ['curve with a closed flag that is not a boolean', { points: POINTS, closed: 'yes' }, 'geometries[1].closed', '"yes"', 'true or false, such as false', /radius/],+ ['curve with radius 0', { points: POINTS, radius: 0 }, 'geometries[1].radius', '0', 'a number above 0, such as 0.05', /closed/],+ ['curve with a negative radius', { points: POINTS, radius: -2 }, 'geometries[1].radius', '-2', 'a number above 0, such as 0.05', /closed/],+ ['curve with splines and a closed flag that is not a boolean', { splines: [{ points: POINTS }, { points: POINTS, closed: 1 }] }, 'geometries[1].splines[1].closed', '1', 'true or false, such as false', /radius/],+ ['curve with splines and radius 0', { splines: [{ points: POINTS }], radius: 0 }, 'geometries[1].radius', '0', 'a number above 0, such as 0.05', /closed/],+ ['curve with kind and a closed flag that is not a boolean', { points: POINTS, kind: 'poly', closed: null }, 'geometries[1].closed', 'null', 'true or false, such as false', /radius/],+];++for (const [name, geometry, path, received, example, notBlamed] of CASES) {+ test(`${name} names its field, the value received and an example`, () => {+ assert.throws(() => createAgentScene(recipeWith(geometry), {}, {}), error => {+ assert.ok(error.message.startsWith(`${path}: got ${received}; expected ${example}`), `message was: ${error.message}`);+ assert.equal(error.fieldPath, path);+ assert.doesNotMatch(error.message.slice(path.length), notBlamed);+ return true;+ });+ });+}++test('the curve builders called directly name the field without a geometries index', () => {+ assert.throws(() => curve(POINTS, { closed: 'yes' }), /^TypeError: closed: got "yes"; expected true or false, such as false\.$/);+ assert.throws(() => curve(POINTS, { radius: 0 }), /^RangeError: radius: got 0; expected a number above 0, such as 0\.05\.$/);+ assert.throws(() => curveSet([{ points: POINTS }, { points: POINTS, closed: 'no' }]), /^TypeError: splines\[1\]\.closed: got "no"; expected true or false/);+ assert.throws(() => curveSet([{ points: POINTS }], { radius: -1 }), /^RangeError: radius: got -1; expected a number above 0/);+});