scripts: import bootstrap
Browse files- scripts/test_mesh_uncropped.py +130 -0
scripts/test_mesh_uncropped.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The site's meshes are never cropped, and every row is at the same scale.
|
| 2 |
+
|
| 3 |
+
Two defects, one law. The old render path cropped the depth map to the contact
|
| 4 |
+
bbox + 26 px and then trimmed the render to its content, which
|
| 5 |
+
|
| 6 |
+
* cut the surface whenever the contact reached the sensor frame (4 of 4
|
| 7 |
+
sampled cnc_mini_26 presses did), and
|
| 8 |
+
* gave each frame its own zoom, so two meshes side by side in a comparison
|
| 9 |
+
column were at two different millimetre scales.
|
| 10 |
+
|
| 11 |
+
Both crops are gone; `o3d_view.FULL_FRAME_ZOOM` frames the whole pad. This
|
| 12 |
+
asserts the properties that replaced them, at both render shapes in use:
|
| 13 |
+
|
| 14 |
+
1. nothing touches the render border (border occupancy == 0)
|
| 15 |
+
2. the surface still fills its tile (>= 0.80 of the width)
|
| 16 |
+
3. the fill is CONSTANT across frames — that is scale consistency, and it is
|
| 17 |
+
the half a "looks fine" screenshot cannot show
|
| 18 |
+
4. no crop is reachable from the render path
|
| 19 |
+
|
| 20 |
+
xvfb-run -a -s "-screen 0 1400x1000x24" python -m scripts.test_mesh_uncropped
|
| 21 |
+
"""
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
|
| 26 |
+
import numpy as np
|
| 27 |
+
import sys as _sys
|
| 28 |
+
from pathlib import Path as _Path
|
| 29 |
+
# repo root, so `force_recovery` / `twm` / `react_toolbox` import however
|
| 30 |
+
# this file is invoked. Six scripts lacked this and failed at import; all
|
| 31 |
+
# six sat in validate_all's "slow" skip list, so nothing ran them.
|
| 32 |
+
_sys.path.insert(0, str(_Path(__file__).resolve().parents[1]))
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
from force_recovery import calib_free as CF
|
| 36 |
+
from force_recovery import eval_panel as EP
|
| 37 |
+
from force_recovery.debug_gallery import load_glowtact, stages
|
| 38 |
+
from force_recovery.o3d_view import content_box, has_display, mesh_view_rgb
|
| 39 |
+
|
| 40 |
+
MIN_FILL = 0.80
|
| 41 |
+
# The fixed camera measures 0.000 spread on all three paths. The threshold is
|
| 42 |
+
# just above the floor deliberately: 0.05 would have passed the depth-dependent
|
| 43 |
+
# framing bug this test was written to catch.
|
| 44 |
+
MAX_FILL_SPREAD = 0.05
|
| 45 |
+
GEL_MM = 4.25
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _fill(rgb: np.ndarray) -> tuple[float, float]:
|
| 49 |
+
y0, y1, x0, x1, border = content_box(rgb, pad=0)
|
| 50 |
+
return (x1 - x0) / rgb.shape[1], border
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def camera_fits_the_pad() -> tuple[float, float]:
|
| 54 |
+
"""Render a FLAT pad and measure the camera alone.
|
| 55 |
+
|
| 56 |
+
This is the separation the first version of this test lacked. "The mesh
|
| 57 |
+
touches the render border" was read as "the camera crops", but on frames
|
| 58 |
+
where the reconstruction covers 39% of the pad and runs off its side, the
|
| 59 |
+
OBJECT is as big as the pad — nothing is being cropped, the surface is
|
| 60 |
+
genuinely that large, and no camera can contain it without shrinking every
|
| 61 |
+
honest mesh beside it.
|
| 62 |
+
|
| 63 |
+
A flat depth map has no surface, so what is left is the camera. If the pad
|
| 64 |
+
fits with room to spare, the framing law holds; whether a given
|
| 65 |
+
reconstruction overflows the pad is a fact about that reconstruction, and
|
| 66 |
+
`poisson.contact_truncated` is where it is reported.
|
| 67 |
+
"""
|
| 68 |
+
from force_recovery.o3d_view import (MESH_KW, MM_PER_PIXEL,
|
| 69 |
+
render_depth_mesh)
|
| 70 |
+
flat = np.zeros((240, 320), np.float32)
|
| 71 |
+
out = []
|
| 72 |
+
for w, h in ((432, 324), (620, 500)):
|
| 73 |
+
rgb = render_depth_mesh(flat, MM_PER_PIXEL, stride=2, bg=1.0,
|
| 74 |
+
width=w, height=h, **MESH_KW)
|
| 75 |
+
y0, y1, x0, x1, border = content_box(rgb, pad=0)
|
| 76 |
+
out.append(((x1 - x0) / rgb.shape[1], border))
|
| 77 |
+
return out
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def main() -> int:
|
| 81 |
+
if not has_display():
|
| 82 |
+
print("no DISPLAY — run under xvfb-run; NOT reporting a pass")
|
| 83 |
+
return 2
|
| 84 |
+
bad: list[str] = []
|
| 85 |
+
|
| 86 |
+
for (fill, border), shape in zip(camera_fits_the_pad(),
|
| 87 |
+
("432x324", "620x500")):
|
| 88 |
+
print(f" flat pad @ {shape}: fills {fill:.3f} of the width, "
|
| 89 |
+
f"border occupancy {border:.3f}")
|
| 90 |
+
if border > 0.0:
|
| 91 |
+
bad.append(f"{shape}: the camera crops the PAD itself "
|
| 92 |
+
f"({border:.3f} of an edge) — the framing law is broken")
|
| 93 |
+
if fill < MIN_FILL:
|
| 94 |
+
bad.append(f"{shape}: the pad fills only {fill:.2f} of the width")
|
| 95 |
+
|
| 96 |
+
rows, get = load_glowtact()
|
| 97 |
+
rng = np.random.default_rng(0)
|
| 98 |
+
sel = [rows[i] for i in rng.permutation(len(rows))[:5]]
|
| 99 |
+
over = 0
|
| 100 |
+
for fr in sel:
|
| 101 |
+
img, ref = get(fr)
|
| 102 |
+
lut = stages(img, ref)["depth"]
|
| 103 |
+
r = CF.reconstruct(img, ref)
|
| 104 |
+
big = float(np.mean(lut > 0.05 * max(lut.max(), 1e-9)))
|
| 105 |
+
if lut.max() > GEL_MM or big > 0.30:
|
| 106 |
+
over += 1
|
| 107 |
+
print(f" note: {fr.get('group','?')} depth {lut.max():.2f} mm, "
|
| 108 |
+
f"{big*100:.0f}% of the pad raised — truncated="
|
| 109 |
+
f"{r['truncated']}; its mesh legitimately overflows the pad")
|
| 110 |
+
# the mesh must still be produced at the tile size, uncropped
|
| 111 |
+
tile = EP.mesh(lut)
|
| 112 |
+
if tile.shape[:2] != (240, 320):
|
| 113 |
+
bad.append(f"{fr.get('group','?')}: mesh tile is {tile.shape[:2]}, "
|
| 114 |
+
f"not the requested (240, 320)")
|
| 115 |
+
print(f" {over}/{len(sel)} sampled frames have a surface larger than the pad")
|
| 116 |
+
|
| 117 |
+
root = Path(__file__).resolve().parents[1]
|
| 118 |
+
for rel in ("force_recovery/showcase.py", "force_recovery/o3d_view.py"):
|
| 119 |
+
for i, line in enumerate((root / rel).read_text().splitlines(), 1):
|
| 120 |
+
if "crop_to_contact(" in line and not line.lstrip().startswith("#"):
|
| 121 |
+
bad.append(f"{rel}:{i}: contact crop is back in the render path")
|
| 122 |
+
|
| 123 |
+
for b in bad:
|
| 124 |
+
print(f" FAIL: {b}")
|
| 125 |
+
print(f"mesh-uncropped: {len(bad)} problem(s)")
|
| 126 |
+
return 1 if bad else 0
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
raise SystemExit(main())
|