Files
kdm-re/tests/golden/gen_fixtures.py
T

211 lines
7.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Derive golden-I/O fixtures for the T9 replay harness from data that ALREADY
EXISTS in the repository. No Windows capture, no network, no new recording.
Provenance of every tuple this emits (all committed, all reproducible on Linux):
1. NUI depth->skeleton projection
Source math: core kinect reimplementation of Kinect's
NuiTransformDepthImageToSkeleton
(kinect/core/mapping/mapping.cpp depth_image_to_skeleton,
kDepthProjectionScale = 3.501e-3 =
NUI_CAMERA_DEPTH_NOMINAL_INVERSE_FOCAL_LENGTH_IN_PIXELS).
Source inputs: the exact depth-image points the shipping round-trip test
exercises (kinect/tests/test_mapping.cpp
ForwardInverseRoundTripStaysBelowOneMillimeter). Expected world positions
are computed here with the documented constant and are additionally
self-checked by the inverse projection (round-trip back to the original
integer pixel), so a corrupted constant cannot silently pass.
2. Skeleton selection / validity gate (game-internals.md section 2, the gate
that cracked in-game skeleton rendering; decompiled from gamekdm
FUN_10018570 / FUN_100e4140 / FUN_101ab6d0, recorded live by
sensor.cpp DumpGameDisplayState -> the game-skel2/3/4 shim log lines).
Recorded constants (DAT_103d3230 validity box, m14, tolerance):
A=(0,1,0), B=(1.22,1,0.675) -> box x[-1.22,1.22] y[0,2] z[-0.675,0.675]
m14 = 2.5 ; accept iff squared distance to box <= 0.05^2
Published floor-clip-plane rule (sensor.cpp BuildSkeletonFrame):
vFloorClipPlane.w = 1 - hip.y -> P1 = (-hip.x, 1.0, m14 - hip.z)
Hips fed to the gate come from subsystem 1 (the projected HIP_CENTER) and
from the documented auto-centering guarantee.
3. Recorded pose dump: kinect/tracker-native/testdata/ref.txt is the committed
output of the shipping PoseEstimator run on the committed sample_rgb.bin
portrait. We lift its recorded presence and hip landmarks (MediaPipe 23/24)
and freeze the hip-center midpoint the tracker computes
(two_person_probe.cpp Selected(): hip = (lm23.x + lm24.x) * 0.5).
"""
import json
import os
import pathlib
HERE = pathlib.Path(__file__).resolve().parent
FIX = HERE / "fixtures"
FIX.mkdir(exist_ok=True)
REF_CANDIDATES = [
pathlib.Path("/home/athapa/kinect/tracker-native/testdata/ref.txt"),
HERE / "fixtures" / "ref.txt",
]
SCALE = 3.501e-3
DW, DH = 640, 480
def clamp(v, lo, hi):
return lo if v < lo else hi if v > hi else v
def depth_to_skel(px, py, packed, w=DW, h=DH):
z = (packed >> 3) / 1000.0
x = (px - w / 2.0) * (320.0 / w) * SCALE * z
y = -(py - h / 2.0) * (240.0 / h) * SCALE * z
x = clamp(x, -2.2, 2.2)
y = clamp(y, -1.6, 1.6)
z = clamp(z, 0.0, 4.0)
return (x, y, z)
def skel_to_depth(x, y, z, w=DW, h=DH):
x = clamp(x, -2.2, 2.2)
y = clamp(y, -1.6, 1.6)
z = clamp(z, 0.0, 4.0)
sz = max(z, 0.001)
dx = w / 2.0 + x / (SCALE * sz) * (w / 320.0)
dy = h / 2.0 - y / (SCALE * sz) * (h / 240.0)
mm = int(round(z * 1000.0))
mm = clamp(mm, 0, 4000)
ix = clamp(int(round(dx)), 0, w)
iy = clamp(int(round(dy)), 0, h)
return (ix, iy, mm << 3)
A = (0.0, 1.0, 0.0)
B = (1.22, 1.0, 0.675)
M14 = 2.5
TOL = 0.05
def gate(hx, hy, hz):
floor_w = 1.0 - hy
p1 = (-hx, hy + floor_w, M14 - hz)
lo = (A[0] - B[0], A[1] - B[1], A[2] - B[2])
hi = (A[0] + B[0], A[1] + B[1], A[2] + B[2])
cl = tuple(clamp(p1[i], lo[i], hi[i]) for i in range(3))
d2 = sum((p1[i] - cl[i]) ** 2 for i in range(3))
return p1, d2, bool(d2 <= TOL * TOL)
def read_ref():
for p in REF_CANDIDATES:
if p.exists():
return p
raise SystemExit("ref.txt not found in any known location: " + str(REF_CANDIDATES))
def parse_ref(path):
presence = None
lm = {}
for line in path.read_text().splitlines():
parts = line.split()
if not parts:
continue
if parts[0] == "presence":
presence = float(parts[1])
elif parts[0] == "lm":
idx = int(parts[1])
lm[idx] = (float(parts[2]), float(parts[3]), float(parts[4]))
return presence, lm
def main():
depth_points = [
(0, 0, 800 << 3),
(137, 91, 1250 << 3),
(320, 240, 2500 << 3),
(434, 171, 2500 << 3),
(630, 468, 4000 << 3),
]
proj = []
for (px, py, pk) in depth_points:
sx, sy, sz = depth_to_skel(px, py, pk)
rt = skel_to_depth(sx, sy, sz)
assert rt == (px, py, pk), f"round-trip failed for {(px,py,pk)} -> {rt}"
proj.append({
"input": {"px": px, "py": py, "packed_depth": pk},
"expected": {"x": sx, "y": sy, "z": sz},
"roundtrip_pixel": {"px": rt[0], "py": rt[1], "packed_depth": rt[2]},
})
(FIX / "depth_projection.json").write_text(json.dumps({
"subsystem": "nui_depth_to_skeleton",
"provenance": "kinect/core/mapping/mapping.cpp depth_image_to_skeleton; "
"inputs from kinect/tests/test_mapping.cpp round-trip test",
"projection_scale": SCALE,
"depth_width": DW, "depth_height": DH,
"tolerance_abs": 1e-6,
"tuples": proj,
}, indent=2) + "\n")
hip_center_pixel = depth_to_skel(320, 240, 2500 << 3)
map_golden_hip0 = (0.14879250, -0.14879250, 2.5)
cases = [
("centered_candidate_from_center_pixel", hip_center_pixel, True),
("mapping_golden_hip_center_joint0", map_golden_hip0, True),
("auto_center_offset_hip_y_still_accepts", (0.10, 0.90, 2.5), True),
("hip_far_right_rejected", (2.00, 0.00, 2.5), False),
("hip_wrong_depth_rejected", (0.00, 0.00, 3.30), False),
("hip_edge_inside_x_boundary", (1.20, 0.00, 2.5), True),
("hip_just_outside_x_boundary", (1.30, 0.00, 2.5), False),
]
gate_tuples = []
for name, (hx, hy, hz), want in cases:
p1, d2, acc = gate(hx, hy, hz)
assert acc == want, f"gate expectation mismatch for {name}: {acc} != {want}"
gate_tuples.append({
"name": name,
"input": {"hip": [hx, hy, hz]},
"expected": {"P1": list(p1), "dist2": d2, "accept": acc},
})
(FIX / "validity_gate.json").write_text(json.dumps({
"subsystem": "skeleton_validity_gate",
"provenance": "game-internals.md section 2; gamekdm FUN_100e4140/FUN_101ab6d0; "
"box DAT_103d3230; recorded by sensor.cpp DumpGameDisplayState",
"box_A": list(A), "box_B": list(B), "m14": M14, "tolerance_dist": TOL,
"float_tolerance_abs": 1e-6,
"tuples": gate_tuples,
}, indent=2) + "\n")
ref_path = read_ref()
presence, lm = parse_ref(ref_path)
lx, ly = lm[23][0], lm[23][1]
rx, ry = lm[24][0], lm[24][1]
hip_mid_x = (lx + rx) * 0.5
hip_mid_y = (ly + ry) * 0.5
(FIX / "pose_ref.json").write_text(json.dumps({
"subsystem": "recorded_pose_hip_center",
"provenance": "kinect/tracker-native/testdata/ref.txt (committed PoseEstimator "
"dump on sample_rgb.bin); hip-center per two_person_probe.cpp",
"float_tolerance_abs": 1e-9,
"input": {
"landmark_23_left_hip": list(lm[23]),
"landmark_24_right_hip": list(lm[24]),
},
"expected": {
"presence": presence,
"hip_center_x": hip_mid_x,
"hip_center_y": hip_mid_y,
},
}, indent=2) + "\n")
local_ref = FIX / "ref.txt"
if ref_path != local_ref:
local_ref.write_text(ref_path.read_text())
print("wrote fixtures to", FIX)
for f in sorted(FIX.iterdir()):
print(" ", f.name, os.path.getsize(f), "bytes")
if __name__ == "__main__":
main()