3D engine that AI agents drive
Reject NURBS knots repeated beyond the order or with a zero-width last span (#841)
Nguyen Xuan Anf554877968af
diff --git a/docs/modeling.md b/docs/modeling.mdindex 0de3707a..19c4ab7b 100644--- a/docs/modeling.md+++ b/docs/modeling.md@@ -79,7 +79,8 @@ in `splines`, never both. A spline is `{ points | shape, closed, kind, weights,- NURBS `order` is an integer from 2 to 8 and at most the point count (default 4, cubic; 2 follows the control polygon, 3 withweights draws exact conic arcs). An open NURBS takes `knotMode` `clamped` (default) or `uniform` (evenly spaced knots, thecurve does not reach its end points), or explicit `knots`: point count plus order numbers that never decrease and rise- between `knots[order - 1]` and `knots[count]`, where the curve is defined; repeated knots lower the smoothness there. `knots`+ between `knots[order - 1]` and `knots[count]`, where the curve is defined; repeated knots lower the smoothness there, but none repeats more than `order` times and+ `knots[count]` stays above `knots[count - 1]`. `knots`and `knotMode` exclude each other and do not apply to a closed spline, which is periodic. Samples spread evenly over thatparameter range, so unevenly spaced knots do not change the sample count. Tests compare the points with the Cox-de Boorrecursion written out from its definition.diff --git a/src/modeling.js b/src/modeling.jsindex 8b0cc570..d099cf81 100644--- a/src/modeling.js+++ b/src/modeling.js@@ -323,6 +323,12 @@ function nurbsBasisFrom(input, count, closed, label) {if (index > 0 && knot < knots[index - 1]) throw new RangeError(`${label}.knots must not decrease; knots[${index}] ${knot} is below knots[${index - 1}] ${knots[index - 1]}.`);});if (!(knots[count] > knots[order - 1])) throw new RangeError(`${label}.knots must rise between knots[${order - 1}] and knots[${count}]; the curve is defined only there.`);+ let runLength = 1;+ for (let index = 1; index < knots.length; index += 1) {+ runLength = knots[index] === knots[index - 1] ? runLength + 1 : 1;+ if (runLength > order) throw new RangeError(`${label}.knots repeats ${knots[index]} more than order ${order} times (at knots[${index}]). Repeat a knot at most ${order} times.`);+ }+ if (!(knots[count] > knots[count - 1])) throw new RangeError(`${label}.knots[${count}] must be above knots[${count - 1}]; the curve's last span would have no width. Raise knots[${count}] or lower knots[${count - 1}].`);}return { order, knots: knots && [...knots], knotMode };}diff --git a/tests/curve-splines.test.js b/tests/curve-splines.test.jsindex f4e8ac01..107cff61 100644--- a/tests/curve-splines.test.js+++ b/tests/curve-splines.test.js@@ -367,6 +367,18 @@ test('curve takes order, knots and knotMode and rejects bad ones with what to pafor (const [options, pattern] of cases) assert.throws(() => curve(points, options), pattern);});+test('NURBS knots repeated beyond the order or leaving a zero-width last span are rejected, valid ones sample finite', () => {+ const points = [[0, 0, 0], [1, 1, 0], [2, 0, 0], [3, 1, 0], [4, 0, 0]];+ const wire = { radius: 0.05, resolution: 4, kind: 'nurbs', order: 4 };+ assert.throws(() => curve(points, { ...wire, knots: [0, 0, 0, 0, 1, 1, 1, 1, 1] }), /repeats 1 more than order 4 times/);+ assert.throws(() => curve(points, { ...wire, knots: [0, 0, 0, 0, 1, 1, 1, 1, 2] }), /knots\[5\] must be above knots\[4\]/);+ const weights = Array(points.length).fill(1);+ for (const knots of [[0, 0, 0, 0, 1, 2, 2, 2, 2], [0, 0, 0, 1, 1, 2, 2, 2, 2]]) {+ const sampled = sampleNurbs(points, weights, false, 6, { order: 4, knots });+ for (const point of sampled.points) for (const value of point) assert.ok(Number.isFinite(value));+ }+});+test('a recipe curve geometry takes order and knots', () => {const recipe = geometry => ({format: '67js.scene',