brokkr_kitbash.ram: Signed signs by the generalized winding number; the piece split and the rays are cut
Inside is where the winding number of the part's triangles about the point is 0.5 or more (Jacobson, Kavan and Sorkine-Hornung 2013), the plain sum in numpy. Overlapping solids, welded or not, wind by their count; a nonconvex prism winds whole; an open cube reads inside up to its hole's plane. _pieces, RAYS, the rays parameter and the ray fixture's points are gone; ram_rays.json is ram_plates.json and keeps the overlapping bars. Live ram_knee: 20 checks, search 34.5 s (the parity oracle beside it 34.5 s). Closes cut-kit-ram-lows.s1 signed-trusts-closed-and-unwelded and rays-three-pinned-by-no-check. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
c4ac06c2dd
commit
404849a30e
7 changed files with 200 additions and 153 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
1
surfaces/blender/tests/fixtures/ram_plates.json
vendored
Normal file
1
surfaces/blender/tests/fixtures/ram_plates.json
vendored
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -9,11 +9,10 @@ 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 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"]])
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue