#!/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]) # At pitch == +/-90 the camera looks along the world up axis, which is the drawing plane's normal, # so the puppet is seen edge-on and the reference layer collapses to a line. Nothing reports it - # the projection still returns a number - so the clamp belongs in the model, not in the drag handler. # The right axis is the unit horizontal, so it does not collapse here; building it as # forward x worldUp would have made it cos(pitch), exactly zero at 90. f, r, u = basis(0, 90) check('right stays unit length at pitch 90', round(math.sqrt(sum(v * v for v in r)), 9), 1.0) check('pitch 90 puts the view along the world up axis', [round(v, 9) for v in f], [0.0, 0.0, -1.0]) check('and the drawing plane is edge-on: up projects to nothing', round(u[2], 9), 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 # near and far are distances from the target, so the slab runs from -near (toward the viewer) to # +far. The depth row maps that slab to the full clip range [-1, 1]; mapping it to [0, 1] would # throw away half the precision for nothing. dz = 2.0 / (near + far) 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[2 * 4 + 3] += near * dz - 1.0 m[15] = 1.0 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)) def window_depth(clip_z): return (clip_z + 1.0) / 2.0 NEAR, FAR = 10.0, 30.0 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, NEAR, FAR) via_matrix = apply_matrix(m, point) depth_ok = abs(window_depth(via_matrix[2]) - (direct[2] + NEAR) / (NEAR + FAR)) < 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('the near end of the slab maps to window depth 0', round(window_depth(apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, NEAR, FAR), (0, -NEAR, 0))[2]), 6), 0.0) check('the far end of the slab maps to window depth 1', round(window_depth(apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, NEAR, FAR), (0, FAR, 0))[2]), 6), 1.0) # Applying the columns instead of the rows is the transpose. It is invisible at zero angles, where the # matrix is diagonal, so the check has to run at a rotated camera. m = ortho_matrix((0, 0, 0), 37, 21, 1, 400, 400, NEAR, FAR) check('the matrix is applied by rows, not columns', round(m[1], 9) != round(m[4], 9), True)