From 27c7740c9616d7f0b9994fb61dad863420bf2314 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Dec 2026 22:34:07 +0100 Subject: [PATCH] brokkr_reference.flow_score: the hiding margin is the edge's own object's or the met object's, the smaller A ground or hull among the meshes read no longer sets the margin. Live: a box over a ground 20 and 40 times its size, a small box behind a long strip (O7), chips before a large box (O9), cameras 30 and 144 diagonals off (O5). Co-Authored-By: Claude Fable 5.2 --- surfaces/blender/brokkr_reference/__init__.py | 34 ++-- surfaces/blender/tests/live/reference_lift.py | 91 ++++++--- .../tests/live/reference_lift.result.json | 178 +++++++++++------- 3 files changed, 197 insertions(+), 106 deletions(-) diff --git a/surfaces/blender/brokkr_reference/__init__.py b/surfaces/blender/brokkr_reference/__init__.py index b6f3c8a..d596b41 100644 --- a/surfaces/blender/brokkr_reference/__init__.py +++ b/surfaces/blender/brokkr_reference/__init__.py @@ -98,22 +98,37 @@ def record(context, name, box): core.numbers(box, 4, "box") if box.strip() else None) -HIDE = 1e-3 # of the diagonal of the meshes read: a sight line stops this short of an edge, so that the edge's own faces do not hide it; a surface nearer the edge than this does not either +HIDE = 1e-3 # of an object's diagonal: a sight line stops this share of the edge's own object short of the edge, so that the edge's own faces do not hide it; a surface nearer the edge than that hides it only from further than this share of its own object + + +def _diagonal(obj, placed, dg): + """The diagonal of an object's evaluated bounding box as `placed` (a world matrix) stands it.""" + return float(np.linalg.norm(np.ptp(np.array([placed @ Vector(corner) for corner in obj.evaluated_get(dg).bound_box]), axis=0))) def built_edges(context, cam, objects, angle): """The edges a camera record sees on the build as it stands, modifiers evaluated: creases (two faces meeting at more than `angle` degrees, one of them toward the camera), contours (one face toward the camera, the other away) and open borders, each kept where two of three points along it are hidden behind nothing: a ray from the - camera toward the point meets no surface before it stops HIDE of the build's diagonal short of it, the same - share at any scale. `objects` are the meshes read, and the build whose diagonal that is; anything the scene - renders hides. Returns {kind: [[u0, v0, u1, v1], ...]} in the record's pixels.""" + camera toward the point meets no surface nearer it than the hiding margin: HIDE of the diagonal of the edge's + own object or of the object met, the smaller of the two, so the margin is the same share of the parts involved + at any scale and whatever else the scene holds (a ground plane, a hull). Past the own object's margin the ray + is cast once more, and the first surface it meets there decides. `objects` are the meshes read; anything the + scene renders hides. Returns {kind: [[u0, v0, u1, v1], ...]} in the record's pixels.""" scene, dg = context.scene, context.evaluated_depsgraph_get() eye, out, cos_a = np.array(cam["C"]), {"crease": [], "contour": [], "border": []}, math.cos(math.radians(angle)) - corners = np.array([obj.matrix_world @ Vector(corner) for obj in objects for corner in obj.evaluated_get(dg).bound_box]).reshape(-1, 3) - short = HIDE * float(np.linalg.norm(np.ptp(corners, axis=0))) if len(corners) else 0.0 + + def hidden(p, short): + d = float(np.linalg.norm(p - eye)) + toward = (p - eye) / d + if scene.ray_cast(dg, Vector(eye), Vector(toward), distance=d - short)[0]: + return True + hit, where, _, _, other, placed = scene.ray_cast(dg, Vector(eye + toward * (d - short)), Vector(toward), distance=short) + return hit and d - float(np.linalg.norm(np.array(where) - eye)) > HIDE * _diagonal(other, placed, dg) + for obj in objects: ev = obj.evaluated_get(dg) + short = HIDE * _diagonal(obj, obj.matrix_world, dg) me = ev.to_mesh() nv, ne, nl, nf = len(me.vertices), len(me.edges), len(me.loops), len(me.polygons) if not nf: @@ -142,12 +157,7 @@ def built_edges(context, cam, objects, angle): ends = ends.reshape(-1, 2) for k in np.nonzero(crease | contour | (count == 1))[0]: a, b = co[ends[k, 0]], co[ends[k, 1]] - clear = 0 - for t in (0.2, 0.5, 0.8): - p = a + t * (b - a) - d = float(np.linalg.norm(p - eye)) - clear += not scene.ray_cast(dg, Vector(eye), Vector((p - eye) / d), distance=d - short)[0] - if clear >= 2: + if sum(not hidden(a + t * (b - a), short) for t in (0.2, 0.5, 0.8)) >= 2: (u0, v0), (u1, v1) = core.project(cam, [a, b]) out["contour" if contour[k] else "crease" if crease[k] else "border"].append([float(u0), float(v0), float(u1), float(v1)]) return out diff --git a/surfaces/blender/tests/live/reference_lift.py b/surfaces/blender/tests/live/reference_lift.py index 2bac59c..2c453f1 100644 --- a/surfaces/blender/tests/live/reference_lift.py +++ b/surfaces/blender/tests/live/reference_lift.py @@ -11,8 +11,10 @@ core.crease_run against Blender's own subdivision. scales 0.01, 1 and 100, its front edge seen, and hidden by a strip standing in front of it by 0.316% of the box's diagonal (mutants O1, nothing hides; O2, a margin of 0.03 units; O3, HIDE ten times over: each reads the hidden edge as seen. O4, no margin at all, loses seen edges to their - own faces here and at the first box. O5, HIDE at 1e-6, passes every check: unclaimed, the margin's - floor is the ray's own rounding, which this fixture does not reach) + own faces here and at the first box). The margin is the objects' own: a box over a ground 20 and + 40 times its size, a small box over a ground behind a long strip (O7, a share of the largest mesh + read), two chips before a large box (O9, no second cast). Cameras 30 and 144 diagonals off + still read every edge (O5, HIDE at 1e-6, loses four contours at 144) blender --background --factory-startup --python-exit-code 1 --python surfaces/blender/tests/live/reference_lift.py -- @@ -207,38 +209,85 @@ try: receipt, _ = send("brokkr_reference.flow_score", **{"camera_name": "Truth", "lines": lines, **fields}) check(f"flow_score refuses {next(iter(fields))}: failed, saying '{said}'", receipt["status"] == "failed" and said in receipt["message"], receipt["message"]) - # --- flow_score: an edge the camera would see, hidden by a nearer surface, at three scales ------------------------ - for s in (0.01, 1.0, 100.0): + # --- flow_score: an edge the camera would see, hidden by a nearer surface; the margin is the objects' own ---------- + def stage(half, ground=0.0, far=0.0): + """A box of half-width `half` standing on z = 0, alone or over a square ground `ground` across, and a camera: + the first box's, scaled with the box, or one `far` box diagonals off with the lens grown to match. Returns + the camera, the box's front edge, and its line as the camera draws it.""" for obj in [o for o in bpy.data.objects if o.type in ("MESH", "CURVE")]: bpy.data.objects.remove(obj) - scaled = bpy.data.objects.new(f"Scaled {s:g}", bpy.data.cameras.new(f"Scaled {s:g}")) - scene.collection.objects.link(scaled) - scaled.data.lens, scaled.data.sensor_width, scaled.data.sensor_fit, scaled.data.clip_start, scaled.data.clip_end = 50.0, 36.0, "HORIZONTAL", 0.01 * s, 1000.0 * s - scaled.matrix_world = Matrix.Translation(truth.matrix_world.translation * s) @ truth.matrix_world.to_3x3().to_4x4() + name = f"Staged {half:g} {ground:g} {far:g}" + staged = bpy.data.objects.new(name, bpy.data.cameras.new(name)) + scene.collection.objects.link(staged) + staged.data.lens, staged.data.sensor_width, staged.data.sensor_fit, staged.data.clip_start, staged.data.clip_end = 50.0 * max(1.0, far / 5.0), 36.0, "HORIZONTAL", 0.01 * half, 1e9 + if far: + centre = Vector((0.0, 0.0, half)) + place = centre + Vector((-0.55, -0.7, 0.4)).normalized() * far * 2.0 * half * math.sqrt(3.0) + staged.matrix_world = Matrix.Translation(place) @ (centre - place).to_track_quat("-Z", "Y").to_matrix().to_4x4() + else: + staged.matrix_world = Matrix.Translation(truth.matrix_world.translation * half) @ truth.matrix_world.to_3x3().to_4x4() bm = bmesh.new() - bmesh.ops.create_cube(bm, size=2.0 * s, matrix=Matrix.Translation((0.0, 0.0, s))) + bmesh.ops.create_cube(bm, size=2.0 * half, matrix=Matrix.Translation((0.0, 0.0, half))) mesh = bpy.data.meshes.new("Box") bm.to_mesh(mesh) bm.free() scene.collection.objects.link(bpy.data.objects.new("Box", mesh)) + if ground: + g = ground / 2.0 + mesh_object("Ground", [(-g, -g, -4.0 * half), (g, -g, -4.0 * half), (g, g, -4.0 * half), (-g, g, -4.0 * half)], [(0, 1, 2, 3)]) bpy.context.view_layer.update() - edge = np.array([(-1.0, -1.0, 0.0), (-1.0, -1.0, 2.0)]) * s - line = f"front: {traced(scaled, edge)}" - receipt, bare = send("brokkr_reference.flow_score", camera_name=scaled.name, lines=line, objects="Box") - # a strip standing along the edge, toward the camera from it: every sight line to the edge meets it `gap` before the edge - toward = np.array(scaled.matrix_world.translation) - edge.mean(0) + edge = np.array([(-1.0, -1.0, 0.0), (-1.0, -1.0, 2.0)]) * half + return staged, edge, f"front: {traced(staged, edge)}" + + def screen(staged, name, point, gap, wide, tall): + """An upright quad `wide` by `tall` that every sight line to `point` meets `gap` before it.""" + toward = np.array(staged.matrix_world.translation) - point toward /= np.linalg.norm(toward) level = np.array([toward[0], toward[1], 0.0]) / np.hypot(toward[0], toward[1]) side = np.cross([0.0, 0.0, 1.0], level) - gap = 3.16e-3 * 2.0 * s * math.sqrt(3.0) # of the box's diagonal: the geometric mean of the shipped HIDE, 1e-3, and one ten times it (mutant O3), 3.16 from each - at = edge.mean(0) + level * gap * float(toward @ level) - mesh_object("Wall", [tuple(at + side * a * 0.2 * s + np.array([0.0, 0.0, b * 1.5 * s])) for a, b in ((-1, -1), (1, -1), (1, 1), (-1, 1))], [(0, 1, 2, 3)]) + at = point + level * gap * float(toward @ level) + mesh_object(name, [tuple(at + side * a * wide / 2.0 + np.array([0.0, 0.0, b * tall / 2.0])) for a, b in ((-1, -1), (1, -1), (1, 1), (-1, 1))], [(0, 1, 2, 3)]) bpy.context.view_layer.update() - receipt2, walled = send("brokkr_reference.flow_score", camera_name=scaled.name, lines=line, objects="Box") + + def front(staged, line, objects): + receipt, flow = send("brokkr_reference.flow_score", camera_name=staged.name, lines=line, **objects) + return (flow["lines"]["front"]["on"], flow["edges"]["crease"], flow["edges"]["contour"]) if receipt["status"] == "accepted" else receipt["message"] + + DIAGONAL = 2.0 * math.sqrt(3.0) # of a box, in its half-widths + for s in (0.01, 1.0, 100.0): + staged, edge, line = stage(s) + bare = front(staged, line, {"objects": "Box"}) + gap = 3.16e-3 * DIAGONAL * s # of the box's diagonal: the geometric mean of the shipped HIDE, 1e-3, and one ten times it (mutant O3), 3.16 from each + screen(staged, "Wall", edge.mean(0), gap, 0.4 * s, 3.0 * s) + walled = front(staged, line, {"objects": "Box"}) check(f"at scale {s:g} the box's front edge is a crease the camera sees (on 1); a strip 0.316% of the box's diagonal in front of it hides it (on 0, one crease fewer): O1 (nothing hides), " - "O2 (the ray stops 0.03 units short) and O3 (ten times HIDE) read on 1", - receipt["status"] == "accepted" and receipt2["status"] == "accepted" and bare["lines"]["front"]["on"] == 1.0 and bare["edges"]["crease"] == 3 - and walled["lines"]["front"]["on"] == 0.0 and walled["edges"]["crease"] == 2, [bare and bare["lines"], bare and bare["edges"], walled and walled["lines"], walled and walled["edges"], gap]) + "O2 (the ray stops 0.03 units short) and O3 (ten times HIDE) read on 1", bare == (1.0, 3, 6) and walled == (0.0, 2, 6), [bare, walled, gap]) + # a larger mesh in the scene and among the meshes read: the margin stays the box's + for half, ground in ((0.5, 20.0), (0.05, 4.0)): + staged, edge, line = stage(half, ground) + bare = front(staged, line, {}) + screen(staged, "Wall", edge.mean(0), 0.01 * DIAGONAL * half, 0.4 * half, 3.0 * half) + walled = front(staged, line, {}) + check(f"a box {2 * half:g} across over a ground {ground:g} across, every mesh read: its front edge is seen, and a strip 1% of the box's diagonal in front hides it " + f"(a margin of the ground's is {1e-3 * ground * math.sqrt(2.0) / (0.01 * DIAGONAL * half):.1f} times the gap)", bare == (1.0, 3, 6) and walled == (0.0, 2, 6), [bare, walled]) + # a small box's edge behind a strip far longer than the box, over a ground: the margin is the box's, not the strip's or the ground's (O7, a share of the largest mesh read, reads on 1) + staged, edge, line = stage(0.05, 4.0) + gap = math.sqrt(1e-3 * DIAGONAL * 0.05 * 1e-3 * math.hypot(0.02, 4.0)) # the geometric mean of the box's margin and the strip's, 4.8 from each + screen(staged, "Wall", edge.mean(0), gap, 0.02, 4.0) + walled = front(staged, line, {}) + check("a box 0.1 across over a ground 4 across, with a strip 4 long standing between the two margins in front of its edge: hidden, the margin is the smaller object's (O7 reads on 1)", walled == (0.0, 2, 6), [walled, gap]) + # two chips far smaller than the box, in front of two of the edge's three read points: the margin is the chips' (O9, no second cast, reads on 1) + staged, edge, line = stage(1.0) + gap = math.sqrt(1e-3 * DIAGONAL * 1e-3 * 0.05 * math.sqrt(2.0)) # the geometric mean of the box's margin and a chip's, 7 from each + for t in (0.2, 0.5): + screen(staged, f"Chip {t}", edge[0] + t * (edge[1] - edge[0]), gap, 0.05, 0.05) + chipped = front(staged, line, {"objects": "Box"}) + check("a box 2 across with two chips 0.05 across standing between the two margins in front of its edge: hidden, the margin is the chips' (O9 reads on 1)", chipped == (0.0, 2, 6), [chipped, gap]) + # far cameras: the margin's floor is the ray's rounding (O5, HIDE 1e-6, loses four contours to their own faces at 144 diagonals; at 30 it reads true here) + for far in (30.0, 144.0): + staged, edge, line = stage(1.0, far=far) + bare = front(staged, line, {"objects": "Box"}) + check(f"a camera {far:g} box diagonals off still reads the box's 3 creases and 6 contours and its front edge on 1" + (" (O5 reads 2 contours)" if far == 144.0 else ""), bare == (1.0, 3, 6), bare) except Exception: # noqa: BLE001 whatever it was, it is a failed check with a name, printed and written, not a missing result file check(f"no uncaught error after: {R['checks'][-1]['name'] if R['checks'] else 'the start'}", False, traceback.format_exc().splitlines()[-6:]) diff --git a/surfaces/blender/tests/live/reference_lift.result.json b/surfaces/blender/tests/live/reference_lift.result.json index 6e8baf8..e80de0e 100644 --- a/surfaces/blender/tests/live/reference_lift.result.json +++ b/surfaces/blender/tests/live/reference_lift.result.json @@ -220,61 +220,33 @@ "name": "at scale 0.01 the box's front edge is a crease the camera sees (on 1); a strip 0.316% of the box's diagonal in front of it hides it (on 0, one crease fewer): O1 (nothing hides), O2 (the ray stops 0.03 units short) and O3 (ten times HIDE) read on 1", "ok": true, "detail": [ - { - "front": { - "on": 1.0, - "off": 0.0, - "run": 1.0 - } - }, - { - "crease": 3, - "contour": 6, - "border": 0 - }, - { - "front": { - "on": 0.0, - "off": 5.0, - "run": 0.0 - } - }, - { - "crease": 2, - "contour": 6, - "border": 0 - }, - 0.00010946561103835305 + [ + 1.0, + 3, + 6 + ], + [ + 0.0, + 2, + 6 + ], + 0.00010946561103835304 ] }, { "name": "at scale 1 the box's front edge is a crease the camera sees (on 1); a strip 0.316% of the box's diagonal in front of it hides it (on 0, one crease fewer): O1 (nothing hides), O2 (the ray stops 0.03 units short) and O3 (ten times HIDE) read on 1", "ok": true, "detail": [ - { - "front": { - "on": 1.0, - "off": 0.0, - "run": 1.0 - } - }, - { - "crease": 3, - "contour": 6, - "border": 0 - }, - { - "front": { - "on": 0.0, - "off": 5.0, - "run": 0.0 - } - }, - { - "crease": 2, - "contour": 6, - "border": 0 - }, + [ + 1.0, + 3, + 6 + ], + [ + 0.0, + 2, + 6 + ], 0.010946561103835304 ] }, @@ -282,32 +254,92 @@ "name": "at scale 100 the box's front edge is a crease the camera sees (on 1); a strip 0.316% of the box's diagonal in front of it hides it (on 0, one crease fewer): O1 (nothing hides), O2 (the ray stops 0.03 units short) and O3 (ten times HIDE) read on 1", "ok": true, "detail": [ - { - "front": { - "on": 1.0, - "off": 0.0, - "run": 1.0 - } - }, - { - "crease": 3, - "contour": 6, - "border": 0 - }, - { - "front": { - "on": 0.0, - "off": 5.0, - "run": 0.0 - } - }, - { - "crease": 2, - "contour": 6, - "border": 0 - }, + [ + 1.0, + 3, + 6 + ], + [ + 0.0, + 2, + 6 + ], 1.0946561103835304 ] + }, + { + "name": "a box 1 across over a ground 20 across, every mesh read: its front edge is seen, and a strip 1% of the box's diagonal in front hides it (a margin of the ground's is 1.6 times the gap)", + "ok": true, + "detail": [ + [ + 1.0, + 3, + 6 + ], + [ + 0.0, + 2, + 6 + ] + ] + }, + { + "name": "a box 0.1 across over a ground 4 across, every mesh read: its front edge is seen, and a strip 1% of the box's diagonal in front hides it (a margin of the ground's is 3.3 times the gap)", + "ok": true, + "detail": [ + [ + 1.0, + 3, + 6 + ], + [ + 0.0, + 2, + 6 + ] + ] + }, + { + "name": "a box 0.1 across over a ground 4 across, with a strip 4 long standing between the two margins in front of its edge: hidden, the margin is the smaller object's (O7 reads on 1)", + "ok": true, + "detail": [ + [ + 0.0, + 2, + 6 + ], + 0.000832363492248106 + ] + }, + { + "name": "a box 2 across with two chips 0.05 across standing between the two margins in front of its edge: hidden, the margin is the chips' (O9 reads on 1)", + "ok": true, + "detail": [ + [ + 0.0, + 2, + 6 + ], + 0.0004949232003839765 + ] + }, + { + "name": "a camera 30 box diagonals off still reads the box's 3 creases and 6 contours and its front edge on 1", + "ok": true, + "detail": [ + 1.0, + 3, + 6 + ] + }, + { + "name": "a camera 144 box diagonals off still reads the box's 3 creases and 6 contours and its front edge on 1 (O5 reads 2 contours)", + "ok": true, + "detail": [ + 1.0, + 3, + 6 + ] } ], "hostErrors": [],