Skip to content

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(audit): preserve tapered tube geometry

Rudy Celeklidc2a4b7b3f53
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf7b18d..bf90b69 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -25,6 +25,9 @@ jobs:
with:
node-version: 22
+ - name: Tapered audit regressions
+ run: node --test scripts/audit-taper.test.mjs
+
- name: Dial indicator
run: |
node examples/dial-indicator/build.mjs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8152ae2..22e9dbf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- Tube geometry audits use tapered radii instead of replacing every segment with an averaged cylinder.
+
- `examples/test-rig/` drew its glass button dark in the light theme. `stretch.css` now has light values for its own colours.
### Changed
diff --git a/kit/audit.mjs b/kit/audit.mjs
index f52d06c..d7c30ce 100644
--- a/kit/audit.mjs
+++ b/kit/audit.mjs
@@ -133,7 +133,7 @@ function polygonDistance(poly, x, y) {
return inside ? -d : d;
}
-function cappedSegment(p, a, b, r) {
+function cappedSegment(p, a, b, r, endRadius = r) {
const bx = b[0] - a[0];
const by = b[1] - a[1];
const bz = b[2] - a[2];
@@ -142,6 +142,12 @@ function cappedSegment(p, a, b, r) {
const pz = p[2] - a[2];
const baba = bx * bx + by * by + bz * bz || 1e-12;
const paba = px * bx + py * by + pz * bz;
+ if (endRadius !== r) {
+ const length = Math.sqrt(baba);
+ const along = paba / length;
+ const radial = Math.hypot(px - bx * paba / baba, py - by * paba / baba, pz - bz * paba / baba);
+ return polyDistance([[0, 0], [0, r], [length, endRadius], [length, 0]], along, radial);
+ }
const x = Math.hypot(px * baba - bx * paba, py * baba - by * paba, pz * baba - bz * paba) - r * baba;
const y = Math.abs(paba - baba * 0.5) - baba * 0.5;
const x2 = x * x;
@@ -201,8 +207,7 @@ function baseSdf(shape, p) {
let best = BIG;
const last = points.length - 1;
for (let index = 0; index < last; index++) {
- const r = (radii[index] + radii[index + 1]) / 2;
- const d = cappedSegment(p, points[index], points[index + 1], r);
+ const d = cappedSegment(p, points[index], points[index + 1], radii[index], radii[index + 1]);
if (d < best) best = d;
}
if (round[0]) best = Math.min(best, len3(sub3(p, points[0])) - radii[0]);
@@ -310,7 +315,7 @@ function tubeDistance(shape, p, segments) {
const last = points.length - 1;
let best = BIG;
for (const index of segments) {
- const d = cappedSegment(p, points[index], points[index + 1], (radii[index] + radii[index + 1]) / 2);
+ const d = cappedSegment(p, points[index], points[index + 1], radii[index], radii[index + 1]);
if (d < best) best = d;
for (const [joint, flag] of [[0, round[0]], [last, round[1]]]) {
if (!flag || (joint !== index && joint !== index + 1)) continue;
diff --git a/scripts/audit-taper.test.mjs b/scripts/audit-taper.test.mjs
new file mode 100644
index 0000000..ee67ae2
--- /dev/null
+++ b/scripts/audit-taper.test.mjs
@@ -0,0 +1,29 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import * as A from "../kit/audit.mjs";
+
+const taper = () => A.tube([[0, 0, 0], [10, 0, 0]], t => 1 + 2 * t);
+const close = (actual, expected) => assert.ok(Math.abs(actual - expected) < 1e-12, `${actual} differs from ${expected}`);
+test("the narrow portion of a taper does not invent a collision", () => {
+ close(A.sdf(taper(), [1, 2, 0]), 0.8 / Math.sqrt(1.04));
+});
+test("the wide portion of a taper does not hide a collision", () => {
+ close(A.sdf(taper(), [9, 2.5, 0]), -0.3 / Math.sqrt(1.04));
+});
+test("the actual conical surface has zero signed distance", () => {
+ close(A.sdf(taper(), [1, 1.2, 0]), 0);
+});
+test("taper distance follows a rotated and translated tube frame", () => {
+ const shape = A.tube([[5, 3, 2], [5, 13, 2]], [1, 3]);
+ close(A.sdf(shape, [7, 4, 2]), 0.8 / Math.sqrt(1.04));
+});
+test("constant-radius cylinders retain their sides, caps and interior", () => {
+ const shape = A.tube([[0, 0, 0], [10, 0, 0]], 2);
+ close(A.sdf(shape, [5, 3, 0]), 1);
+ close(A.sdf(shape, [-1, 0, 0]), 1);
+ close(A.sdf(shape, [5, 0, 0]), -2);
+});
+test("taper end caps keep their true radii", () => {
+ close(A.sdf(taper(), [-1, 0, 0]), 1);
+ close(A.sdf(taper(), [11, 0, 0]), 1);
+});