An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.
fix(lines): bound cubic and quadratic curve sampling in screen space
Rudy Celeklid0fc75b3c07c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8c9e657..63730ce 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha### Fixed+- Line-end audits follow cubic and quadratic strokes with a bounded screen-space approximation, so lines attached between curve samples are not reported as loose.+- `capture.mjs` waits for the page to load before it touches it, even with `--wait 0`, and gives up with an error when Chrome leaves a request unanswered for 60 s instead of hanging.- `drive.mjs` `until` times a condition inside the page, so one that is already true passes however short the wait, even on a slow machine; a promise that never settles still times out.- Start headless Chrome on Windows from an elevated terminal: `capture.mjs`, `drive.mjs` and `turn-check.mjs` pass `--do-not-de-elevate` there. Chrome otherwise relaunches itself without the elevation, the process the script started exits with code 0, and the check stops with `Chrome exited before startup: 0` while the relaunched browser stays running.diff --git a/scripts/curve-density.test.mjs b/scripts/curve-density.test.mjsnew file mode 100644index 0000000..73fe0da--- /dev/null+++ b/scripts/curve-density.test.mjs@@ -0,0 +1,59 @@+import assert from "node:assert/strict";+import { execFile } from "node:child_process";+import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { promisify } from "node:util";+import test from "node:test";++const execute = promisify(execFile);+for (const [name, path, x, y] of [+ ["cubic between fixed samples", "M20 400C20 20 480 20 480 400", 25.166015625, 333.203125],+ ["quadratic between fixed samples", "M20 400Q250 20 480 400", 58.33333333333333, 341.94444444444446],+]) test(`native SVG ${name} roots an attached line`, async () => {+ const directory = await mkdtemp(join(tmpdir(), "anatomy-curve-density-"));+ try {+ const file = join(directory, "figure.html");+ await writeFile(file, `<!doctype html><div class="iso-plate"><svg width="520" height="440" viewBox="0 0 520 440"><path id="curve" fill="none" stroke="black" d="${path}"/><path class="iso-line" data-free="start" fill="none" stroke="black" d="M${x} 10L${x} ${y}"/></svg></div>`);+ const actions = JSON.stringify([["eval", `document.querySelector('#curve').isPointInStroke(new DOMPoint(${x},${y}))`], ["lines"]]);+ let result;+ try { result = { code: 0, ...await execute(process.execPath, ["scripts/drive.mjs", file, actions], { timeout: 30000 }) }; }+ catch (error) { result = { code: error.code, stdout: error.stdout, stderr: error.stderr }; }+ assert.match(result.stdout, /eval: true/);+ assert.match(result.stdout, /line ends: (?:0|1) of 1/);+ assert.equal(result.code, 0, `native audit exit expected 0 received ${result.code}\n${result.stdout}`);+ assert.match(result.stdout, /line ends: 0 of 1/);+ } finally { await rm(directory, { recursive: true, force: true }); }+});++for (const [name, path, x, y, transform, attached] of [+ ["nonuniform transformed cubic", "M20 400C20 20 480 20 480 400", 25.166015625, 333.203125, "matrix(3 0 0 0.5 0 0)", true],+ ["collinear overshooting cubic", "M20 120C400 120 400 120 100 120", 315, 120, "", true],+ ["detached cubic endpoint", "M20 400C20 20 480 20 480 400", 45.166015625, 333.203125, "", false],+]) test(`native SVG ${name} retains actual attachment`, async () => {+ const directory = await mkdtemp(join(tmpdir(), "anatomy-curve-bound-"));+ try {+ const file = join(directory, "figure.html");+ await writeFile(file, `<!doctype html><div class="iso-plate"><svg width="1560" height="440" viewBox="0 0 1560 440"><g transform="${transform}"><path id="curve" fill="none" stroke="black" d="${path}"/><path class="iso-line" data-free="start" fill="none" stroke="black" d="M${x} 10L${x} ${y}"/></g></svg></div>`);+ const actions = JSON.stringify([["eval", `document.querySelector('#curve').isPointInStroke(new DOMPoint(${x},${y}))`], ["lines"]]);+ let result;+ try { result = { code: 0, ...await execute(process.execPath, ["scripts/drive.mjs", file, actions], { timeout: 30000 }) }; }+ catch (error) { result = { code: error.code, stdout: error.stdout, stderr: error.stderr }; }+ assert.match(result.stdout, new RegExp(`eval: ${attached}`));+ assert.equal(result.code, attached ? 0 : 1, result.stdout);+ assert.match(result.stdout, new RegExp(`line ends: ${attached ? 0 : 1} of 1`));+ } finally { await rm(directory, { recursive: true, force: true }); }+});++test("native audit refuses curves whose approximation budget cannot prove the bound", async () => {+ const directory = await mkdtemp(join(tmpdir(), "anatomy-curve-budget-"));+ try {+ const file = join(directory, "figure.html");+ await writeFile(file, '<!doctype html><div class="iso-plate"><svg width="520" height="440"><path fill="none" stroke="black" d="M20 400C1000000000 0 0 1000000000 480 400"/></svg></div>');+ let result;+ try { result = { code: 0, ...await execute(process.execPath, ["scripts/drive.mjs", file, '[["lines"]]'], { timeout: 30000 }) }; }+ catch (error) { result = { code: error.code, stdout: error.stdout, stderr: error.stderr }; }+ assert.equal(result.code, 1);+ assert.match(result.stderr, /Curve audit could not establish its screen-space sampling bound/);+ } finally { await rm(directory, { recursive: true, force: true }); }+});diff --git a/scripts/line-ends.mjs b/scripts/line-ends.mjsindex 33872d1..50c42c1 100644--- a/scripts/line-ends.mjs+++ b/scripts/line-ends.mjs@@ -19,7 +19,7 @@ function lineEnds({ select = null, tolerance = 1, root: rootSlack = 2, mark = 7,return getComputedStyle(el).visibility === "hidden" ? 0 : alpha;};const numbers = (text) => (text.match(/-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) || []).map(Number);- const subpathsOf = (d) => {+ const subpathsOf = (d, curveTolerance) => {const out = [];let current = null;let x = 0;@@ -38,24 +38,42 @@ function lineEnds({ select = null, tolerance = 1, root: rootSlack = 2, mark = 7,x = sx = px;y = sy = py;};- const cubic = (x1, y1, x2, y2, px, py) => {- const x0 = x;- const y0 = y;- for (let i = 1; i <= 8; i++) {- const t = i / 8;- const u = 1 - t;- push(u * u * u * x0 + 3 * u * u * t * x1 + 3 * u * t * t * x2 + t * t * t * px, u * u * u * y0 + 3 * u * u * t * y1 + 3 * u * t * t * y2 + t * t * t * py);+ let curveBudget = 16384;+ const chordDistance = (point, first, last) => {+ const dx = last[0] - first[0];+ const dy = last[1] - first[1];+ const span = dx * dx + dy * dy;+ const along = span > 0 ? Math.max(0, Math.min(1, ((point[0] - first[0]) * dx + (point[1] - first[1]) * dy) / span)) : 0;+ return Math.hypot(point[0] - first[0] - along * dx, point[1] - first[1] - along * dy);+ };+ const curve = (points) => {+ const pending = [points];+ while (pending.length) {+ if (--curveBudget < 0) throw new Error("Curve audit could not establish its screen-space sampling bound");+ const controlPoints = pending.pop();+ const first = controlPoints[0];+ const last = controlPoints[controlPoints.length - 1];+ if (controlPoints.slice(1, -1).every((point) => chordDistance(point, first, last) <= curveTolerance)) {+ push(...last);+ continue;+ }+ const left = [first];+ const right = [last];+ let level = controlPoints;+ while (level.length > 1) {+ level = level.slice(1).map((point, index) => [(level[index][0] + point[0]) / 2, (level[index][1] + point[1]) / 2]);+ left.push(level[0]);+ right.push(level[level.length - 1]);+ }+ pending.push(right.reverse(), left);}+ };+ const cubic = (x1, y1, x2, y2, px, py) => {+ curve([[x, y], [x1, y1], [x2, y2], [px, py]]);control = { kind: "cubic", point: [x2, y2] };};const quad = (x1, y1, px, py) => {- const x0 = x;- const y0 = y;- for (let i = 1; i <= 6; i++) {- const t = i / 6;- const u = 1 - t;- push(u * u * x0 + 2 * u * t * x1 + t * t * px, u * u * y0 + 2 * u * t * y1 + t * t * py);- }+ curve([[x, y], [x1, y1], [px, py]]);control = { kind: "quadratic", point: [x1, y1] };};for (const [, command, args] of d.matchAll(/([MmLlHhVvCcSsQqTtAaZz])([^MmLlHhVvCcSsQqTtAaZz]*)/g)) {@@ -110,10 +128,10 @@ function lineEnds({ select = null, tolerance = 1, root: rootSlack = 2, mark = 7,}return out;};- const geometryOf = (el) => {+ const geometryOf = (el, curveTolerance) => {const tag = el.tagName.toLowerCase();const num = (name) => Number(el.getAttribute(name) || 0);- if (tag === "path") return subpathsOf(el.getAttribute("d") || "");+ if (tag === "path") return subpathsOf(el.getAttribute("d") || "", curveTolerance);if (tag === "line") return [{ points: [[num("x1"), num("y1")], [num("x2"), num("y2")]], closed: false }];if (tag === "polyline" || tag === "polygon") {const v = numbers(el.getAttribute("points") || "");@@ -179,7 +197,8 @@ function lineEnds({ select = null, tolerance = 1, root: rootSlack = 2, mark = 7,const owner = el.closest("svg");const viewCtm = owner && owner.getScreenCTM();if (viewCtm) scale = Math.hypot(viewCtm.a, viewCtm.b) || scale;- const subpaths = geometryOf(el).map((sub, index) => ({ ...sub, index, flat: sub.points.map(map) }));+ const curveTolerance = 0.15 / Math.max(1, Math.hypot(ctm.a, ctm.b, ctm.c, ctm.d));+ const subpaths = geometryOf(el, curveTolerance).map((sub, index) => ({ ...sub, index, flat: sub.points.map(map) }));subpaths.forEach((sub) => {for (let i = 0; i < sub.flat.length - 1; i++) {const a = sub.flat[i];