partforge 0.114.0 → 0.115.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3642,7 +3642,14 @@ symptom first** — it maps error text → cause → fix. The invariants, one li
3642
3642
  `slotPolygon`, `ringSectorPolygon` and `circleProfile`; mirror a symmetric half with
3643
3643
  `mirrorProfile`.
3644
3644
  - **Preview vs print quality:** Manifold bakes segment counts in at primitive creation,
3645
- so builds are quality-agnostic; the export path uses a separate high-res "print" kernel.
3645
+ so builds are quality-agnostic; the export path uses a separate "print" kernel. Preview
3646
+ facets every circle at 116 segments. Print sizes each circle by chord tolerance — the
3647
+ fewest segments that keep the facet sagitta under 0.01 mm — never fewer than the
3648
+ preview's 116 and never more than 480, so a small feature exports at exactly the density
3649
+ you previewed and only circles wider than about 54 mm get finer. A part that previews
3650
+ is a part that exports: the old flat 480 turned a 0.75 mm rivet into 115,200 triangles
3651
+ and a body with a few hundred of them into an out-of-memory trap at export
3652
+ ([export-kernel-out-of-memory](ERROR-PATTERNS.md#export-kernel-out-of-memory)).
3646
3653
  - **Display placement is view-independent**; only `place(..., { purpose: "export" })` may
3647
3654
  depend on `view` ([view-dependent-display-place](ERROR-PATTERNS.md#view-dependent-display-place)).
3648
3655
  - **Keep geometry backend-agnostic** (kernel calls only); only STEP requires OCCT
@@ -823,6 +823,12 @@ between the Manifold preview and the OCCT STEP export.
823
823
  - **Cause:** *(partforge ≥ 0.112.)* A hand-authored 2-D profile (a point list, a `pathProfile` contour, a `{outer, holes}` region — handed to a factory op, to `k.shape2d`, or as a `Shape2D` boolean operand) crosses itself. Manifold fills a point ring even-odd, so the crossing quietly inverts the fill on one side instead of failing. The usual author of the crossing is an arc sampled into points by hand (a `Math.cos` loop) whose sweep sign or endpoint order is wrong, or a mirrored half whose point order was not reversed. The kernel now runs `validateProfile` on the way in and reports each crossing on the build result's `warnings`; a `Shape2D` is not re-validated, `text2d`/`vector2d` lifts are trusted, and a profile over 4000 segments is skipped (for `loft`, the ceiling is the sum over all its rings). At most **three** crossings are reported per profile — the third reads `… (and N more crossings on this profile)` — so one badly-drawn star cannot evict every other warning in the build. A hole whose edge touches or runs along its outer is **not** reported: that builds exactly as drawn, and only a contour crossing itself inverts the fill.
824
824
  - **Fix:** Rebuild the curved parts of the outline with `pathProfile(start).lineTo(p).arcTo(to, via).close()` — a three-point arc sweeps through `via`, so its direction cannot flip — or with the `partforge/geometry` helpers (`roundedProfile`, `filletPolygon`, `slotPolygon`, `ringSectorPolygon`, `circleProfile`); build a symmetric half once and `mirrorProfile` it rather than writing the mirror by hand. Confirm with `validateProfile(profile).ok` before extruding. The reported coordinate is in the profile's own frame (before any `rotate`/`at`).
825
825
 
826
+ ## export-kernel-out-of-memory
827
+
828
+ - **Symptom:** `Out of bounds memory access` (Safari) or `memory access out of bounds` (Chrome, Node) from an STL or 3MF export — or from a build — of a part whose preview renders fine; often followed, on every later build in the same session, by `Manifold instance already deleted`, `Out of bounds call_indirect`, `call_indirect to a signature that does not match`, `table index is out of bounds`, or `null function or function signature mismatch`.
829
+ - **Cause:** The mesh kernel's WASM heap ran out while building the print-quality mesh, and a WASM trap leaves that kernel instance corrupt — every later call into it fails until the worker is replaced or the page reloaded. Before partforge 0.115 the print tier meshed every circle at a flat 480 segments whatever its radius, so a part with a few hundred small spheres or cylinders (a 0.75 mm rivet was 115,200 triangles — 176 of them are 20 M before a single boolean) exhausted a 4 GB heap on its first export while its whole unioned preview was a few hundred thousand triangles. Since 0.115 print sizes circles by a 0.01 mm chord tolerance, floored at the preview count, so a part that previews normally exports at roughly the preview's cost; a part that still traps is genuinely too heavy for the browser at ANY quality — usually thousands of repeated small features, or a boolean chain whose intermediates dwarf the result.
830
+ - **Fix:** Reload the page (or let the host replace the kernel worker) before retrying anything — the trapped instance cannot recover. Then reduce what the export has to hold at once: build repeated detail as one union of instances rather than a chain of per-feature booleans, drop feature counts that exceed what the print can show (a 0.75 mm sphere prints as a dot), or pass `segs` to `revolve` where a coarser sweep is acceptable ([Preview vs print quality](AUTHORING-PARTS.md#conventions--gotchas)). Do NOT strip visible detail from the part to dodge a pre-0.115 trap — update partforge instead; the geometry was never the problem.
831
+
826
832
  # Hardware library
827
833
 
828
834
  Reserved for `hardware-*` patterns (issue #30). No entries yet.
@@ -375,7 +375,12 @@ Normative signatures: `kernel.js`'s `@typedef Solid`.
375
375
 
376
376
  `quality` (`"preview"` | `"print"`) is **advisory**: it trades tessellation density for
377
377
  speed and a backend may bake it at kernel creation (Manifold does). A part must never
378
- depend on triangle counts, segment counts, or normals being present.
378
+ depend on triangle counts, segment counts, or normals being present. The in-repo
379
+ backends both define `print` as a **chord tolerance of 0.01 mm** (OCCT's linear
380
+ deflection; Manifold's per-circle segment rule in `geometry/circle-segs.js`, floored at
381
+ the preview's 116 segments so print is never coarser than preview and capped at 480), so
382
+ a small feature costs the export exactly what its preview cost — the property that makes
383
+ "if it previews, it exports" hold. Preview is a flat 116 on Manifold, a visual choice.
379
384
 
380
385
  ### Shading intent (toMesh normals and edges)
381
386
 
@@ -554,8 +559,9 @@ curve-exact (they integrate the real curves; they do not measure a tessellation)
554
559
  so it is backend-identical too, like everything else in this list.
555
560
 
556
561
  **Lazy materialization.** Backend geometry is built only where it is unavoidable.
557
- Three readbacks tessellate to point rings at the backend's own LOD (Manifold 116
558
- preview / 480 print, OCCT 64): `toRegions()`, `simple()` (its unwrapped form), and
562
+ Three readbacks tessellate to point rings at the backend's own LOD (Manifold 116 per
563
+ circle at preview and, at print, the fewest segments holding a 0.01 mm chord sagitta
564
+ between 116 and 480 — per arc, by its radius; OCCT 64): `toRegions()`, `simple()` (its unwrapped form), and
559
565
  `regions()` — scission currently round-trips through `toRegions()`, so each returned
560
566
  `Shape2D` is a faceted copy, not a curve-native slice of the original. `extrude` and
561
567
  `revolve` materialize the shape into the backend's own form instead (Manifold: a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "partforge",
3
- "version": "0.114.0",
3
+ "version": "0.115.0",
4
4
  "description": "Turn a declarative part definition into a parametric-CAD web app (three.js + Manifold/Replicad). Requires a Vite-based consumer.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,56 @@
1
+ // Per-circle segment counts for the mesh backend's two quality tiers.
2
+ //
3
+ // `preview` is a flat count: 116 segments per full circle whatever the radius. It is
4
+ // a VISUAL choice — the density every part is previewed, captured and thumbnailed
5
+ // at — and it already holds a 0.05 mm chord sagitta out to a 136 mm radius, so
6
+ // nothing is gained by scaling it.
7
+ //
8
+ // `print` used to be a flat 480, sized (whether anyone meant it or not) for a circle
9
+ // nearly a metre across: 480 segments meet a 0.01 mm sagitta at r ≈ 467 mm. Spent on
10
+ // a 0.75 mm rivet sphere that is 115,200 triangles for a chord error of 1.6e-5 mm —
11
+ // a thousandth of any printer's resolution — and a part carrying 176 such rivets
12
+ // (20 M triangles of rivets before a single boolean, against a preview whose whole
13
+ // unioned body was 530k) trapped the WASM kernel with "memory access out of bounds"
14
+ // on its first STL export, on a fresh 4 GB instance. So the print tier is now
15
+ // tolerance-based: the fewest segments that keep the chord sagitta r·(1 − cos(π/n))
16
+ // under SAGITTA_TOL.print — the same 0.01 mm the OCCT backend's print tessellation
17
+ // uses, and the same formula `roundAllSegs` (mesh-roundall.js) and `blendSegs`
18
+ // (mesh-fillet.js) already apply to their own circles. Those two keep their own
19
+ // tolerances and clamps on purpose (roundAll has a preview tolerance and a 12..64
20
+ // window; the fillet blends at 1 µm) — this table is not theirs to share, and
21
+ // `roundAllSegs(r, "preview")` against it would collapse to a flat 12. Two clamps
22
+ // make the rule here safe:
23
+ //
24
+ // - FLOOR at the preview count. An export must never be coarser than the preview the
25
+ // user approved on screen, so below the radius where 116 segments already meet the
26
+ // tolerance (r ≈ 27 mm) print and preview facet identically — which is also what
27
+ // makes "if it previews, it exports" true for small features: the export costs what
28
+ // the preview already paid.
29
+ // - CAP at the old flat count. A circle large enough to need more than 480 keeps
30
+ // exactly the density it always had; nothing gets FINER than before.
31
+ //
32
+ // One rule, one place: every Manifold-backend site that facets a circle of known
33
+ // radius — sphere, cylinder, boredCylinder, roundedBox, revolve, and the arc and
34
+ // Bézier samplers behind prism/extrude/Shape2D — sizes through `circleSegs`. The
35
+ // helix tube's station/ring counts (TUBE in manifold-backend.js) and mesh-fillet's
36
+ // blend bands keep their own sizing; loft rings keep LOFT_SEGS.
37
+ //
38
+ // Pure, dependency-free: profile.js's samplers take a `(r) => n` function in place of
39
+ // a count, and this is what the backend hands them.
40
+
41
+ export const SEGS = { preview: 116, print: 480 }; // full-circle segments (flat / cap)
42
+ export const SAGITTA_TOL = { print: 0.01 }; // mm — max chord sagitta per tier
43
+
44
+ // Segments per full circle for a circle of radius `r` at `quality`. A tier with no
45
+ // tolerance (preview, or an unknown tier) is flat. A degenerate radius (0, negative,
46
+ // NaN, undefined) takes the floor: it facets like the preview and never throws.
47
+ export function circleSegs(r, quality) {
48
+ const cap = SEGS[quality] ?? SEGS.preview;
49
+ const tol = SAGITTA_TOL[quality];
50
+ if (tol === undefined) return cap;
51
+ const floor = SEGS.preview;
52
+ if (!(r > tol)) return floor;
53
+ // acos(1 − tol/r) is the half-angle of a chord with sagitta tol; π over it is the
54
+ // full-circle count. r = Infinity gives acos(1) = 0 → Infinity → the cap.
55
+ return Math.min(cap, Math.max(floor, Math.ceil(Math.PI / Math.acos(1 - tol / r))));
56
+ }
@@ -19,6 +19,7 @@ import { creasedNormals } from "./creased-normals.js";
19
19
  import { loftShadingPolicy, SMOOTH, BLEND } from "./shading-policy.js";
20
20
  import { meshFillet, meshChamfer, UnsupportedEdgeError } from "./mesh-fillet.js";
21
21
  import { meshRoundAll, prismSection, roundAllSegs } from "./mesh-roundall.js";
22
+ import { SEGS, circleSegs } from "./circle-segs.js";
22
23
  import { checkBooleanResult } from "./boolean-gate.js";
23
24
  import { KernelCapabilityError } from "./errors.js";
24
25
  import { heightfieldMesh, hashGridData } from "./heightfield.js";
@@ -26,7 +27,10 @@ import { heightfieldMesh, hashGridData } from "./heightfield.js";
26
27
  const PLANE_NORMAL = { XY: [0, 0, 1], XZ: [0, 1, 0], YZ: [1, 0, 0] };
27
28
  // 'preview' = interactive view (fast); 'print' = STL export (high-res, used only
28
29
  // by the export path — Manifold meshing is cheap, so we tessellate generously).
29
- const SEGS = { preview: 116, print: 480 }; // circular segments
30
+ // Full-circle segment counts live in circle-segs.js: preview is a flat SEGS.preview,
31
+ // print sizes each circle by chord tolerance through circleSegs (floored at the
32
+ // preview count, capped at SEGS.print). `segs` below is the tier's cap — the hash
33
+ // key and the count handed to consumers that size themselves (mesh-fillet, loft).
30
34
  const TUBE = { preview: { stationsPerTurn: 38, ringSegs: 24 }, print: { stationsPerTurn: 160, ringSegs: 40 } };
31
35
 
32
36
  // true axis-angle rotation as a column-major 4x4 (manifold Mat4), translation 0
@@ -44,6 +48,9 @@ function axisAngleMat4(axis, deg) {
44
48
  export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
45
49
  const { Manifold, CrossSection } = wasm;
46
50
  const segs = SEGS[quality], tube = TUBE[quality];
51
+ // Per-radius count for every site that facets a circle it knows the radius of; the
52
+ // samplers behind prism/extrude/Shape2D take it as a function (profile.js).
53
+ const segsAt = (r) => circleSegs(r, quality);
47
54
 
48
55
  // Manifold/CrossSection are WASM objects with no garbage collection — every
49
56
  // primitive and boolean op allocates a new one. Track them all and free them
@@ -166,7 +173,7 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
166
173
  // on it in pure JS; no CrossSection is built until a shape is handed to a kernel op.
167
174
  // `extrude`/`revolve` are thunks because `kernel` below is defined after this.
168
175
  const shape2d = makeShape2dFactory({
169
- segs,
176
+ segs: segsAt, // the readbacks (toRegions/simple) tessellate at the same per-radius LOD as the kernel ops
170
177
  extrude: (o) => kernel.extrude(o),
171
178
  revolve: (o) => kernel.revolve(o),
172
179
  recordWarning,
@@ -177,7 +184,7 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
177
184
  // once, and the cache's pin/dispose keeps the WASM object alive exactly as long as
178
185
  // the entry (cleanup() skips pinned objects).
179
186
  const csFor = (shape) => cache.lookup(h("cs2d", shape._hash, segs), () => {
180
- const cs = T(CrossSection.ofPolygons(regionPolys(shape._regions, segs), "EvenOdd"));
187
+ const cs = T(CrossSection.ofPolygons(regionPolys(shape._regions, segsAt), "EvenOdd"));
181
188
  return { value: cs, pin: cs, dispose: () => cs.delete?.() };
182
189
  });
183
190
 
@@ -401,13 +408,13 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
401
408
  if (typeof selector === "function") throw new KernelCapabilityError("fillet: function selectors need the OCCT backend");
402
409
  if (r === 0) return wrap(m, hash); // contract: zero magnitude is the identity
403
410
  return cached(h("fillet", hash, r, selector ?? null, segs), () =>
404
- meshCadOp("fillet", m, () => meshFillet(kernel, wrap(m, hash), { r, edges: selector, segs })));
411
+ meshCadOp("fillet", m, () => meshFillet(kernel, wrap(m, hash), { r, edges: selector, segs, segsAt })));
405
412
  },
406
413
  _chamferRaw: (d, selector) => {
407
414
  if (typeof selector === "function") throw new KernelCapabilityError("chamfer: function selectors need the OCCT backend");
408
415
  if (d === 0) return wrap(m, hash);
409
416
  return cached(h("chamfer", hash, d, selector ?? null, segs), () =>
410
- meshCadOp("chamfer", m, () => meshChamfer(kernel, wrap(m, hash), { d, edges: selector, segs })));
417
+ meshCadOp("chamfer", m, () => meshChamfer(kernel, wrap(m, hash), { d, edges: selector, segs, segsAt })));
411
418
  },
412
419
 
413
420
  // The AUTHOR-FACING ops degrade on failure instead of failing the build (the
@@ -628,14 +635,16 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
628
635
  };
629
636
 
630
637
  const kernel = finishKernel({
631
- cylinder: (rb, rt, h2, { center = false } = {}) =>
632
- wrap(T(Manifold.cylinder(h2, rb, rt, segs, center)), h("cylinder", rb, rt, h2, center, segs)),
638
+ cylinder: (rb, rt, h2, { center = false } = {}) => {
639
+ const n = segsAt(Math.max(rb, rt)); // a cone is sized by its wider end
640
+ return wrap(T(Manifold.cylinder(h2, rb, rt, n, center)), h("cylinder", rb, rt, h2, center, n));
641
+ },
633
642
  // Compound op: hashed ATOMICALLY from its own args, so it is a single cache
634
643
  // node — its internal cylinders/cut are never retained. The template for
635
644
  // future compounds: build internals with T(), return the final tracked solid.
636
- boredCylinder: ({ od, h: height, bore }) => cached(h("boredCylinder", od, height, bore, segs), () => {
637
- const body = T(Manifold.cylinder(height, od / 2, od / 2, segs, false));
638
- const tool0 = T(Manifold.cylinder(height + 4, bore / 2, bore / 2, segs, false));
645
+ boredCylinder: ({ od, h: height, bore }) => cached(h("boredCylinder", od, height, bore, segsAt(od / 2), segsAt(bore / 2)), () => {
646
+ const body = T(Manifold.cylinder(height, od / 2, od / 2, segsAt(od / 2), false));
647
+ const tool0 = T(Manifold.cylinder(height + 4, bore / 2, bore / 2, segsAt(bore / 2), false));
639
648
  const tool = T(tool0.translate([0, 0, -2])); // raw ops: track each result
640
649
  return T(body.subtract(tool));
641
650
  }),
@@ -644,19 +653,20 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
644
653
  // spec). Reuses loftMesh's stitch/cap/winding machinery. Atomic cache
645
654
  // node hashed from its own args, like boredCylinder.
646
655
  roundedBox: ({ size, center, round }) => cached(
647
- h("roundedBox", size, center, round.side, round.top, round.bottom, segs),
656
+ h("roundedBox", size, center, round.side, round.top, round.bottom, segsAt(Math.max(round.side, round.top, round.bottom))),
648
657
  () => {
649
- const solid = T(loftMesh(wasm, roundedBoxRings(size, round, segs)));
658
+ // One count serves every corner and rim arc, so it is sized for the largest.
659
+ const solid = T(loftMesh(wasm, roundedBoxRings(size, round, segsAt(Math.max(round.side, round.top, round.bottom)))));
650
660
  return center ? T(solid.translate([0, 0, -size[2] / 2])) : solid;
651
661
  }),
652
- sphere: (r) => wrap(T(Manifold.sphere(r, segs)), h("sphere", r, segs)),
662
+ sphere: (r) => wrap(T(Manifold.sphere(r, segsAt(r))), h("sphere", r, segsAt(r))),
653
663
  box: (min, max) => {
654
664
  const cube = T(Manifold.cube([max[0] - min[0], max[1] - min[1], max[2] - min[2]]));
655
665
  return wrap(T(cube.translate(min)), h("box", min, max));
656
666
  },
657
667
  prism: (pts, height, { twist = 0, scaleTop = 1 } = {}) =>
658
668
  cached(h("prism", pts, height, twist, scaleTop, segs), () => {
659
- const cs = T(CrossSection.ofPolygons([tessellateContour(pts, segs)]));
669
+ const cs = T(CrossSection.ofPolygons([tessellateContour(pts, segsAt)]));
660
670
  if (twist === 0 && scaleTop === 1) return T(cs.extrude(height));
661
671
  const nDiv = Math.max(1, Math.ceil(Math.abs(twist) / 5));
662
672
  // Manifold's extrude scaleTop is a Vec2 — a scalar is NOT broadcast (it scales
@@ -711,7 +721,7 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
711
721
  const shape = profile && profile._shape2d ? profile : null;
712
722
  return cached(h("extrude", shape ? shape._hash : profile, height, twist, scaleTop, segs), () => {
713
723
  const cs = shape ? csFor(shape) : (() => {
714
- const { outer, holes } = tessellateProfile(profile, segs);
724
+ const { outer, holes } = tessellateProfile(profile, segsAt);
715
725
  return T(CrossSection.ofPolygons([outer, ...holes], "EvenOdd"));
716
726
  })();
717
727
  if (twist === 0 && scaleTop === 1) return T(cs.extrude(height));
@@ -757,10 +767,26 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
757
767
  // opts.segs may only COARSEN below the kernel's quality (min), never exceed it:
758
768
  // callers use it where a small feature's sagitta bound needs fewer facets than
759
769
  // the per-circle quality would spend (mesh-fillet's free-standing corner arcs).
770
+ // Density around the axis: by default sized for the profile's outermost radius —
771
+ // the largest circle the revolve sweeps — through the tier's per-radius rule. An
772
+ // explicit `segs` is the caller's own sizing (mesh-fillet's blend tools compute
773
+ // theirs from a 1 µm sagitta bound and rely on getting exactly that count — the
774
+ // dephase and horn-containment arithmetic assume it), bounded only by the tier's
775
+ // cap: it may still never EXCEED kernel quality, but it is not re-bounded by the
776
+ // part-scale rule meant for circles nobody sized by hand.
760
777
  revolve: (pts, { degrees = 360, segs: segsOverride } = {}) => {
761
- const density = Math.min(segs, segsOverride ?? segs);
762
- if (pts && pts._shape2d)
763
- return cached(h("revolve", pts._hash, degrees, density), () => T(csFor(pts).revolve(density, degrees)));
778
+ const densityFor = (maxR) => segsOverride != null ? Math.min(segs, segsOverride) : segsAt(maxR);
779
+ if (pts && pts._shape2d) {
780
+ // Keyed on the override, not the resolved density, so a cache hit never
781
+ // materializes the CrossSection just to measure its bounds.
782
+ return cached(h("revolve", pts._hash, degrees, segsOverride ?? null), () => {
783
+ const cs = csFor(pts);
784
+ const b = cs.bounds();
785
+ return T(cs.revolve(densityFor(Math.max(Math.abs(b.min[0]), Math.abs(b.max[0]))), degrees));
786
+ });
787
+ }
788
+ const maxR = Array.isArray(pts) ? pts.reduce((m, p) => Math.max(m, Math.abs(p?.[0] ?? 0)), 0) : 0;
789
+ const density = densityFor(maxR);
764
790
  return cached(h("revolve", pts, degrees, density), () => T(Manifold.revolve([pts], density, degrees)));
765
791
  },
766
792
  // A one-solid union is an identity — no new WASM / cache entry (avoids double-free):
@@ -670,8 +670,12 @@ function prismTool(k, chain, magnitude, mode, segs, pSegs = segs) {
670
670
  // closed-revolve dephase, which are about matching the neighboring tessellation and
671
671
  // must not follow the blend cap. `pSegs` is the sagitta-bounded density for the blend
672
672
  // cross-section itself (blendSegs above).
673
- function revolveTool(k, chain, magnitude, mode, segs, pSegs = segs) {
673
+ function revolveTool(k, chain, magnitude, mode, segs, pSegs = segs, flankAt = () => segs) {
674
674
  const { O, w, u0, v0, R, span, closed, n1, n2, convex } = chain;
675
+ // The count the flank's own circle was built at (see apply): the cap on a flat
676
+ // tier, the per-radius rule on print. Every "matching the neighbouring
677
+ // tessellation" figure below reads this, never `segs`.
678
+ const flankSegs = flankAt(R);
675
679
  // Seam-grazing guard. The edge circle passes through the flank tessellation's
676
680
  // VERTICES (circumradius) while its facets sit at the apothem, so a revolved
677
681
  // tool built exactly at R grazes every facet seam tangentially — Manifold
@@ -689,7 +693,7 @@ function revolveTool(k, chain, magnitude, mode, segs, pSegs = segs) {
689
693
  // and a radial knife-fin of wall survived both cutters, drawing a line along the
690
694
  // band (the label-backing bug). A synthetic corner arc measures nothing — its two
691
695
  // points span the whole corner, and its flanks are planes, not a tessellation.
692
- const kernelSag = (R + magnitude) * (1 - Math.cos(Math.PI / segs));
696
+ const kernelSag = (R + magnitude) * (1 - Math.cos(Math.PI / flankSegs));
693
697
  let dip = 0;
694
698
  if (!chain.synthetic) {
695
699
  const pts = chain.points;
@@ -732,7 +736,7 @@ function revolveTool(k, chain, magnitude, mode, segs, pSegs = segs) {
732
736
  // with the flank's own tessellation of the same circle (the dephase note below).
733
737
  // A SYNTHETIC corner arc (cornerArcAt) is free-standing between planes, so its
734
738
  // angular density follows the same sagitta bound as the cross-section.
735
- const aSegs = chain.synthetic ? cornerArcSegs(segs, R, magnitude) : segs;
739
+ const aSegs = chain.synthetic ? cornerArcSegs(segs, R, magnitude) : flankSegs;
736
740
  let tool = k.revolve(poly, { degrees, segs: aSegs });
737
741
  // pose: Z → w, then twist so the revolve's start azimuth (+X) lands on the
738
742
  // chain's start direction (backed off by the angular overshoot)
@@ -749,7 +753,7 @@ function revolveTool(k, chain, magnitude, mode, segs, pSegs = segs) {
749
753
  // vertex at every step (the degenerate-needle generator). Half a step lands
750
754
  // every crossing mid-facet. Partial arcs have a slightly different pitch
751
755
  // (degrees don't divide evenly) and never align in the first place.
752
- const dephase = closed ? Math.PI / segs : 0;
756
+ const dephase = closed ? Math.PI / flankSegs : 0;
753
757
  const twist = Math.atan2(dot(w, cross(xImage, startDir)), dot(xImage, startDir)) + dephase;
754
758
  if (Math.abs(twist) > 1e-9) tool = tool.rotateAbout({ axis: w, deg: (twist * 180) / Math.PI });
755
759
  return tool.translate(O);
@@ -891,7 +895,7 @@ function weldChainPoints(pts, wallNs, closed) {
891
895
  return { pts: outP, wallNs: outW };
892
896
  }
893
897
 
894
- function planarTool(k, chain, magnitude, mode, segs, pSegs = segs, endTins = null) {
898
+ function planarTool(k, chain, magnitude, mode, segs, pSegs = segs, endTins = null, flankAt = () => segs) {
895
899
  const { points, closed, convex, faceN } = chain;
896
900
  let { wallNs } = chain;
897
901
  let pts = closed ? points.slice(0, -1) : points; // drop the duplicated closure point
@@ -1095,9 +1099,9 @@ function planarTool(k, chain, magnitude, mode, segs, pSegs = segs, endTins = nul
1095
1099
  tools.push(...buildStretch(path, wallNs[s % nSeg]));
1096
1100
  }
1097
1101
  for (const got of cornerArcs.values()) {
1098
- tools.push(revolveTool(k, got.arc, magnitude, mode, segs, pSegs));
1102
+ tools.push(revolveTool(k, got.arc, magnitude, mode, segs, pSegs, flankAt));
1099
1103
  if (len(sub(got.vertex, got.arc.O)) - got.arc.R > 0.02 * magnitude)
1100
- tools.push(cornerHornTool(k, got, magnitude, segs));
1104
+ tools.push(cornerHornTool(k, got, magnitude, segs, flankAt));
1101
1105
  }
1102
1106
  for (const piv of pivots) tools.push(reflexPivotTool(k, piv, magnitude, mode, segs, pSegs));
1103
1107
  return tools;
@@ -1211,7 +1215,7 @@ function cornerArcAt(vertex, f, tin1, tin2, wall1, wall2, len1, len2, magnitude,
1211
1215
  // of its step count, so its apothem ≥ R·cos(π/aSegs) > every horn vertex radius. The
1212
1216
  // cost is a micron-deep extra bite at the corner base, covered near the tangent lines
1213
1217
  // by the neighbors' own overshoot.
1214
- function cornerHornTool(k, { vertex, f, arc }, magnitude, segs) {
1218
+ function cornerHornTool(k, { vertex, f, arc }, magnitude, segs, flankAt = () => segs) {
1215
1219
  const { O, w, u0, R, span } = arc;
1216
1220
  const delta = 0.02 * magnitude;
1217
1221
  const rH = R * Math.cos(Math.PI / cornerArcSegs(segs, R, magnitude)) - Math.min(1e-3, 0.02 * magnitude);
@@ -1488,7 +1492,7 @@ function roundSalientCorners(selected, magnitude) {
1488
1492
  // cube's outer walls land inside the material the edge cutters already remove,
1489
1493
  // so the only new surface is the octant. Non-orthogonal corners keep the mitre
1490
1494
  // — the safe, documented default.
1491
- function cornerPatches(k, selected, r, segs) {
1495
+ function cornerPatches(k, selected, r, segs, flankAt = () => segs) {
1492
1496
  const byVertex = new Map();
1493
1497
  const push = (pt, dirOut) => {
1494
1498
  const key = pt.map((v) => Math.round(v * 1e4)).join(",");
@@ -1527,7 +1531,7 @@ function cornerPatches(k, selected, r, segs) {
1527
1531
  // is tangent to each flat face at a point and meets the edge-fillet
1528
1532
  // cylinders tangentially at the cube walls, and tessellated tangency
1529
1533
  // produces the same grazing-noise creases the edge tools guard against.
1530
- const bury = r * (1 - Math.cos(Math.PI / segs)) + 1e-3;
1534
+ const bury = r * (1 - Math.cos(Math.PI / flankAt(r))) + 1e-3; // the kernel sphere below is built at flankAt(r)
1531
1535
  const inward = norm(add(add(e1, e2), e3));
1532
1536
  // corner block: cube spanned by the edge frame, oversized only outward
1533
1537
  let block = k.box({ min: [-dOut, -dOut, -dOut], max: [r, r, r] });
@@ -1554,8 +1558,17 @@ function cornerPatches(k, selected, r, segs) {
1554
1558
  export function meshFillet(k, solid, opts) { return apply(k, solid, "fillet", opts?.r, opts); }
1555
1559
  export function meshChamfer(k, solid, opts) { return apply(k, solid, "chamfer", opts?.d, opts); }
1556
1560
 
1557
- function apply(k, solid, mode, magnitude, { edges, segs = DEFAULT_SEGS, sharpDeg = 20 } = {}) {
1561
+ // `segs` is the kernel's per-circle CAP: it bounds the blend densities (blendSegs)
1562
+ // and is what every circle was built at on a flat tier. `segsAt(r)` is what a circle
1563
+ // of radius r was ACTUALLY built at — the print tier sizes circles by chord tolerance
1564
+ // (circle-segs.js), so a flank's facet pitch is no longer the cap. The three places
1565
+ // that reason about the neighbouring tessellation (revolveTool's seam-grazing sag and
1566
+ // closed-revolve dephase, cornerHornTool's sphere burial) ask it; everything sized
1567
+ // from the blend's own sagitta bound keeps the cap. Absent, it is the cap — the
1568
+ // pre-print-rule behaviour, and byte-identical at preview either way.
1569
+ function apply(k, solid, mode, magnitude, { edges, segs = DEFAULT_SEGS, sharpDeg = 20, segsAt = null } = {}) {
1558
1570
  if (!(magnitude > 0)) throw new Error(`mesh ${mode}: magnitude must be > 0`);
1571
+ const flankAt = segsAt ?? (() => segs);
1559
1572
  const chains = chainEdges(detectSharpEdges(solid.toIndexedMesh(), { sharpDeg }));
1560
1573
  const selected = chains.filter((ch) => matchesSelector(ch, edges));
1561
1574
  if (!selected.length) throw new UnsupportedEdgeError(`${mode} selector matched no sharp edges`);
@@ -1597,13 +1610,15 @@ function apply(k, solid, mode, magnitude, { edges, segs = DEFAULT_SEGS, sharpDeg
1597
1610
  const pSegs = blendSegs(segs, magnitude);
1598
1611
  const toolsFor = (ch) =>
1599
1612
  ch.kind === "planar"
1600
- ? planarTool(k, ch, magnitude, mode, segs, pSegs, endTins)
1601
- : [(ch.kind === "arc" ? revolveTool : prismTool)(k, ch, magnitude, mode, segs, pSegs)];
1613
+ ? planarTool(k, ch, magnitude, mode, segs, pSegs, endTins, flankAt)
1614
+ : ch.kind === "arc"
1615
+ ? [revolveTool(k, ch, magnitude, mode, segs, pSegs, flankAt)]
1616
+ : [prismTool(k, ch, magnitude, mode, segs, pSegs)];
1602
1617
  const cutters = [...effective, ...arcs].filter((ch) => ch.convex).flatMap(toolsFor);
1603
- cutters.push(...horns.map((h) => cornerHornTool(k, h, magnitude, segs)));
1618
+ cutters.push(...horns.map((h) => cornerHornTool(k, h, magnitude, segs, flankAt)));
1604
1619
  cutters.push(...pivots.map((p) => reflexPivotTool(k, p, magnitude, mode, segs, pSegs)));
1605
1620
  const fillers = effective.filter((ch) => !ch.convex).flatMap(toolsFor);
1606
- if (mode === "fillet") cutters.push(...cornerPatches(k, effective, magnitude, segs));
1621
+ if (mode === "fillet") cutters.push(...cornerPatches(k, effective, magnitude, segs, flankAt));
1607
1622
  let out = solid;
1608
1623
  if (cutters.length) out = out.cutAll(cutters);
1609
1624
  if (fillers.length) out = k.union([out, ...fillers]);
@@ -79,13 +79,16 @@ export function arcGeometry(p0, via, p1) {
79
79
  // three points; the sweep direction is the one whose arc actually passes through `via`
80
80
  // (sign-free, winding-free). Facet count scales with the sweep's fraction of the kernel's
81
81
  // full-circle resolution `segs`, matching the piePolygon/circleProfile convention, so an
82
- // arc and a circleProfile of equal radius facet identically. A degenerate (collinear)
83
- // triple falls back to a single straight segment to p1 — the same "plain line" the OCCT
84
- // side gets when roundedProfile emits no `via`.
82
+ // arc and a circleProfile of equal radius facet identically. `segs` is either that count
83
+ // or a function of the arc's radius returning one — the mesh backend's print tier sizes
84
+ // circles by chord tolerance (circle-segs.js) and hands the samplers the rule rather
85
+ // than a number. A degenerate (collinear) triple falls back to a single straight segment
86
+ // to p1 — the same "plain line" the OCCT side gets when roundedProfile emits no `via`.
85
87
  export function sampleArc(p0, via, p1, segs) {
86
88
  const g = arcGeometry(p0, via, p1);
87
89
  if (!g) return [[p1[0], p1[1]]]; // collinear → straight line
88
- const steps = Math.max(2, Math.ceil((segs * Math.abs(g.dA)) / (2 * Math.PI)));
90
+ const n = typeof segs === "function" ? segs(g.r) : segs;
91
+ const steps = Math.max(2, Math.ceil((n * Math.abs(g.dA)) / (2 * Math.PI)));
89
92
  const out = [];
90
93
  for (let s = 1; s <= steps; s++) {
91
94
  const ang = g.a0 + g.dA * (s / steps);
@@ -102,8 +105,26 @@ export function sampleArc(p0, via, p1, segs) {
102
105
  // cubic tracing a circular arc facets like the arc primitive at the same segs. Summing
103
106
  // |turn| at BOTH interior control points also catches S-curves a pure endpoint-tangent
104
107
  // test would miss. Depth cap guarantees termination. Pure in (args, segs).
108
+ //
109
+ // `segs` may be a function of radius (see sampleArc). A cubic has no single radius, so
110
+ // the budget is decided per sub-curve from the radius it traces, recovered from its
111
+ // chord c and turn t as the circle on which a chord c subtends t: r = c / (2·sin(t/2)).
112
+ // That is EXACT for a circular arc. For any other cubic the control polygon's turn t
113
+ // overstates the curve's, so r errs small and the count errs LOW — a slightly coarser
114
+ // budget, not a finer one. The bias is bounded by the ratio of polygon turn to curve
115
+ // turn, which the recursion drives to 1 as the pieces shrink, and at the turns that
116
+ // pass (≤ 2π/116) it is under 0.02 % of the count. A cubic tracing a circle of radius
117
+ // r therefore facets like the arc primitive of radius r under the same rule.
105
118
  export function sampleBezier(p0, c1, c2, p1, segs) {
106
- const maxTurn = (2 * Math.PI) / Math.max(3, segs);
119
+ const segsAt = typeof segs === "function" ? segs : null;
120
+ const flatTurn = segsAt ? null : (2 * Math.PI) / Math.max(3, segs);
121
+ const maxTurnFor = (a, d, t) => {
122
+ if (!segsAt) return flatTurn;
123
+ const chord = Math.hypot(d[0] - a[0], d[1] - a[1]);
124
+ const half = Math.sin(t / 2); // t ≤ 2π here: two turns of ≤ π each
125
+ const r = half > 1e-9 ? chord / (2 * half) : Infinity; // straight (t ≈ 0): the cap, and t ≤ budget anyway
126
+ return (2 * Math.PI) / Math.max(3, segsAt(r));
127
+ };
107
128
  const out = [];
108
129
  const mid = (a, b) => [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
109
130
  const turn = (u, v) => {
@@ -117,7 +138,8 @@ export function sampleBezier(p0, c1, c2, p1, segs) {
117
138
  const ab = [b[0] - a[0], b[1] - a[1]];
118
139
  const bc = [c[0] - b[0], c[1] - b[1]];
119
140
  const cd = [d[0] - c[0], d[1] - c[1]];
120
- if (depth >= 12 || turn(ab, bc) + turn(bc, cd) <= maxTurn) { out.push([d[0], d[1]]); return; }
141
+ const t = turn(ab, bc) + turn(bc, cd);
142
+ if (depth >= 12 || t <= maxTurnFor(a, d, t)) { out.push([d[0], d[1]]); return; }
121
143
  const p01 = mid(a, b), p12 = mid(b, c), p23 = mid(c, d);
122
144
  const p012 = mid(p01, p12), p123 = mid(p12, p23), m = mid(p012, p123);
123
145
  recurse(a, p01, p012, m, depth + 1);