| """The picture a probe clip shows is the action the sampler generated. |
| |
| Two independent things can be wrong between an action array and a video, and |
| this session found one of each kind, so both are checked separately. |
| |
| SAMPLING. The rotation set held the RIGID BODY position fixed. A pose 7-vec |
| is the OptiTrack marker cluster's; the gel sits 65.7 mm away from it, so |
| turning the quaternion swings the gel through an arc of up to 52.8 mm. The |
| clips labelled "pure rotation" translated across the screen, which is what |
| a viewer sees and calls a rendering bug. It was not: the renderer drew the |
| gel exactly where the poses put it. `draw_sensor_frame`'s own docstring |
| even says "under a pure rotation about the gel centre the dot does not |
| move at all" — it assumed a pivot the sampler never implemented. |
| |
| RENDERING. Nothing had ever checked that the drawn triad lands where the |
| calibration says the gel is. `project_gel_frame` returning a plausible |
| wrong pixel looks exactly like it returning a right one. |
| |
| So the check below reads the ORIGIN BACK OUT OF THE RENDERED PIXELS — it |
| finds the white centre dot the renderer draws — and compares it against |
| projection math written out longhand here, importing nothing from the |
| toolbox. Two implementations that agree are evidence; one implementation |
| compared against itself is a tautology. |
| |
| python scripts/test_probe_render_matches_action.py |
| """ |
| from __future__ import annotations |
|
|
| import sys |
| from pathlib import Path |
|
|
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
|
|
| import numpy as np |
|
|
| from react_toolbox.staging import staging_dir |
|
|
| RESULTS: list[tuple[bool, str, str]] = [] |
|
|
|
|
| def check(ok: bool, name: str, evidence: str) -> None: |
| RESULTS.append((bool(ok), name, evidence)) |
|
|
|
|
| def project_longhand(pose7, gel_mm, cam): |
| """Gel centre -> pixel, written from the calibration files directly.""" |
| from scipy.spatial.transform import Rotation |
| p = np.asarray(pose7, float) |
| R = Rotation.from_quat(p[3:7]).as_matrix() |
| X_world = p[:3] * 1000.0 + R @ np.asarray(gel_mm, float) |
| T = np.asarray(cam["T_mocap_to_cam"], float) |
| X = T[:3, :3] @ X_world + T[:3, 3] |
| K = cam["intrinsics"] |
| return np.array([K["fx"] * X[0] / X[2] + K["ppx"], |
| K["fy"] * X[1] / X[2] + K["ppy"]]) |
|
|
|
|
| def drawn_origin(img_before, img_after): |
| """Centroid of the white dot the renderer marks the gel centre with.""" |
| d = np.abs(img_after.astype(float) - img_before.astype(float)).sum(2) |
| white = (img_after.min(axis=2) > 230) & (d > 30) |
| if white.sum() < 3: |
| return None |
| ys, xs = np.nonzero(white) |
| return np.array([xs.mean(), ys.mean()]) |
|
|
|
|
| def project_origin_longhand(pose7, cam): |
| """The OptiTrack rigid-body origin -> pixel, longhand.""" |
| p = np.asarray(pose7, float) |
| T = np.asarray(cam["T_mocap_to_cam"], float) |
| X = T[:3, :3] @ (p[:3] * 1000.0) + T[:3, 3] |
| K = cam["intrinsics"] |
| return np.array([K["fx"] * X[0] / X[2] + K["ppx"], |
| K["fy"] * X[1] / X[2] + K["ppy"]]) |
|
|
|
|
| def _pose_projecting_to(uv, depth_mm, cam, gel_mm): |
| """A pose whose GEL CENTRE lands on pixel `uv` at `depth_mm`. |
| |
| Back-projects the pixel to a camera-frame ray, walks to `depth_mm`, maps |
| into world coordinates, then subtracts the gel offset so the RIGID origin |
| is what the 7-vec carries — the same distinction the rotation bug turned |
| on. |
| """ |
| K = cam["intrinsics"] |
| Xc = np.array([(uv[0] - K["ppx"]) / K["fx"] * depth_mm, |
| (uv[1] - K["ppy"]) / K["fy"] * depth_mm, depth_mm]) |
| T = np.asarray(cam["T_mocap_to_cam"], float) |
| X_world = T[:3, :3].T @ (Xc - T[:3, 3]) |
| return np.concatenate([(X_world - np.asarray(gel_mm, float)) / 1000.0, |
| [0.0, 0.0, 0.0, 1.0]]) |
|
|
|
|
| def main() -> int: |
| import shutil |
| import tempfile |
|
|
| from react_toolbox.calibration import load_calibration |
| from react_toolbox.synth_actions import make_rotation_set, make_translation_set |
| from react_toolbox.viz import draw_sensor_frame |
| from twm.calib_epoch import calib_dir |
|
|
| stage = staging_dir() |
| shutil.copytree(calib_dir("motherboard"), stage / "calibration") |
| cal = load_calibration(stage) |
| cam = cal["cams"]["middle"] |
| gel = cal["gel_left"] |
|
|
| |
| |
| |
| |
| |
| |
| start = _pose_projecting_to(np.array([320.0, 240.0]), 700.0, cam, gel) |
| uv0 = project_longhand(start, gel, cam) |
| assert np.linalg.norm(uv0 - [320, 240]) < 1.0, f"start lands at {uv0}" |
| blank = np.zeros((480, 640, 3), np.uint8) |
|
|
| |
| |
| errs = [] |
| for pose in (start, make_translation_set(start, seed=3)[0]["poses"][-1]): |
| img = draw_sensor_frame(blank, pose, gel, cam) |
| got = drawn_origin(blank, img) |
| want = project_longhand(pose, gel, cam) |
| if got is None: |
| errs.append("nothing drawn") |
| else: |
| errs.append(float(np.linalg.norm(got - want))) |
| ok = all(isinstance(e, float) and e < 1.5 for e in errs) |
| check(ok, "the drawn triad origin is the projected gel centre", |
| f"pixel error {['%.2f' % e if isinstance(e, float) else e for e in errs]} " |
| f"vs longhand projection (tolerance 1.5 px)") |
|
|
| |
| |
| ro = make_rotation_set(start, gel, seed=3) |
| moved = [] |
| for r in ro: |
| pts = [drawn_origin(blank, draw_sensor_frame(blank, q, gel, cam)) |
| for q in r["poses"][::10]] |
| pts = [p for p in pts if p is not None] |
| if len(pts) < 3: |
| moved.append((r["name"], float("nan"))) |
| continue |
| pts = np.asarray(pts) |
| moved.append((r["name"], float(np.max(np.linalg.norm(pts - pts[0], axis=1))))) |
| worst = max(moved, key=lambda x: (np.nan_to_num(x[1], nan=1e9))) |
| check(worst[1] < 1.5, "a rotation clip holds its origin still on screen", |
| f"origin wanders at most {worst[1]:.2f} px ({worst[0]}) over the " |
| f"whole horizon") |
|
|
| |
| |
| |
| r = ro[0] |
| a = draw_sensor_frame(blank, r["poses"][0], gel, cam) |
| b = draw_sensor_frame(blank, r["poses"][-1], gel, cam) |
| diff = int((np.abs(a.astype(int) - b.astype(int)).sum(2) > 30).sum()) |
| check(diff > 100, "and its axes visibly turn", |
| f"{diff} pixels differ between the first and last frame of " |
| f"{r['name']} ({r['amplitude_deg']:.0f} deg)") |
|
|
| |
| bad = [] |
| for t in make_translation_set(start, seed=3): |
| p0, p1 = t["poses"][0], t["poses"][-1] |
| g0 = drawn_origin(blank, draw_sensor_frame(blank, p0, gel, cam)) |
| g1 = drawn_origin(blank, draw_sensor_frame(blank, p1, gel, cam)) |
| if g0 is None or g1 is None: |
| continue |
| want = project_longhand(p1, gel, cam) - project_longhand(p0, gel, cam) |
| got = g1 - g0 |
| if np.linalg.norm(want) > 5 and np.linalg.norm(got - want) > 2.0: |
| bad.append(f"{t['name']}: {np.linalg.norm(got-want):.1f} px") |
| check(not bad, "a translation clip moves where the action says", |
| f"6/6 within 2 px of the longhand displacement" |
| if not bad else f"{bad}") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| stem = draw_sensor_frame(blank, start, gel, cam, stem=True) |
| plain = draw_sensor_frame(blank, start, gel, cam) |
| extra = np.abs(stem.astype(int) - plain.astype(int)).sum(2) > 25 |
| org = project_origin_longhand(start, cam) |
| |
| |
| |
| |
| |
| |
| gelpx = project_longhand(start, gel, cam) |
| if extra.sum(): |
| ys, xs = np.nonzero(extra) |
| pts = np.stack([xs, ys], 1).astype(float) |
| reach = float(np.min(np.linalg.norm(pts - org, axis=1))) |
| span = float(np.max(np.linalg.norm(pts - gelpx, axis=1))) |
| else: |
| reach, span = float("inf"), 0.0 |
| want = float(np.linalg.norm(org - gelpx)) |
| check(extra.sum() > 10 and reach < 1.5 and abs(span - want) < 3.0, |
| "the overlay draws the tool body back to the marker cluster", |
| f"stem adds {int(extra.sum())} px, reaches to {reach:.1f} px of the " |
| f"projected rigid origin, and spans {span:.1f} px of the {want:.1f} px gap") |
|
|
| w = max(len(x) for _, x, _ in RESULTS) |
| print() |
| for ok, name, ev in RESULTS: |
| print(f" [{'ok' if ok else 'FAIL'}] {name:<{w}} {ev}") |
| n = sum(not ok for ok, _, _ in RESULTS) |
| print(f"\nprobe render matches action: {len(RESULTS)} checks, {n} failing") |
| return 1 if n else 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|