diff --git a/surfaces/blender/brokkr_kitbash/ram.py b/surfaces/blender/brokkr_kitbash/ram.py index 7799526..f27ac23 100644 --- a/surfaces/blender/brokkr_kitbash/ram.py +++ b/surfaces/blender/brokkr_kitbash/ram.py @@ -24,7 +24,8 @@ 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, piece by piece, signed by three rays + winding(vertices, triangles, points) the generalized winding number of a triangle soup about points + Signed(shells, cap) the distance oracle over meshes, signed by the winding number 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`: @@ -79,7 +80,6 @@ SCHEMA, VERSION = "brokkr.ram", 2 ZONE = ("part", "pin", "radius", "depth") # a zone of `touch`, as the record writes it BAND = 1e-9 # the loop is refused within this of a dead centre of the rocker and coupler FINE = 64 # in a zone a segment between samples is halved down to spacing / FINE -RAYS = ((0.5377, 0.6211, 0.5701), (-0.7071, 0.2903, 0.6447), (0.1873, -0.8011, -0.5684)) # Signed's, in order class RamError(ValueError): @@ -749,72 +749,64 @@ 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 +def winding(vertices, triangles, points): + """The generalized winding number of a triangle soup about each of `points` (Jacobson, Kavan and Sorkine-Hornung + 2013): the triangles' signed solid angles, summed, over 4 pi. A triangle counts positive from the side its normal + points away from (counterclockwise seen from outside). About a closed mesh it is the whole number of times the + surface wraps the point: 0 outside, 1 inside, 2 where two solids of one mesh overlap, whether they share vertices + or cut through one another. About an open mesh it is a fraction that falls off toward the hole: the closed + mesh's number less the missing faces' share of the sphere about the point, so across a flat hole it passes + through 0.5 on the hole's own plane. The plain sum: a point costs every triangle. The corners' offsets from the + point are taken in double precision and the angles in single (2.4 times the pace): the number is good to 1e-5 a + millimetre off a face and to 1e-2 at 1e-7 off it, and nearer than that its side of the face is not held.""" + v, t, pts = np.asarray(vertices, dtype=float).reshape(-1, 3), np.asarray(triangles, dtype=int).reshape(-1, 3), np.asarray(points, dtype=float).reshape(-1, 3) + A, B, C = (np.ascontiguousarray(v[t[:, k]].T) for k in range(3)) # each (3, F): a corner of every triangle + out, step = np.zeros(len(pts)), max(1, 32768 // max(len(t), 1)) # a block of pairs that stays in cache: 3.1 times the pace of 400,000 + for k in range(0, len(pts), step): + p = pts[k:k + step].T[:, :, None] + (ax, ay, az), (bx, by, bz), (cx, cy, cz) = ((corner[:, None, :] - p).astype(np.float32) for corner in (A, B, C)) + la, lb, lc = np.sqrt(ax * ax + ay * ay + az * az), np.sqrt(bx * bx + by * by + bz * bz), np.sqrt(cx * cx + cy * cy + cz * cz) + volume = ax * (by * cz - bz * cy) + ay * (bz * cx - bx * cz) + az * (bx * cy - by * cx) + out[k:k + step] = np.arctan2(volume, la * lb * lc + (ax * bx + ay * by + az * bz) * lc + (bx * cx + by * cy + bz * cz) * la + (cx * ax + cy * ay + cz * az) * lb).sum(axis=1, dtype=float) + return out / (2.0 * math.pi) 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)], 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).""" + """The distance oracle over meshes: distance(part, points) is the nearest face's distance, negative where the + point is inside the part (its depth below the nearest face), capped at `cap` outside only. shells: part -> + [(vertices (N, 3), faces)], in the rest frame of the part's body, faces wound counterclockwise seen from + outside. Inside is where the winding number of all the part's faces (winding; a face of more than three corners + as a fan) is 0.5 or more: solids that overlap, in one mesh or several, welded or not, read inside wherever any + is. A mesh wound inside out reads outside everywhere. An open mesh is read as its faces wind: no point is + refused, and a point inside by a flat hole reads inside up to the hole's plane, where the sign turns with no + face there, so the reading steps by twice the distance to the hole's rim. The depth is to the nearest face of + any of the part's meshes, one buried in another included. An unknown part raises. Needs Blender's mathutils + (BVHTree) for the nearest face.""" - def __init__(self, shells, cap=None, rays=3): + def __init__(self, shells, cap=None): from mathutils import Vector 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(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): - 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) + self.cap, self.asked, self.vector, self.parts = cap, 0, Vector, {} + for part, made in shells.items(): + verts, faces, at = [], [], 0 + for v, f in made: + v = np.asarray(v, dtype=float).reshape(-1, 3) + verts.append(v) + faces += [tuple(at + int(i) for i in face) for face in f] + at += len(v) + verts = np.concatenate(verts) + fan = np.array([(face[0], face[k], face[k + 1]) for face in faces for k in range(1, len(face) - 1)], dtype=int) + self.parts[part] = (BVHTree.FromPolygons(verts.tolist(), faces, epsilon=0.0), verts, fan) def __call__(self, part, pts): - pts = np.asarray(pts, dtype=float) + pts = np.asarray(pts, dtype=float).reshape(-1, 3) self.asked += len(pts) + tree, verts, fan = self.parts[part] 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 = self.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] + for i in np.flatnonzero(winding(verts, fan, pts) >= 0.5): + out[i] = -tree.find_nearest(self.vector(pts[i]))[3] + for i in np.flatnonzero(out >= 0): + hit = tree.find_nearest(self.vector(pts[i]), self.cap) if self.cap else tree.find_nearest(self.vector(pts[i])) + if hit[0] is not None: + out[i] = hit[3] return out diff --git a/surfaces/blender/docs/kitbash.md b/surfaces/blender/docs/kitbash.md index 5bb0ad6..8eb7ab9 100644 --- a/surfaces/blender/docs/kitbash.md +++ b/surfaces/blender/docs/kitbash.md @@ -1030,17 +1030,20 @@ Bore 0.042, range 0..-125. The kinematic rows are tests/test_ram.py's. The clear test generates (thigh with its front plate, shin with caps and armour, the bracket that carries R, the horn that carries `c`, twin link plates stacked 5 mm apart at x 0.31..0.345 and 0.35..0.385, pins through the knee's width, collars at the eye and a base clevis whose foot runs 0.01 into the thigh; 12 parts) and the package's signed BVH -oracle, `ram.Signed(shells, cap)`: the nearest face for the depth, the crossing parity of three rays, by majority, -for the side. One ray that runs through an edge or a vertex miscounts and signs a sample inside a plate it stands -clear of. `tests/fixtures/ram_rays.json` holds the ray count: the rocker plates as the joint library builds them -and 14 points beside their bars, each on the first ray short of an edge or a vertex. One ray signs all 14 -inside; three sign none. Three rays are not a proof: of 146 such points one ray missigned, two rays missigned 16. -Two rays, or a second and third along x and y, read those 14 as three do: the fixture holds the count above one. -A mesh is read piece by piece: `Signed` splits it into the pieces its faces join, each closed, and a point is -inside the part where it is inside any piece. The joint library's plates are one mesh of four bars, a side's two -overlapping about the pivot; parity over the whole mesh reads the overlap as outside (1,906 of 38,448 grid -points inside the bars, the direction the proof between samples cannot bear), and piece by piece none of them -reads outside, which the live check holds. +oracle, `ram.Signed(shells, cap)`: the nearest face for the depth, the generalized winding number for the side +(Jacobson, Kavan and Sorkine-Hornung 2013). `ram.winding` sums the signed solid angle of every triangle of the part +about the point, over 4 pi: about a closed mesh the whole number of times the surface wraps the point, 0 outside, 1 +inside, 2 where two solids overlap. A point is inside the part at 0.5 or more. The joint library's plates are one +mesh of four bars, a side's two overlapping about the pivot (`tests/fixtures/ram_plates.json`, with 29 points +inside them, 13 inside two bars): each reads inside, as does the overlap of two boxes welded at a corner and a +staircase prism a line crosses six times. Faces are wound counterclockwise seen from outside; a mesh wound inside +out reads outside everywhere. An open mesh is answered, not refused: the number is the closed mesh's less the +missing faces' share of the sphere about the point, so a cube with a face open reads inside up to the hole's plane +(0.5009 at 0.001 under the centre of a unit face) and the sign turns on that plane with no face there. The gate's +proof between samples assumes a distance changes by no more than the way between two points, which holds across a +face and not across a hole: a part with a hole the axis may reach is closed before it is given. The depth is to the +nearest face of any of the part's meshes, one buried inside another included, so inside two overlapping solids a +point may read shallower than its depth below their union. | drive | reading | | --- | --- | @@ -1055,8 +1058,10 @@ Cost on Starfire, Python 3.13, 100 repetitions paired and interleaved: one candi 49 steps took 0.79 ms median on the bell crank; its clearance against the analytic parts (50 pairs) took 42 ms (5th to 95th percentile 38 to 57) over 97,875 points, of which 23,520 are the samples and the rest the halving in zones (the own plates, stated with no slack, to the floor); the samples alone took 11 ms. A search without a world ran 1.6 to 1.8 ms a candidate over 3,456, so `limit` -(20,000) is 30 to 40 s. On the meshes the bell crank's 60 pairs at 49 steps took 1.95 to 2.12 s capped (two runs), in a -Python loop of BVH queries with three rays a sample, and the 13,824-candidate search with its 22 worlds 43 s (one run). +(20,000) is 30 to 40 s. On the meshes the bell crank's 60 pairs at 49 steps took 0.80 to 1.00 s capped (two runs), the +winding number as the plain sum over a part's 124 to 408 triangles in numpy (single precision over double offsets, +blocks of 32,768 pairs) and the depth a Python loop of BVH queries, and the 13,824-candidate search with its 22 +worlds 34.5 and 38.5 s (two runs). With the angles in double precision the search took 47.7 to 47.9 s (three runs). ## The actions diff --git a/surfaces/blender/tests/fixtures/ram_plates.json b/surfaces/blender/tests/fixtures/ram_plates.json new file mode 100644 index 0000000..be72f30 --- /dev/null +++ b/surfaces/blender/tests/fixtures/ram_plates.json @@ -0,0 +1 @@ +{"about":"The rocker's twin plates of the R-3 bell crank as Imagination's jointlib builds them: one mesh, triangulated, at rest, of four closed bars, a side's two overlapping about the pivot and sharing their faces' planes. `inside`: 29 points 0.001 or more inside a bar, 13 of them where two bars overlap. The winding number about each is the count of bars it is inside, 1 or 2: ram.Signed reads every one inside. Crossing parity reads the 13 outside.","verts":[[-0.349999994,0.740011215,0.389170021],[-0.349999994,0.752862871,0.394919485],[-0.349999994,0.763308823,0.404358923],[-0.349999994,0.770326555,0.416564375],[-0.349999994,0.773229182,0.430341005],[-0.349999994,0.77173245,0.444340348],[-0.349999994,0.765982985,0.457191974],[-0.349999994,0.756543517,0.467637926],[-0.349999994,0.744338095,0.474655658],[-0.349999994,0.730561495,0.477558255],[-0.349999994,0.716562152,0.476061553],[-0.349999994,0.305388123,0.365099549],[-0.349999994,0.292536497,0.359350085],[-0.349999994,0.282090545,0.349910617],[-0.349999994,0.275072783,0.337705195],[-0.349999994,0.272170216,0.323928565],[-0.349999994,0.273666918,0.309929222],[-0.349999994,0.279416382,0.297077596],[-0.349999994,0.288855821,0.286631644],[-0.349999994,0.301061243,0.279613912],[-0.349999994,0.314837903,0.276711315],[-0.349999994,0.328837216,0.278208017],[-0.38499999,0.740011215,0.389170021],[-0.38499999,0.752862871,0.394919485],[-0.38499999,0.763308823,0.404358923],[-0.38499999,0.770326555,0.416564375],[-0.38499999,0.773229182,0.430341005],[-0.38499999,0.77173245,0.444340348],[-0.38499999,0.765982985,0.457191974],[-0.38499999,0.756543517,0.467637926],[-0.38499999,0.744338095,0.474655658],[-0.38499999,0.730561495,0.477558255],[-0.38499999,0.716562152,0.476061553],[-0.38499999,0.305388123,0.365099549],[-0.38499999,0.292536497,0.359350085],[-0.38499999,0.282090545,0.349910617],[-0.38499999,0.275072783,0.337705195],[-0.38499999,0.272170216,0.323928565],[-0.38499999,0.273666918,0.309929222],[-0.38499999,0.279416382,0.297077596],[-0.38499999,0.288855821,0.286631644],[-0.38499999,0.301061243,0.279613912],[-0.38499999,0.314837903,0.276711315],[-0.38499999,0.328837216,0.278208017],[-0.349999994,0.514849424,0.463428885],[-0.349999994,0.522187293,0.473566681],[-0.349999994,0.526033282,0.485475808],[-0.349999994,0.526010931,0.497990549],[-0.349999994,0.522122383,0.509885848],[-0.349999994,0.514748275,0.519997358],[-0.349999994,0.504610479,0.527335227],[-0.349999994,0.492701381,0.531181216],[-0.349999994,0.480186641,0.531158805],[-0.349999994,0.468291312,0.527270257],[-0.349999994,0.458179832,0.519896209],[-0.349999994,0.288777888,0.349887431],[-0.349999994,0.28144002,0.339749664],[-0.349999994,0.27759403,0.327840507],[-0.349999994,0.277616382,0.315325767],[-0.349999994,0.281504929,0.303430468],[-0.349999994,0.288879007,0.293318987],[-0.349999994,0.299016804,0.285981119],[-0.349999994,0.310925931,0.282135129],[-0.349999994,0.323440671,0.282157511],[-0.349999994,0.335336,0.286046058],[-0.349999994,0.345447481,0.293420136],[-0.38499999,0.514849424,0.463428885],[-0.38499999,0.522187293,0.473566681],[-0.38499999,0.526033282,0.485475808],[-0.38499999,0.526010931,0.497990549],[-0.38499999,0.522122383,0.509885848],[-0.38499999,0.514748275,0.519997358],[-0.38499999,0.504610479,0.527335227],[-0.38499999,0.492701381,0.531181216],[-0.38499999,0.480186641,0.531158805],[-0.38499999,0.468291312,0.527270257],[-0.38499999,0.458179832,0.519896209],[-0.38499999,0.288777888,0.349887431],[-0.38499999,0.28144002,0.339749664],[-0.38499999,0.27759403,0.327840507],[-0.38499999,0.277616382,0.315325767],[-0.38499999,0.281504929,0.303430468],[-0.38499999,0.288879007,0.293318987],[-0.38499999,0.299016804,0.285981119],[-0.38499999,0.310925931,0.282135129],[-0.38499999,0.323440671,0.282157511],[-0.38499999,0.335336,0.286046058],[-0.38499999,0.345447481,0.293420136],[0.349999994,0.740011215,0.389170021],[0.349999994,0.752862871,0.394919485],[0.349999994,0.763308823,0.404358923],[0.349999994,0.770326555,0.416564375],[0.349999994,0.773229182,0.430341005],[0.349999994,0.77173245,0.444340348],[0.349999994,0.765982985,0.457191974],[0.349999994,0.756543517,0.467637926],[0.349999994,0.744338095,0.474655658],[0.349999994,0.730561495,0.477558255],[0.349999994,0.716562152,0.476061553],[0.349999994,0.305388123,0.365099549],[0.349999994,0.292536497,0.359350085],[0.349999994,0.282090545,0.349910617],[0.349999994,0.275072783,0.337705195],[0.349999994,0.272170216,0.323928565],[0.349999994,0.273666918,0.309929222],[0.349999994,0.279416382,0.297077596],[0.349999994,0.288855821,0.286631644],[0.349999994,0.301061243,0.279613912],[0.349999994,0.314837903,0.276711315],[0.349999994,0.328837216,0.278208017],[0.38499999,0.740011215,0.389170021],[0.38499999,0.752862871,0.394919485],[0.38499999,0.763308823,0.404358923],[0.38499999,0.770326555,0.416564375],[0.38499999,0.773229182,0.430341005],[0.38499999,0.77173245,0.444340348],[0.38499999,0.765982985,0.457191974],[0.38499999,0.756543517,0.467637926],[0.38499999,0.744338095,0.474655658],[0.38499999,0.730561495,0.477558255],[0.38499999,0.716562152,0.476061553],[0.38499999,0.305388123,0.365099549],[0.38499999,0.292536497,0.359350085],[0.38499999,0.282090545,0.349910617],[0.38499999,0.275072783,0.337705195],[0.38499999,0.272170216,0.323928565],[0.38499999,0.273666918,0.309929222],[0.38499999,0.279416382,0.297077596],[0.38499999,0.288855821,0.286631644],[0.38499999,0.301061243,0.279613912],[0.38499999,0.314837903,0.276711315],[0.38499999,0.328837216,0.278208017],[0.349999994,0.514849424,0.463428885],[0.349999994,0.522187293,0.473566681],[0.349999994,0.526033282,0.485475808],[0.349999994,0.526010931,0.497990549],[0.349999994,0.522122383,0.509885848],[0.349999994,0.514748275,0.519997358],[0.349999994,0.504610479,0.527335227],[0.349999994,0.492701381,0.531181216],[0.349999994,0.480186641,0.531158805],[0.349999994,0.468291312,0.527270257],[0.349999994,0.458179832,0.519896209],[0.349999994,0.288777888,0.349887431],[0.349999994,0.28144002,0.339749664],[0.349999994,0.27759403,0.327840507],[0.349999994,0.277616382,0.315325767],[0.349999994,0.281504929,0.303430468],[0.349999994,0.288879007,0.293318987],[0.349999994,0.299016804,0.285981119],[0.349999994,0.310925931,0.282135129],[0.349999994,0.323440671,0.282157511],[0.349999994,0.335336,0.286046058],[0.349999994,0.345447481,0.293420136],[0.38499999,0.514849424,0.463428885],[0.38499999,0.522187293,0.473566681],[0.38499999,0.526033282,0.485475808],[0.38499999,0.526010931,0.497990549],[0.38499999,0.522122383,0.509885848],[0.38499999,0.514748275,0.519997358],[0.38499999,0.504610479,0.527335227],[0.38499999,0.492701381,0.531181216],[0.38499999,0.480186641,0.531158805],[0.38499999,0.468291312,0.527270257],[0.38499999,0.458179832,0.519896209],[0.38499999,0.288777888,0.349887431],[0.38499999,0.28144002,0.339749664],[0.38499999,0.27759403,0.327840507],[0.38499999,0.277616382,0.315325767],[0.38499999,0.281504929,0.303430468],[0.38499999,0.288879007,0.293318987],[0.38499999,0.299016804,0.285981119],[0.38499999,0.310925931,0.282135129],[0.38499999,0.323440671,0.282157511],[0.38499999,0.335336,0.286046058],[0.38499999,0.345447481,0.293420136]],"faces":[[19,21,11],[24,22,32],[22,1,0],[23,2,1],[24,3,2],[25,4,3],[26,5,4],[27,6,5],[28,7,6],[29,8,7],[30,9,8],[31,10,9],[32,11,10],[33,12,11],[34,13,12],[35,14,13],[36,15,14],[37,16,15],[38,17,16],[39,18,17],[40,19,18],[41,20,19],[42,21,20],[43,0,21],[63,65,55],[68,66,76],[66,45,44],[67,46,45],[68,47,46],[69,48,47],[70,49,48],[71,50,49],[72,51,50],[73,52,51],[74,53,52],[75,54,53],[76,55,54],[77,56,55],[78,57,56],[79,58,57],[80,59,58],[81,60,59],[82,61,60],[83,62,61],[84,63,62],[85,64,63],[86,65,64],[87,44,65],[95,93,91],[122,124,128],[89,110,88],[90,111,89],[91,112,90],[92,113,91],[93,114,92],[94,115,93],[95,116,94],[96,117,95],[97,118,96],[98,119,97],[99,120,98],[100,121,99],[101,122,100],[102,123,101],[103,124,102],[104,125,103],[105,126,104],[106,127,105],[107,128,106],[108,129,107],[109,130,108],[88,131,109],[141,139,135],[166,168,172],[133,154,132],[134,155,133],[135,156,134],[136,157,135],[137,158,136],[138,159,137],[139,160,138],[140,161,139],[141,162,140],[142,163,141],[143,164,142],[144,165,143],[145,166,144],[146,167,145],[147,168,146],[148,169,147],[149,170,148],[150,171,149],[151,172,150],[152,173,151],[153,174,152],[132,175,153],[21,0,10],[0,1,10],[1,2,3],[3,4,5],[5,6,7],[7,8,9],[9,10,3],[10,11,21],[3,10,1],[11,12,13],[13,14,15],[15,16,19],[16,17,19],[17,18,19],[19,20,21],[3,5,7],[7,9,3],[11,13,19],[13,15,19],[22,43,33],[43,42,33],[42,41,40],[40,39,38],[38,37,40],[37,36,40],[36,35,34],[34,33,40],[33,32,22],[40,33,42],[32,31,30],[30,29,28],[28,27,26],[26,25,28],[25,24,28],[24,23,22],[36,34,40],[32,30,24],[30,28,24],[22,23,1],[23,24,2],[24,25,3],[25,26,4],[26,27,5],[27,28,6],[28,29,7],[29,30,8],[30,31,9],[31,32,10],[32,33,11],[33,34,12],[34,35,13],[35,36,14],[36,37,15],[37,38,16],[38,39,17],[39,40,18],[40,41,19],[41,42,20],[42,43,21],[43,22,0],[65,44,54],[44,45,54],[45,46,47],[47,48,49],[49,50,47],[50,51,47],[51,52,53],[53,54,47],[54,55,65],[47,54,45],[55,56,57],[57,58,59],[59,60,61],[61,62,63],[63,64,65],[51,53,47],[55,57,63],[57,59,63],[59,61,63],[66,87,77],[87,86,77],[86,85,84],[84,83,80],[83,82,80],[82,81,80],[80,79,78],[78,77,84],[77,76,66],[84,77,86],[76,75,74],[74,73,72],[72,71,68],[71,70,68],[70,69,68],[68,67,66],[80,78,84],[76,74,68],[74,72,68],[66,67,45],[67,68,46],[68,69,47],[69,70,48],[70,71,49],[71,72,50],[72,73,51],[73,74,52],[74,75,53],[75,76,54],[76,77,55],[77,78,56],[78,79,57],[79,80,58],[80,81,59],[81,82,60],[82,83,61],[83,84,62],[84,85,63],[85,86,64],[86,87,65],[87,66,44],[89,88,98],[88,109,99],[109,108,107],[107,106,105],[105,104,107],[104,103,107],[103,102,101],[101,100,99],[99,98,88],[98,97,91],[97,96,95],[95,94,93],[93,92,91],[91,90,89],[109,107,99],[103,101,107],[101,99,107],[98,91,89],[97,95,91],[130,131,121],[131,110,120],[110,111,112],[112,113,116],[113,114,116],[114,115,116],[116,117,118],[118,119,120],[120,121,131],[121,122,128],[122,123,124],[124,125,128],[125,126,128],[126,127,128],[128,129,130],[110,112,120],[116,118,112],[118,120,112],[121,128,130],[89,111,110],[90,112,111],[91,113,112],[92,114,113],[93,115,114],[94,116,115],[95,117,116],[96,118,117],[97,119,118],[98,120,119],[99,121,120],[100,122,121],[101,123,122],[102,124,123],[103,125,124],[104,126,125],[105,127,126],[106,128,127],[107,129,128],[108,130,129],[109,131,130],[88,110,131],[133,132,142],[132,153,143],[153,152,151],[151,150,149],[149,148,147],[147,146,145],[145,144,143],[143,142,132],[142,141,135],[141,140,139],[139,138,135],[138,137,135],[137,136,135],[135,134,133],[153,151,143],[151,149,147],[147,145,151],[145,143,151],[142,135,133],[174,175,165],[175,154,164],[154,155,156],[156,157,158],[158,159,156],[159,160,156],[160,161,162],[162,163,164],[164,165,175],[165,166,172],[166,167,168],[168,169,170],[170,171,168],[171,172,168],[172,173,174],[154,156,164],[160,162,156],[162,164,156],[165,172,174],[133,155,154],[134,156,155],[135,157,156],[136,158,157],[137,159,158],[138,160,159],[139,161,160],[140,162,161],[141,163,162],[142,164,163],[143,165,164],[144,166,165],[145,167,166],[146,168,167],[147,169,168],[148,170,169],[149,171,170],[150,172,171],[151,173,172],[152,174,173],[153,175,174],[132,154,175]],"inside":[[-0.374881795,0.506437858,0.465394675],[-0.366131796,0.753098523,0.427236497],[0.367678355,0.451073686,0.418925236],[0.367678355,0.479256774,0.475544262],[0.36188077,0.558458164,0.350741692],[0.35499999,0.520441353,0.474452346],[-0.365999994,0.497151121,0.512630403],[-0.376249991,0.504683891,0.465963334],[0.367499992,0.497122383,0.509885848],[0.357,0.405670216,0.383211315],[0.357,0.417670216,0.389211315],[0.357,0.426670216,0.392211315],[-0.378,0.405670216,0.386211315],[-0.378,0.420670216,0.371211315],[-0.378,0.429670216,0.380211315],[-0.357,0.279670216,0.335211315],[-0.357,0.294670216,0.356211315],[0.3675,0.276670216,0.329211315],[0.3675,0.276670216,0.335211315],[-0.3675,0.354670216,0.365211315],[-0.357,0.387670216,0.380211315],[0.378,0.351670216,0.359211315],[-0.3675,0.357670216,0.305211315],[-0.357,0.372670216,0.311211315],[0.378,0.351670216,0.299211315],[-0.3675,0.291670216,0.314211315],[0.357,0.303670216,0.317211315],[-0.3675,0.354670216,0.362211315],[-0.357,0.357670216,0.326211315]]} \ No newline at end of file diff --git a/surfaces/blender/tests/fixtures/ram_rays.json b/surfaces/blender/tests/fixtures/ram_rays.json deleted file mode 100644 index ab8fd15..0000000 --- a/surfaces/blender/tests/fixtures/ram_rays.json +++ /dev/null @@ -1 +0,0 @@ -{"about":"The rocker's twin plates of the R-3 bell crank as Imagination's jointlib builds them: one mesh, triangulated, at rest, of four closed bars, a side's two overlapping about the pivot and sharing their faces' planes. `points`: 14 points 0.002 or more outside every bar and inside a bar's bounding box, each on the oracle's first ray 0.004 to 0.02 short of a vertex or an edge. One ray reads each inside; three read none inside. `inside`: 29 points 0.001 or more inside a bar, nine aimed along a ray of RAYS, x or y at a vertex or an edge, the rest from a grid where the bars overlap. Parity over the whole mesh reads the overlap outside (1,906 of 38,448 grid points inside the bars); read bar by bar, as ram.Signed reads a mesh, none of them and none of these reads outside.","verts":[[-0.349999994,0.740011215,0.389170021],[-0.349999994,0.752862871,0.394919485],[-0.349999994,0.763308823,0.404358923],[-0.349999994,0.770326555,0.416564375],[-0.349999994,0.773229182,0.430341005],[-0.349999994,0.77173245,0.444340348],[-0.349999994,0.765982985,0.457191974],[-0.349999994,0.756543517,0.467637926],[-0.349999994,0.744338095,0.474655658],[-0.349999994,0.730561495,0.477558255],[-0.349999994,0.716562152,0.476061553],[-0.349999994,0.305388123,0.365099549],[-0.349999994,0.292536497,0.359350085],[-0.349999994,0.282090545,0.349910617],[-0.349999994,0.275072783,0.337705195],[-0.349999994,0.272170216,0.323928565],[-0.349999994,0.273666918,0.309929222],[-0.349999994,0.279416382,0.297077596],[-0.349999994,0.288855821,0.286631644],[-0.349999994,0.301061243,0.279613912],[-0.349999994,0.314837903,0.276711315],[-0.349999994,0.328837216,0.278208017],[-0.38499999,0.740011215,0.389170021],[-0.38499999,0.752862871,0.394919485],[-0.38499999,0.763308823,0.404358923],[-0.38499999,0.770326555,0.416564375],[-0.38499999,0.773229182,0.430341005],[-0.38499999,0.77173245,0.444340348],[-0.38499999,0.765982985,0.457191974],[-0.38499999,0.756543517,0.467637926],[-0.38499999,0.744338095,0.474655658],[-0.38499999,0.730561495,0.477558255],[-0.38499999,0.716562152,0.476061553],[-0.38499999,0.305388123,0.365099549],[-0.38499999,0.292536497,0.359350085],[-0.38499999,0.282090545,0.349910617],[-0.38499999,0.275072783,0.337705195],[-0.38499999,0.272170216,0.323928565],[-0.38499999,0.273666918,0.309929222],[-0.38499999,0.279416382,0.297077596],[-0.38499999,0.288855821,0.286631644],[-0.38499999,0.301061243,0.279613912],[-0.38499999,0.314837903,0.276711315],[-0.38499999,0.328837216,0.278208017],[-0.349999994,0.514849424,0.463428885],[-0.349999994,0.522187293,0.473566681],[-0.349999994,0.526033282,0.485475808],[-0.349999994,0.526010931,0.497990549],[-0.349999994,0.522122383,0.509885848],[-0.349999994,0.514748275,0.519997358],[-0.349999994,0.504610479,0.527335227],[-0.349999994,0.492701381,0.531181216],[-0.349999994,0.480186641,0.531158805],[-0.349999994,0.468291312,0.527270257],[-0.349999994,0.458179832,0.519896209],[-0.349999994,0.288777888,0.349887431],[-0.349999994,0.28144002,0.339749664],[-0.349999994,0.27759403,0.327840507],[-0.349999994,0.277616382,0.315325767],[-0.349999994,0.281504929,0.303430468],[-0.349999994,0.288879007,0.293318987],[-0.349999994,0.299016804,0.285981119],[-0.349999994,0.310925931,0.282135129],[-0.349999994,0.323440671,0.282157511],[-0.349999994,0.335336,0.286046058],[-0.349999994,0.345447481,0.293420136],[-0.38499999,0.514849424,0.463428885],[-0.38499999,0.522187293,0.473566681],[-0.38499999,0.526033282,0.485475808],[-0.38499999,0.526010931,0.497990549],[-0.38499999,0.522122383,0.509885848],[-0.38499999,0.514748275,0.519997358],[-0.38499999,0.504610479,0.527335227],[-0.38499999,0.492701381,0.531181216],[-0.38499999,0.480186641,0.531158805],[-0.38499999,0.468291312,0.527270257],[-0.38499999,0.458179832,0.519896209],[-0.38499999,0.288777888,0.349887431],[-0.38499999,0.28144002,0.339749664],[-0.38499999,0.27759403,0.327840507],[-0.38499999,0.277616382,0.315325767],[-0.38499999,0.281504929,0.303430468],[-0.38499999,0.288879007,0.293318987],[-0.38499999,0.299016804,0.285981119],[-0.38499999,0.310925931,0.282135129],[-0.38499999,0.323440671,0.282157511],[-0.38499999,0.335336,0.286046058],[-0.38499999,0.345447481,0.293420136],[0.349999994,0.740011215,0.389170021],[0.349999994,0.752862871,0.394919485],[0.349999994,0.763308823,0.404358923],[0.349999994,0.770326555,0.416564375],[0.349999994,0.773229182,0.430341005],[0.349999994,0.77173245,0.444340348],[0.349999994,0.765982985,0.457191974],[0.349999994,0.756543517,0.467637926],[0.349999994,0.744338095,0.474655658],[0.349999994,0.730561495,0.477558255],[0.349999994,0.716562152,0.476061553],[0.349999994,0.305388123,0.365099549],[0.349999994,0.292536497,0.359350085],[0.349999994,0.282090545,0.349910617],[0.349999994,0.275072783,0.337705195],[0.349999994,0.272170216,0.323928565],[0.349999994,0.273666918,0.309929222],[0.349999994,0.279416382,0.297077596],[0.349999994,0.288855821,0.286631644],[0.349999994,0.301061243,0.279613912],[0.349999994,0.314837903,0.276711315],[0.349999994,0.328837216,0.278208017],[0.38499999,0.740011215,0.389170021],[0.38499999,0.752862871,0.394919485],[0.38499999,0.763308823,0.404358923],[0.38499999,0.770326555,0.416564375],[0.38499999,0.773229182,0.430341005],[0.38499999,0.77173245,0.444340348],[0.38499999,0.765982985,0.457191974],[0.38499999,0.756543517,0.467637926],[0.38499999,0.744338095,0.474655658],[0.38499999,0.730561495,0.477558255],[0.38499999,0.716562152,0.476061553],[0.38499999,0.305388123,0.365099549],[0.38499999,0.292536497,0.359350085],[0.38499999,0.282090545,0.349910617],[0.38499999,0.275072783,0.337705195],[0.38499999,0.272170216,0.323928565],[0.38499999,0.273666918,0.309929222],[0.38499999,0.279416382,0.297077596],[0.38499999,0.288855821,0.286631644],[0.38499999,0.301061243,0.279613912],[0.38499999,0.314837903,0.276711315],[0.38499999,0.328837216,0.278208017],[0.349999994,0.514849424,0.463428885],[0.349999994,0.522187293,0.473566681],[0.349999994,0.526033282,0.485475808],[0.349999994,0.526010931,0.497990549],[0.349999994,0.522122383,0.509885848],[0.349999994,0.514748275,0.519997358],[0.349999994,0.504610479,0.527335227],[0.349999994,0.492701381,0.531181216],[0.349999994,0.480186641,0.531158805],[0.349999994,0.468291312,0.527270257],[0.349999994,0.458179832,0.519896209],[0.349999994,0.288777888,0.349887431],[0.349999994,0.28144002,0.339749664],[0.349999994,0.27759403,0.327840507],[0.349999994,0.277616382,0.315325767],[0.349999994,0.281504929,0.303430468],[0.349999994,0.288879007,0.293318987],[0.349999994,0.299016804,0.285981119],[0.349999994,0.310925931,0.282135129],[0.349999994,0.323440671,0.282157511],[0.349999994,0.335336,0.286046058],[0.349999994,0.345447481,0.293420136],[0.38499999,0.514849424,0.463428885],[0.38499999,0.522187293,0.473566681],[0.38499999,0.526033282,0.485475808],[0.38499999,0.526010931,0.497990549],[0.38499999,0.522122383,0.509885848],[0.38499999,0.514748275,0.519997358],[0.38499999,0.504610479,0.527335227],[0.38499999,0.492701381,0.531181216],[0.38499999,0.480186641,0.531158805],[0.38499999,0.468291312,0.527270257],[0.38499999,0.458179832,0.519896209],[0.38499999,0.288777888,0.349887431],[0.38499999,0.28144002,0.339749664],[0.38499999,0.27759403,0.327840507],[0.38499999,0.277616382,0.315325767],[0.38499999,0.281504929,0.303430468],[0.38499999,0.288879007,0.293318987],[0.38499999,0.299016804,0.285981119],[0.38499999,0.310925931,0.282135129],[0.38499999,0.323440671,0.282157511],[0.38499999,0.335336,0.286046058],[0.38499999,0.345447481,0.293420136]],"faces":[[19,21,11],[24,22,32],[22,1,0],[23,2,1],[24,3,2],[25,4,3],[26,5,4],[27,6,5],[28,7,6],[29,8,7],[30,9,8],[31,10,9],[32,11,10],[33,12,11],[34,13,12],[35,14,13],[36,15,14],[37,16,15],[38,17,16],[39,18,17],[40,19,18],[41,20,19],[42,21,20],[43,0,21],[63,65,55],[68,66,76],[66,45,44],[67,46,45],[68,47,46],[69,48,47],[70,49,48],[71,50,49],[72,51,50],[73,52,51],[74,53,52],[75,54,53],[76,55,54],[77,56,55],[78,57,56],[79,58,57],[80,59,58],[81,60,59],[82,61,60],[83,62,61],[84,63,62],[85,64,63],[86,65,64],[87,44,65],[95,93,91],[122,124,128],[89,110,88],[90,111,89],[91,112,90],[92,113,91],[93,114,92],[94,115,93],[95,116,94],[96,117,95],[97,118,96],[98,119,97],[99,120,98],[100,121,99],[101,122,100],[102,123,101],[103,124,102],[104,125,103],[105,126,104],[106,127,105],[107,128,106],[108,129,107],[109,130,108],[88,131,109],[141,139,135],[166,168,172],[133,154,132],[134,155,133],[135,156,134],[136,157,135],[137,158,136],[138,159,137],[139,160,138],[140,161,139],[141,162,140],[142,163,141],[143,164,142],[144,165,143],[145,166,144],[146,167,145],[147,168,146],[148,169,147],[149,170,148],[150,171,149],[151,172,150],[152,173,151],[153,174,152],[132,175,153],[21,0,10],[0,1,10],[1,2,3],[3,4,5],[5,6,7],[7,8,9],[9,10,3],[10,11,21],[3,10,1],[11,12,13],[13,14,15],[15,16,19],[16,17,19],[17,18,19],[19,20,21],[3,5,7],[7,9,3],[11,13,19],[13,15,19],[22,43,33],[43,42,33],[42,41,40],[40,39,38],[38,37,40],[37,36,40],[36,35,34],[34,33,40],[33,32,22],[40,33,42],[32,31,30],[30,29,28],[28,27,26],[26,25,28],[25,24,28],[24,23,22],[36,34,40],[32,30,24],[30,28,24],[22,23,1],[23,24,2],[24,25,3],[25,26,4],[26,27,5],[27,28,6],[28,29,7],[29,30,8],[30,31,9],[31,32,10],[32,33,11],[33,34,12],[34,35,13],[35,36,14],[36,37,15],[37,38,16],[38,39,17],[39,40,18],[40,41,19],[41,42,20],[42,43,21],[43,22,0],[65,44,54],[44,45,54],[45,46,47],[47,48,49],[49,50,47],[50,51,47],[51,52,53],[53,54,47],[54,55,65],[47,54,45],[55,56,57],[57,58,59],[59,60,61],[61,62,63],[63,64,65],[51,53,47],[55,57,63],[57,59,63],[59,61,63],[66,87,77],[87,86,77],[86,85,84],[84,83,80],[83,82,80],[82,81,80],[80,79,78],[78,77,84],[77,76,66],[84,77,86],[76,75,74],[74,73,72],[72,71,68],[71,70,68],[70,69,68],[68,67,66],[80,78,84],[76,74,68],[74,72,68],[66,67,45],[67,68,46],[68,69,47],[69,70,48],[70,71,49],[71,72,50],[72,73,51],[73,74,52],[74,75,53],[75,76,54],[76,77,55],[77,78,56],[78,79,57],[79,80,58],[80,81,59],[81,82,60],[82,83,61],[83,84,62],[84,85,63],[85,86,64],[86,87,65],[87,66,44],[89,88,98],[88,109,99],[109,108,107],[107,106,105],[105,104,107],[104,103,107],[103,102,101],[101,100,99],[99,98,88],[98,97,91],[97,96,95],[95,94,93],[93,92,91],[91,90,89],[109,107,99],[103,101,107],[101,99,107],[98,91,89],[97,95,91],[130,131,121],[131,110,120],[110,111,112],[112,113,116],[113,114,116],[114,115,116],[116,117,118],[118,119,120],[120,121,131],[121,122,128],[122,123,124],[124,125,128],[125,126,128],[126,127,128],[128,129,130],[110,112,120],[116,118,112],[118,120,112],[121,128,130],[89,111,110],[90,112,111],[91,113,112],[92,114,113],[93,115,114],[94,116,115],[95,117,116],[96,118,117],[97,119,118],[98,120,119],[99,121,120],[100,122,121],[101,123,122],[102,124,123],[103,125,124],[104,126,125],[105,127,126],[106,128,127],[107,129,128],[108,130,129],[109,131,130],[88,110,131],[133,132,142],[132,153,143],[153,152,151],[151,150,149],[149,148,147],[147,146,145],[145,144,143],[143,142,132],[142,141,135],[141,140,139],[139,138,135],[138,137,135],[137,136,135],[135,134,133],[153,151,143],[151,149,147],[147,145,151],[145,143,151],[142,135,133],[174,175,165],[175,154,164],[154,155,156],[156,157,158],[158,159,156],[159,160,156],[160,161,162],[162,163,164],[164,165,175],[165,166,172],[166,167,168],[168,169,170],[170,171,168],[171,172,168],[172,173,174],[154,156,164],[160,162,156],[162,164,156],[165,172,174],[133,155,154],[134,156,155],[135,157,156],[136,158,157],[137,159,158],[138,160,159],[139,161,160],[140,162,161],[141,163,162],[142,164,163],[143,165,164],[144,166,165],[145,167,166],[146,168,167],[147,169,168],[148,170,169],[149,171,170],[150,172,171],[151,173,172],[152,174,173],[153,175,174],[132,154,175]],"points":[[-0.356452715,0.274636974,0.343069077],[0.378547269,0.274636974,0.343069077],[0.380698176,0.529455168,0.329127992],[0.378547269,0.526970645,0.326847479],[-0.373952713,0.274636974,0.343069077],[0.380698176,0.273612617,0.339246879],[-0.354301808,0.529455168,0.329127992],[-0.356452715,0.526970645,0.326847479],[0.374245455,0.745663229,0.388236637],[0.378547269,0.738983472,0.385203213],[0.361047271,0.274636974,0.343069077],[-0.354301808,0.273612617,0.339246879],[-0.360754529,0.745663229,0.388236637],[-0.356452715,0.738983472,0.385203213]],"inside":[[-0.374881795,0.506437858,0.465394675],[-0.366131796,0.753098523,0.427236497],[0.367678355,0.451073686,0.418925236],[0.367678355,0.479256774,0.475544262],[0.36188077,0.558458164,0.350741692],[0.35499999,0.520441353,0.474452346],[-0.365999994,0.497151121,0.512630403],[-0.376249991,0.504683891,0.465963334],[0.367499992,0.497122383,0.509885848],[0.357,0.405670216,0.383211315],[0.357,0.417670216,0.389211315],[0.357,0.426670216,0.392211315],[-0.378,0.405670216,0.386211315],[-0.378,0.420670216,0.371211315],[-0.378,0.429670216,0.380211315],[-0.357,0.279670216,0.335211315],[-0.357,0.294670216,0.356211315],[0.3675,0.276670216,0.329211315],[0.3675,0.276670216,0.335211315],[-0.3675,0.354670216,0.365211315],[-0.357,0.387670216,0.380211315],[0.378,0.351670216,0.359211315],[-0.3675,0.357670216,0.305211315],[-0.357,0.372670216,0.311211315],[0.378,0.351670216,0.299211315],[-0.3675,0.291670216,0.314211315],[0.357,0.303670216,0.317211315],[-0.3675,0.354670216,0.362211315],[-0.357,0.357670216,0.326211315]]} \ No newline at end of file diff --git a/surfaces/blender/tests/live/ram_knee.py b/surfaces/blender/tests/live/ram_knee.py index e32d3d1..272fe15 100644 --- a/surfaces/blender/tests/live/ram_knee.py +++ b/surfaces/blender/tests/live/ram_knee.py @@ -9,11 +9,10 @@ Run it with private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USE without them). is an absolute path; writes .result.json and .r3_.png (the bell crank at 0, -62.5 and -125) and exits 1 when a check failed. -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 beside their bars 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. -The same mesh is four closed bars joined into one, a side's two overlapping: the oracle reads it bar by bar, and no -point of a grid inside the bars, nor of the fixture's 29 inside points, reads outside. +The oracle is ram.Signed: a point is inside a part where the winding number of the part's faces about it is 0.5 or +more. tests/fixtures/ram_plates.json holds the rocker plates as Imagination's jointlib builds them, four closed bars +joined into one mesh, a side's two overlapping, and 29 points inside them: none reads outside. Nor does a point where +two welded boxes overlap, or inside a cube with a face left open. 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. @@ -306,57 +305,45 @@ try: # plates and none is inside; where the plate is under the point the reading is the 0.0225 ys, zs = np.meshgrid(np.linspace(0.1, 0.9, 96), np.linspace(0.1, 0.7, 72)) plane = np.stack([np.full(ys.size, X_C), ys.ravel(), zs.ravel()], axis=1) - three, one = Signed(shells)("rocker.plate", plane), Signed(shells, rays=1)("rocker.plate", plane) + off_plates = Signed(shells)("rocker.plate", plane) def off(a, b): # each grid point's distance from the segment a -> b, across the axis a, b = np.array(a), np.array(b) t = np.clip(((plane[:, 1:] - a) @ (b - a)) / ((b - a) @ (b - a)), 0.0, 1.0) return np.linalg.norm(plane[:, 1:] - a - t[:, None] * (b - a), axis=1) under = (off(CRANK["rocker"], CRANK["p"]) < W_ROCKER - 0.005) | (off(CRANK["rocker"], CRANK["d"]) < W_ARM - 0.005) - RES["readings"]["FLAT"] = float(np.abs(three[under] - (SLAB_R[0] - X_C)).max()) - RES["readings"]["one ray: samples signed inside, of %d" % len(plane)] = int((one < 0).sum()) - check("three-rays: on a plane 0.0225 off the rocker plates no sample reads inside or nearer, and under a plate each reads the 0.0225", - (three >= SLAB_R[0] - X_C - FLAT).all() and under.sum() > 500 and RES["readings"]["FLAT"] < FLAT, [float(three.min()), int(under.sum()), RES["readings"]["FLAT"]]) - # against shells whose inside is known in closed form: a box and a disc, random points, each ray's edges and vertices among them + RES["readings"]["FLAT"] = float(np.abs(off_plates[under] - (SLAB_R[0] - X_C)).max()) + check("off-the-plates: on a plane 0.0225 off the rocker plates no sample reads inside or nearer, and under a plate each reads the 0.0225", + (off_plates >= SLAB_R[0] - X_C - FLAT).all() and under.sum() > 500 and RES["readings"]["FLAT"] < FLAT, [float(off_plates.min()), int(under.sum()), RES["readings"]["FLAT"]]) + # against a shell whose inside is known in closed form: a box, random points rng = np.random.default_rng(7) cloud = rng.uniform((-0.2, -0.2, 0.2), (0.2, 0.2, 1.25), (4000, 3)) core = Signed({"core": [box((0, 0, 0.725), (0.26, 0.28, 0.85))]})("core", cloud) 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())) + check("box: 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 beside a bar, in its bounding box, 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 14 inside; the oracle as made reads none. The bound on one ray's count is the geometric mean of 1 and 14 - check("ray-count: beside the jointlib rocker plates' bars, 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", - (made > 0).all() and (single < 0).sum() >= 4, [int((made < 0).sum()), int((single < 0).sum())]) - - # the other way, which the gate's proof between samples leans on: no point inside the plates reads outside. The - # fixture's mesh is four closed bars, a side's two overlapping about the pivot; a point is truly inside where it is - # 0.001 or more inside one bar (each a convex prism: inside every face's plane). A grid 0.003 apart at three depths - # of each plate, and the fixture's 29 points. A count of points: shipped 0 (half a point, - # lodur:ruling:deterministic-bound-zero-reading), parity over the whole mesh (M-whole) 1,906; the check asks 0 - hulls = [] - for verts, faces in R._pieces(*plates["plates"][0]): - tri = np.array([[verts[i] for i in face[:3]] for face in faces]) - normal = np.cross(tri[:, 1] - tri[:, 0], tri[:, 2] - tri[:, 0]) - normal /= np.linalg.norm(normal, axis=1)[:, None] - level = (normal * tri[:, 0]).sum(axis=1) - out = (normal @ verts.mean(axis=0) - level) > 0 - normal[out], level[out] = -normal[out], -level[out] - hulls.append((normal, level)) - lo, hi = (np.array(fixture["verts"]).min(axis=0), np.array(fixture["verts"]).max(axis=0)) - cloud = np.array([(s * (SLAB_R[0] + 2 * HALF * f), y, z) for s in (-1.0, 1.0) for f in (0.2, 0.5, 0.8) - for y in np.arange(lo[1] + 0.0015, hi[1], 0.003) for z in np.arange(lo[2] + 0.0015, hi[2], 0.003)]) - deep = np.max([-(cloud @ normal.T - level).max(axis=1) for normal, level in hulls], axis=0) >= 0.001 - within = np.concatenate([cloud[deep], np.array(fixture["inside"])]) - missed = int((Signed(plates)("plates", within) >= 0).sum()) - RES["readings"]["jointlib plates: bars the mesh is read as"] = len(hulls) - RES["readings"]["jointlib plates: inside points read outside, of %d" % len(within)] = missed - check("inside-reads-inside: no point inside the jointlib rocker plates, where its bars overlap or not, reads outside by the oracle as it is made", - len(hulls) == 4 and int(deep.sum()) > 30000 and missed == 0, [len(hulls), int(deep.sum()), missed]) + # what the gate's proof between samples leans on: no point inside a part reads outside. The jointlib rocker plates + # are four closed bars in one mesh, a side's two overlapping about the pivot (crossing parity read the overlap + # outside); the fixture's 29 points are 0.001 or more inside a bar, 13 of them inside two. A count of points + fixture = json.loads((SURFACE / "tests" / "fixtures" / "ram_plates.json").read_text(encoding="utf-8")) + plates = Signed({"plates": [(np.array(fixture["verts"]), fixture["faces"])]})("plates", np.array(fixture["inside"])) + # two boxes cut through one another and welded at the corner they share; and a cube with its top left open + def wound(lo, hi): + verts = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])], float) + return verts, [(0, 1, 3, 2), (4, 6, 7, 5), (0, 4, 5, 1), (2, 3, 7, 6), (0, 2, 6, 4), (1, 5, 7, 3)] + (v1, f1), (v2, f2) = wound((0, 0, 0), (1, 1, 1)), wound((0, 0, 0), (2, 0.5, 0.5)) + welded = (np.concatenate([v1, v2[1:]]), f1 + [tuple(i + 7 if i else 0 for i in face) for face in f2]) + cloud = rng.uniform(-0.2, 2.2, (6000, 3)) + truth = ((cloud >= 0) & (cloud <= 1)).all(axis=1) | ((cloud >= 0) & (cloud <= (2, 0.5, 0.5))).all(axis=1) + both = ((cloud >= 0) & (cloud <= (1, 0.5, 0.5))).all(axis=1) + got = Signed({"welded": [welded]})("welded", cloud) < 0 + inner = rng.uniform(0.001, 0.999, (4000, 3)) + opened = Signed({"open": [(v1, [face for face in f1 if face != (1, 5, 7, 3)])]})("open", inner) + RES["readings"]["jointlib plates: inside points read outside, of %d" % len(plates)] = int((plates >= 0).sum()) + RES["readings"]["welded boxes: points missigned, of %d (%d in the overlap)" % (len(cloud), int(both.sum()))] = int((got != truth).sum()) + RES["readings"]["open cube: inside points read outside, of %d" % len(inner)] = int((opened >= 0).sum()) + check("inside-reads-inside: no point inside the jointlib rocker plates' bars, in two welded boxes' overlap or in a cube with a face open reads outside, and none outside the boxes reads inside", + len(plates) == 29 and int((plates >= 0).sum()) == 0 and int(both.sum()) > 50 and int((got != truth).sum()) == 0 and int((opened >= 0).sum()) == 0, + [int((plates >= 0).sum()), int(both.sum()), int((got != truth).sum()), int((opened >= 0).sum())]) SECTION[0] = "seat" seat = np.concatenate([v for v, _ in shells["parent.seat"]]) diff --git a/surfaces/blender/tests/live/ram_knee.result.json b/surfaces/blender/tests/live/ram_knee.result.json index efb906b..34bb0b1 100644 --- a/surfaces/blender/tests/live/ram_knee.result.json +++ b/surfaces/blender/tests/live/ram_knee.result.json @@ -7,22 +7,17 @@ "detail": null }, { - "name": "three-rays: on a plane 0.0225 off the rocker plates no sample reads inside or nearer, and under a plate each reads the 0.0225", + "name": "off-the-plates: on a plane 0.0225 off the rocker plates no sample reads inside or nearer, and under a plate each reads the 0.0225", "ok": true, "detail": null }, { - "name": "three-rays: 4000 random points about a box are signed as the box signs them", + "name": "box: 4000 random points about a box are signed as the box signs them", "ok": true, "detail": null }, { - "name": "ray-count: beside the jointlib rocker plates' bars, 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": "inside-reads-inside: no point inside the jointlib rocker plates, where its bars overlap or not, reads outside by the oracle as it is made", + "name": "inside-reads-inside: no point inside the jointlib rocker plates' bars, in two welded boxes' overlap or in a cube with a face open reads outside, and none outside the boxes reads inside", "ok": true, "detail": null }, @@ -113,23 +108,21 @@ "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.946, + "bell crank: seconds, 49 steps capped": 0.8, "bell crank: zoned pairs": 20, "four bar: barrel inside the shin's clearance": -0.122, - "jointlib plates: bars the mesh is read as": 4, - "jointlib plates: inside points read outside, of 38477": 0, - "jointlib plates: points one ray signs inside, of 14": 14, - "jointlib plates: points the oracle signs inside": 0, + "jointlib plates: inside points read outside, of 29": 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, - "one ray: samples signed inside, of 6912": 0, + "open cube: inside points read outside, of 4000": 0, "search: candidates": 13824, "search: pass the gates before clearance": 78, "search: refused on their own parts": 21, - "search: seconds": 32.7, + "search: seconds": 34.5, "search: worlds built": 22, - "the base clevis's foot inside the thigh": -0.01 + "the base clevis's foot inside the thigh": -0.01, + "welded boxes: points missigned, of 6000 (91 in the overlap)": 0 }, "zones": { "barrel[0] | parent.seat": { @@ -503,9 +496,9 @@ } }, "renders": [ - "F:/aeth-wt/fable-ramlows/scratch/out/live5.r3_0.png", - "F:/aeth-wt/fable-ramlows/scratch/out/live5.r3_62p5.png", - "F:/aeth-wt/fable-ramlows/scratch/out/live5.r3_125.png" + "F:/aeth-wt/fable-ramlows/scratch/out/h3f.r3_0.png", + "F:/aeth-wt/fable-ramlows/scratch/out/h3f.r3_62p5.png", + "F:/aeth-wt/fable-ramlows/scratch/out/h3f.r3_125.png" ], "ok": true } \ No newline at end of file diff --git a/surfaces/blender/tests/test_ram.py b/surfaces/blender/tests/test_ram.py index 2a94872..2074ff8 100644 --- a/surfaces/blender/tests/test_ram.py +++ b/surfaces/blender/tests/test_ram.py @@ -83,6 +83,40 @@ def yz(p, x=0.0): return (x, float(p[0]), float(p[1])) +def block(lo, hi): + """A box as a mesh: 8 vertices and 12 triangles wound counterclockwise seen from outside.""" + lo, hi = np.asarray(lo, float), np.asarray(hi, float) + verts = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])]) + quads = ((0, 1, 3, 2), (4, 6, 7, 5), (0, 4, 5, 1), (2, 3, 7, 6), (0, 2, 6, 4), (1, 5, 7, 3)) + return verts, np.array([t for a, b, c, d in quads for t in ((a, b, c), (a, c, d))]) + + +def joined(first, second, weld): + """Two meshes as one; with `weld`, vertices of the second that stand on one of the first take its index.""" + (v1, t1), (v2, t2) = first, second + verts, index = [tuple(p) for p in v1], [] + for p in map(tuple, v2): + if weld and p in verts: + index.append(verts.index(p)) + else: + index.append(len(verts)) + verts.append(p) + return np.array(verts), np.concatenate([t1, np.array(index)[t2]]) + + +def staircase(): + """A prism of three steps along y from 0 to 1: under z = 1 for x in 0..1, z = 2 for 1..2, z = 3 for 2..3.""" + profile = ((0, 0), (3, 0), (3, 3), (2, 3), (2, 2), (1, 2), (1, 1), (0, 1)) # (x, z) + verts = np.array([(x, y, z) for y in (0.0, 1.0) for x, z in profile], float) + tris = [] + for k in range(8): + n = (k + 1) % 8 + tris += [(k, n + 8, n), (k, k + 8, n + 8)] + for a, b, c in ((0, 1, 6), (0, 6, 7), (1, 4, 5), (1, 5, 6), (1, 2, 3), (1, 3, 4)): # the cap at y = 0, and at y = 1 turned over + tris += [(a, b, c), (a + 8, c + 8, b + 8)] + return verts, np.array(tris) + + def grid(lo, hi, counts): lo, hi = np.asarray(lo, float), np.asarray(hi, float) axes = [np.linspace(lo[k], hi[k], n) if n > 1 else np.array([(lo[k] + hi[k]) / 2]) for k, n in enumerate(counts)] @@ -505,6 +539,7 @@ ZONED = 2.2e-10 # a rocker sample against its own plates: shipped 0 (hal BESIDE = 1.2e-10 # a wall 0.02 beside the coupler, read at the nearest sample: shipped 0 (half an ulp of 0.0242, 1.7e-18), B1 (the proof by the nearer end's reading less the span, which halves and reads beside the wall) 0.00423 off, separation 4.9e7 each side FLOOR_UNDER = 0.319 # how near the axis a wall held outside reads as touched, in spacing / 64: shipped under 0.451, FINE 128 under 0.2255, separation 1.41 each side FLOOR_OVER = 0.638 # shipped under 0.451, FINE 32 under 0.902, separation 1.41 each side +WHOLE = 3.8e-4 # a winding number against the count or share a hand value states: shipped 6.3e-6 at most (the plates; the angles are single precision), W1 (the denominator's a.b term without the third length) 0.0224 off on the open cube's axis, separation 60 each side SPAN_RAM = 0.848 # the ram's span with its base 0.1 from the rocker's pivot: shipped 0.5390 (about that pivot), the span about the knee's pivot 1.3347, separation 1.57 each side @@ -1268,18 +1303,53 @@ 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_overlapping_solids_wind_by_their_count(self): + # two boxes that cut through one another in one mesh, each with its own vertices, then sharing the one index + # at the corner they have in common (all a merge by distance welds): the winding number is the count of + # boxes about the point, 2 in the overlap, where crossing parity read outside + big, small = block((0, 0, 0), (1, 1, 1)), block((0, 0, 0), (2, 0.5, 0.5)) + pts = np.random.default_rng(1).uniform(-0.2, 2.2, (6000, 3)) + count = sum(((pts >= lo) & (pts <= hi)).all(axis=1).astype(int) for lo, hi in (((0, 0, 0), (1, 1, 1)), ((0, 0, 0), (2, 0.5, 0.5)))) + self.assertGreater(int((count == 2).sum()), 50) + for weld in (False, True): + verts, tris = joined(big, small, weld) + self.assertEqual(len(verts), 15 if weld else 16) + self.assertLess(float(np.abs(R.winding(verts, tris, pts) - count).max()), WHOLE, weld) + + def test_the_plates_overlapping_bars_wind_by_their_count(self): + # the jointlib rocker plates: four closed bars in one mesh, a side's two overlapping. Every inside point of + # the fixture winds 1, or 2 where two bars overlap + fixture = json.loads((SURFACE / "tests" / "fixtures" / "ram_plates.json").read_text(encoding="utf-8")) + w = R.winding(fixture["verts"], fixture["faces"], fixture["inside"]) + self.assertLess(float(np.abs(w - np.round(w)).max()), WHOLE) + self.assertEqual((int((np.round(w) == 1).sum()), int((np.round(w) == 2).sum())), (16, 13)) + + def test_a_staircase_winds_whole(self): + # a nonconvex prism of three steps, which a line crosses up to six times: 1 inside, 0 outside + verts, tris = staircase() + g = np.arange(-0.375, 3.5, 0.25) + pts = np.array([(x, y, z) for x in g for y in (-0.25, 0.125, 0.5, 0.875, 1.25) for z in g]) + truth = (pts[:, 1] > 0) & (pts[:, 1] < 1) & (pts[:, 0] > 0) & (pts[:, 0] < 3) & (pts[:, 2] > 0) & (pts[:, 2] < np.ceil(pts[:, 0])) + self.assertGreater(min(int(truth.sum()), int((~truth).sum())), 200) + self.assertLess(float(np.abs(R.winding(verts, tris, pts) - truth).max()), WHOLE) + + def test_an_open_cube_winds_by_what_its_hole_takes(self): + # a unit cube less its top face: about a point on the hole's axis the winding number is 1 less the hole's + # share of the sphere inside, and that share outside, 4 asin(a^2 / (a^2 + h^2)) of 4 pi for a square of half + # side a seen from h off its centre. So it is over 0.5 everywhere inside and under 0.5 over the hole, and + # passes through 0.5 on the hole's plane, where no face is. Less one triangle of the top it is the same + verts, tris = block((0, 0, 0), (1, 1, 1)) + top = [k for k, t in enumerate(tris) if (verts[t][:, 2] == 1.0).all()] + self.assertEqual(len(top), 2) + z = np.array((0.5, 0.9, 0.999, 1.001, 1.1, 1.5)) + share = np.arcsin(0.25 / (0.25 + (1.0 - z) ** 2)) / math.pi + w = R.winding(verts, np.delete(tris, top, axis=0), np.stack([np.full(6, 0.5), np.full(6, 0.5), z], axis=1)) + self.assertLess(float(np.abs(w - np.where(z < 1, 1.0 - share, share)).max()), WHOLE) + self.assertTrue((w[:3] > 0.5).all() and (w[3:] < 0.5).all(), w.tolist()) # 0.001 under the plane 0.5009, 0.001 over it 0.4991 + # no point inside either open cube, down to 0.001 from its faces and its hole, winds under 0.5: a count + inner = np.random.default_rng(3).uniform(0.001, 0.999, (4000, 3)) + for gone in (top, top[:1]): + self.assertEqual(int((R.winding(verts, np.delete(tris, gone, axis=0), inner) < 0.5).sum()), 0) def test_no_blender(self): source = (SURFACE / "brokkr_kitbash" / "ram.py").read_text(encoding="utf-8")