code wiki / (root) / nx_twinfit_lib.nx

nx_twinfit_lib.nx source

↩ module page · 870 lines · 44515 B

1// nx_twinfit_lib.nx -- aesthetictwin AT34/AT35 (2026-09-17): THE TWIN LOOP'S SUBSTRATE. One library that (1) builds a face from 2// explicit morph percents over the inside-out nx_faceanat face, (2) renders it through the sovereign SDF renderer, (3) hands the 3// framebuffer to the photo ruler IN-PROCESS (no PNG round trip) so a render is judged exactly as a photograph is, (4) renders a 4// persongen identity to a PNG for the file door, (5) reads a target file of card axes and decides REACHABLE against an envelope. 5// Consumers: nx_twinfit (envelope / axes), nx_twinface (render to PNG), nx_twinfit_gate (planted controls). 6// The seven ranges are persongen_build's declared identity ranges (nx_persongen.nx pg_range calls), copied here as DATA with 7// that provenance; a morph outside its range is not a face the generator would ever emit. 8// license_tier: ORIGINAL No hw writes (Rule 26). 9import "nx_syscalls.nx" 10import "nx_persongen.nx" 11import "nx_sdfrender_mt.nx" 12import "nx_png.nx" 13import "nx_photomark_lib.nx" 14 15const TW_GENES: i64 = 7 16const TW_LEVELS: i64 = 3 17const TW_AXES: i64 = 5 18const TW_I64: i64 = 8 19const TW_SLOTS: i64 = 8 20const TW_CAMZ_UNITS: i64 = 4 // the camera distance every face gate renders at 21// every render goes through the threaded renderer (byte-identical to sdf_render by its own gate, T1 T2 T5): 0 = one worker per 22// hardware thread. A gate that renders three faces single-threaded outlived the ship loop's 15-minute proof deadline on a loaded NAS 23const TW_RENDER_WORKERS: i64 = 0 24const TW_YAW_FRONTAL: i64 = 0 25const TW_SKIN_R: i64 = 232 // persongen's fair-medium tone family, the default so a sweep moves geometry only 26const TW_SKIN_G: i64 = 196 27const TW_SKIN_B: i64 = 166 28const TW_SKIN_FIELDS: i64 = 3 29const TW_SKIN_SLOT_R: i64 = 0 30const TW_SKIN_SLOT_G: i64 = 1 31const TW_SKIN_SLOT_B: i64 = 2 32const TW_BYTE: i64 = 255 33const TW_SHIFT_G: i64 = 8 34const TW_SHIFT_B: i64 = 16 35const TW_RGB_BPP: i64 = 3 36const TW_PERMIL: i64 = 1000 37const TW_HALF: i64 = 2 38const TW_NL: i64 = 10 39const TW_UNSET: i64 = 0 - 1 40const TW_BIG: i64 = 1000000000 41const TW_PNG_ANNOUNCE: i64 = 1 42// the seven persongen identity ranges (floor, ceiling) -- provenance: nx_persongen.nx persongen_build pg_range calls 43const TW_W_LO: i64 = 85 44const TW_W_HI: i64 = 118 45const TW_L_LO: i64 = 88 46const TW_L_HI: i64 = 114 47const TW_J_LO: i64 = 80 48const TW_J_HI: i64 = 126 49const TW_E_LO: i64 = 85 50const TW_E_HI: i64 = 120 51const TW_P_LO: i64 = 78 52const TW_P_HI: i64 = 132 53const TW_N_LO: i64 = 84 54const TW_N_HI: i64 = 122 55const TW_C_LO: i64 = 82 56const TW_C_HI: i64 = 128 57// axis slots in the envelope tables 58const TW_AX_TILT_R: i64 = 0 59const TW_AX_TILT_L: i64 = 1 60const TW_AX_ICI: i64 = 2 61const TW_AX_ICF: i64 = 3 62const TW_AX_LIP: i64 = 4 63const TW_FIELD_NAME: i64 = 1 64const TW_FIELD_VALUE: i64 = 2 65const TW_FIELD_ERR: i64 = 3 66 67func tw_gene_name(k: i64) -> *u8 { 68 if k == 0 { return "width" as *u8 } 69 if k == 1 { return "length" as *u8 } 70 if k == 2 { return "jaw" as *u8 } 71 if k == 3 { return "eyes" as *u8 } 72 if k == 4 { return "lips" as *u8 } 73 if k == 5 { return "nose" as *u8 } 74 return "cheek" as *u8 75} 76func tw_axis_name(a: i64) -> *u8 { 77 if a == TW_AX_TILT_R { return "canthal_tilt_R" as *u8 } 78 if a == TW_AX_TILT_L { return "canthal_tilt_L" as *u8 } 79 if a == TW_AX_ICI { return "intercanthal_index" as *u8 } 80 if a == TW_AX_ICF { return "intercanthal_eq_fissure" as *u8 } 81 return "upper_over_lower_lip" as *u8 82} 83func tw_level_name(l: i64) -> *u8 { 84 if l == 0 { return "floor" as *u8 } 85 if l == 1 { return "middle" as *u8 } 86 return "ceiling" as *u8 87} 88func tw_lo(k: i64) -> i64 { 89 if k == 0 { return TW_W_LO } 90 if k == 1 { return TW_L_LO } 91 if k == 2 { return TW_J_LO } 92 if k == 3 { return TW_E_LO } 93 if k == 4 { return TW_P_LO } 94 if k == 5 { return TW_N_LO } 95 return TW_C_LO 96} 97func tw_hi(k: i64) -> i64 { 98 if k == 0 { return TW_W_HI } 99 if k == 1 { return TW_L_HI } 100 if k == 2 { return TW_J_HI } 101 if k == 3 { return TW_E_HI } 102 if k == 4 { return TW_P_HI } 103 if k == 5 { return TW_N_HI } 104 return TW_C_HI 105} 106func tw_mid(k: i64) -> i64 { return (tw_lo(k) + tw_hi(k)) / TW_HALF } 107func tw_level_pct(k: i64, l: i64) -> i64 { 108 if l == 0 { return tw_lo(k) } 109 if l == 1 { return tw_mid(k) } 110 return tw_hi(k) 111} 112func tw_kv(k: *u8, v: i64) -> i64 { ri_puts(k); ri_putn(v); return 0 } 113func tw_atoi(s: *u8) -> i64 { 114 var p: i64 = 0 115 var neg: i64 = 0 116 if (s[0] as i64) == PM_MINUS { neg = 1; p = 1 } 117 var v: i64 = 0 118 var go: i64 = 1 119 while go == 1 { 120 let c: i64 = s[p] as i64 121 if c >= PM_D0 { if c <= PM_D9 { v = v * PM_TEN + (c - PM_D0); p = p + 1 } else { go = 0 } } else { go = 0 } 122 } 123 if neg == 1 { return 0 - v } 124 return v 125} 126func tw_skin_default(skin: *i64) -> i64 { 127 skin[TW_SKIN_SLOT_R] = TW_SKIN_R; skin[TW_SKIN_SLOT_G] = TW_SKIN_G; skin[TW_SKIN_SLOT_B] = TW_SKIN_B 128 return 0 129} 130// build the canon face and apply the seven morphs at the given percents (100 = the canon identity) 131func tw_build(base: i64, g: *i64) -> i64 { 132 faceanat_build(base) 133 faceanat_morph_width(base, g[0]) 134 faceanat_morph_length(base, g[1]) 135 faceanat_morph_jaw(base, g[2]) 136 faceanat_morph_eyes(base, g[3]) 137 faceanat_morph_lips(base, g[4]) 138 faceanat_morph_nose(base, g[5]) 139 faceanat_morph_cheek(base, g[6]) 140 return 0 141} 142// the renderer's packed framebuffer (R | G<<8 | B<<16 per pixel) into the ruler's interleaved RGB bytes 143func tw_unpack(fb: *i64, n: i64, rgb: *u8) -> i64 { 144 var i: i64 = 0 145 while i < n { 146 let c: i64 = fb[i] 147 rgb[i * TW_RGB_BPP] = (c & TW_BYTE) as u8 148 rgb[i * TW_RGB_BPP + 1] = ((c >> TW_SHIFT_G) & TW_BYTE) as u8 149 rgb[i * TW_RGB_BPP + 2] = ((c >> TW_SHIFT_B) & TW_BYTE) as u8 150 i = i + 1 151 } 152 return 0 153} 154// one genome -> render -> the ruler's RGB bytes (frontal, the shared camera distance) 155func tw_render_rgb(base: i64, g: *i64, skin: *i64, rgb: *u8) -> i64 { 156 tw_build(base, g) 157 sdfmt_render(base, TW_YAW_FRONTAL, TW_CAMZ_UNITS, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 158 let fb: *i64 = (base + fb_off()) as *i64 159 tw_unpack(fb, ww() * hh(), rgb) 160 return ww() * hh() * TW_RGB_BPP 161} 162// one genome -> render -> measure; returns the ruler's code (PM_OK when both eyes placed) and fills res 163func tw_measure(base: i64, g: *i64, skin: *i64, rgb: *u8, res: *i64, face_c: *HaarCascade, eye_c: *HaarCascade) -> i64 { 164 tw_render_rgb(base, g, skin, rgb) 165 return pm_landmarks_auto(rgb, ww(), hh(), res, face_c, eye_c) 166} 167// a persongen identity (seed) rendered to a PNG for the file door; skin and the seven percents come back in skin/idv; 168// returns the PNG's bytes on disk (png_publish's answer, negative when nothing landed) 169func tw_render_png(base: i64, seed: i64, yaw: i64, path: *u8, skin: *i64, idv: *i64) -> i64 { 170 persongen_build(base, seed, skin, idv) 171 sdfmt_render(base, yaw, TW_CAMZ_UNITS, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 172 let fb: *i64 = (base + fb_off()) as *i64 173 return png_publish(fb, ww(), hh(), path, TW_PNG_ANNOUNCE) 174} 175func tw_axis_value(res: *i64, a: i64) -> i64 { 176 if a == TW_AX_TILT_R { return res[PM_R_TILT_R] } 177 if a == TW_AX_TILT_L { return res[PM_R_TILT_L] } 178 if a == TW_AX_ICI { return res[PM_R_ICI] } 179 if a == TW_AX_ICF { return res[PM_R_ICF] } 180 return res[PM_R_LIP] 181} 182func tw_print_axes(res: *i64) -> i64 { 183 tw_kv(" eyes=" as *u8, res[PM_R_EYES]); tw_kv(" mouth=" as *u8, res[PM_R_MOUTH]) 184 tw_kv(" tilt_R_deg10=" as *u8, res[PM_R_TILT_R]); tw_kv(" tilt_L_deg10=" as *u8, res[PM_R_TILT_L]) 185 tw_kv(" tilt_err_deg10=" as *u8, res[PM_R_TILT_ERR]) 186 tw_kv(" ici_permil=" as *u8, res[PM_R_ICI]); tw_kv(" icf_permil=" as *u8, res[PM_R_ICF]); tw_kv(" lip_permil=" as *u8, res[PM_R_LIP]) 187 tw_kv(" fissure_R_px=" as *u8, res[PM_R_FISSURE_R]); tw_kv(" fissure_L_px=" as *u8, res[PM_R_FISSURE_L]) 188 return 0 189} 190func tw_streq_span(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 { 191 var i: i64 = 0 192 while s + i < e { 193 if lit[i] == (0 as u8) { return 0 } 194 if buf[s + i] != lit[i] { return 0 } 195 i = i + 1 196 } 197 if lit[i] != (0 as u8) { return 0 } 198 return 1 199} 200func tw_target_clear(tv: *i64, te: *i64) -> i64 { 201 var a: i64 = 0 202 while a < TW_AXES { tv[a] = TW_UNSET; te[a] = 0; a = a + 1 } 203 return 0 204} 205// read axis|<name>|<value>|<err> rows into tv/te (TW_UNSET where absent); returns the number of axes read, 0 when unreadable 206func tw_load_target(path: *u8, tv: *i64, te: *i64) -> i64 { 207 tw_target_clear(tv, te) 208 let ln: *i64 = sys_mmap(TW_I64) as *i64 209 let buf: *u8 = ri_load(path, ln) 210 if (buf as i64) == 0 { return 0 } 211 let n: i64 = ln[0] 212 let fb: *i64 = sys_mmap(2 * TW_I64) as *i64 213 let t: *i64 = sys_mmap(2 * TW_I64) as *i64 214 var got: i64 = 0 215 var ls: i64 = 0 216 while ls < n { 217 var le: i64 = ls 218 while le < n { if (buf[le] as i64) == TW_NL { le = n + le } else { le = le + 1 } } 219 if le > n { le = le - n } 220 if pm_field(buf, ls, le, 0, fb) == 1 { 221 if tw_streq_span(buf, fb[0], fb[1], "axis" as *u8) == 1 { 222 if pm_field(buf, ls, le, TW_FIELD_NAME, fb) == 1 { 223 var ax: i64 = TW_UNSET 224 var k: i64 = 0 225 while k < TW_AXES { if tw_streq_span(buf, fb[0], fb[1], tw_axis_name(k)) == 1 { ax = k } k = k + 1 } 226 if ax >= 0 { 227 if pm_field(buf, ls, le, TW_FIELD_VALUE, fb) == 1 { 228 if pm_span_int(buf, fb[0], fb[1], t) == 1 { 229 tv[ax] = t[0] 230 te[ax] = 0 231 if pm_field(buf, ls, le, TW_FIELD_ERR, fb) == 1 { if pm_span_int(buf, fb[0], fb[1], t) == 1 { te[ax] = t[0] } } 232 got = got + 1 233 } 234 } 235 } 236 } 237 } 238 } 239 ls = le + 1 240 } 241 return got 242} 243// a target with its error bar is REACHABLE when the band [target-err, target+err] meets the envelope [amin, amax] 244func tw_reach(target: i64, err: i64, amin: i64, amax: i64) -> i64 { 245 if target + err < amin { return 0 } 246 if target - err > amax { return 0 } 247 return 1 248} 249func tw_load_cascades(out: *i64) -> i64 { 250 let rc: *i64 = sys_mmap(TW_I64) as *i64 251 let face_c: *HaarCascade = ff_load_face(rc) 252 let eye_c: *HaarCascade = ff_load_eye(rc) 253 out[0] = face_c as i64 254 out[1] = eye_c as i64 255 if (face_c as i64) == 0 { return 0 } 256 if (eye_c as i64) == 0 { return 0 } 257 return 1 258} 259 260// ---- AT39 (2026-09-17): THE LANDMARK TRUTH OF THE RENDER ITSELF ------------------------------------------------------------ 261// The render is the one subject whose geometry the estate owns. nx_faceanat measures the two closures of the palpebral fissure 262// FROM THE SDF SURFACE (fa_palpebral_fissure: found, never placed) and the camera of sdf_render is a pinhole whose constants 263// live in the renderer, so projecting those closures through that camera gives the four canthi in the pixels of the frame with 264// no model and no outside oracle. The photo ruler is then judged PER LANDMARK with a SIGNED error on every face the generator 265// can emit: a bias has a direction, and an NME alone cannot say which way a finder leans. Truth is held in DECI-PIXELS because 266// the surface scan steps FA_PF_XSTEP model units, under one pixel at the shared camera distance. The same record carries the 267// closures in MODEL units, the height and width of each aperture, and the left-right asymmetry of the four closures, so a 268// generator defect (lids that do not follow a morph) reads as a named reason and a number rather than as a ruler failure. 269const TW_DECI: i64 = 10 270const TW_PIX_CENTRE_DECI: i64 = 5 // the renderer shoots through pixel centres: index = position - one half 271const TW_T_RLAT_X: i64 = 0 // the order the ruler uses (PM_R_RLAT_X ..): RLAT, RMED, LMED, LLAT, each x then y 272const TW_T_RLAT_Y: i64 = 1 273const TW_T_RMED_X: i64 = 2 274const TW_T_RMED_Y: i64 = 3 275const TW_T_LMED_X: i64 = 4 276const TW_T_LMED_Y: i64 = 5 277const TW_T_LLAT_X: i64 = 6 278const TW_T_LLAT_Y: i64 = 7 279const TW_T_OK: i64 = 8 280const TW_T_EXO: i64 = 9 281const TW_T_TILT_R: i64 = 10 282const TW_T_TILT_L: i64 = 11 283const TW_T_REASON_R: i64 = 12 284const TW_T_REASON_L: i64 = 13 285const TW_T_ASYM_PERMIL: i64 = 14 // mean mirror error of the four closures, permil of the mean fissure width 286const TW_T_MODEL_R: i64 = 16 // model units, per eye: med x, med y, lat x, lat y, aperture height, fissure width, surface gap 287const TW_T_MODEL_L: i64 = 23 288const TW_TM_MED_X: i64 = 0 289const TW_TM_MED_Y: i64 = 1 290const TW_TM_LAT_X: i64 = 2 291const TW_TM_LAT_Y: i64 = 3 292const TW_TM_HEIGHT: i64 = 4 293const TW_TM_WIDTH: i64 = 5 294const TW_TM_GAP: i64 = 6 // the larger stated-to-found distance of the two corners, model units 295const TW_T_N: i64 = 32 296const TW_LANDMARKS: i64 = 4 297const TW_XY: i64 = 2 298const TW_EYE_IMAGE_LEFT: i64 = 0 - 1 // model x < 0 lands in the left half of the image at yaw 0: the R eye of the ruler 299const TW_EYE_IMAGE_RIGHT: i64 = 1 300const TW_TRUTH_NO_SURFACE: i64 = FA_PF_R_NO_SURFACE 301// the record of the judge: per landmark (dx, dy, nme), then the worst and the mean NME 302const TW_J_FIELDS: i64 = 3 303const TW_J_DX: i64 = 0 304const TW_J_DY: i64 = 1 305const TW_J_NME: i64 = 2 306const TW_J_NMAX: i64 = 12 307const TW_J_NMEAN: i64 = 13 308const TW_J_N: i64 = 16 309 310func tw_landmark_name(k: i64) -> *u8 { 311 if k == 0 { return "RLAT" as *u8 } 312 if k == 1 { return "RMED" as *u8 } 313 if k == 2 { return "LMED" as *u8 } 314 return "LLAT" as *u8 315} 316func tw_truth_reason_name(r: i64) -> *u8 { 317 if r == FA_PF_R_OK { return "OK" as *u8 } 318 if r == FA_PF_R_NO_SURFACE { return "NO-SURFACE" as *u8 } 319 if r == FA_PF_R_NO_APERTURE { return "NO-APERTURE" as *u8 } 320 if r == FA_PF_R_NO_CLOSURE { return "NO-CLOSURE" as *u8 } 321 return "UNNAMED" as *u8 322} 323// a model point through the camera of sdf_render -- origin TW_CAMZ_UNITS*FX behind the face, yawed about Y, focal FOCAL, 324// centre (HW, HH) -- to deci-pixel INDICES in out2. The arithmetic is the inverse of the ray set-up in sdf_render_rows, term 325// for term. Returns 0 when the point is not in front of the camera 326func tw_project(px: i64, py: i64, pz: i64, yaw: i64, out2: *i64) -> i64 { 327 let R: i64 = TW_CAMZ_UNITS * FX 328 let cy4: i64 = it_cos4096(yaw) 329 let sy4: i64 = it_sin4096(yaw) 330 let rox: i64 = 0 - sy4 * R / IT_FX 331 let roz: i64 = 0 - cy4 * R / IT_FX 332 let vx: i64 = px - rox 333 let vz: i64 = pz - roz 334 let cx: i64 = (cy4 * vx - sy4 * vz) / IT_FX 335 let cz: i64 = (sy4 * vx + cy4 * vz) / IT_FX 336 if cz <= 0 { return 0 } 337 out2[0] = TW_DECI * HW + (TW_DECI * FOCAL * cx) / cz - TW_PIX_CENTRE_DECI 338 out2[1] = TW_DECI * HH - (TW_DECI * FOCAL * py) / cz - TW_PIX_CENTRE_DECI 339 return 1 340} 341// the two canthi of one eye. TRUTH IS THE CONSTRUCTION: the lid shell states where its opening ends (fa_lid_corner), exactly, 342// for every genome. THE SURFACE IS THE SECOND READING: fa_palpebral_fissure finds a closure on the blended field near each 343// stated corner, and the larger of the two distances between stated and found (model units) is published as the gap -- a 344// construction the surface does not realise is a defect in the generator, and the reason code says so. The stated corner is 345// lifted to the visible surface point under it and projected. Returns the scan's reason (FA_PF_R_OK when the surface confirms) 346func tw_truth_eye(base: i64, eye_sign: i64, yaw: i64, pf: *i64, p2: *i64, t: *i64, slot_med: i64, slot_lat: i64, slot_model: i64) -> i64 { 347 fa_palpebral_fissure(base, eye_sign, pf) 348 t[slot_model + TW_TM_HEIGHT] = pf[FA_PF_O_PEAK_SPAN] 349 if pf[FA_PF_O_REASON] != FA_PF_R_OK { return pf[FA_PF_O_REASON] } 350 if fa_lid_corner(base, eye_sign, FA_CORNER_MEDIAL, p2) == 0 { return FA_PF_R_NO_APERTURE } 351 let mx: i64 = p2[0] 352 let my: i64 = p2[1] 353 fa_lid_corner(base, eye_sign, FA_CORNER_LATERAL, p2) 354 let lx: i64 = p2[0] 355 let ly: i64 = p2[1] 356 t[slot_model + TW_TM_MED_X] = mx 357 t[slot_model + TW_TM_MED_Y] = my 358 t[slot_model + TW_TM_LAT_X] = lx 359 t[slot_model + TW_TM_LAT_Y] = ly 360 t[slot_model + TW_TM_WIDTH] = pm_dist(mx, my, lx, ly) 361 let gm: i64 = pm_dist(mx, my, pf[FA_PF_O_MED_X], pf[FA_PF_O_MED_Y]) 362 let gl: i64 = pm_dist(lx, ly, pf[FA_PF_O_LAT_X], pf[FA_PF_O_LAT_Y]) 363 t[slot_model + TW_TM_GAP] = pm_max(gm, gl) 364 let mz: i64 = fa_lid_corner_z(base, eye_sign, mx, my) 365 let lz: i64 = fa_lid_corner_z(base, eye_sign, lx, ly) 366 if mz == FA_LIP_NOSURF { return TW_TRUTH_NO_SURFACE } 367 if lz == FA_LIP_NOSURF { return TW_TRUTH_NO_SURFACE } 368 if tw_project(mx, my, mz, yaw, p2) == 0 { return TW_TRUTH_NO_SURFACE } 369 t[slot_med] = p2[0] 370 t[slot_med + 1] = p2[1] 371 if tw_project(lx, ly, lz, yaw, p2) == 0 { return TW_TRUTH_NO_SURFACE } 372 t[slot_lat] = p2[0] 373 t[slot_lat + 1] = p2[1] 374 return FA_PF_R_OK 375} 376// tw_project at any sub-pixel scale (10 = deci-pixels, 1000 = milli-pixels): the same inverse-camera arithmetic, scale a parameter 377func tw_project_s(px: i64, py: i64, pz: i64, yaw: i64, scale: i64, out2: *i64) -> i64 { 378 let R: i64 = TW_CAMZ_UNITS * FX 379 let cy4: i64 = it_cos4096(yaw) 380 let sy4: i64 = it_sin4096(yaw) 381 let rox: i64 = 0 - sy4 * R / IT_FX 382 let roz: i64 = 0 - cy4 * R / IT_FX 383 let vx: i64 = px - rox 384 let vz: i64 = pz - roz 385 let cx: i64 = (cy4 * vx - sy4 * vz) / IT_FX 386 let cz: i64 = (sy4 * vx + cy4 * vz) / IT_FX 387 if cz <= 0 { return 0 } 388 out2[0] = scale * HW + (scale * FOCAL * cx) / cz - scale / TW_HALF 389 out2[1] = scale * HH - (scale * FOCAL * py) / cz - scale / TW_HALF 390 return 1 391} 392// THE TILTS AND THE ROLL ARE ANGLES OF SHORT SEGMENTS, so they are taken from MILLI-pixel projections of the stated corners: a 393// fissure is about 370 deci-pixels long, and one deci-pixel of rise is 1.5 deg10, so tilts computed from the deci-pixel record 394// move in plateaus -- a fit's unreachable tilt stopped two short of its gene's ceiling because the last step changed nothing it 395// could see. Fills TW_T_TILT_R, TW_T_TILT_L and TW_T_ROLL; returns 0 when an eye has no lid shell 396const TW_MILLI: i64 = 1000 397const TW_T_ROLL: i64 = 15 398func tw_fine_tilts(base: i64, yaw: i64, t: *i64) -> i64 { 399 let c2: *i64 = sys_mmap(TW_XY * TW_I64) as *i64 400 let m: *i64 = sys_mmap(TW_LANDMARKS * TW_XY * TW_I64) as *i64 // RLAT, RMED, LMED, LLAT in milli-pixels 401 var e: i64 = 0 402 while e < 2 { 403 var sign: i64 = TW_EYE_IMAGE_LEFT 404 var smed: i64 = TW_T_RMED_X 405 var slat: i64 = TW_T_RLAT_X 406 if e == 1 { sign = TW_EYE_IMAGE_RIGHT; smed = TW_T_LMED_X; slat = TW_T_LLAT_X } 407 if fa_lid_corner(base, sign, FA_CORNER_MEDIAL, c2) == 0 { return 0 } 408 let mx: i64 = c2[0] 409 let my: i64 = c2[1] 410 fa_lid_corner(base, sign, FA_CORNER_LATERAL, c2) 411 let lx: i64 = c2[0] 412 let ly: i64 = c2[1] 413 let mz: i64 = fa_lid_corner_z(base, sign, mx, my) 414 let lz: i64 = fa_lid_corner_z(base, sign, lx, ly) 415 if mz == FA_LIP_NOSURF { return 0 } 416 if lz == FA_LIP_NOSURF { return 0 } 417 if tw_project_s(mx, my, mz, yaw, TW_MILLI, c2) == 0 { return 0 } 418 m[smed] = c2[0] 419 m[smed + 1] = c2[1] 420 if tw_project_s(lx, ly, lz, yaw, TW_MILLI, c2) == 0 { return 0 } 421 m[slat] = c2[0] 422 m[slat + 1] = c2[1] 423 e = e + 1 424 } 425 t[TW_T_TILT_R] = pm_canthal_tilt(m[TW_T_RMED_Y] - m[TW_T_RLAT_Y], pm_abs(m[TW_T_RMED_X] - m[TW_T_RLAT_X])) 426 t[TW_T_TILT_L] = pm_canthal_tilt(m[TW_T_LMED_Y] - m[TW_T_LLAT_Y], pm_abs(m[TW_T_LLAT_X] - m[TW_T_LMED_X])) 427 t[TW_T_ROLL] = pm_canthal_tilt(m[TW_T_LMED_Y] - m[TW_T_RMED_Y], pm_abs(m[TW_T_LMED_X] - m[TW_T_RMED_X])) 428 return 1 429} 430// the four canthi of the face CURRENTLY BUILT in base, in deci-pixels of the frame sdf_render draws at that yaw, with the 431// outer-canthal span and tilts of the truth (the tilt arithmetic of the ruler, so the two are comparable) and the left-right 432// asymmetry of the closures in model space (a mirrored face reads 0). Returns 1 when both eyes measured; the per-eye reasons 433// stay in the record either way 434func tw_truth_landmarks(base: i64, yaw: i64, t: *i64) -> i64 { 435 var k: i64 = 0 436 while k < TW_T_N { t[k] = 0; k = k + 1 } 437 let pf: *i64 = sys_mmap(FA_PF_O_FIELDS * TW_I64 + FA_LIP_SLACK) as *i64 438 let p2: *i64 = sys_mmap(TW_XY * TW_I64) as *i64 439 t[TW_T_REASON_R] = tw_truth_eye(base, TW_EYE_IMAGE_LEFT, yaw, pf, p2, t, TW_T_RMED_X, TW_T_RLAT_X, TW_T_MODEL_R) 440 t[TW_T_REASON_L] = tw_truth_eye(base, TW_EYE_IMAGE_RIGHT, yaw, pf, p2, t, TW_T_LMED_X, TW_T_LLAT_X, TW_T_MODEL_L) 441 if t[TW_T_REASON_R] != FA_PF_R_OK { return 0 } 442 if t[TW_T_REASON_L] != FA_PF_R_OK { return 0 } 443 t[TW_T_EXO] = pm_dist(t[TW_T_RLAT_X], t[TW_T_RLAT_Y], t[TW_T_LLAT_X], t[TW_T_LLAT_Y]) 444 tw_fine_tilts(base, yaw, t) 445 let mr: i64 = TW_T_MODEL_R 446 let ml: i64 = TW_T_MODEL_L 447 let mirror: i64 = pm_abs(t[mr + TW_TM_MED_X] + t[ml + TW_TM_MED_X]) + pm_abs(t[mr + TW_TM_MED_Y] - t[ml + TW_TM_MED_Y]) + pm_abs(t[mr + TW_TM_LAT_X] + t[ml + TW_TM_LAT_X]) + pm_abs(t[mr + TW_TM_LAT_Y] - t[ml + TW_TM_LAT_Y]) 448 let wsum: i64 = t[mr + TW_TM_WIDTH] + t[ml + TW_TM_WIDTH] 449 t[TW_T_ASYM_PERMIL] = mirror * PM_PERMIL / pm_max(wsum * TW_XY, 1) 450 t[TW_T_OK] = 1 451 return 1 452} 453// the four canthi of the ruler against the truth: per landmark the SIGNED error in deci-pixels (ruler minus truth, +x image 454// right, +y image down) and the NME in permil of the TRUTH outer-canthal span (the shape the photo referee uses). Returns the 455// worst NME 456func tw_judge(res: *i64, t: *i64, j: *i64) -> i64 { 457 var k: i64 = 0 458 while k < TW_J_N { j[k] = 0; k = k + 1 } 459 var nmax: i64 = 0 460 var nsum: i64 = 0 461 k = 0 462 while k < TW_LANDMARKS { 463 let rx: i64 = res[PM_R_RLAT_X + k * TW_XY] * TW_DECI 464 let ry: i64 = res[PM_R_RLAT_Y + k * TW_XY] * TW_DECI 465 let tx: i64 = t[TW_T_RLAT_X + k * TW_XY] 466 let ty: i64 = t[TW_T_RLAT_Y + k * TW_XY] 467 let nme: i64 = pm_dist(rx, ry, tx, ty) * PM_PERMIL / pm_max(t[TW_T_EXO], 1) 468 j[k * TW_J_FIELDS + TW_J_DX] = rx - tx 469 j[k * TW_J_FIELDS + TW_J_DY] = ry - ty 470 j[k * TW_J_FIELDS + TW_J_NME] = nme 471 if nme > nmax { nmax = nme } 472 nsum = nsum + nme 473 k = k + 1 474 } 475 j[TW_J_NMAX] = nmax 476 j[TW_J_NMEAN] = nsum / TW_LANDMARKS 477 return nmax 478} 479// a persongen identity (seed) -> render -> measure, in-process; skin and the seven percents come back in skin/idv 480func tw_measure_seed(base: i64, seed: i64, skin: *i64, idv: *i64, rgb: *u8, res: *i64, face_c: *HaarCascade, eye_c: *HaarCascade) -> i64 { 481 persongen_build(base, seed, skin, idv) 482 sdfmt_render(base, TW_YAW_FRONTAL, TW_CAMZ_UNITS, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 483 let fb: *i64 = (base + fb_off()) as *i64 484 tw_unpack(fb, ww() * hh(), rgb) 485 return pm_landmarks_auto(rgb, ww(), hh(), res, face_c, eye_c) 486} 487// one truth record as text: reasons by name, the projected canthi, the model-space closures, the height and width of each 488// aperture, the asymmetry 489func tw_print_truth(t: *i64) -> i64 { 490 ri_puts(" truth_R=" as *u8); ri_puts(tw_truth_reason_name(t[TW_T_REASON_R])); ri_puts(" truth_L=" as *u8); ri_puts(tw_truth_reason_name(t[TW_T_REASON_L])) 491 var k: i64 = 0 492 while k < TW_LANDMARKS { 493 ri_puts(" " as *u8); ri_puts(tw_landmark_name(k)); tw_kv("_deci=" as *u8, t[TW_T_RLAT_X + k * TW_XY]); tw_kv("," as *u8, t[TW_T_RLAT_Y + k * TW_XY]) 494 k = k + 1 495 } 496 tw_kv(" exo_deci=" as *u8, t[TW_T_EXO]); tw_kv(" tilt_truth_deg10=" as *u8, t[TW_T_TILT_R]); tw_kv("," as *u8, t[TW_T_TILT_L]) 497 tw_kv(" model_R_med=" as *u8, t[TW_T_MODEL_R + TW_TM_MED_X]); tw_kv("," as *u8, t[TW_T_MODEL_R + TW_TM_MED_Y]) 498 tw_kv(" model_R_lat=" as *u8, t[TW_T_MODEL_R + TW_TM_LAT_X]); tw_kv("," as *u8, t[TW_T_MODEL_R + TW_TM_LAT_Y]) 499 tw_kv(" model_L_med=" as *u8, t[TW_T_MODEL_L + TW_TM_MED_X]); tw_kv("," as *u8, t[TW_T_MODEL_L + TW_TM_MED_Y]) 500 tw_kv(" model_L_lat=" as *u8, t[TW_T_MODEL_L + TW_TM_LAT_X]); tw_kv("," as *u8, t[TW_T_MODEL_L + TW_TM_LAT_Y]) 501 tw_kv(" aperture_h=" as *u8, t[TW_T_MODEL_R + TW_TM_HEIGHT]); tw_kv("," as *u8, t[TW_T_MODEL_L + TW_TM_HEIGHT]) 502 tw_kv(" fissure_w=" as *u8, t[TW_T_MODEL_R + TW_TM_WIDTH]); tw_kv("," as *u8, t[TW_T_MODEL_L + TW_TM_WIDTH]) 503 tw_kv(" surface_gap=" as *u8, t[TW_T_MODEL_R + TW_TM_GAP]); tw_kv("," as *u8, t[TW_T_MODEL_L + TW_TM_GAP]) 504 tw_kv(" asym_permil=" as *u8, t[TW_T_ASYM_PERMIL]) 505 return 0 506} 507// explicit morph percents -> render at a chosen yaw and camera distance -> PNG through the file door. A close camera is the 508// eye unit judged at its own scale: the same geometry, more pixels across the opening 509func tw_render_genome_png(base: i64, g: *i64, skin: *i64, yaw: i64, camz_units: i64, path: *u8) -> i64 { 510 tw_build(base, g) 511 sdfmt_render(base, yaw, camz_units, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 512 let fb: *i64 = (base + fb_off()) as *i64 513 return png_publish(fb, ww(), hh(), path, TW_PNG_ANNOUNCE) 514} 515 516// ---- AT40 (2026-09-17): THE FIT, FROM STATED LANDMARKS, NO RENDER IN THE LOOP -------------------------------------------------- 517// A reference card carries axes with error bars (the outside oracle's medians over a person's stills). The generator now states 518// its own canthi for any genome, so the card's eye axes are a function of thirteen genes -- the seven face morphs and the six eye 519// genes -- that costs four surface lookups to evaluate. The fit is coordinate descent over those genes inside their DECLARED 520// ranges: halving integer steps, a move kept only when the cost strictly falls, so a gene that does not touch a targeted axis 521// stays where it started and a gene that ends on a range limit NAMES the range the generator would have to widen. The cost puts 522// every axis inside its error band first and nearer its centre second. The answer is confirmed once on the surface; an axis 523// the generator cannot state (the lips, until the lip unit lands) is reported UNSTATED and the verdict is PARTIAL, never quietly 524// dropped. 525const TW_ALL_GENES: i64 = TW_GENES + FA_EG_N 526const TW_CANON_PCT: i64 = 100 527const TW_FIT_BAND_WEIGHT: i64 = 16 // one unit outside an error band costs as much as sixteen units of distance inside it 528const TW_FIT_STEP_FIRST: i64 = 8 529const TW_FIT_MAX_PASSES: i64 = 12 530const TW_FIT_DIAGONALS: i64 = 4 // the four sign pairs of a two-gene move 531const TW_AXIS_REACHED: i64 = 1 532const TW_AXIS_UNREACHED: i64 = 0 533const TW_AXIS_UNSTATED: i64 = 2 534const TW_AXIS_NO_TARGET: i64 = 3 535 536func tw_all_name(k: i64) -> *u8 { if k < TW_GENES { return tw_gene_name(k) } return fa_eye_gene_name(k - TW_GENES) } 537func tw_all_lo(k: i64) -> i64 { if k < TW_GENES { return tw_lo(k) } return fa_eye_gene_lo(k - TW_GENES) } 538func tw_all_hi(k: i64) -> i64 { if k < TW_GENES { return tw_hi(k) } return fa_eye_gene_hi(k - TW_GENES) } 539func tw_all_canon(k: i64) -> i64 { if k < TW_GENES { return TW_CANON_PCT } return fa_eye_gene_canon(k - TW_GENES) } 540func tw_all_canon_fill(ga: *i64) -> i64 { var k: i64 = 0; while k < TW_ALL_GENES { ga[k] = tw_all_canon(k); k = k + 1 } return 0 } 541// build the face of a thirteen-gene vector: the seven morphs, then the six eye genes on the eye units the morphs produced 542func tw_build_all(base: i64, ga: *i64) -> i64 { 543 tw_build(base, ga) 544 faceanat_eye_genes(base, ((ga as i64) + TW_GENES * TW_I64) as *i64) 545 return 0 546} 547// the four STATED canthi of the built face projected into the frame, with the span and raw tilts -- tw_truth_landmarks without 548// the surface scan, for a loop that evaluates hundreds of genomes. Returns 1 when both eyes carry a lid shell 549func tw_stated_landmarks(base: i64, yaw: i64, t: *i64) -> i64 { 550 var k: i64 = 0 551 while k < TW_T_N { t[k] = 0; k = k + 1 } 552 let p2: *i64 = sys_mmap(TW_XY * TW_I64) as *i64 553 var e: i64 = 0 554 while e < 2 { 555 var sign: i64 = TW_EYE_IMAGE_LEFT 556 var smed: i64 = TW_T_RMED_X 557 var slat: i64 = TW_T_RLAT_X 558 if e == 1 { sign = TW_EYE_IMAGE_RIGHT; smed = TW_T_LMED_X; slat = TW_T_LLAT_X } 559 if fa_lid_corner(base, sign, FA_CORNER_MEDIAL, p2) == 0 { return 0 } 560 let mx: i64 = p2[0] 561 let my: i64 = p2[1] 562 fa_lid_corner(base, sign, FA_CORNER_LATERAL, p2) 563 let lx: i64 = p2[0] 564 let ly: i64 = p2[1] 565 let mz: i64 = fa_lid_corner_z(base, sign, mx, my) 566 let lz: i64 = fa_lid_corner_z(base, sign, lx, ly) 567 if mz == FA_LIP_NOSURF { return 0 } 568 if lz == FA_LIP_NOSURF { return 0 } 569 if tw_project(mx, my, mz, yaw, p2) == 0 { return 0 } 570 t[smed] = p2[0] 571 t[smed + 1] = p2[1] 572 if tw_project(lx, ly, lz, yaw, p2) == 0 { return 0 } 573 t[slat] = p2[0] 574 t[slat + 1] = p2[1] 575 e = e + 1 576 } 577 t[TW_T_EXO] = pm_dist(t[TW_T_RLAT_X], t[TW_T_RLAT_Y], t[TW_T_LLAT_X], t[TW_T_LLAT_Y]) 578 tw_fine_tilts(base, yaw, t) 579 t[TW_T_OK] = 1 580 return 1 581} 582// the card's axes from a landmark record, by the ruler's own arithmetic (pm_axes): the head's roll off the medial line, the two 583// tilts corrected for it, the intercanthal index and the intercanthal-to-fissure ratio. The lip axis has no stated truth: UNSET 584func tw_axes_from_landmarks(t: *i64, ax: *i64) -> i64 { 585 let roll: i64 = t[TW_T_ROLL] 586 ax[TW_AX_TILT_R] = t[TW_T_TILT_R] - roll 587 ax[TW_AX_TILT_L] = t[TW_T_TILT_L] + roll 588 let endo: i64 = pm_dist(t[TW_T_RMED_X], t[TW_T_RMED_Y], t[TW_T_LMED_X], t[TW_T_LMED_Y]) 589 let fr: i64 = pm_dist(t[TW_T_RLAT_X], t[TW_T_RLAT_Y], t[TW_T_RMED_X], t[TW_T_RMED_Y]) 590 let fl: i64 = pm_dist(t[TW_T_LMED_X], t[TW_T_LMED_Y], t[TW_T_LLAT_X], t[TW_T_LLAT_Y]) 591 ax[TW_AX_ICI] = endo * PM_PERMIL / pm_max(t[TW_T_EXO], 1) 592 ax[TW_AX_ICF] = endo * PM_PERMIL * TW_HALF / pm_max(fr + fl, 1) 593 ax[TW_AX_LIP] = TW_UNSET 594 return 0 595} 596// the cost of a set of axes against a card: per targeted, stated axis the distance beyond the error band (weighted) plus the 597// distance itself; an axis with no target or no stated value costs nothing here and is reported by name elsewhere 598func tw_fit_cost(ax: *i64, tv: *i64, te: *i64) -> i64 { 599 var c: i64 = 0 600 var a: i64 = 0 601 while a < TW_AXES { 602 if tv[a] != TW_UNSET { if ax[a] != TW_UNSET { 603 let d: i64 = pm_abs(ax[a] - tv[a]) 604 var over: i64 = d - te[a] 605 if over < 0 { over = 0 } 606 c = c + over * TW_FIT_BAND_WEIGHT + d 607 } } 608 a = a + 1 609 } 610 return c 611} 612func tw_fit_eval(base: i64, ga: *i64, tv: *i64, te: *i64, t: *i64, ax: *i64) -> i64 { 613 tw_build_all(base, ga) 614 if tw_stated_landmarks(base, TW_YAW_FRONTAL, t) == 0 { return TW_BIG } 615 tw_axes_from_landmarks(t, ax) 616 return tw_fit_cost(ax, tv, te) 617} 618// one PAIRED pass at a step size: two genes moved together in each of the four diagonal directions. A card's ratios share genes 619// (the face width and the eye size both move the intercanthal index AND the fissure ratio), so the cost has valleys that run 620// diagonally, and a descent that may only move along one gene at a time stops on the valley's wall. Returns 1 when a move was kept 621func tw_fit_pair_pass(base: i64, tv: *i64, te: *i64, ga: *i64, t: *i64, axt: *i64, step: i64, best: *i64, evals: *i64) -> i64 { 622 var moved: i64 = 0 623 var i: i64 = 0 624 while i < TW_ALL_GENES { 625 var j: i64 = i + 1 626 while j < TW_ALL_GENES { 627 var q: i64 = 0 628 while q < TW_FIT_DIAGONALS { 629 var di: i64 = step 630 var dj: i64 = step 631 if q == 1 { dj = 0 - step } 632 if q == 2 { di = 0 - step } 633 if q == 3 { di = 0 - step; dj = 0 - step } 634 let vi0: i64 = ga[i] 635 let vj0: i64 = ga[j] 636 let vi1: i64 = vi0 + di 637 let vj1: i64 = vj0 + dj 638 var inside: i64 = 1 639 if vi1 < tw_all_lo(i) { inside = 0 } 640 if vi1 > tw_all_hi(i) { inside = 0 } 641 if vj1 < tw_all_lo(j) { inside = 0 } 642 if vj1 > tw_all_hi(j) { inside = 0 } 643 if inside == 1 { 644 ga[i] = vi1 645 ga[j] = vj1 646 let c: i64 = tw_fit_eval(base, ga, tv, te, t, axt) 647 evals[0] = evals[0] + 1 648 if c < best[0] { best[0] = c; moved = 1 } else { ga[i] = vi0; ga[j] = vj0 } 649 } 650 q = q + 1 651 } 652 j = j + 1 653 } 654 i = i + 1 655 } 656 return moved 657} 658// one SINGLE-gene pass at a step size: each gene tried one step down and one step up. Returns 1 when a move was kept 659func tw_fit_single_pass(base: i64, tv: *i64, te: *i64, ga: *i64, t: *i64, axt: *i64, step: i64, best: *i64, evals: *i64) -> i64 { 660 var moved: i64 = 0 661 var k: i64 = 0 662 while k < TW_ALL_GENES { 663 var dir: i64 = 0 - 1 664 while dir <= 1 { 665 let v0: i64 = ga[k] 666 let v1: i64 = v0 + dir * step 667 if v1 >= tw_all_lo(k) { if v1 <= tw_all_hi(k) { 668 ga[k] = v1 669 let c: i64 = tw_fit_eval(base, ga, tv, te, t, axt) 670 evals[0] = evals[0] + 1 671 if c < best[0] { best[0] = c; moved = 1 } else { ga[k] = v0 } 672 } } 673 dir = dir + 2 674 } 675 k = k + 1 676 } 677 return moved 678} 679// pattern search from the vector in ga (the caller's start, normally the canon): at each halving step size, single-gene passes 680// until none moves, then one paired pass, repeated until neither moves (bounded). Returns the number of genomes evaluated; ga 681// holds the fitted vector, ax its axes, and cost_out[0] the final cost 682func tw_fit(base: i64, tv: *i64, te: *i64, ga: *i64, ax: *i64, cost_out: *i64) -> i64 { 683 let t: *i64 = sys_mmap(TW_T_N * TW_I64) as *i64 684 let axt: *i64 = sys_mmap(TW_AXES * TW_I64) as *i64 685 let best: *i64 = sys_mmap(TW_I64) as *i64 686 let evals: *i64 = sys_mmap(TW_I64) as *i64 687 evals[0] = 1 688 best[0] = tw_fit_eval(base, ga, tv, te, t, ax) 689 var step: i64 = TW_FIT_STEP_FIRST 690 while step >= 1 { 691 var rounds: i64 = 0 692 var moving: i64 = 1 693 while moving == 1 { 694 moving = 0 695 if rounds < TW_FIT_MAX_PASSES { 696 if tw_fit_single_pass(base, tv, te, ga, t, axt, step, best, evals) == 1 { moving = 1 } 697 if moving == 0 { if tw_fit_pair_pass(base, tv, te, ga, t, axt, step, best, evals) == 1 { moving = 1 } } 698 rounds = rounds + 1 699 } 700 } 701 step = step / TW_HALF 702 } 703 cost_out[0] = tw_fit_eval(base, ga, tv, te, t, ax) 704 return evals[0] + 1 705} 706// how one axis ended: REACHED inside its band, UNREACHED outside it, UNSTATED when the generator cannot state it, NO-TARGET 707func tw_axis_state(ax: *i64, tv: *i64, te: *i64, a: i64) -> i64 { 708 if tv[a] == TW_UNSET { return TW_AXIS_NO_TARGET } 709 if ax[a] == TW_UNSET { return TW_AXIS_UNSTATED } 710 if pm_abs(ax[a] - tv[a]) <= te[a] { return TW_AXIS_REACHED } 711 return TW_AXIS_UNREACHED 712} 713func tw_axis_state_name(s: i64) -> *u8 { 714 if s == TW_AXIS_REACHED { return "REACHED" as *u8 } 715 if s == TW_AXIS_UNREACHED { return "UNREACHED" as *u8 } 716 if s == TW_AXIS_UNSTATED { return "UNSTATED" as *u8 } 717 return "NO-TARGET" as *u8 718} 719// a thirteen-gene vector -> render at a chosen yaw and camera distance -> PNG through the file door 720func tw_render_all_png(base: i64, ga: *i64, skin: *i64, yaw: i64, camz_units: i64, path: *u8) -> i64 { 721 tw_build_all(base, ga) 722 sdfmt_render(base, yaw, camz_units, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 723 let fb: *i64 = (base + fb_off()) as *i64 724 return png_publish(fb, ww(), hh(), path, TW_PNG_ANNOUNCE) 725} 726 727// ---- AT44 (2026-09-18): THE MOUTH'S LANDMARK TRUTH AND ITS JUDGE ----------------------------------------------------------------- 728// The lip unit STATES its vermilion lines (fa_lip_stated: stomion, labrale superius and inferius at the midline, the commissure's 729// half-width and seam height), so the mouth has construction truth exactly as the eye unit does. Each stated point is lifted to 730// the visible surface under it (the file's one marcher, fa_surface_z) and projected through the renderer's own camera (tw_project) 731// into deci-pixels. The photo ruler's five mouth landmarks are then judged per landmark with a SIGNED error and an NME in permil 732// of the stated eye unit's outer-canthal span -- the normaliser the canthi are judged by, so one NME means one thing on both units 733// -- and its lip ratio is held to the ratio of the projected points, which is what a perfect ruler reading pixels would print. 734const TW_LT_LS_X: i64 = 0 // image order: labrale superius, stomion, labrale inferius, image-left and image-right cheilion 735const TW_LT_STO_X: i64 = 2 736const TW_LT_LI_X: i64 = 4 737const TW_LT_CHR_X: i64 = 6 // image left: the subject's right commissure (model x < 0) 738const TW_LT_CHL_X: i64 = 8 739const TW_LT_OK: i64 = 10 740const TW_LT_REASON: i64 = 11 741const TW_LT_RATIO_STATED: i64 = 12 // upper over lower in model units, permil (fa_lip_stated's own ratio) 742const TW_LT_RATIO_PIXEL: i64 = 13 // the same ratio on the projected points, permil 743const TW_LT_EXO: i64 = 14 // the stated eye unit's outer-canthal span, deci-pixels 744const TW_LT_MODEL: i64 = 16 // + 3k: landmark k in MODEL units -- x, y and the depth z it was lifted to 745const TW_LT_MODEL_FIELDS: i64 = 3 746const TW_LT_N: i64 = 32 747const TW_LIP_LANDMARKS: i64 = 5 748const TW_LT_R_OK: i64 = 0 749const TW_LT_R_NO_SURFACE: i64 = 1 // a stated lip point has no surface under it on the march 750const TW_LT_R_NO_EYES: i64 = 2 // the eye unit states no canthi, so there is no span to normalise by 751const TW_LT_R_BEHIND: i64 = 3 // a lifted lip point is not in front of the camera 752// the judge's record: per landmark (dx, dy, nme) in the truth's order, then the worst and the mean NME and the ratio pair 753const TW_LJ_NMAX: i64 = 15 754const TW_LJ_NMEAN: i64 = 16 755const TW_LJ_RATIO_RULER: i64 = 17 756const TW_LJ_RATIO_TRUTH: i64 = 18 757const TW_LJ_N: i64 = 20 758 759func tw_lip_landmark_name(k: i64) -> *u8 { 760 if k == 0 { return "LS" as *u8 } 761 if k == 1 { return "STO" as *u8 } 762 if k == 2 { return "LI" as *u8 } 763 if k == 3 { return "CHR" as *u8 } 764 return "CHL" as *u8 765} 766func tw_lip_reason_name(r: i64) -> *u8 { 767 if r == TW_LT_R_OK { return "OK" as *u8 } 768 if r == TW_LT_R_NO_SURFACE { return "NO-SURFACE" as *u8 } 769 if r == TW_LT_R_NO_EYES { return "NO-EYES" as *u8 } 770 return "BEHIND-CAMERA" as *u8 771} 772// one stated model point at depth z (FA_LIP_NOSURF = lift it to the surface the march finds), projected into lt[slot], lt[slot+1]; 773// returns a TW_LT_R_* reason 774func tw_lip_point(base: i64, x: i64, y: i64, zin: i64, yaw: i64, lt: *i64, slot: i64, p2: *i64) -> i64 { 775 var z: i64 = zin 776 if z == FA_LIP_NOSURF { z = fa_surface_z(base, x, y) } 777 let m: i64 = TW_LT_MODEL + (slot / TW_XY) * TW_LT_MODEL_FIELDS 778 lt[m] = x 779 lt[m + 1] = y 780 lt[m + 2] = z 781 if z == FA_LIP_NOSURF { return TW_LT_R_NO_SURFACE } 782 if tw_project(x, y, z, yaw, p2) == 0 { return TW_LT_R_BEHIND } 783 lt[slot] = p2[0] 784 lt[slot + 1] = p2[1] 785 return TW_LT_R_OK 786} 787// THE SEAM IS SEEN AT ITS OPENING, NOT AT ITS FLOOR. The oral slit is carved behind the lips' front, and a ray to its floor meets the 788// upper lip first -- MEASURED 2026-09-18 on seed 42: the midline floor (z -504) projects to row 252.5 while the upper lip's own 789// lower edge (y -367, z -702) projects to 254.8, BELOW the floor, and the render's dark line sits at 255..257, the opening. So the 790// stomion and the two commissures are taken on the lip sweep's own FRONT as constructed -- the tube's axis less its depth at that 791// point of the sweep (sdf_lipsweep: axis cz + arch t^2, depth rz f(taper, t)), meaned over the upper and the lower lip -- while the 792// two vermilion lines, which lie on the visible skin-lip junction the renderer colours, are lifted to the surface the march finds. 793func tw_lip_front_z(base: i64, part: i64, t: i64) -> i64 { 794 let pp: *i64 = (base + O_PARTS) as *i64 795 let t2: i64 = t * t / SDF_PERMIL 796 let axis: i64 = pp[part * FA_PART_FIELDS + FA_F_CZ] + sdf_get_prim(base, part, SDF_PL_ARCH) * t2 / SDF_PERMIL 797 let depth: i64 = pp[part * FA_PART_FIELDS + FA_F_RZ] * sdf_lip_f(sdf_get_prim(base, part, SDF_PL_TAPER), t) / SDF_PERMIL 798 return axis - depth 799} 800func tw_lip_seam_z(base: i64, t: i64) -> i64 { return (tw_lip_front_z(base, FA_LIP_U, t) + tw_lip_front_z(base, FA_LIP_L, t)) / TW_HALF } 801// the five mouth landmarks of the face CURRENTLY BUILT in base, in deci-pixels of the frame sdf_render draws at that yaw, with the 802// stated and the projected ratio and the normalising span. Returns 1 when every point projected and the eyes state their canthi 803func tw_lip_truth(base: i64, yaw: i64, lt: *i64) -> i64 { 804 var k: i64 = 0 805 while k < TW_LT_N { lt[k] = 0; k = k + 1 } 806 let ls: *i64 = sys_mmap(FA_LS_O_FIELDS * TW_I64 + FA_LIP_SLACK) as *i64 807 fa_lip_stated(base, ls) 808 lt[TW_LT_RATIO_STATED] = ls[FA_LS_O_RATIO] 809 let pp: *i64 = (base + O_PARTS) as *i64 810 let cx: i64 = pp[FA_LIP_U * FA_PART_FIELDS + FA_F_CX] 811 let hw: i64 = ls[FA_LS_O_CHEILION_X] 812 let p2: *i64 = sys_mmap(TW_XY * TW_I64) as *i64 813 let zmid: i64 = tw_lip_seam_z(base, 0) 814 let zcorner: i64 = tw_lip_seam_z(base, SDF_PERMIL) 815 var r: i64 = tw_lip_point(base, cx, ls[FA_LS_O_LS], FA_LIP_NOSURF, yaw, lt, TW_LT_LS_X, p2) 816 if r == TW_LT_R_OK { r = tw_lip_point(base, cx, ls[FA_LS_O_STOMION], zmid, yaw, lt, TW_LT_STO_X, p2) } 817 if r == TW_LT_R_OK { r = tw_lip_point(base, cx, ls[FA_LS_O_LI], FA_LIP_NOSURF, yaw, lt, TW_LT_LI_X, p2) } 818 if r == TW_LT_R_OK { r = tw_lip_point(base, cx - hw, ls[FA_LS_O_CHEILION_Y], zcorner, yaw, lt, TW_LT_CHR_X, p2) } 819 if r == TW_LT_R_OK { r = tw_lip_point(base, cx + hw, ls[FA_LS_O_CHEILION_Y], zcorner, yaw, lt, TW_LT_CHL_X, p2) } 820 lt[TW_LT_REASON] = r 821 if r != TW_LT_R_OK { return 0 } 822 let up: i64 = lt[TW_LT_STO_X + 1] - lt[TW_LT_LS_X + 1] 823 let lo: i64 = lt[TW_LT_LI_X + 1] - lt[TW_LT_STO_X + 1] 824 lt[TW_LT_RATIO_PIXEL] = up * PM_PERMIL / pm_max(lo, 1) 825 let t: *i64 = sys_mmap(TW_T_N * TW_I64) as *i64 826 if tw_stated_landmarks(base, yaw, t) == 0 { lt[TW_LT_REASON] = TW_LT_R_NO_EYES; return 0 } 827 lt[TW_LT_EXO] = t[TW_T_EXO] 828 lt[TW_LT_OK] = 1 829 return 1 830} 831// the ruler's five mouth landmarks (PM_R_LS_X .. PM_R_CHL_Y, the truth's order) against the truth: per landmark the SIGNED error in 832// deci-pixels (ruler minus truth, +x image right, +y image down) and the NME in permil of the truth's outer-canthal span; the ratio 833// pair beside it. Returns the worst NME 834func tw_lip_judge(res: *i64, lt: *i64, j: *i64) -> i64 { 835 var k: i64 = 0 836 while k < TW_LJ_N { j[k] = 0; k = k + 1 } 837 var nmax: i64 = 0 838 var nsum: i64 = 0 839 k = 0 840 while k < TW_LIP_LANDMARKS { 841 let rx: i64 = res[PM_R_LS_X + k * TW_XY] * TW_DECI 842 let ry: i64 = res[PM_R_LS_X + k * TW_XY + 1] * TW_DECI 843 let tx: i64 = lt[TW_LT_LS_X + k * TW_XY] 844 let ty: i64 = lt[TW_LT_LS_X + k * TW_XY + 1] 845 let nme: i64 = pm_dist(rx, ry, tx, ty) * PM_PERMIL / pm_max(lt[TW_LT_EXO], 1) 846 j[k * TW_J_FIELDS + TW_J_DX] = rx - tx 847 j[k * TW_J_FIELDS + TW_J_DY] = ry - ty 848 j[k * TW_J_FIELDS + TW_J_NME] = nme 849 if nme > nmax { nmax = nme } 850 nsum = nsum + nme 851 k = k + 1 852 } 853 j[TW_LJ_NMAX] = nmax 854 j[TW_LJ_NMEAN] = nsum / TW_LIP_LANDMARKS 855 j[TW_LJ_RATIO_RULER] = res[PM_R_LIP] 856 j[TW_LJ_RATIO_TRUTH] = lt[TW_LT_RATIO_PIXEL] 857 return nmax 858} 859func tw_print_lip_truth(lt: *i64) -> i64 { 860 ri_puts(" lip_truth=" as *u8); ri_puts(tw_lip_reason_name(lt[TW_LT_REASON])) 861 var k: i64 = 0 862 while k < TW_LIP_LANDMARKS { 863 ri_puts(" " as *u8); ri_puts(tw_lip_landmark_name(k)); tw_kv("_deci=" as *u8, lt[TW_LT_LS_X + k * TW_XY]); tw_kv("," as *u8, lt[TW_LT_LS_X + k * TW_XY + 1]) 864 let m: i64 = TW_LT_MODEL + k * TW_LT_MODEL_FIELDS 865 tw_kv("_model=" as *u8, lt[m]); tw_kv("," as *u8, lt[m + 1]); tw_kv("," as *u8, lt[m + 2]) 866 k = k + 1 867 } 868 tw_kv(" lip_ratio_stated=" as *u8, lt[TW_LT_RATIO_STATED]); tw_kv(" lip_ratio_pixel=" as *u8, lt[TW_LT_RATIO_PIXEL]); tw_kv(" lip_exo_deci=" as *u8, lt[TW_LT_EXO]) 869 return 0 870}