3D engine that AI agents drive
Name the leg index in drive path errors (issue #653)
Nguyen Xuan An785c386652a3
diff --git a/src/agent-drive.js b/src/agent-drive.jsindex 748a9d0d..5fb7058d 100644--- a/src/agent-drive.js+++ b/src/agent-drive.js@@ -35,7 +35,7 @@ function buildAssetDrive(asset, drive, materialIds) {throw invalidField(`lights.tailLights[${index}].material`, tail.material, `the id of a material in the asset, one of ${[...materialIds].slice(0, 5).join(', ')}`);}}- const vehicle = driveAlongLegs(rig, (legs ?? [{ path }]).map(leg => ({ ...leg, path: resolvePath(leg.path) })), driveOptions);+ const vehicle = driveAlongLegs(rig, (legs ?? [{ path }]).map(leg => ({ ...leg, path: resolvePath(leg.path) })), driveOptions, legs ? undefined : () => 'path');const tailLights = lights.tailLights ?? [];return Object.freeze({rig,diff --git a/src/vehicle.js b/src/vehicle.jsindex 5aacef14..7df52443 100644--- a/src/vehicle.js+++ b/src/vehicle.js@@ -467,13 +467,13 @@ export function createVehicleRig({ parts, graph, asset, body, wheels = 'auto', f});}-function validatePoint(point, index) {+function validatePoint(point, index, pathField) {if ((!Array.isArray(point) && !ArrayBuffer.isView(point)) || point.length !== 3 || !Array.from(point).every(Number.isFinite)) {- throw invalidField(`path[${index}]`, point, 'an array of 3 finite numbers, such as [0, 0, 10]', TypeError);+ throw invalidField(`${pathField}[${index}]`, point, 'an array of 3 finite numbers, such as [0, 0, 10]', TypeError);}}-function pointsFromPath(path, pathSamples) {+function pointsFromPath(path, pathSamples, pathField) {if (Array.isArray(path)) return path.map(point => Array.from(point));if (path && typeof path.sample === 'function' && Number.isFinite(path.duration) && path.duration > 0) {const points = [];@@ -482,7 +482,7 @@ function pointsFromPath(path, pathSamples) {}return points;}- throw invalidField('path', path, 'an array of [x, y, z] points, such as [[0, 0, 0], [0, 0, 10]], or a camera path with duration and sample(time)', TypeError);+ throw invalidField(pathField, path, 'an array of [x, y, z] points, such as [[0, 0, 0], [0, 0, 10]], or a camera path with duration and sample(time)', TypeError);}function removeRepeatedPoints(points) {@@ -521,13 +521,13 @@ function splinePolyline(points) {return polyline;}-function resampleUniformly(polyline) {+function resampleUniformly(polyline, pathField) {const lengths = [0];for (let index = 1; index < polyline.length; index++) {lengths.push(lengths[index - 1] + vec3.distance(polyline[index - 1], polyline[index]));}const total = lengths.at(-1);- if (!(total > 1e-6)) throw invalidField('path', 'a path of zero length', 'points that are not all the same, such as [[0, 0, 0], [0, 0, 10]]');+ if (!(total > 1e-6)) throw invalidField(pathField, 'a path of zero length', 'points that are not all the same, such as [[0, 0, 0], [0, 0, 10]]');const intervals = Math.max(2, Math.ceil(total / Math.max(PATH_SPACING_METRES, total / MAX_PATH_SAMPLES)));const spacing = total / intervals;const positions = [];@@ -552,12 +552,12 @@ function smoothed(values, radius) {});}-function buildPathTable(path, pathSamples) {- const points = pointsFromPath(path, pathSamples);- points.forEach((point, index) => validatePoint(point, index));+function buildPathTable(path, pathSamples, pathField) {+ const points = pointsFromPath(path, pathSamples, pathField);+ points.forEach((point, index) => validatePoint(point, index, pathField));const distinct = removeRepeatedPoints(points);- if (distinct.length < 2) throw invalidField('path', `${distinct.length} distinct point`, 'at least 2 distinct points, such as [[0, 0, 0], [0, 0, 10]]');- const { positions, spacing, length } = resampleUniformly(splinePolyline(distinct));+ if (distinct.length < 2) throw invalidField(pathField, `${distinct.length} distinct point`, 'at least 2 distinct points, such as [[0, 0, 0], [0, 0, 10]]');+ const { positions, spacing, length } = resampleUniformly(splinePolyline(distinct), pathField);const headings = [];const slopes = [];positions.forEach((_, index) => {@@ -762,10 +762,10 @@ function suspensionOffsets(rig, bodyMatrix, groundHeight, travel) {* `maxLateralAcceleration`. The vehicle holds the path end once it arrives.*/export function driveAlongPath(rig, path, options = {}) {- return driveAlongLegs(rig, [{ path }], options);+ return driveAlongLegs(rig, [{ path }], options, () => 'path');}-function buildLegs(legDescriptions, settings) {+function buildLegs(legDescriptions, settings, pathFieldOf) {const legs = [];legDescriptions.forEach((description, index) => {const { path, direction = 'forward', pause = 0 } = description ?? {};@@ -774,7 +774,7 @@ function buildLegs(legDescriptions, settings) {const previous = legs.at(-1);const sign = direction === 'reverse' ? -1 : 1;const startTime = previous ? previous.endTime + pause : settings.startTime;- const table = buildPathTable(path, settings.pathSamples);+ const table = buildPathTable(path, settings.pathSamples, pathFieldOf(index));// Every leg stops at both ends except the outer ones, which keep startSpeed and endSpeed.const motion = createMotion(table, {...settings,@@ -807,11 +807,11 @@ function buildLegs(legDescriptions, settings) {* The vehicle stops at the end of every leg. A reverse leg's path is the way the car travels; the car keeps facing the other way.* `pose.distance` and `pose.speed` are signed along the car's forward axis, so they fall while reversing.*/-export function driveAlongLegs(rig, legDescriptions, options = {}) {+export function driveAlongLegs(rig, legDescriptions, options = {}, pathFieldOf = index => `legs[${index}].path`) {if (!rig || !rig.wheels || !rig.restFrame) throw new TypeError('driveAlongPath needs a rig from createVehicleRig.');if (!Array.isArray(legDescriptions) || legDescriptions.length === 0) throw invalidField('legs', legDescriptions, 'a non-empty array of { path, direction, pause }, such as [{ "path": [[0, 0, 0], [0, 0, 10]] }]', TypeError);const settings = resolveDriveOptions(rig, options);- const legs = buildLegs(legDescriptions, settings);+ const legs = buildLegs(legDescriptions, settings, pathFieldOf);const duration = legs.at(-1).endTime;const length = legs.reduce((total, leg) => total + leg.table.length, 0);const legAt = time => legs.findLast(leg => leg.startTime <= time) ?? legs[0];diff --git a/tests/actionable-errors.test.js b/tests/actionable-errors.test.jsindex f357ec41..e8412d38 100644--- a/tests/actionable-errors.test.js+++ b/tests/actionable-errors.test.js@@ -114,6 +114,8 @@ const CASES = [['drive speed below zero', badAsset(recipe => { recipe.motion = { drive: { path: DRIVE_PATH, speed: -3 } }; }), 'motion.drive.speed', '-3'],['drive steering unknown', badAsset(recipe => { recipe.motion = { drive: { path: DRIVE_PATH, speed: 3, steering: 'both' } }; }), 'motion.drive.steering', '"both"'],['drive path with one point', badAsset(recipe => { recipe.motion = { drive: { path: [[0, 0, 0]], speed: 3 } }; }), 'motion.drive.path', '"1 points"'],+ ['drive leg with a repeated point', badAsset(recipe => { recipe.motion = { drive: { legs: [{ path: DRIVE_PATH }, { path: [[1, 0, 0], [1, 0, 0]] }], speed: 3 } }; }), 'motion.drive.legs[1].path', '"1 distinct point"'],+ ['drive leg with a bad point', badAsset(recipe => { recipe.motion = { drive: { legs: [{ path: DRIVE_PATH }, { path: [[0, 0, 0], [0, 'x', 5]] }], speed: 3 } }; }), 'motion.drive.legs[1].path[1]', '[0,"x",5]'],['drive tail light signal unknown', badAsset(recipe => { recipe.motion = { drive: { path: DRIVE_PATH, speed: 3, lights: { tailLights: [{ material: 'material-1', signal: 'fog' }] } } }; }), 'motion.drive.lights.tailLights[0].signal', '"fog"'],['asset root scale zero', badAsset(recipe => { recipe.root = { scale: [0, 1, 1] }; }), 'root.scale', '[0,1,1]'],['asset id not hexadecimal', badAsset(recipe => { recipe.assetId = 'zz'; }), 'assetId', '"zz"'],