3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse invalid angle, sides and index options in mesh topology helpers (#2500)
Nguyen Xuan An2d8d179923f4
diff --git a/changes/2500.md b/changes/2500.mdnew file mode 100644index 00000000..f862460e--- /dev/null+++ b/changes/2500.md@@ -0,0 +1 @@+- Refuse invalid angle, sides and index options in joinTriangles, dissolveLimited, fillHoles and createTriangleSurfaceIndex instead of joining, filling or rounding silently (#2500, #2501, #2502).diff --git a/changes/2500.size.md b/changes/2500.size.mdnew file mode 100644index 00000000..6b3e2507--- /dev/null+++ b/changes/2500.size.md@@ -0,0 +1,2 @@+- scene:agent-scene+lazy: four option checks (about 90 bytes gzip) stop a NaN angle joining every triangle pair and a fractional index rebuilding a different surface; each reuses requireFiniteInRange or one typed-array comparison.+- goal:agent-scene: the same four option checks; agent-scene is already over its goal on main and these guards cost 90 bytes gzip.diff --git a/src/mesh-convert.js b/src/mesh-convert.jsindex fb001066..b181748d 100644--- a/src/mesh-convert.js+++ b/src/mesh-convert.js@@ -1,6 +1,7 @@import { createMeshData, deriveEdges, DOMAINS, meshBounds, TAG_PREFIX } from './mesh-data.js';import { faceNormals } from './mesh-normals.js';import { cornerCycles, cornerFaces, edgeCornerTable } from './mesh-topology.js';+import { requireFiniteInRange } from './modeling-limits.js';const NORMAL_SPLIT_TOLERANCE = 1e-4;const AXIS_TAGS = Object.freeze([@@ -220,6 +221,7 @@ function orderByError(errors, count) {* Best-shaped pairs (corners closest to right angles) join first.*/export function joinTriangles(mesh, { maxAngle = Math.PI / 180 } = {}) {+ requireFiniteInRange('joinTriangles', 'maxAngle', maxAngle, 0, Math.PI);const { normals } = faceNormals(mesh);const table = edgeCornerTable(mesh);const { nextCorner } = cornerCycles(mesh);diff --git a/src/mesh-dissolve.js b/src/mesh-dissolve.jsindex 887c8410..4b93e0ec 100644--- a/src/mesh-dissolve.js+++ b/src/mesh-dissolve.js@@ -1,4 +1,5 @@import { faceNormals } from './mesh-normals.js';+import { requireFiniteInRange } from './modeling-limits.js';import {facePolygons, buildPolygonMesh, boundaryLoops, edgeFaces, vertexNeighbors, faceOfCorner,} from './mesh-polygons.js';@@ -11,6 +12,7 @@ import {* edges. The cap copies non-tag face data from a face beside the hole and gets the tag `<name>.fill`.*/export function fillHoles(mesh, { sides = Infinity, name = 'fill' } = {}) {+ if (sides !== Infinity) requireFiniteInRange('fillHoles', 'sides', sides, 3, 4096, true);const polygons = facePolygons(mesh);let filled = 0;let skipped = 0;@@ -179,6 +181,7 @@ function edgeDirection(mesh, from, to) {* allows), then vertices left with two edges that run straight within the same angle. Faces keep their tags.*/export function dissolveLimited(mesh, edgeMask, { angle = 5, name = 'dissolve' } = {}) {+ requireFiniteInRange('dissolveLimited', 'angle', angle, 0, 180);const table = edgeFaces(mesh);const { normals } = faceNormals(mesh);const limit = Math.cos(angle * Math.PI / 180) - 1e-9;diff --git a/src/mesh-query.js b/src/mesh-query.jsindex 65efd3ea..a76a79cf 100644--- a/src/mesh-query.js+++ b/src/mesh-query.js@@ -638,5 +638,7 @@ export function createMeshPairQuery(indexA, indexB, options = {}) {/** A query index over indexed triangles in any typed or plain arrays, for callers that hold render geometry or MeshData triangles. */export function createTriangleSurfaceIndex({ positions, indices }) {- return createMeshQueryIndex({ positions: Float32Array.from(positions), indices: Uint32Array.from(indices) });+ const wholeIndices = Uint32Array.from(indices);+ if (wholeIndices.some((index, slot) => index !== indices[slot])) throw new TypeError('indices must be whole vertex numbers');+ return createMeshQueryIndex({ positions: Float32Array.from(positions), indices: wholeIndices });}diff --git a/tests/mesh-topology-option-guards.test.js b/tests/mesh-topology-option-guards.test.jsnew file mode 100644index 00000000..d62e15cd--- /dev/null+++ b/tests/mesh-topology-option-guards.test.js@@ -0,0 +1,38 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { createMeshData } from '../src/mesh-data.js';+import { joinTriangles } from '../src/mesh-convert.js';+import { dissolveLimited, fillHoles } from '../src/mesh-dissolve.js';+import { createTriangleSurfaceIndex } from '../src/mesh-query.js';++function meshFromFaces(positions, faces) {+ const faceOffsets = [0];+ const cornerVertices = [];+ for (const face of faces) {+ cornerVertices.push(...face);+ faceOffsets.push(cornerVertices.length);+ }+ return createMeshData({ positions: Float32Array.from(positions), faceOffsets: Uint32Array.from(faceOffsets), cornerVertices: Uint32Array.from(cornerVertices) });+}++const foldedPair = () => meshFromFaces([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1], [[0, 1, 2], [0, 2, 3]]);+const openTriangle = () => meshFromFaces([0, 0, 0, 1, 0, 0, 0, 1, 0], [[0, 1, 2]]);+const square = () => ({ positions: Float32Array.of(0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0), indices: [0, 1, 2, 0, 2, 3] });++test('joinTriangles refuses a maxAngle that would join a folded pair', () => {+ assert.equal(joinTriangles(foldedPair()).faceCount, 2);+ for (const maxAngle of [NaN, -1, 4]) assert.throws(() => joinTriangles(foldedPair(), { maxAngle }), /maxAngle/);+});++test('createTriangleSurfaceIndex refuses fractional and NaN indices instead of rounding them', () => {+ const { positions } = square();+ assert.equal(createTriangleSurfaceIndex(square()).info.triangleCount, 2);+ for (const bad of [2.7, NaN]) assert.throws(() => createTriangleSurfaceIndex({ positions, indices: [0, 1, 2, 0, bad, 3] }), /whole vertex numbers/);+});++test('dissolveLimited and fillHoles refuse NaN, negative and out-of-range options', () => {+ const mask = new Uint8Array(1).fill(1);+ for (const angle of [NaN, -90, 181]) assert.throws(() => dissolveLimited(openTriangle(), mask, { angle }), /angle/);+ for (const sides of [NaN, 2, 3.5]) assert.throws(() => fillHoles(openTriangle(), { sides }), /sides/);+ assert.equal(fillHoles(openTriangle(), { sides: 3 }).filled, 1);+});