#!/usr/bin/env python3 """Proof harness for the three pure parts of the reference-layer design. Mirrors the Kotlin sketch in sketch/. Nothing here touches a GPU, so every assertion is a fact the sketch can state as a test expectation. 1. GLB container + accessor resolution 2. Forward kinematics of the mannequin joint chain 3. Orbit orthographic camera (world -> NDC), and its agreement with ViewportCamera at yaw = pitch = 0 Convention used throughout, matching Umamo's world space: X right, Y depth (into the screen), Z up. Row-major 4x4 matrices. Euler applied as R = Rz * Ry * Rx. """ import json import math import struct FAILURES = [] def check(name, got, want): ok = got == want if not ok: FAILURES.append((name, got, want)) print(f"[{'ok ' if ok else 'FAIL'}] {name}: {got}") def approx(name, got, want, eps=1e-6): ok = all(abs(a - b) < eps for a, b in zip(got, want)) if not ok: FAILURES.append((name, got, want)) print(f"[{'ok ' if ok else 'FAIL'}] {name}: {[round(v, 6) for v in got]}") # ---------------------------------------------------------------- 1. GLB GLB_MAGIC = 0x46546C67 CHUNK_JSON = 0x4E4F4A42 CHUNK_BIN = 0x004E4942 COMPONENT_BYTES = {5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4} COMPONENT_ELEMENTS = {5120: 'b', 5121: 'B', 5122: 'h', 5123: 'H', 5125: 'I', 5126: 'f'} TYPE_COMPONENTS = {'SCALAR': 1, 'VEC2': 2, 'VEC3': 3, 'VEC4': 4} def build_glb(json_obj, bin_chunk): body = json.dumps(json_obj, separators=(',', ':')).encode('utf-8') body += b' ' * ((4 - len(body) % 4) % 4) padded_bin = bin_chunk + b'\x00' * ((4 - len(bin_chunk) % 4) % 4) out = struct.pack(' one pixel right', px, (201.0, 200.0)) approx('and NDC matches ViewportCamera.worldToNdc', ndc[:2], (2 / 400, 0.0)) ndc, px = project((0, 0, 1), (0, 0, 0), 0, 0, 1, 400, 400) approx('one world unit up -> one pixel UP (screen y measures down)', px, (200.0, 199.0)) ndc, px = project((0, 1, 0), (0, 0, 0), 90, 0, 1, 400, 400) approx('yaw 90: the depth axis becomes screen right', px, (201.0, 200.0)) ndc, px = project((0, 0, 1), (0, 0, 0), 0, 90, 1, 400, 400) approx('pitch 90 (looking straight down): the up axis projects to nothing', px, (200.0, 200.0)) ndc, _ = project((0, 5, 0), (0, 0, 0), 0, 0, 1, 400, 400) check('depth increases away from the camera', ndc[2], 5.0) ndc, _ = project((0, -5, 0), (0, 0, 0), 0, 0, 1, 400, 400) check('a point in front of the camera has negative depth', ndc[2], -5.0) f, r, u = basis(37, 21) approx('basis stays orthonormal at yaw 37 pitch 21', [round(f[0] * r[0] + f[1] * r[1] + f[2] * r[2], 9), round(f[0] * u[0] + f[1] * u[1] + f[2] * u[2], 9), round(r[0] * u[0] + r[1] * u[1] + r[2] * u[2], 9)], [0.0, 0.0, 0.0]) # Real degeneracy: at pitch == +/-90 the camera looks along the world up axis, so the # screen-right axis is undefined. The camera must clamp pitch to the open interval and # the UI must not allow a drag that reaches it. f, r, u = basis(0, 90) check('pitch 90 collapses the right axis (must be clamped)', round(math.sqrt(sum(v * v for v in r)), 9), 0.0) f, r, u = basis(0, 89.9) approx('pitch 89.9 still gives a usable right axis', [round(v, 6) for v in r], (0.001745, 0.0, 0.0)) print() if FAILURES: print(f'{len(FAILURES)} FAILURES') for name, got, want in FAILURES: print(f' {name}: got {got} want {want}') raise SystemExit(1) print('all checks pass') # ---------------------------------------------------------------- 4. Matrix form def ortho_matrix(target, yaw, pitch, zoom, w, h, near, far): forward, right, up = basis(yaw, pitch) sx, sy = 2 * zoom / w, 2 * zoom / h dz = 1.0 / (far - near) def dot(a, b): return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] m = [0.0] * 16 for r, axis, scale in ((0, right, sx), (1, up, sy), (2, forward, dz)): for c in range(3): m[r * 4 + c] = axis[c] * scale m[r * 4 + 3] = -dot(axis, target) * scale m[15] = 1.0 m[2 * 4 + 3] -= near * dz return m def apply_matrix(m, p): return tuple(m[r * 4 + 0] * p[0] + m[r * 4 + 1] * p[1] + m[r * 4 + 2] * p[2] + m[r * 4 + 3] for r in range(3)) for point, yaw, pitch in (((1, 0, 0), 0, 0), ((0, 0, 1), 0, 0), ((0, 1, 0), 90, 0), ((0.3, 0.4, 0.5), 37, 21)): direct, _ = project(point, (0, 0, 0), yaw, pitch, 1, 400, 400) m = ortho_matrix((0, 0, 0), yaw, pitch, 1, 400, 400, -10.0, 10.0) via_matrix = apply_matrix(m, point) depth_ok = abs(via_matrix[2] - (direct[2] + 10) / 20) < 1e-9 xy_ok = all(abs(a - b) < 1e-9 for a, b in zip(via_matrix[:2], direct[:2])) check(f'matrix form agrees with the direct form at yaw {yaw} pitch {pitch}', (round(xy_ok), round(depth_ok)), (1, 1)) check('depth maps near to 0 and far to 1', [round(v, 6) for v in apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, -10.0, 10.0), (0, -10, 0))], [0.0, 0.0, 0.0])