diff --git a/surfaces/blender/brokkr_kitbash/ram.py b/surfaces/blender/brokkr_kitbash/ram.py index d7cb60f..1383b5e 100644 --- a/surfaces/blender/brokkr_kitbash/ram.py +++ b/surfaces/blender/brokkr_kitbash/ram.py @@ -31,6 +31,9 @@ it turns with: a thigh's core, the bracket that carries the rocker's pivot, a li for each member of MEMBERS[kind], zones (part, pin, radius, depth). Every member meets every part. A zone lowers what the member's samples keep from one part, and only within `radius` of the axis of `pin`, one of the member's own (RIDES), measured across the hinge axis: there a sample may come as near the part as -depth and no nearer. +The part is one of a body that pin joins (JOINS: a the parent; the rocker's pivot the parent and the rocker; p the +rocker and the coupler; c the coupler and the child; b the child; d the rocker), or the record is refused: what a +member meets at a pin is of the bodies pinned there, and a part of any other body is never let near it. depth is 0 for what is stacked beside the member on the pin (a plate, the bracket or horn the pin stands in: the member's axis stays outside it, so a pin whose place along the axis is inside the part it claims to stand beside is refused), the pin's radius for the pin through the member's eye, half the thickness for the member's own plate @@ -68,6 +71,9 @@ MEMBERS = {"direct": ("barrel", "rod"), "four_bar": ("barrel", "rod", "rocker", EYE = {"direct": "b", "four_bar": "d", "bell_crank": "d"} # the pin the rod's eye rides RIDES = {kind: {"barrel": ("a", EYE[kind]), "rod": ("a", EYE[kind]), "rocker": ("rocker", "p"), "coupler": ("p", "c"), "rocker arm": ("rocker", "d")} for kind in KINDS} # member -> the pins at its start and its end +JOINS = {kind: {"a": ("parent",), "b": ("child",)} if kind == "direct" else + {"a": ("parent",), "rocker": ("parent", "rocker"), "p": ("rocker", "coupler"), "c": ("coupler", "child"), "d": ("rocker",)} + for kind in KINDS} # pin -> the bodies it joins: only their parts are zoned about it GATES = ("assembly", "stroke", "toggle", "arm", "load", "clash") SCHEMA, VERSION = "brokkr.ram", 1 ZONE = ("part", "pin", "radius", "depth") # a zone of `touch`, as the record writes it @@ -216,6 +222,9 @@ class Mechanism: radius, depth = _number(self.kind, f"{member} {part} radius", radius, 0.0), _number(self.kind, f"{member} {part} depth", depth) if depth < 0.0: raise RamError(self.kind, "record", f"a depth of 0 or more, got {depth!r}", pin=f"{member} {part} depth") + if self.parts[part] not in JOINS[self.kind][pin]: + raise RamError(self.kind, "record", f"{member} may touch about {pin} a part of a body the pin joins ({', '.join(JOINS[self.kind][pin])}); " + f"{part!r} turns with {self.parts[part]}", pin=f"{member} {part} pin") span = _span(self, member) for name, value in (("radius", radius), ("depth", depth)): if value > span: diff --git a/surfaces/blender/tests/test_ram.py b/surfaces/blender/tests/test_ram.py index 1fbe829..8a8c12d 100644 --- a/surfaces/blender/tests/test_ram.py +++ b/surfaces/blender/tests/test_ram.py @@ -562,8 +562,9 @@ class DeepKnee(unittest.TestCase): def test_a_part_in_the_rams_way_on_each_link(self): # a lug on the rocker, then on the coupler, on the rod's axis 0.12 from the eye at -60: refused naming the part - # and its body. A zone about the eye lets the rod and the barrel by it only if it reaches the lug and is as - # deep as the lug; a zone of another member's changes nothing + # and its body. A zone about the eye lets the rod and the barrel by the rocker's only if it reaches the lug and + # is as deep as the lug; a zone of another member's changes nothing. The coupler's lug is let by no zone of + # the ram's: the eye's pin does not join the coupler m, ram = r3("bell_crank", CRANK), R.Ram(BORE) at = R.pose(m, -60.0) on_rod = (at.eye - 0.12 * at.u)[:, None, :] @@ -574,13 +575,20 @@ class DeepKnee(unittest.TestCase): def zones(radius, depth, members=("rod", "barrel")): return {**touch, **{member: touch[member] + [("lug", "d" if member != "coupler" else "p", radius, depth)] for member in members}} - for label, given in (("nothing let", touch), ("the coupler's zone", zones(0.4, 0.02, ("coupler",))), ("a zone short of the lug", zones(0.1, 0.02)), - ("a zone too shallow", zones(0.4, 0.01))): + if body == "coupler": # d joins the rocker alone: no zone of the ram's names a part of the coupler + with self.assertRaises(R.RamError) as caught: + dressed(m, bodies, zones(0.4, 0.02)) + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", "barrel lug pin")) + self.assertIn("a body the pin joins (rocker); 'lug' turns with coupler", str(caught.exception)) + for label, given in (("nothing let", touch), ("the coupler's zone", zones(0.4, 0.02, ("coupler",))), + *((("a zone short of the lug", zones(0.1, 0.02)), ("a zone too shallow", zones(0.4, 0.01))) if body == "rocker" else ())): with self.assertRaises(R.RamError) as caught: R.fit(dressed(m, bodies, given), ram, steps=49, distance=bodies) self.assertEqual((caught.exception.gate, caught.exception.fit.clear_at[2]), ("clash", "lug"), (body, label)) self.assertIn(caught.exception.fit.clear_at[1], ("rod[0]", "barrel[0]")) self.assertIn(f"part 'lug' ({body})", str(caught.exception)) + if body == "coupler": + continue f = R.fit(dressed(m, bodies, zones(0.4, 0.02)), ram, steps=49, distance=bodies) self.assertIn(("rod[0]", "lug"), f.allowed) self.assertNotIn(("rocker arm", "lug"), f.allowed) @@ -645,16 +653,16 @@ class DeepKnee(unittest.TestCase): self.assertAlmostEqual(R._span(m, "rocker arm"), 0.24, places=9) self.assertGreaterEqual(R._span(m, "rod"), float(R.pose(m, np.linspace(-125.0, 0.0, 126)).L.max())) self.assertEqual([ram.eye(*of) for of in (("barrel", "a"), ("rod", "a"), ("rod", "d"), ("coupler", "c"))], [ram.e_a, ram.e_a, ram.e_b, ram.link_r]) - for member, pin in (("barrel", "a"), ("rod", "d"), ("rocker", "p"), ("coupler", "c"), ("rocker arm", "rocker")): + for member, pin, part in (("barrel", "a", "thigh"), ("rod", "d", "pin d"), ("rocker", "p", "coupler plates"), ("coupler", "c", "horn"), ("rocker arm", "rocker", "bracket")): span, eye = R._span(m, member), ram.eye(member, pin) def zoned(radius, depth): - return dressed(m, bodies, {member: [("thigh", pin, radius, depth)]}) + return dressed(m, bodies, {member: [(part, pin, radius, depth)]}) zoned(span, 0.0) for radius, depth, field in ((1.2 * span, 0.0, "radius"), (1e308, 0.0, "radius"), (1e9, 1e9, "radius"), (0.1, 1.2 * span, "depth"), (0.1, 1e308, "depth")): with self.assertRaises(R.RamError) as caught: zoned(radius, depth) - self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", f"{member} thigh {field}")) + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", f"{member} {part} {field}")) self.assertIn(f"{member} is at most {span:.4f} from pin to pin", str(caught.exception)) R.clearance(zoned(0.1, eye), ram, [-60.0], bodies) wrote = R.record(zoned(0.1, eye), ram) @@ -664,19 +672,66 @@ class DeepKnee(unittest.TestCase): lambda: R.from_record(wrote)): with self.assertRaises(R.RamError) as caught: read() - self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", f"{member} thigh depth")) + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", f"{member} {part} depth")) self.assertIn(f"{member}'s on {pin} is {eye:.4f}", str(caught.exception)) + def test_a_zone_names_a_part_of_a_body_its_pin_joins(self): + # Soul's whole-member zones: two of 0.51 of its length about p and c covered the coupler and let its axis + # 0.0294 into a capsule of the thigh. No bound on a radius stops that; the record says which bodies a pin + # joins, and a part of any other is zoned about it by no member, whatever the radius and depth + joins = {"a": ("parent",), "b": ("child",), "rocker": ("parent", "rocker"), "p": ("rocker", "coupler"), "c": ("coupler", "child"), "d": ("rocker",)} + for m in (R.Mechanism("direct", O, X, -62, 0, a=(0.05, -0.3, 0.8), b=(0.05, -0.3, -0.6)), r3("four_bar", DEEP), r3("bell_crank", CRANK)): + parts = {"of " + body: body for body in R.BODIES[m.kind]} + for member in R.MEMBERS[m.kind]: + for pin in R.RIDES[m.kind][member]: + for part, body in parts.items(): + zone = {member: [(part, pin, 0.01, 0.0)]} + if body in joins[pin]: + self.assertEqual(replace(m, parts=parts, touch=zone).touch[member], ((part, pin, 0.01, 0.0),)) + continue + with self.assertRaises(R.RamError) as caught: + replace(m, parts=parts, touch=zone) + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", f"{member} {part} pin"), (m.kind, member, pin, body)) + self.assertIn(f"{member} may touch about {pin} a part of a body the pin joins ({', '.join(joins[pin])}); " + f"'{part}' turns with {body}", str(caught.exception)) + # Soul's capsule of the parent under the coupler, zoned as the probe zoned it: refused by the record, by + # from_record and as a world's answer; the shipped zones, each a part of a body its pin joins, are read + m, ram = r3("bell_crank", CRANK), R.Ram(BORE) + bodies, touch = r3_bodies(m, ram) + wire = json.loads(json.dumps(R.record(dressed(m, bodies, touch), ram))) + self.assertEqual(R.from_record(wire)[0].touch, dressed(m, bodies, touch).touch) + bodies.parts["under"], bodies.shapes["under"] = "parent", [("capsule", (X_COUPLER, 0.5, 0.2), (X_COUPLER, 0.6, 0.3), 0.08)] + halves = [("under", "p", 0.51 * m.lc, ram.link_r), ("under", "c", 0.51 * m.lc, ram.link_r)] + wire["parts"]["under"] = "parent" + for k, zone in enumerate(halves): + given = {**touch, "coupler": touch["coupler"] + halves[k:]} + wrote = {**wire, "touch": {**wire["touch"], "coupler": wire["touch"]["coupler"] + [dict(zip(R.ZONE, zone))]}} + hinge, seat = {"pivot": O, "axis": X, "lo": -125, "hi": 0}, {name: [getattr(m, name)] for name in ("a", "rocker", "c")} + for read in (lambda: dressed(m, bodies, given), lambda: R.from_record(wrote), + lambda: R.search("bell_crank", hinge, ram, {**seat, "link": (m.lr, m.lr, 1), "d": (0.24, 0.24, 1)}, world=lambda made: (bodies.parts, given, bodies))): + with self.assertRaises(R.RamError) as caught: + read() + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", "coupler under pin")) + self.assertIn(f"about {zone[1]} a part of a body the pin joins", str(caught.exception)) + # the body is refused before the zone is measured: a zone both foreign and wider than its member names the pin + with self.assertRaises(R.RamError) as caught: + dressed(m, bodies, {"coupler": [("under", "p", 1e9, 0.0)]}) + self.assertEqual(caught.exception.pin, "coupler under pin") + def test_a_wall_the_axis_crosses_in_a_zone_is_read_whatever_its_thickness(self): - # Soul's tunnel: a wall of the thigh across the coupler, midway between two of its samples, inside a zone - # about p of depth 0. Every sample stands outside it; the segment between them is what crosses + # Soul's tunnel: a wall of the rocker across the coupler, midway between two of its samples, inside a zone + # about p of depth 0. Every sample stands outside it; the segment between them is what crosses. The wall is + # the rocker's, which p joins, and stands in the rocker's frame, where the coupler's samples at -60 are `samples` m, ram = r3("bell_crank", CRANK), R.Ram(BORE) at = R.pose(m, -60.0) start = at.P[0] - np.array(X) * ((at.P[0] - at.C[0]) @ np.array(X)) count = int(math.ceil(m.lc / ram.spacing())) + 1 - samples = start + (at.C[0] - start) * np.linspace(0.0, 1.0, count)[:, None] + + def seen(n): + return R._into(m, at, "rocker", (start + (at.C[0] - start) * np.linspace(0.0, 1.0, n)[:, None])[None])[0] + samples = seen(count) spot = 0.5 * (samples[2] + samples[3]) - along = (at.C[0] - start) / m.lc + along = (samples[-1] - samples[0]) / m.lc k = 1 if abs(along[1]) > abs(along[2]) else 2 def reading(half, shift=0.0): @@ -684,7 +739,7 @@ class DeepKnee(unittest.TestCase): lo[k], hi[k] = spot[k] - half, spot[k] + half lo[0], hi[0] = X_COUPLER - 0.05 + shift, X_COUPLER + 0.05 + shift bodies = r3_parts(m, ram) - bodies.parts["wall"], bodies.shapes["wall"] = "parent", [("box", lo, hi)] + bodies.parts["wall"], bodies.shapes["wall"] = "rocker", [("box", lo, hi)] self.assertGreater(float(bodies("wall", samples).min()), 0.0) return R.clearance(dressed(m, bodies, {"coupler": [("wall", "p", 0.28, 0.0)]}), ram, [-60.0], bodies).pairs[("coupler", "wall")][0] for half in (0.01, 0.003, 1e-4, 1e-7, 1e-10): @@ -697,9 +752,9 @@ class DeepKnee(unittest.TestCase): self.assertGreaterEqual(reading(0.01, 0.05 + 1.2 * ram.spacing() / R.FINE), 0.0) # a segment with one end in the zone is proved as well. Sampled 0.3 apart, the sample beyond the zone keeps its # 0.186 from a bead on the axis 0.008 past the last sample in the zone; the bead is read all the same - wide = start + (at.C[0] - start) * np.linspace(0.0, 1.0, 3)[:, None] + wide = seen(3) bodies, spot = r3_parts(m, ram), wide[1] + 0.012 * along - bodies.parts["bead"], bodies.shapes["bead"] = "parent", [("capsule", spot, spot, 0.004)] + bodies.parts["bead"], bodies.shapes["bead"] = "rocker", [("capsule", spot, spot, 0.004)] self.assertGreater(float(bodies("bead", wide[:2]).min()), 0.0) self.assertGreaterEqual(float(bodies("bead", wide[2:])[0]), near(ram, "coupler", 0.3)) got = R.clearance(dressed(m, bodies, {"coupler": [("bead", "p", 0.25, 0.0)]}), ram, [-60.0], bodies, spacing=0.3) @@ -843,8 +898,13 @@ class OracleRule(unittest.TestCase): lugged = two(a=seats.shapes["a"] and ("parent", seats.shapes["a"]), b=("child", seats.shapes["b"]), **{"b.lug": ("child", seats.shapes["b"])}) got = R.clearance(dressed(m, lugged, zones), ram, 0.0, lugged) self.assertEqual({pair: round(reading[0], 9) for pair, reading in got.pairs.items() if reading[0] < 0.0}, {("rod[0]", "b.lug"): round(-0.03 - near(ram, "rod"), 9)}) - for crossed in ({"barrel": [("b", "b", 0.2, 0.03)], "rod": [("a", "a", 0.2, 0.03)]}, {"barrel": [("a", "b", 0.2, 0.03)], "rod": [("b", "a", 0.2, 0.03)]}): - self.assertLess(float(R.clearance(dressed(m, seats, crossed), ram, 0.0, seats).margin[0]), 0.0) + crossed = {"barrel": [("b", "b", 0.2, 0.03)], "rod": [("a", "a", 0.2, 0.03)]} + self.assertLess(float(R.clearance(dressed(m, seats, crossed), ram, 0.0, seats).margin[0]), 0.0) + # and a part zoned about the pin at the member's other end, which does not join its body, is no zone at all + for crossed, pin in (({"barrel": [("a", "b", 0.2, 0.03)]}, "barrel a pin"), ({"rod": [("b", "a", 0.2, 0.03)]}, "rod b pin")): + with self.assertRaises(R.RamError) as caught: + dressed(m, seats, crossed) + self.assertEqual((caught.exception.gate, caught.exception.pin), ("record", pin)) def test_every_body_owns_a_part(self): ball = Bodies({"ball": ("parent", [("capsule", (0, 0, 0), (0, 0, 0), 0.1)])}) @@ -942,19 +1002,20 @@ class SearchBestFirst(unittest.TestCase): ((ball.parts, {}, None), "search", "world(m) returns"), ((ball.parts, {"rod": ["parent"]}, ball), "record", "zone is (part, pin, radius, depth)"), ((ball.parts, {"rod": [("parent", "c", 1.0, 0.0)]}, ball), "record", "a pin it rides (a, b)"), ((ball.parts, {"rod": [("thigh", "b", 1.0, 0.0)]}, ball), "record", "no part of the record"), - ((ball.parts, {"rod": [("parent", "b", 1e308, 0.0)]}, ball), "record", "rod is at most"), - ((ball.parts, {"rod": [("parent", "b", 0.5, 1e308)]}, ball), "record", "rod is at most"), - ((ball.parts, {"rod": [("parent", "b", 1e9, 1e9)]}, ball), "record", "rod is at most"), - ((ball.parts, {"rod": [("parent", "b", float("inf"), 0.0)]}, ball), "record", "a finite number"), - ((ball.parts, {"rod": [("parent", "b", 0.5, float("nan"))]}, ball), "record", "a finite number"), - ((ball.parts, {"rod": [("parent", "b", 0.5, 1.2 * ram.e_b)]}, ball), "record", "rod's on b is 0.0850"), + ((ball.parts, {"rod": [("child", "b", 1e308, 0.0)]}, ball), "record", "rod is at most"), + ((ball.parts, {"rod": [("parent", "b", 0.5, 0.0)]}, ball), "record", "a body the pin joins (child); 'parent' turns with parent"), + ((ball.parts, {"rod": [("child", "b", 0.5, 1e308)]}, ball), "record", "rod is at most"), + ((ball.parts, {"rod": [("child", "b", 1e9, 1e9)]}, ball), "record", "rod is at most"), + ((ball.parts, {"rod": [("child", "b", float("inf"), 0.0)]}, ball), "record", "a finite number"), + ((ball.parts, {"rod": [("child", "b", 0.5, float("nan"))]}, ball), "record", "a finite number"), + ((ball.parts, {"rod": [("child", "b", 0.5, 1.2 * ram.e_b)]}, ball), "record", "rod's on b is 0.0850"), (({"ball": "rocker"}, {}, ball), "record", "turns with 'rocker'"), (({"ball": "parent"}, {}, ball), "record", "no part turns with body child")): with self.assertRaises(R.RamError) as caught: R.search("direct", self.HINGE, ram, seats, world=lambda m: made) self.assertEqual(caught.exception.gate, gate, said) self.assertIn(said, str(caught.exception)) self.assertTrue(R.search("direct", self.HINGE, ram, seats, world=lambda m: (ball.parts, {}, ball)).found) - self.assertTrue(R.search("direct", self.HINGE, ram, seats, world=lambda m: (ball.parts, {"rod": [("parent", "b", 0.5, ram.e_b)]}, ball)).found) + self.assertTrue(R.search("direct", self.HINGE, ram, seats, world=lambda m: (ball.parts, {"rod": [("child", "b", 0.5, ram.e_b)]}, ball)).found) def test_the_limit_is_refused_naming_the_count(self): seats = {"a": grid((0, -0.5, 0.4), (0, -0.2, 1.2), (1, 3, 5)), "b": grid((0, -0.5, -1.2), (0, -0.2, -0.4), (1, 3, 5))} @@ -1082,7 +1143,8 @@ class RecordDrives(unittest.TestCase): ({"parts": {"knee": "parent"}, "touch": {"rod": "knee"}}, "rod's zones are a list"), ({"parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": 0.1}]}}, "missing ['depth']"), ({"parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": 0.1, "depth": 0.0, "whole": True}]}}, "unknown ['whole']"), - ({"parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": None, "depth": 0.0}]}}, "(pin rod knee radius)")): + ({"parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": None, "depth": 0.0}]}}, "(pin rod knee radius)"), + ({"parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": 0.1, "depth": 0.0}]}}, "(pin rod knee pin): rod may touch about b a part of a body the pin joins (child)")): with self.assertRaises(R.RamError) as caught: R.from_record({**data, **change}) self.assertEqual(caught.exception.gate, "record", change) @@ -1090,7 +1152,7 @@ class RecordDrives(unittest.TestCase): with self.assertRaises(R.RamError) as caught: R.from_record([data]) self.assertIn("a record is an object, got list", str(caught.exception)) - zoned = R.from_record({**data, "parts": {"knee": "parent"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": 0.1, "depth": 0}]}})[0] + zoned = R.from_record({**data, "parts": {"knee": "child"}, "touch": {"rod": [{"part": "knee", "pin": "b", "radius": 0.1, "depth": 0}]}})[0] self.assertEqual(zoned.touch, {"rod": (("knee", "b", 0.1, 0.0),)}) # a ram refuses the same numbers when built by hand for bad in ({"bore": -0.05}, {"bore": 0.0}, {"bore": None}, {"bore": True}, {"bore": 0.05, "gap": float("nan")}, {"bore": 0.05, "tail": float("inf")}, {"bore": 0.05, "rod": "0.02"}): @@ -1144,6 +1206,8 @@ class Refusals(unittest.TestCase): self.refused("record", "rod knee radius", **knee, touch={"rod": [("knee", "d", "0.1", 0.0)]}) self.refused("record", "rod knee depth", **knee, touch={"rod": [("knee", "d", 0.1, -0.01)]}) self.refused("record", "rod knee depth", **knee, touch={"rod": [("knee", "d", 0.1, float("nan"))]}) + self.assertIn("a body the pin joins (rocker); 'knee' turns with parent", str(self.refused("record", "rod knee pin", **knee, touch={"rod": [("knee", "d", 0.1, 0.0)]}))) + self.assertEqual(R.Mechanism("four_bar", O, X, -125, 0, **{name: yz(DEEP[name]) for name in DEEP}, **knee, touch={"rod": [("knee", "a", 0.1, 0.0)]}).touch["rod"], (("knee", "a", 0.1, 0.0),)) self.refused("record", "a", a=(0, 1)) self.refused("record", "c", c=(0, float("nan"), 1)) self.refused("record", "b", b=(0, 0, -1)) # a four-bar has no b