Files
solidcraft/tools/kernel-ladder/ladder_lib.py
T
jyvikoandClaude Opus 5.5 b249100eae ladder: 116 cases in 11 tiers, from primitives to a threaded, shelled housing
Tiers: primitives; booleans in the degeneracy classes (coincident and coplanar faces, edge-on-edge,
vertex-on-face, tangent along a line and a circle, near-tangent gaps and overlaps of 1e-3 and
1e-6 mm, seam and pole crossings) and cuts that end in a single line (a knife-edge wedge, a V
groove and a cylinder that pinch the part in two, a hole tangent to a face, a notch flush with an
end); line-and-arc extrudes and revolves (partial, touching the axis); spline profiles, sweeps
and lofts; holes and tapped holes (blind, drilled, counterbore, countersink, M5-M8 taps, into a
curved face, near and tangent to an edge, crossing); modelled threads (external, fine, partial,
to a shoulder, internal, into a chamfer, at a filleted shoulder); fillets (constant, tangent
chains, convex and concave, variable down to 1e-3 and to 0 at a vertex, corners with equal and
unequal radii, g2 corners); chamfers (two distances, angle, three- and two-edge corners, curved);
shells and offsets; patterns and mirrors (overlapping instances that cut a core free); and
cumulative parts.

A running part grows one tier at a time through `base` (back wall, revolved boss and slot,
spline window, tapped holes, rounds, chamfers, shell, vents), and the last case adds its thread:
it carries every tier. Answers are analytic wherever the geometry has them (Pappus, Steinmetz,
ISO 68-1 basic thread profiles, exact Gauss quadrature of Bezier profiles, inclusion-exclusion
for chamfered corners), otherwise invariants; each `method` gives the derivation, and
tools/kernel-ladder/cases.py (standard-library Python, like tools/oracle-analytic) writes them.
Capabilities SolidCraft lacks are `missing` cases: multi-start threads, thread runout lengths,
fillet corner setbacks.

The golden is the first recording on truck, each tier sorted by its time. SMLib booleans are
not on this branch yet, so the smlib column records "not supported yet" for every case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QWhMwdqJYQKQq8RCLSL1LT
2026-10-27 02:28:46 -04:00

153 lines
5.1 KiB
Python

"""Helpers for the kernel ladder's cases (tests/kernel-cases/ladder): command builders, plain
Python 3 quadrature (standard library only) and the case writers. See cases.py."""
import json
import math
import os
PI = math.pi
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "tests", "kernel-cases", "ladder")
CASES = [] # (tier, name)
TIERS = {}
def C(cmd, **p):
return {"command": cmd, "params": p}
def box(corner, l, w, h, **kw):
return C("solid.box", corner=corner, length=l, width=w, height=h, **kw)
def cyl(base, r, h, **kw):
return C("solid.cylinder", base=base, radius=r, height=h, **kw)
def sketch(plane, *items, name=None):
p = {"plane": plane}
if name:
p["name"] = name
return [C("sketch.create", **p), *items, C("sketch.finish")]
def line(points, ids=None, closed=False):
p = {"points": points}
if ids:
p["ids"] = ids
if closed:
p["closed"] = True
return C("sketch.line", **p)
def plane(origin, x, y):
return {"origin": origin, "x_dir": x, "y_dir": y}
def vplane(origin):
"""A vertical sketch plane: sketch x = world x, sketch y = world z (normal -Y)."""
return plane(origin, [1, 0, 0], [0, 0, 1])
def flat(*cmds):
out = []
for c in cmds:
out.extend(c if isinstance(c, list) else [c])
return out
def leggauss(n):
"""Gauss-Legendre nodes and weights on [-1, 1] (Newton on the Legendre polynomial)."""
xs, ws = [], []
for i in range(1, n + 1):
x = math.cos(math.pi * (i - 0.25) / (n + 0.5))
for _ in range(100):
p0, p1 = 1.0, x
for k in range(2, n + 1):
p0, p1 = p1, ((2 * k - 1) * x * p1 - (k - 1) * p0) / k
dp = n * (x * p1 - p0) / (x * x - 1)
dx = p1 / dp
x -= dx
if abs(dx) < 1e-16:
break
xs.append(x)
ws.append(2 / ((1 - x * x) * dp * dp))
return xs, ws
_GL = {}
def quad(f, a, b, n=64):
if n not in _GL:
_GL[n] = leggauss(n)
x, w = _GL[n]
return sum(wi * f(a + (b - a) * (xi + 1) / 2) for xi, wi in zip(x, w)) * (b - a) / 2
def quad_split(f, a, b, pieces=200, n=32):
h = (b - a) / pieces
return sum(quad(f, a + i * h, a + (i + 1) * h, n) for i in range(pieces))
def bez(P, t):
c = ((1 - t) ** 3, 3 * t * (1 - t) ** 2, 3 * t * t * (1 - t), t ** 3)
return tuple(sum(ci * p[k] for ci, p in zip(c, P)) for k in range(2))
def dbez(P, t):
c = (3 * (1 - t) ** 2, 6 * t * (1 - t), 3 * t * t)
d = [tuple(P[i + 1][k] - P[i][k] for k in range(2)) for i in range(3)]
return tuple(sum(ci * q[k] for ci, q in zip(c, d)) for k in range(2))
def norm(v):
return math.hypot(*v)
def bez_len(P):
return quad_split(lambda t: norm(dbez(P, t)), 0, 1, 50)
def bez_min_radius(P):
best = 1e18
for i in range(2001):
t = i / 2000
d = dbez(P, t)
dd = tuple(6 * (1 - t) * (P[2][k] - 2 * P[1][k] + P[0][k]) + 6 * t * (P[3][k] - 2 * P[2][k] + P[1][k]) for k in range(2))
kap = abs(d[0] * dd[1] - d[1] * dd[0]) / norm(d) ** 3
if kap > 0:
best = min(best, 1 / kap)
return best
def bez_area_under(P, base):
"""Area between a Bezier arch (ends on y = base) and the line y = base: -int (y-base) dx."""
return abs(quad(lambda t: (bez(P, t)[1] - base) * dbez(P, t)[0], 0, 1))
SPANDREL = 1 - PI / 4 # area factor of a fillet's removed section, times r^2
XBAR = (10 - 3 * PI) / (12 - 3 * PI) # spandrel centroid from the corner, times r
def case(tier, name, what, commands, expected, method, source="analytic", base=None, sources=None):
d = os.path.join(ROOT, tier, name)
os.makedirs(d, exist_ok=True)
recipe = {}
if base:
recipe["base"] = base
recipe["commands"] = commands
exp = {"sources": sources or [], "what": what, "source": source, "method": method}
for k, v in expected.items():
if isinstance(v, float):
v = round(v, 9)
exp[k] = v
with open(os.path.join(d, "recipe.json"), "w") as f:
json.dump(recipe, f, indent=1)
f.write("\n")
with open(os.path.join(d, "expected.json"), "w") as f:
json.dump(exp, f, indent=1)
f.write("\n")
CASES.append((tier, name))
def missing(tier, name, what, why, method):
d = os.path.join(ROOT, tier, name)
os.makedirs(d, exist_ok=True)
with open(os.path.join(d, "recipe.json"), "w") as f:
json.dump({"commands": []}, f, indent=1)
f.write("\n")
with open(os.path.join(d, "expected.json"), "w") as f:
json.dump({"sources": [], "what": what, "source": "invariants", "method": method, "missing": why}, f, indent=1)
f.write("\n")
CASES.append((tier, name))
def iso_material_fraction_ext(d, P):
"""ISO 68-1 basic external profile: material fraction m(r) per pitch, r in [r_root, d/2]."""
H = math.sqrt(3) / 2 * P
r1 = d / 2 - 5 * H / 8
t = math.tan(PI / 6)
return r1, (lambda r: 1.0 if r <= r1 else 1 - (P / 4 + 2 * (r - r1) * t) / P)
def iso_groove_fraction_int(d, P):
"""ISO 68-1 basic internal profile: fraction of a pitch the groove takes at radius r <= d/2."""
t = math.tan(PI / 6)
return lambda r: (P / 8 + 2 * (d / 2 - r) * t) / P