3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Keep repeated curve points from zeroing directions or multiplying points (#2345)
Nguyen Xuan Anebdd3893c486
diff --git a/changes/2345.md b/changes/2345.mdnew file mode 100644index 00000000..acf41269--- /dev/null+++ b/changes/2345.md@@ -0,0 +1 @@+- Curves with a repeated point keep a unit direction when resampled and stay one point when filleted, and a mesh edge loop that meets a junction becomes a closed spline (#2345, #2346, #2347).diff --git a/src/curve-operations.js b/src/curve-operations.jsindex 19e4dda0..c7b5350f 100644--- a/src/curve-operations.js+++ b/src/curve-operations.js@@ -44,6 +44,13 @@ function segmentsOf(points, closed) {return segments;}+// A zero-length segment has no direction of its own: it takes the next real segment's, or the last real one's at the end.+function directionOfSegment(segments, index) {+ const isReal = candidate => candidate.length >= MINIMUM_LENGTH;+ const segment = segments.slice(index).find(isReal) ?? segments.findLast(isReal);+ return unit(subtract(segment.to, segment.from));+}+/** Total length of a polyline. */export function curveLength(points, closed = false) {return segmentsOf(points, closed).reduce((total, segment) => total + segment.length, 0);@@ -72,7 +79,7 @@ export function resampleCurve(points, count, closed = false) {const along = segment.length < MINIMUM_LENGTH ? 0 : Math.min(1, Math.max(0, (distance - segmentStart) / segment.length));samples.push({point: addScaled(segment.from, subtract(segment.to, segment.from), along),- direction: unit(subtract(segment.to, segment.from)),+ direction: directionOfSegment(segments, segmentIndex),});}return samples;@@ -81,6 +88,8 @@ export function resampleCurve(points, count, closed = false) {function filletCorner(previous, corner, next, radius, arcSegments) {const toPrevious = unit(subtract(previous, corner));const toNext = unit(subtract(next, corner));+ // A repeated point has no arm to round: the corner stays one point instead of becoming a run of copies.+ if (lengthOf(subtract(previous, corner)) < MINIMUM_LENGTH || lengthOf(subtract(next, corner)) < MINIMUM_LENGTH) return [corner];const cosine = dot(toPrevious, toNext);if (cosine <= -STRAIGHT_LIMIT || cosine >= STRAIGHT_LIMIT) return [corner];const halfAngle = Math.acos(cosine) / 2;diff --git a/src/mesh-to-curve.js b/src/mesh-to-curve.jsindex fe6e8c06..58c86b72 100644--- a/src/mesh-to-curve.js+++ b/src/mesh-to-curve.js@@ -94,7 +94,10 @@ function chainsOf(adjacency) {if (adjacency.get(start).length === 2) continue;for (const step of adjacency.get(start)) {if (usedEdges.has(step[1])) continue;- chains.push({ vertices: walk(start, step, adjacency, usedEdges), closed: false });+ const vertices = walk(start, step, adjacency, usedEdges);+ // A loop that leaves a junction and returns to it is closed: drop the repeated end vertex instead of doubling it.+ const returnsToStart = vertices.length > 3 && vertices.at(-1) === start;+ chains.push({ vertices: returnsToStart ? vertices.slice(0, -1) : vertices, closed: returnsToStart });}}// What is left is closed loops; each starts at its lowest vertex.diff --git a/tests/curve-degenerate-points.test.js b/tests/curve-degenerate-points.test.jsnew file mode 100644index 00000000..eb7eeeb1--- /dev/null+++ b/tests/curve-degenerate-points.test.js@@ -0,0 +1,42 @@+import './support/load-agent-scene-modules.mjs';+import assert from 'node:assert/strict';+import test from 'node:test';+import { filletCurve, resampleCurve } from '../src/curve-operations.js';+import { grid } from '../src/geometry.js';+import { meshFromGeometry, meshEdgesToSplines } from '../src/mesh.js';++test('resampling a curve that starts or ends on a repeated point still gives every sample a unit direction (#2345)', () => {+ const leading = resampleCurve([[0, 0, 0], [0, 0, 0], [1, 0, 0]], 3);+ assert.deepEqual(leading.map(sample => sample.direction), [[1, 0, 0], [1, 0, 0], [1, 0, 0]]);+ const middle = resampleCurve([[0, 0, 0], [1, 0, 0], [1, 0, 0], [1, 1, 0]], 4);+ assert.ok(middle.every(sample => Math.abs(Math.hypot(...sample.direction) - 1) < 1e-9));+ const loop = resampleCurve([[0, 0, 0], [0, 0, 0], [1, 0, 0], [1, 1, 0]], 4, true);+ assert.ok(loop.every(sample => Math.abs(Math.hypot(...sample.direction) - 1) < 1e-9));+});++test('a repeated point stays one point when the corners are filleted (#2346)', () => {+ const filleted = filletCurve([[0, 0, 0], [1, 0, 0], [1, 0, 0], [1, 1, 0]], 0.2, 4);+ assert.equal(filleted.length, 4);+ const closed = filletCurve([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 0, 0]], 0.2, 4, true);+ assert.ok(closed.every(point => point.every(Number.isFinite)));+});++test('a loop that meets a junction is a closed spline without a repeated end point (#2347)', () => {+ const mesh = meshFromGeometry(grid(3));+ const edgeBetween = (first, second) => {+ for (let edge = 0; edge < mesh.edgeCount; edge++) {+ const pair = [mesh.edgeVertices[edge * 2], mesh.edgeVertices[edge * 2 + 1]];+ if (pair.includes(first) && pair.includes(second)) return edge;+ }+ throw new Error(`no edge between ${first} and ${second}`);+ };+ const vertex = (column, row) => row * 4 + column;+ const loop = [[vertex(0, 0), vertex(1, 0)], [vertex(1, 0), vertex(1, 1)], [vertex(1, 1), vertex(0, 1)], [vertex(0, 1), vertex(0, 0)]];+ const withTail = [...loop, [vertex(1, 1), vertex(2, 1)]];+ const splines = meshEdgesToSplines(mesh, { edges: withTail.map(([first, second]) => edgeBetween(first, second)) });+ const closedLoop = splines.find(spline => spline.closed);+ assert.ok(closedLoop, 'the loop is a closed spline');+ assert.equal(closedLoop.points.length, 4);+ assert.equal(new Set(closedLoop.points.map(point => point.join())).size, 4);+ assert.equal(splines.filter(spline => !spline.closed).length, 1, 'the tail stays open');+});