From 17552d0715afef458d1c1a050d8f5c1bbde40b1b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Dec 2026 23:05:56 +0100 Subject: [PATCH] brokkr_kitbash.ram: Signed reads a mesh piece by piece: inside the part is inside any piece Self's ruling on the fork cut-kit-ram-lows.h1 reported: callers feed joined meshes such as jointlib's, whose closed pieces overlap. Parity over the whole mesh reads the overlap as outside, the direction the proof between samples cannot bear. _pieces splits a mesh into the pieces its faces join; each gets its own tree and bounding box. Unit test of the split. Co-Authored-By: Claude Fable 5.2 --- surfaces/blender/brokkr_kitbash/ram.py | 35 ++++++++++++++++++++++---- surfaces/blender/tests/test_ram.py | 13 ++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/surfaces/blender/brokkr_kitbash/ram.py b/surfaces/blender/brokkr_kitbash/ram.py index 48875af..7799526 100644 --- a/surfaces/blender/brokkr_kitbash/ram.py +++ b/surfaces/blender/brokkr_kitbash/ram.py @@ -24,7 +24,7 @@ the coupler bar at c's, the ram at a's plus each of `offsets`. search(kind, hinge, ram, seats, ...) the best of a seat grid, or the tally and the nearest miss record(m, ram), from_record the JSON record a rig or runtime drives from swept(fit, body, ...) points of the swept ram's surface in a body's rest frame - Signed(shells, cap) the distance oracle over closed meshes, signed by three rays + Signed(shells, cap) the distance oracle over closed meshes, piece by piece, signed by three rays Clearance. The record names the mechanism's `parts`, each a piece of one body (part -> the body whose rest frame it turns with: a thigh's core, the bracket that carries the rocker's pivot, a link's plates, a pin), and `touch`: @@ -749,11 +749,36 @@ def swept(f, body, margin=0.0, steps=13, around=8): return _into(m, p, body, np.concatenate(pts, axis=1)).reshape(-1, 3) +def _pieces(vertices, faces): + """A mesh's pieces, [(vertices, faces)]: each the faces joined to one another through shared vertices.""" + vertices, root = np.asarray(vertices, dtype=float), list(range(len(vertices))) + + def find(i): + while root[i] != i: + root[i] = root[root[i]] + i = root[i] + return i + for face in faces: + for i in face[1:]: + root[find(i)] = find(face[0]) + groups = {} + for face in faces: + groups.setdefault(find(face[0]), []).append(face) + out = [] + for group in groups.values(): + used = sorted({i for face in group for i in face}) + at = {i: k for k, i in enumerate(used)} + out.append((vertices[used], [tuple(at[i] for i in face) for face in group])) + return out + + class Signed: """The distance oracle over closed meshes: distance(part, points) is the nearest face's distance, negative where the point is inside one of the part's shells (its depth below the nearest face), capped at `cap` outside - only. shells: part -> [(vertices (N, 3), faces)], each shell closed, in the rest frame of the part's body. - Inside is the crossing parity of `rays` of RAYS, by majority: one ray that runs through an edge or a vertex + only. shells: part -> [(vertices (N, 3), faces)], in the rest frame of the part's body. A mesh is read piece by + piece (_pieces), each piece closed: a point is inside the part where it is inside any piece, so pieces joined + into one mesh may overlap (parity over the whole mesh would read where two overlap as outside). Inside a + piece is the crossing parity of `rays` of RAYS, by majority: one ray that runs through an edge or a vertex miscounts, and a sample then reads inside a plate it stands clear of. An unknown part raises. Needs Blender's mathutils (BVHTree).""" @@ -762,8 +787,8 @@ class Signed: from mathutils.bvhtree import BVHTree self.cap, self.asked, self.vector = cap, 0, Vector self.rays = [Vector(ray).normalized() for ray in RAYS[:rays]] - self.trees = {part: [(BVHTree.FromPolygons(np.asarray(v, dtype=float).tolist(), [tuple(face) for face in f], epsilon=0.0), - np.min(v, axis=0), np.max(v, axis=0)) for v, f in made] for part, made in shells.items()} + self.trees = {part: [(BVHTree.FromPolygons(v.tolist(), [tuple(face) for face in f], epsilon=0.0), np.min(v, axis=0), np.max(v, axis=0)) + for mesh in made for v, f in _pieces(*mesh)] for part, made in shells.items()} @staticmethod def crossings(tree, v, ray): diff --git a/surfaces/blender/tests/test_ram.py b/surfaces/blender/tests/test_ram.py index 3c5af6a..2a94872 100644 --- a/surfaces/blender/tests/test_ram.py +++ b/surfaces/blender/tests/test_ram.py @@ -1268,6 +1268,19 @@ class Refusals(unittest.TestCase): self.assertEqual((caught.exception.gate, caught.exception.pin), ("arm", "b")) self.assertIn(f"arm at {caught.exception.angle:g} deg (pin b)", str(caught.exception)) + def test_a_mesh_is_read_piece_by_piece(self): + # Signed's split: two tetrahedra in one mesh, the second's vertices listed first and its faces interleaved, + # come back as two closed pieces with their own vertices; a mesh of one piece comes back whole + tet = [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)] + a, b = np.array([(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)], float), np.array([(0.2, 0.2, 0.2), (5, 0, 0), (0, 5, 0), (0, 0, 5)], float) + faces = [face for pair in zip([tuple(i + 4 for i in f) for f in tet], tet) for face in pair] + pieces = R._pieces(np.concatenate([b, a]), faces) + self.assertEqual(len(pieces), 2) + for (verts, got), want in zip(pieces, (a, b)): + self.assertEqual((verts.tolist(), sorted(got)), (want.tolist(), tet)) + whole = R._pieces(a, tet) + self.assertEqual((len(whole), whole[0][0].tolist(), whole[0][1]), (1, a.tolist(), tet)) + def test_no_blender(self): source = (SURFACE / "brokkr_kitbash" / "ram.py").read_text(encoding="utf-8") for word in ("bpy", "np.gradient"):