live ram_knee: the package's oracle; the ray count held by the jointlib rocker plates; own plates zoned about both pins; parting zones no wider than the member

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-12-02 20:59:27 +01:00
commit f97beab77e
3 changed files with 163 additions and 117 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
"""Live proof, in a background Blender, of brokkr_kitbash/ram.py's clearance gate on meshes: the R-3 knee (thigh,
shin, the bracket and horn, a bell crank's or a four-bar's plates, pins and clevises), every piece a closed mesh this
file generates, each a named part of a body, and the gate answered by a signed BVH oracle over them. Not part of the
unittest run: it needs Blender (mathutils' BVHTree).
file generates, each a named part of a body, and the gate answered by the package's signed BVH oracle (ram.Signed)
over them. Not part of the unittest run: it needs Blender (mathutils' BVHTree).
blender --background --factory-startup --python-exit-code 1 --python surfaces/blender/tests/live/ram_knee.py -- <out>
@ -9,9 +9,9 @@ Run it with private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USE
without them). <out> is an absolute path; writes <out>.result.json and <out>.r3_<angle>.png (the bell crank at 0,
-62.5 and -125) and exits 1 when a check failed.
The oracle. distance(part, points) is the nearest face's distance, negative where the point is inside one of the
part's shells. Inside is the crossing parity of three rays, by majority: one ray that grazes an edge or a vertex
miscounts, and an isolated sample then reads inside a plate it stands clear of.
The oracle is ram.Signed, three rays by majority. Its ray count is held by tests/fixtures/ram_rays.json: the rocker
plates as Imagination's jointlib builds them and points between the plates that stand on the first ray short of an
edge or a vertex. One ray reads them inside a plate they stand clear of; the oracle as it is made reads none inside.
A clearance verdict is a sign, the gate's own. FLAT follows lodur:ruling:deterministic-bound-margin. The checks run
inside one try: an uncaught error is a failed check that names its section, and the result file is always written.
@ -30,7 +30,6 @@ import bmesh
import bpy
import numpy as np
from mathutils import Matrix, Vector
from mathutils.bvhtree import BVHTree
SURFACE = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(SURFACE / "tools"))
@ -46,6 +45,7 @@ spec = importlib.util.spec_from_file_location("brokkr_kitbash_ram", SURFACE / "b
R = importlib.util.module_from_spec(spec) # ram.py as the unit tests load it
sys.modules[spec.name] = R
spec.loader.exec_module(R)
Signed = R.Signed
RES = {"blender": bpy.app.version_string, "checks": [], "readings": {}}
SECTION = ["setup"]
@ -67,7 +67,6 @@ CRANK = {"a": (0.45, 1.15), "rocker": (0.3171126791600819, 0.3216537818398838),
"c": (0.34929485118682846, 0.14862538079826304), "d": (0.48651463351122515, 0.49166254003196397)}
ROOTS = {"bell_crank": {"horn": (0.10, -0.40), "bracket": (0.10, 0.55)}, "four_bar": {"horn": (0.0, -0.42), "bracket": (-0.10, 0.55)}}
THIGH_FRONT, EMBED = 0.14, 0.01 # the thigh core's faces stand at y +-0.14; a foot runs this far into it
RAYS = [Vector(v).normalized() for v in ((0.5377, 0.6211, 0.5701), (-0.7071, 0.2903, 0.6447), (0.1873, -0.8011, -0.5684))]
def check(name, ok, detail=None):
@ -138,44 +137,6 @@ def knee(kind, pn, links=True):
return shells, {part: {"parent": "parent", "child": "child", "rocker": "rocker", "coupler": "coupler"}[part.split(".")[0]] for part in shells}
class Signed:
"""distance(part, points): negative inside a part's shell (the depth below its nearest face), capped outside
only. Inside is the crossing parity of `rays` rays, by majority. An unknown part raises."""
def __init__(self, shells, cap=None, rays=3):
self.cap, self.asked, self.rays = cap, 0, RAYS[:rays]
self.trees = {part: [(BVHTree.FromPolygons(v.tolist(), f, epsilon=0.0), v.min(axis=0), v.max(axis=0)) for v, f in made] for part, made in shells.items()}
@staticmethod
def crossings(tree, v, ray):
count, o = 0, v
for _ in range(64):
loc = tree.ray_cast(o, ray)[0]
if loc is None:
break
count, o = count + 1, loc + ray * 1e-6
return count
def inside(self, tree, v):
return 2 * sum(self.crossings(tree, v, ray) % 2 for ray in self.rays) > len(self.rays)
def __call__(self, part, pts):
pts = np.asarray(pts, dtype=float)
self.asked += len(pts)
out = np.full(len(pts), math.inf if self.cap is None else self.cap)
for tree, lo, hi in self.trees[part]:
boxed = ((pts >= lo) & (pts <= hi)).all(axis=1)
for i, p in enumerate(pts):
v = Vector(p)
if boxed[i] and self.inside(tree, v):
out[i] = min(out[i], -tree.find_nearest(v)[3])
continue
hit = tree.find_nearest(v, self.cap) if self.cap else tree.find_nearest(v)
if hit[0] is not None and hit[3] < out[i]:
out[i] = hit[3]
return out
def near(ram, member):
return {"barrel": ram.barrel_r, "rod": ram.rod}.get(member, ram.link_r) + ram.gap + ram.spacing() / 2
@ -205,23 +166,30 @@ def crossing(m, kind):
def zones(m, ram, parts):
"""Each member's zones. About a pin: the pin's, collar's or cheek's radius plus what the member keeps, as deep as
the pin where it runs through the member's eye and 0 beside a collar, a cheek, the bracket or the horn; a link's
own plate over its length, half its thickness deep; a plate beside a link as far as the two part. A depth that
is not 0 is stated SLACK over the burial."""
own plate about both its pins, each a little over half its length (a zone is no wider than its member), half
the plate's thickness deep; a plate beside a link as far as the two part. A depth that is not 0 is stated SLACK
over the burial."""
kind, link, at = m.kind, near(ram, "rocker"), crossing(m, m.kind)
PIN, AXLE, HALF = (value + SLACK for value in (globals()["PIN"], globals()["AXLE"], globals()["HALF"]))
reach = {"pin": globals()["PIN"] + link, "axle": globals()["AXLE"] + link}
eye = "rocker.pin.d" if kind == "bell_crank" else "rocker.pin.p"
def own(part, a, b, span):
return [(part, a, 0.51 * span, HALF), (part, b, 0.51 * span, HALF)]
def parts_at(member, w, degrees): # never wider than the member: two that cross at a few degrees part beyond its length
return min(parting(ram, w, degrees), R._span(m, member))
touch = {member: [("parent.seat", "a", 1.7 * BORE + near(ram, member), 0.0), ("rocker.seat", "d", COLLAR + near(ram, member), 0.0),
(eye, "d", globals()["PIN"] + near(ram, member), PIN if member == "rod" else 0.0)] for member in ("barrel", "rod")}
pivot = [("parent.bracket", "rocker", None, 0.0), ("rocker.pin.R", "rocker", reach["axle"], AXLE)]
touch["rocker"] = pivot + [("rocker.pin.p", "p", reach["pin"], PIN), ("rocker.plate", "rocker", 1.01 * m.lr, HALF), ("coupler.plate", "p", parting(ram, W_COUPLER, at["p"]), 0.0)]
touch["coupler"] = [("child.horn", "c", parting(ram, W_HORN, at["coupler"]), 0.0), ("coupler.pin.c", "c", reach["pin"], PIN), ("rocker.pin.p", "p", reach["pin"], PIN),
("coupler.plate", "p", 1.01 * m.lc, HALF), ("rocker.plate", "p", parting(ram, W_ROCKER, at["p"]), 0.0)]
touch["rocker"] = pivot + [("rocker.pin.p", "p", reach["pin"], PIN), *own("rocker.plate", "rocker", "p", m.lr), ("coupler.plate", "p", parts_at("rocker", W_COUPLER, at["p"]), 0.0)]
touch["coupler"] = [("child.horn", "c", parts_at("coupler", W_HORN, at["coupler"]), 0.0), ("coupler.pin.c", "c", reach["pin"], PIN), ("rocker.pin.p", "p", reach["pin"], PIN),
*own("coupler.plate", "p", "c", m.lc), ("rocker.plate", "p", parts_at("coupler", W_ROCKER, at["p"]), 0.0)]
if kind == "bell_crank":
touch["rocker arm"] = pivot + [("rocker.pin.d", "d", reach["pin"], PIN), ("rocker.plate", "rocker", 1.01 * float(np.linalg.norm(R._flat(m.d - m.rocker, m.axis))), HALF)]
touch["rocker arm"] = pivot + [("rocker.pin.d", "d", reach["pin"], PIN), *own("rocker.plate", "rocker", "d", R._span(m, "rocker arm"))]
for member in ("rocker", "rocker arm"):
if member in touch:
touch[member][0] = ("parent.bracket", "rocker", parting(ram, W_BRACKET, at[member]), 0.0)
touch[member][0] = ("parent.bracket", "rocker", parts_at(member, W_BRACKET, at[member]), 0.0)
return {member: [zone for zone in given if zone[0] in parts] for member, given in touch.items()}
@ -353,6 +321,16 @@ try:
exact = np.maximum.reduce([np.abs(cloud[:, 0]) - 0.13, np.abs(cloud[:, 1]) - 0.14, np.abs(cloud[:, 2] - 0.725) - 0.425])
check("three-rays: 4000 random points about a box are signed as the box signs them", ((core < 0) == (exact < 0)).all(), int(((core < 0) != (exact < 0)).sum()))
# the ray count: the jointlib rocker plates, and points between them aimed along the first ray at an edge or a vertex
fixture = json.loads((SURFACE / "tests" / "fixtures" / "ram_rays.json").read_text(encoding="utf-8"))
plates, aimed = {"plates": [(np.array(fixture["verts"]), fixture["faces"])]}, np.array(fixture["points"])
made, single = Signed(plates)("plates", aimed), Signed(plates, rays=1)("plates", aimed)
RES["readings"]["jointlib plates: points one ray signs inside, of %d" % len(aimed)] = int((single < 0).sum())
RES["readings"]["jointlib plates: points the oracle signs inside"] = int((made < 0).sum())
# one ray reads all 24 inside; the oracle as made reads none. The bound on one ray's count is the geometric mean of 1 and 24
check("ray-count: between the jointlib rocker plates, points on the first ray short of an edge or a vertex read outside by the oracle as it is made, inside by one ray",
(np.abs(aimed[:, 0]) < 0.349).all() and (made > 0).all() and (single < 0).sum() >= 5, [int((made < 0).sum()), int((single < 0).sum())])
SECTION[0] = "seat"
seat = np.concatenate([v for v, _ in shells["parent.seat"]])
deepest = float(Signed(shells)("parent.body", seat).min())
@ -375,8 +353,15 @@ try:
check(f"bell-crank-clears-on-meshes (side {side:+.0f}): and at 251 steps with the oracle uncapped", fine.ok, [fine.report(), refused(fine)])
bare = R._fit(replace(m, touch={}), ram, steps=49, distance=Signed(shells, ram.reach()))
need = needs(m, ram, parts, Signed(shells))
stated = {(label(member), zone[0]): zone for member, given in touch.items() for zone in given}
slack = {pair: (stated[pair][1] == pin and stated[pair][2] >= radius and stated[pair][3] >= depth) for pair, (pin, radius, depth) in need.items() if pair in stated}
stated = {}
for member, given in touch.items():
for zone in given:
stated.setdefault((label(member), zone[0]), []).append(zone)
def takes(given, pin, radius, depth): # one zone about the pin, or zones about both of the member's pins that meet
return all(zone[3] >= depth for zone in given) and (any(zone[1] == pin and zone[2] >= radius for zone in given)
or (len({zone[1] for zone in given}) == 2 and sum(zone[2] for zone in given) >= radius))
slack = {pair: takes(stated[pair], *least) for pair, least in need.items() if pair in stated}
check(f"zones (side {side:+.0f}): with nothing let the gate refuses, and only pairs the record zones; each stated zone takes in the least that lets its pair",
{pair for pair, _, _ in refused(bare)} == set(need) and set(need) <= zoned and all(slack.values()), [refused(bare), sorted(set(need) - zoned), [k for k, v in slack.items() if not v]])
if side > 0:
@ -387,7 +372,7 @@ try:
"bell crank: least margin of a zoned pair, 251 steps uncapped": min(reading[0] for pair, reading in fine.pairs.items() if pair in zoned),
"bell crank: pairs": len(f.pairs), "bell crank: zoned pairs": len(f.allowed), "bell crank: pairs refused with nothing let": len(refused(bare)),
"bell crank: seconds, 49 steps capped": seconds})
RES["zones"] = {f"{pair[0]} | {pair[1]}": {"stated": list(stated[pair][1:]), "least": [pin, round(radius, 4), round(depth, 4)]} for pair, (pin, radius, depth) in sorted(need.items()) if pair in stated}
RES["zones"] = {f"{pair[0]} | {pair[1]}": {"stated": [list(zone[1:]) for zone in stated[pair]], "least": [pin, round(radius, 4), round(depth, 4)]} for pair, (pin, radius, depth) in sorted(need.items()) if pair in stated}
RES["least pairs"] = sorted((round(v[0], 4), v[1], k[0], k[1]) for k, v in fine.pairs.items() if k not in zoned)[:8]
SECTION[0] = "zones end"

View file

@ -16,6 +16,11 @@
"ok": true,
"detail": null
},
{
"name": "ray-count: between the jointlib rocker plates, points on the first ray short of an edge or a vertex read outside by the oracle as it is made, inside by one ray",
"ok": true,
"detail": null
},
{
"name": "the base clevis is seated: its foot runs 0.01 into the thigh's core, through the front plate",
"ok": true,
@ -103,9 +108,11 @@
"bell crank: least margin of a zoned pair, 251 steps uncapped": 0.000118,
"bell crank: pairs": 60,
"bell crank: pairs refused with nothing let": 19,
"bell crank: seconds, 49 steps capped": 0.835,
"bell crank: seconds, 49 steps capped": 2.12,
"bell crank: zoned pairs": 20,
"four bar: barrel inside the shin's clearance": -0.122,
"jointlib plates: points one ray signs inside, of 24": 24,
"jointlib plates: points the oracle signs inside": 0,
"moved x: both": -0.0543,
"moved x: the coupler at x 0.25, inside the horn's 0.23..0.31": -0.02,
"moved x: the rocker at x 0.25, inside the bracket's 0.13..0.35": -0.0543,
@ -113,16 +120,18 @@
"search: candidates": 13824,
"search: pass the gates before clearance": 78,
"search: refused on their own parts": 21,
"search: seconds": 42.8,
"search: seconds": 43.3,
"search: worlds built": 22,
"the base clevis's foot inside the thigh": -0.01
},
"zones": {
"barrel[0] | parent.seat": {
"stated": [
"a",
0.15645000000000003,
0.0
[
"a",
0.15645000000000003,
0.0
]
],
"least": [
"a",
@ -132,9 +141,11 @@
},
"barrel[0] | rocker.pin.d": {
"stated": [
"d",
0.11105000000000001,
0.0
[
"d",
0.11105000000000001,
0.0
]
],
"least": [
"d",
@ -144,9 +155,11 @@
},
"barrel[0] | rocker.seat": {
"stated": [
"d",
0.13505,
0.0
[
"d",
0.13505,
0.0
]
],
"least": [
"d",
@ -156,9 +169,11 @@
},
"coupler | child.horn": {
"stated": [
"c",
0.11195,
0.0
[
"c",
0.11195,
0.0
]
],
"least": [
"c",
@ -168,9 +183,11 @@
},
"coupler | coupler.pin.c": {
"stated": [
"c",
0.08795,
0.027
[
"c",
0.08795,
0.027
]
],
"least": [
"c",
@ -180,9 +197,16 @@
},
"coupler | coupler.plate": {
"stated": [
"p",
0.47832364050530635,
0.018500000000000016
[
"p",
0.24152975906703586,
0.018500000000000016
],
[
"c",
0.24152975906703586,
0.018500000000000016
]
],
"least": [
"p",
@ -192,9 +216,11 @@
},
"coupler | rocker.pin.p": {
"stated": [
"p",
0.08795,
0.027
[
"p",
0.08795,
0.027
]
],
"least": [
"p",
@ -204,9 +230,11 @@
},
"coupler | rocker.plate": {
"stated": [
"p",
0.2818973893266964,
0.0
[
"p",
0.2818973893266964,
0.0
]
],
"least": [
"p",
@ -216,9 +244,11 @@
},
"rocker | coupler.plate": {
"stated": [
"p",
0.26840002998268986,
0.0
[
"p",
0.26840002998268986,
0.0
]
],
"least": [
"p",
@ -228,9 +258,11 @@
},
"rocker | parent.bracket": {
"stated": [
"rocker",
0.11695,
0.0
[
"rocker",
0.11695,
0.0
]
],
"least": [
"rocker",
@ -240,9 +272,11 @@
},
"rocker | rocker.pin.R": {
"stated": [
"rocker",
0.08995,
0.029
[
"rocker",
0.08995,
0.029
]
],
"least": [
"rocker",
@ -252,9 +286,11 @@
},
"rocker | rocker.pin.p": {
"stated": [
"p",
0.08795,
0.027
[
"p",
0.08795,
0.027
]
],
"least": [
"p",
@ -264,9 +300,16 @@
},
"rocker | rocker.plate": {
"stated": [
"rocker",
0.43014219644091495,
0.018500000000000016
[
"rocker",
0.2172005150345214,
0.018500000000000016
],
[
"p",
0.2172005150345214,
0.018500000000000016
]
],
"least": [
"rocker",
@ -276,9 +319,11 @@
},
"rocker arm | parent.bracket": {
"stated": [
"rocker",
0.11695,
0.0
[
"rocker",
0.11695,
0.0
]
],
"least": [
"rocker",
@ -288,9 +333,11 @@
},
"rocker arm | rocker.pin.R": {
"stated": [
"rocker",
0.08995,
0.029
[
"rocker",
0.08995,
0.029
]
],
"least": [
"rocker",
@ -300,9 +347,11 @@
},
"rocker arm | rocker.pin.d": {
"stated": [
"d",
0.08795,
0.027
[
"d",
0.08795,
0.027
]
],
"least": [
"d",
@ -312,9 +361,16 @@
},
"rocker arm | rocker.plate": {
"stated": [
"rocker",
0.24240000000000003,
0.018500000000000016
[
"rocker",
0.12240000000000001,
0.018500000000000016
],
[
"d",
0.12240000000000001,
0.018500000000000016
]
],
"least": [
"rocker",
@ -324,9 +380,11 @@
},
"rod[0] | rocker.pin.d": {
"stated": [
"d",
0.07955,
0.027
[
"d",
0.07955,
0.027
]
],
"least": [
"d",
@ -336,9 +394,11 @@
},
"rod[0] | rocker.seat": {
"stated": [
"d",
0.10355,
0.0
[
"d",
0.10355,
0.0
]
],
"least": [
"d",
@ -436,9 +496,9 @@
}
},
"renders": [
"F:/aeth-wt/fable-ramcore/scratch/h3/out/live3.r3_0.png",
"F:/aeth-wt/fable-ramcore/scratch/h3/out/live3.r3_62p5.png",
"F:/aeth-wt/fable-ramcore/scratch/h3/out/live3.r3_125.png"
"F:/aeth-wt/fable-ramcore/scratch/h4/out/live_h4.r3_0.png",
"F:/aeth-wt/fable-ramcore/scratch/h4/out/live_h4.r3_62p5.png",
"F:/aeth-wt/fable-ramcore/scratch/h4/out/live_h4.r3_125.png"
],
"ok": true
}