nx_skullskin_gate.nx source
↩ module page · 500 lines · 33129 B
1// nx_skullskin_gate.nx -- gate for the skull-bound tissue-depth skin (aesthetictwin AT46, 2026-09-18).
2//
3// ACCEPT RULE, PRE-DECLARED BEFORE THE FIRST RENDER (the coordinator's rule, with the two bars this lane could measure):
4// (i) the photo ruler (nx_photomark_lib, the promoted ruler's own library, in-process) must FIND a face on the skin render
5// with BOTH canthi of BOTH eyes (the baseline diora_fit_close.png reads UNMEASURED: no eye pair, measured 2026-09-18);
6// (ii) the scale-free axes the ruler measures are PRINTED (canthal tilt per side, intercanthal_index, intercanthal_eq_fissure)
7// and the intercanthal index must sit inside the canon band knowledge/facecanon.conf declares (intercanthal_index
8// 380..420). The NME against knowledge/refintake/diora_baird.oracle is NOT measurable in-lane: the oracle carries rows
9// for the reference stills only and a render gets an oracle row only from the outside oracle (MediaPipe on the laptop);
10// (iii) the skin's midline profile extrema sit at the canon's thirds: the glabella-to-subnasale span equals the subnasale-
11// to-menton span within the tissue-depth table's own spread (max voting depth minus min voting depth);
12// (iv) no skin vertex penetrates the skull: the depth field is positive at every vertex; min/median/max are printed;
13// (v) neg-control-zero-depth-field: every depth zero -> the skin equals the skull (max displacement 0);
14// neg-control-doubled-cheek-fat: doubling one compartment moves ONLY vertices inside its 3-sigma support (partition
15// printed and summed). Values are emitted with gv_kv/gv_check_eq/gv_check_near for independent adjudication.
16// The arithmetic teeth run on a synthetic sphere fixture with PLANTED anchors (closed-form expectations computed here, not
17// by the library); the anatomy teeth run on the real generated skull (a precondition: supplied as argv[1], else
18// ./nx_skullsdf.elf is run from the estate root, ~130 s at 2 mm cells; absent -> those teeth SKIP, never acquit).
19// Fixtures under /tmp/nx_skullskin_gate/. license_tier: ORIGINAL No hw writes (Rule 26).
20import "nx_syscalls.nx"
21import "nx_gate_verdict.nx"
22import "nx_tissuefield_lib.nx"
23import "nx_photomark_lib.nx"
24
25const SKG_DIR: *u8 = "/tmp/nx_skullskin_gate"
26const SKG_SPHERE: *u8 = "/tmp/nx_skullskin_gate/sphere.nxmesh"
27const SKG_SKULL: *u8 = "/tmp/nx_skullskin_gate/skull.nxmesh"
28// A checked-in skull fixture is the deterministic oracle for served and concurrent qualification.
29// The old first-choice /tmp path let overlapping runs overwrite one another and could trigger a fresh,
30// time-dependent SDF generation. Runtime scratch remains a fallback for explicitly missing fixtures.
31const SKG_SKULL_REPO: *u8 = "knowledge/gates/nx_skullskin_gate.skull.nxmesh"
32const SKG_SKULL_ROOT: *u8 = "buildroot/knowledge/gates/nx_skullskin_gate.skull.nxmesh"
33const SKG_SKIN: *u8 = "/tmp/nx_skullskin_gate/skin.nxmesh"
34const SKG_RENDER: *u8 = "/tmp/nx_skullskin_gate/skin_close.png"
35const SKG_CONF: *u8 = "knowledge/tissue_depth.conf"
36const SKG_CONF_PARENT: *u8 = "../knowledge/tissue_depth.conf"
37const SKG_CANON: *u8 = "knowledge/facecanon.conf"
38const SKG_CANON_PARENT: *u8 = "../knowledge/facecanon.conf"
39const SKG_SKULL_PARENT: *u8 = "../buildroot/knowledge/gates/nx_skullskin_gate.skull.nxmesh"
40const SKG_MODE755: i64 = 493
41const SKG_I64: i64 = 8
42const SKG_W: i64 = 512
43const SKG_H: i64 = 384
44const SKG_SEX_FEMALE_LEANING: i64 = 300
45const SKG_ROBUST: i64 = 500
46const SKG_CELL_UM: i64 = 2000
47const SKG_SPHERE_R: i64 = 800 // an 80 mm sphere: skull-sized, so the kernel widths are in the real range
48const SKG_RINGS: i64 = 24
49const SKG_SEGS: i64 = 32
50const SKG_MIN_ROWS: i64 = 20
51const SKG_EXP_TOL: i64 = 4 // Q16 units: the series truncation of two products
52const SKG_PLANT_D0: i64 = 50
53const SKG_PLANT_D1: i64 = 80
54const SKG_PLANT_D2: i64 = 120
55const SKG_FAT_MAG: i64 = 30
56const SKG_FAT_RADIUS: i64 = 200
57const SKG_PIPE: i64 = 124
58const SKG_NL: i64 = 10
59const SKG_RGB_BPP: i64 = 3
60const SKG_BYTE: i64 = 255
61const SKG_YAW_FRONTAL: i64 = 0
62
63func skg_pow2(a: i64) -> i64 { return a * a }
64// a UV sphere as a mesh record (unique vertices, radial normals) -- the synthetic skull the arithmetic teeth run on
65func skg_sphere(M: *i64, r: i64) -> i64 {
66 let rings: i64 = SKG_RINGS
67 let segs: i64 = SKG_SEGS
68 let nv: i64 = (rings + 1) * segs
69 let nt: i64 = rings * segs * 2
70 let vx: *i64 = sys_mmap(nv * SKG_I64) as *i64; let vy: *i64 = sys_mmap(nv * SKG_I64) as *i64; let vz: *i64 = sys_mmap(nv * SKG_I64) as *i64
71 let nx: *i64 = sys_mmap(nv * SKG_I64) as *i64; let ny: *i64 = sys_mmap(nv * SKG_I64) as *i64; let nz: *i64 = sys_mmap(nv * SKG_I64) as *i64
72 let ta: *i64 = sys_mmap(nt * SKG_I64) as *i64; let tb: *i64 = sys_mmap(nt * SKG_I64) as *i64; let tc: *i64 = sys_mmap(nt * SKG_I64) as *i64
73 var i: i64 = 0
74 while i <= rings {
75 let th: i64 = IT_PI * i / rings
76 var j: i64 = 0
77 while j < segs {
78 let ph: i64 = 2 * IT_PI * j / segs
79 let sr: i64 = it_sin4096(th)
80 let ux: i64 = sr * it_cos4096(ph) / IT_FX
81 let uz: i64 = sr * it_sin4096(ph) / IT_FX
82 let uy: i64 = it_cos4096(th)
83 let k: i64 = i * segs + j
84 vx[k] = ux * r / IT_FX; vy[k] = uy * r / IT_FX; vz[k] = uz * r / IT_FX
85 nx[k] = ux; ny[k] = uy; nz[k] = uz
86 j = j + 1
87 }
88 i = i + 1
89 }
90 var t: i64 = 0
91 i = 0
92 while i < rings {
93 var j: i64 = 0
94 while j < segs {
95 let j1: i64 = (j + 1) % segs
96 let a: i64 = i * segs + j; let b: i64 = i * segs + j1; let c: i64 = (i + 1) * segs + j1; let d: i64 = (i + 1) * segs + j
97 ta[t] = a; tb[t] = b; tc[t] = c; t = t + 1
98 ta[t] = a; tb[t] = c; tc[t] = d; t = t + 1
99 j = j + 1
100 }
101 i = i + 1
102 }
103 M[TFL_M_NV] = nv; M[TFL_M_NT] = nt
104 M[TFL_M_VX] = vx as i64; M[TFL_M_VY] = vy as i64; M[TFL_M_VZ] = vz as i64
105 M[TFL_M_NX] = nx as i64; M[TFL_M_NY] = ny as i64; M[TFL_M_NZ] = nz as i64
106 M[TFL_M_TA] = ta as i64; M[TFL_M_TB] = tb as i64; M[TFL_M_TC] = tc as i64
107 tfl_mesh_bbox_refresh(M)
108 M[TFL_M_EDGE] = 2 * IT_PI * r / segs / IT_FX
109 return nv
110}
111// plant a voting row anchored at sphere vertex v with depth d (state located, anchor = the vertex itself)
112func skg_plant(T: *i64, M: *i64, name: *u8, v: i64, d: i64) -> i64 {
113 let i: i64 = T[TFL_T_NROWS]
114 let r: *i64 = tfl_row(T, i)
115 let vx: *i64 = M[TFL_M_VX] as *i64; let vy: *i64 = M[TFL_M_VY] as *i64; let vz: *i64 = M[TFL_M_VZ] as *i64
116 let src: *u8 = "planted" as *u8
117 let note: *u8 = "" as *u8
118 r[TFL_R_NAME] = name as i64; r[TFL_R_DEPTH] = d; r[TFL_R_SRC] = src as i64; r[TFL_R_NOTE] = note as i64
119 r[TFL_R_X] = vx[v]; r[TFL_R_Y] = vy[v]; r[TFL_R_Z] = vz[v]
120 r[TFL_R_AX] = vx[v]; r[TFL_R_AY] = vy[v]; r[TFL_R_AZ] = vz[v]; r[TFL_R_AIDX] = v; r[TFL_R_ADIST] = 0
121 r[TFL_R_STATE] = TFL_ST_LOCATED; r[TFL_R_PENDING] = 0
122 T[TFL_T_NROWS] = i + 1
123 return i
124}
125// the canon band of one facecanon row: out[0]=target out[1]=lo out[2]=hi; 1 found, 0 absent
126func skg_canon(id: *u8, out: *i64) -> i64 {
127 let lp: *i64 = sys_mmap(2 * SKG_I64) as *i64
128 lp[0] = 0
129 var b: *u8 = sys_read_file(SKG_CANON, lp)
130 if (b as i64) == 0 { lp[0] = 0; b = sys_read_file(SKG_CANON_PARENT, lp) }
131 if (b as i64) == 0 { return 0 }
132 let n: i64 = lp[0]
133 let f: *i64 = sys_mmap(16 * SKG_I64) as *i64
134 var i: i64 = 0
135 while i < n {
136 var e: i64 = i
137 var going: i64 = 1
138 while going == 1 { if e >= n { going = 0 } else { if b[e] == (SKG_NL as u8) { going = 0 } else { e = e + 1 } } }
139 if e < n { b[e] = 0 as u8 }
140 let line: *u8 = ((b as i64) + i) as *u8
141 if line[0] == (99 as u8) {
142 let nf: i64 = tfl_split(line, f, 16)
143 if nf >= 7 { if tfl_streq(f[1] as *u8, id) == 1 {
144 out[0] = tfl_atoi(f[4] as *u8); out[1] = tfl_atoi(f[5] as *u8); out[2] = tfl_atoi(f[6] as *u8)
145 return 1
146 } }
147 }
148 i = e + 1
149 }
150 return 0
151}
152// the skin's midline profile landmarks, each the extremum of the skin's own profile inside a window the skull's landmarks
153// bound: glabella = the most anterior band within a quarter nasal height of the skull's glabella; pronasale = the most
154// anterior band between the canon subnasale and the glabella; subnasale = the deepest band between the pronasale and the
155// stomion; menton = the lowest band. out[0..3] = their y; 1 ok, 0 refused
156func skg_skin_thirds(S: *i64, T: *i64, out: *i64) -> i64 {
157 let g: *i64 = sys_mmap(3 * SKG_I64) as *i64
158 let sn: *i64 = sys_mmap(3 * SKG_I64) as *i64
159 let st: *i64 = sys_mmap(3 * SKG_I64) as *i64
160 if tfl_get(T, "glabella" as *u8, g) == 0 { return 0 }
161 if tfl_get(T, "subnasale_canon" as *u8, sn) == 0 { return 0 }
162 if tfl_get(T, "stomion" as *u8, st) == 0 { return 0 }
163 let edge: i64 = S[TFL_M_EDGE]
164 let cap: i64 = (S[TFL_M_Y1] - S[TFL_M_Y0]) / edge + 2
165 let prof: *i64 = sys_mmap(cap * SKG_I64) as *i64
166 let pidx: *i64 = sys_mmap(cap * SKG_I64) as *i64
167 let nb: i64 = tfl_profile(S, prof, pidx, cap)
168 let vy: *i64 = S[TFL_M_VY] as *i64
169 let quarter: i64 = (g[1] - sn[1]) / 4
170 var gla: i64 = 0 - 1; var gz: i64 = TFL_ZNONE
171 var pro: i64 = 0 - 1; var pz: i64 = TFL_ZNONE
172 var snb: i64 = 0 - 1; var sz: i64 = TFL_FAR
173 var bot: i64 = 0 - 1
174 var b: i64 = 0
175 while b < nb {
176 if prof[b] > TFL_ZNONE {
177 let y: i64 = vy[pidx[b]]
178 if bot < 0 { bot = b }
179 if y >= g[1] - quarter { if y <= g[1] + quarter { if prof[b] > gz { gz = prof[b]; gla = b } } }
180 if y >= sn[1] { if y <= g[1] - quarter { if prof[b] > pz { pz = prof[b]; pro = b } } }
181 }
182 b = b + 1
183 }
184 if gla < 0 { return 0 }
185 if pro < 0 { return 0 }
186 b = 0
187 while b < nb {
188 if prof[b] > TFL_ZNONE {
189 let y: i64 = vy[pidx[b]]
190 if y >= st[1] { if y < vy[pidx[pro]] { if prof[b] < sz { sz = prof[b]; snb = b } } }
191 }
192 b = b + 1
193 }
194 if snb < 0 { return 0 }
195 if bot < 0 { return 0 }
196 out[0] = vy[pidx[gla]]; out[1] = vy[pidx[pro]]; out[2] = vy[pidx[snb]]; out[3] = vy[pidx[bot]]
197 return 1
198}
199func skg_fb_to_rgb(fb: *i64, n: i64, rgb: *u8) -> i64 {
200 var i: i64 = 0
201 while i < n {
202 let c: i64 = fb[i]
203 rgb[i * SKG_RGB_BPP] = (c & SKG_BYTE) as u8
204 rgb[i * SKG_RGB_BPP + 1] = ((c >> 8) & SKG_BYTE) as u8
205 rgb[i * SKG_RGB_BPP + 2] = ((c >> 16) & SKG_BYTE) as u8
206 i = i + 1
207 }
208 return n
209}
210func main(argc: i64, argv: *i64) -> i64 {
211 gv_head("nx_skullskin_gate: the skin as an offset of the generated skull by the forensic tissue-depth field -- kernel arithmetic, controls, anatomy, ruler" as *u8)
212 let ctr: *i64 = gv_ctr()
213 let t0: i64 = sys_now_ms()
214 sys_mkdir(SKG_DIR, SKG_MODE755)
215 // ---- the conf ----
216 let T: *i64 = tfl_tab_new()
217 var loaded: i64 = tfl_conf_load(SKG_CONF, T)
218 if loaded == 0 { loaded = tfl_conf_load(SKG_CONF_PARENT, T) }
219 gv_need("tissue-depth-conf-readable" as *u8, loaded, ctr)
220 if loaded == 1 {
221 gv_check("conf-carries-at-least-twenty-depth-rows" as *u8, (T[TFL_T_NROWS] >= SKG_MIN_ROWS) as i64, ctr)
222 gv_check_eq("conf-has-no-malformed-rows" as *u8, T[TFL_T_MALFORMED], 0, ctr)
223 gv_check("conf-carries-fat-compartments-and-patch-rows" as *u8, ((T[TFL_T_NFAT] > 0) as i64) * ((T[TFL_T_NPATCH] > 0) as i64), ctr)
224 }
225 // ---- the kernel ----
226 let q: i64 = TFL_Q16
227 gv_check_eq("kernel-exp-of-zero-is-one" as *u8, tfl_exp_q16(0), q, ctr)
228 let e1: i64 = tfl_exp_q16(q)
229 let e2: i64 = tfl_exp_q16(2 * q)
230 gv_check_near("kernel-exp-product-rule-exp1-squared-is-exp2" as *u8, e1 * e1 / q, e2, SKG_EXP_TOL, ctr)
231 gv_check("kernel-exp-is-monotone-decreasing" as *u8, ((tfl_exp_q16(q / 2) > e1) as i64) * ((e1 > e2) as i64), ctr)
232 gv_check_eq("kernel-gaussian-at-zero-distance-is-one" as *u8, tfl_gauss_q16(0, 100), q, ctr)
233 gv_check_eq("kernel-gaussian-at-one-sigma-is-exp-minus-half" as *u8, tfl_gauss_q16(100 * 100, 100), tfl_exp_q16(q / 2), ctr)
234 // ---- the sphere fixture: write, reload (welds the soup back), plant anchors ----
235 let M0: *i64 = tfl_mesh_new()
236 let nv0: i64 = skg_sphere(M0, SKG_SPHERE_R)
237 let wb: i64 = tfl_mesh_write(M0, SKG_SPHERE, 500, 500, 500)
238 gv_need("fixture-sphere-written" as *u8, (wb > 0) as i64, ctr)
239 let M: *i64 = tfl_mesh_new()
240 let ok: i64 = tfl_mesh_load(SKG_SPHERE, M)
241 gv_need("fixture-sphere-reloads" as *u8, ok, ctr)
242 if ok == 1 {
243 // the UV sphere carries SKG_SEGS coincident vertices at each pole; welding keeps one per pole
244 gv_check_eq("welding-recovers-the-unique-vertex-count" as *u8, M[TFL_M_NV], (SKG_RINGS - 1) * SKG_SEGS + 2, ctr)
245 gv_check_eq("welding-reads-three-soup-vertices-per-triangle" as *u8, M[TFL_M_RAW], 3 * M[TFL_M_NT], ctr)
246 // planted anchors found BY GEOMETRY on the welded mesh (welding renumbers): the equator vertices nearest to the
247 // analytic points at segments 0, 1 and 4, so the spacings are the sphere's own chords
248 let near: *i64 = sys_mmap(2 * SKG_I64) as *i64
249 let va: i64 = tfl_nearest(M, SKG_SPHERE_R, 0, 0, near)
250 let ph1: i64 = 2 * IT_PI * 1 / SKG_SEGS
251 let vb: i64 = tfl_nearest(M, SKG_SPHERE_R * it_cos4096(ph1) / IT_FX, 0, SKG_SPHERE_R * it_sin4096(ph1) / IT_FX, near)
252 let ph4: i64 = 2 * IT_PI * 4 / SKG_SEGS
253 let vc: i64 = tfl_nearest(M, SKG_SPHERE_R * it_cos4096(ph4) / IT_FX, 0, SKG_SPHERE_R * it_sin4096(ph4) / IT_FX, near)
254 gv_check("planted-anchors-are-three-distinct-vertices" as *u8, ((va != vb) as i64) * ((vb != vc) as i64) * ((va != vc) as i64), ctr)
255 let T2: *i64 = tfl_tab_new()
256 skg_plant(T2, M, "a" as *u8, va, SKG_PLANT_D0)
257 skg_plant(T2, M, "b" as *u8, vb, SKG_PLANT_D1)
258 skg_plant(T2, M, "c" as *u8, vc, SKG_PLANT_D2)
259 let vx: *i64 = M[TFL_M_VX] as *i64; let vy: *i64 = M[TFL_M_VY] as *i64; let vz: *i64 = M[TFL_M_VZ] as *i64
260 let dab: i64 = vm_isqrt(skg_pow2(vx[va] - vx[vb]) + skg_pow2(vy[va] - vy[vb]) + skg_pow2(vz[va] - vz[vb]))
261 let dbc: i64 = vm_isqrt(skg_pow2(vx[vb] - vx[vc]) + skg_pow2(vy[vb] - vy[vc]) + skg_pow2(vz[vb] - vz[vc]))
262 let dac0: i64 = vm_isqrt(skg_pow2(vx[va] - vx[vc]) + skg_pow2(vy[va] - vy[vc]) + skg_pow2(vz[va] - vz[vc]))
263 // each anchor's nearest-other spacing, then the median of the three -- the closed form of what the library derives
264 let na: i64 = tfl_min(dab, dac0)
265 let nb: i64 = tfl_min(dab, dbc)
266 let nc: i64 = tfl_min(dac0, dbc)
267 var med: i64 = na
268 if (nb >= na) * (nb <= nc) == 1 { med = nb } else { if (nb <= na) * (nb >= nc) == 1 { med = nb } else { if (nc >= na) * (nc <= nb) == 1 { med = nc } else { if (nc <= na) * (nc >= nb) == 1 { med = nc } } } }
269 gv_check_eq("kernel-width-is-the-median-nearest-landmark-spacing" as *u8, tfl_sigma(T2), med, ctr)
270 gv_kv("planted_spacing_ab_u10" as *u8, dab); gv_kv("planted_spacing_bc_u10" as *u8, dbc); gv_kv("planted_spacing_ac_u10" as *u8, dac0)
271 let K: *i64 = sys_mmap(TFL_K_N * SKG_I64) as *i64
272 let D: *i64 = sys_mmap(M[TFL_M_NV] * SKG_I64) as *i64
273 tfl_depth_field(M, T2, D, K)
274 gv_check("field-positive-at-every-fixture-vertex" as *u8, (K[TFL_K_DMIN] >= 1) as i64, ctr)
275 // closed form at anchor c (its neighbours a,b are >= 3 spacings away, so only the prior competes):
276 // D(c) = (Q*d_c + w_prior*d_med) / (Q + w_prior) with w_prior = exp(-1/2), d_med the median of the three depths
277 let wp: i64 = tfl_exp_q16(q / 2)
278 let dmed: i64 = SKG_PLANT_D1
279 let wa: i64 = tfl_gauss_q16(skg_pow2(dbc), med) // b's weight at c, at the derived kernel width
280 let wa2: i64 = tfl_gauss_q16(skg_pow2(dac0), med) // a's weight at c (the chord, not the sum of chords)
281 let expect_c: i64 = (q * SKG_PLANT_D2 + wp * dmed + wa * SKG_PLANT_D1 + wa2 * SKG_PLANT_D0) / (q + wp + wa + wa2)
282 gv_check_near("field-at-an-anchor-matches-the-closed-form" as *u8, D[vc], expect_c, 2, ctr)
283 let S: *i64 = tfl_mesh_clone_positions(M)
284 tfl_offset(M, D, S)
285 let sx: *i64 = S[TFL_M_VX] as *i64; let sy: *i64 = S[TFL_M_VY] as *i64; let sz: *i64 = S[TFL_M_VZ] as *i64
286 let disp_c: i64 = vm_isqrt(skg_pow2(sx[vc] - vx[vc]) + skg_pow2(sy[vc] - vy[vc]) + skg_pow2(sz[vc] - vz[vc]))
287 gv_check_near("skin-offset-distance-equals-the-depth-at-the-anchor" as *u8, disp_c, D[vc], 2, ctr)
288 gv_check("skin-lies-outside-the-fixture-sphere-everywhere" as *u8, (tfl_max_displacement(M, S) >= 1) as i64, ctr)
289 // neg-control: every depth zero -> the skin IS the skull
290 let T0: *i64 = tfl_tab_new()
291 skg_plant(T0, M, "a" as *u8, va, SKG_PLANT_D0)
292 skg_plant(T0, M, "b" as *u8, vb, SKG_PLANT_D1)
293 skg_plant(T0, M, "c" as *u8, vc, SKG_PLANT_D2)
294 tfl_depths_scale(T0, 0)
295 let K0: *i64 = sys_mmap(TFL_K_N * SKG_I64) as *i64
296 let D0: *i64 = sys_mmap(M[TFL_M_NV] * SKG_I64) as *i64
297 tfl_depth_field(M, T0, D0, K0)
298 let S0: *i64 = tfl_mesh_clone_positions(M)
299 tfl_offset(M, D0, S0)
300 gv_check_eq("neg-control-zero-depth-field-skin-equals-skull" as *u8, tfl_max_displacement(M, S0), 0, ctr)
301 gv_check_eq("neg-control-zero-depth-field-max-depth-is-zero" as *u8, K0[TFL_K_DMAX], 0, ctr)
302 // neg-control: a doubled compartment moves only its support
303 let Tf: *i64 = tfl_tab_new()
304 skg_plant(Tf, M, "a" as *u8, va, SKG_PLANT_D0)
305 skg_plant(Tf, M, "b" as *u8, vb, SKG_PLANT_D1)
306 skg_plant(Tf, M, "c" as *u8, vc, SKG_PLANT_D2)
307 let fr: *i64 = tfl_fatrow(Tf, 0)
308 let vf: i64 = tfl_nearest(M, 0 - SKG_SPHERE_R, 0, 0, near) // the far side of the equator, found by geometry
309 let cheek: *u8 = "cheek" as *u8
310 let planted: *u8 = "planted" as *u8
311 let empty: *u8 = "" as *u8
312 fr[TFL_R_NAME] = cheek as i64; fr[TFL_R_DEPTH] = SKG_FAT_MAG; fr[TFL_R_SRC] = planted as i64; fr[TFL_R_NOTE] = empty as i64
313 fr[TFL_R_X] = vx[vf]; fr[TFL_R_Y] = vy[vf]; fr[TFL_R_Z] = vz[vf]; fr[TFL_R_ADIST] = SKG_FAT_RADIUS; fr[TFL_R_STATE] = TFL_ST_LOCATED
314 Tf[TFL_T_NFAT] = 1
315 let Kf: *i64 = sys_mmap(TFL_K_N * SKG_I64) as *i64
316 let Df0: *i64 = sys_mmap(M[TFL_M_NV] * SKG_I64) as *i64
317 let Df1: *i64 = sys_mmap(M[TFL_M_NV] * SKG_I64) as *i64
318 tfl_depth_field(M, Tf, Df0, Kf)
319 fr[TFL_R_DEPTH] = 2 * SKG_FAT_MAG
320 tfl_depth_field(M, Tf, Df1, Kf)
321 let part: *i64 = sys_mmap(3 * SKG_I64) as *i64
322 let total: i64 = tfl_moved_partition(M, Df0, Df1, vx[vf], vy[vf], vz[vf], SKG_FAT_RADIUS, part)
323 gv_kv("doubled_fat_moved_inside" as *u8, part[0]); gv_kv("doubled_fat_moved_outside" as *u8, part[1]); gv_kv("doubled_fat_unmoved" as *u8, part[2])
324 gv_check_eq("doubled-cheek-fat-partition-sums-to-the-vertex-count" as *u8, total, M[TFL_M_NV], ctr)
325 gv_check("doubled-cheek-fat-moves-vertices-inside-its-support" as *u8, (part[0] > 0) as i64, ctr)
326 gv_check_eq("neg-control-doubled-cheek-fat-moves-nothing-outside-its-support" as *u8, part[1], 0, ctr)
327 }
328 // ---- the real skull ----
329 // Prefer the repository fixture so served, Alpha and concurrent runs consume the same mesh.
330 // A caller-supplied mesh still wins for a deliberate controlled comparison.
331 var skullp: *u8 = SKG_SKULL_REPO
332 if argc > 1 { skullp = argv[1] as *u8 }
333 let MS: *i64 = tfl_mesh_new()
334 var have: i64 = tfl_mesh_load(skullp, MS)
335 if have == 0 && argc <= 1 { skullp = SKG_SKULL_ROOT; have = tfl_mesh_load(skullp, MS) }
336 if have == 0 && argc <= 1 { skullp = SKG_SKULL; have = tfl_mesh_load(skullp, MS) }
337 if have == 0 { skullp = SKG_SKULL_PARENT; have = tfl_mesh_load(skullp, MS) }
338 var gen_ms: i64 = 0
339 if have == 0 {
340 let tg: i64 = sys_now_ms()
341 let rc: i64 = tfl_run_skullsdf(SKG_SKULL, SKG_SEX_FEMALE_LEANING, SKG_ROBUST, SKG_CELL_UM)
342 gen_ms = sys_now_ms() - tg
343 gv_kv("skullsdf_child_rc" as *u8, rc); gv_kv("skullsdf_child_ms" as *u8, gen_ms)
344 if rc == 0 { skullp = SKG_SKULL; have = tfl_mesh_load(skullp, MS) }
345 }
346 gv_need("real-skull-mesh-available" as *u8, have, ctr)
347 if have == 1 {
348 gv_kv("skull_tris" as *u8, MS[TFL_M_NT]); gv_kv("skull_welded_verts" as *u8, MS[TFL_M_NV]); gv_kv("skull_edge_u10" as *u8, MS[TFL_M_EDGE])
349 let KS: *i64 = sys_mmap(TFL_K_N * SKG_I64) as *i64
350 let nloc: i64 = tfl_locate(MS, T, KS)
351 gv_check("landmarks-located-on-the-skull" as *u8, (nloc > 0) as i64, ctr)
352 gv_check_eq("every-conf-landmark-is-located" as *u8, KS[TFL_K_UNLOCATED], 0, ctr)
353 gv_kv("landmarks_located" as *u8, KS[TFL_K_LOCATED]); gv_kv("landmarks_fallback" as *u8, KS[TFL_K_FALLBACK]); gv_kv("landmark_voters" as *u8, KS[TFL_K_VOTERS])
354 gv_check("anchors-within-two-mesh-edges" as *u8, (KS[TFL_K_ANCHOR_MAX] <= 2 * MS[TFL_M_EDGE]) as i64, ctr)
355 gv_kv("anchor_dist_max_u10" as *u8, KS[TFL_K_ANCHOR_MAX])
356 tfl_fat_place(T)
357 let DS: *i64 = sys_mmap(MS[TFL_M_NV] * SKG_I64) as *i64
358 tfl_depth_field(MS, T, DS, KS)
359 gv_check("field-positive-at-every-skull-vertex-no-penetration" as *u8, (KS[TFL_K_DMIN] >= 1) as i64, ctr)
360 gv_kv("sigma_u10" as *u8, KS[TFL_K_SIGMA]); gv_kv("depth_min_mm10" as *u8, KS[TFL_K_DMIN]); gv_kv("depth_median_mm10" as *u8, KS[TFL_K_DMED]); gv_kv("depth_max_mm10" as *u8, KS[TFL_K_DMAX])
361 let SS: *i64 = tfl_mesh_clone_positions(MS)
362 tfl_offset(MS, DS, SS)
363 let clamped: i64 = tfl_smooth_skin(MS, SS, DS, KS[TFL_K_DMED])
364 gv_kv("smooth_clamped_off_bone" as *u8, clamped)
365 // the rendered skin = the offset surface plus its patches (eyes over the orbits, the nose over the empty aperture)
366 let skin_col: i64 = tfl_patch(T, "skin_r" as *u8) + tfl_patch(T, "skin_g" as *u8) * 256 + tfl_patch(T, "skin_b" as *u8) * 256 * 256
367 tm_reset()
368 tfl_material_apply(T)
369 let base: i64 = tm_nv()
370 tfl_push_mesh(SS, skin_col, 0)
371 tfl_paint_skin(SS, T, base, skin_col)
372 let eyes: *i64 = sys_mmap(4 * SKG_I64) as *i64
373 let nose: *i64 = sys_mmap(4 * SKG_I64) as *i64
374 tfl_push_eyes(MS, SS, T, 0, skin_col, eyes)
375 tfl_push_nose(SS, T, skin_col, nose)
376 gv_check_eq("both-eye-patches-built-on-a-sampled-orbital-rim" as *u8, eyes[0], 2, ctr)
377 gv_kv("nose_tip_x" as *u8, nose[0]); gv_kv("nose_tip_y" as *u8, nose[1]); gv_kv("nose_tip_z" as *u8, nose[2])
378 let SX: *i64 = tfl_scene_to_mesh()
379 tfl_mesh_write(SX, SKG_SKIN, 910, 890, 845)
380 // (iii) the thirds on the rendered skin's own midline, tolerance = the table's spread of voting depths
381 var dlo: i64 = TFL_FAR
382 var dhi: i64 = 0
383 var ri: i64 = 0
384 while ri < T[TFL_T_NROWS] { let r: *i64 = tfl_row(T, ri); if r[TFL_R_DEPTH] >= 0 { if r[TFL_R_DEPTH] < dlo { dlo = r[TFL_R_DEPTH] } if r[TFL_R_DEPTH] > dhi { dhi = r[TFL_R_DEPTH] } } ri = ri + 1 }
385 let tol: i64 = dhi - dlo
386 let th: *i64 = sys_mmap(4 * SKG_I64) as *i64
387 let tok: i64 = skg_skin_thirds(SX, T, th)
388 gv_check("skin-profile-has-glabella-pronasale-subnasale-menton" as *u8, tok, ctr)
389 if tok == 1 {
390 gv_kv("skin_glabella_y" as *u8, th[0]); gv_kv("skin_pronasale_y" as *u8, th[1]); gv_kv("skin_subnasale_y" as *u8, th[2]); gv_kv("skin_menton_y" as *u8, th[3])
391 // the SUBSTRATE tooth that stood here (the skin's middle third equals its lower third within the table's spread)
392 // MOVED on 2026-09-18 to nx_skull_canon_gate (anatomy AN17), where the same spans are read on the SKULL against
393 // the scanned oracle's own imbalance: the skin's thirds follow the generated skull's proportions, so a RED there
394 // is the generator's rung to close and this gate measures the skin only. The spread stays printed beside the
395 // spans so a reader can still compute the old predicate by hand.
396 gv_kv("skin_middle_third_u10" as *u8, th[0] - th[2]); gv_kv("skin_lower_third_u10" as *u8, th[2] - th[3]); gv_kv("table_spread_mm10" as *u8, tol)
397 }
398 // (i)(ii) the render through the rasteriser and the ruler in-process
399 let rc2: *i64 = sys_mmap(SKG_I64) as *i64
400 let face_c: *HaarCascade = ff_load_face(rc2)
401 let eye_c: *HaarCascade = ff_load_eye(rc2)
402 var casc: i64 = 1
403 if (face_c as i64) == 0 { casc = 0 }
404 if (eye_c as i64) == 0 { casc = 0 }
405 gv_need("photo-ruler-cascades-loaded" as *u8, casc, ctr)
406 if casc == 1 {
407 let vt: *i64 = sys_mmap(3 * SKG_I64) as *i64
408 let gn: *i64 = sys_mmap(3 * SKG_I64) as *i64
409 let epl: *i64 = sys_mmap(3 * SKG_I64) as *i64
410 let epr: *i64 = sys_mmap(3 * SKG_I64) as *i64
411 let zyl: *i64 = sys_mmap(3 * SKG_I64) as *i64
412 let zyr: *i64 = sys_mmap(3 * SKG_I64) as *i64
413 tfl_get(T, "vertex" as *u8, vt); tfl_get(T, "gnathion" as *u8, gn)
414 tfl_get(T, "eye_l" as *u8, epl); tfl_get(T, "eye_r" as *u8, epr)
415 tfl_get(T, "zygion_l" as *u8, zyl); tfl_get(T, "zygion_r" as *u8, zyr)
416 gv_kv("expected_eye_l_x_u10" as *u8, epl[0]); gv_kv("expected_eye_l_y_u10" as *u8, epl[1]); gv_kv("expected_eye_l_z_u10" as *u8, epl[2])
417 gv_kv("expected_eye_r_x_u10" as *u8, epr[0]); gv_kv("expected_eye_r_y_u10" as *u8, epr[1]); gv_kv("expected_eye_r_z_u10" as *u8, epr[2])
418 gv_kv("bizygomatic_span_u10" as *u8, tfl_abs(zyl[0] - zyr[0])); gv_kv("declared_pupil_span_permil" as *u8, tfl_patch(T, "pupil_span_permil" as *u8))
419 let head: i64 = vt[1] - gn[1] + KS[TFL_K_DMED]
420 let xc: i64 = (MS[TFL_M_X0] + MS[TFL_M_X1]) / 2
421 let nsn: *i64 = sys_mmap(3 * SKG_I64) as *i64
422 tfl_get(T, "nasion" as *u8, nsn)
423 var zf: i64 = tfl_front_at(SS, xc, nsn[1], 3)
424 if zf <= TFL_ZNONE { zf = SS[TFL_M_Z1] }
425 tfl_recentre(0 - xc, (vt[1] + gn[1]) / 2, 0 - zf)
426 let fb: *i64 = sys_mmap(SKG_W * SKG_H * SKG_I64) as *i64
427 let tr: i64 = sys_now_ms()
428 let camz: i64 = tfl_render(fb, SKG_W, SKG_H, SKG_YAW_FRONTAL, head, tfl_patch(T, "frame_head_permil" as *u8))
429 gv_kv("render_ms" as *u8, sys_now_ms() - tr); gv_kv("camz_u10" as *u8, camz); gv_kv("head_u10" as *u8, head)
430 let pb: i64 = png_publish(fb, SKG_W, SKG_H, SKG_RENDER, 1)
431 gv_check("skin-render-written" as *u8, (pb > 0) as i64, ctr)
432 let rgb: *u8 = sys_mmap(SKG_W * SKG_H * SKG_RGB_BPP)
433 skg_fb_to_rgb(fb, SKG_W * SKG_H, rgb)
434 var dark_l: i64 = 0; var dark_r: i64 = 0
435 var dark_l_x0: i64 = SKG_W; var dark_l_x1: i64 = 0; var dark_l_y0: i64 = SKG_H; var dark_l_y1: i64 = 0
436 var dark_r_x0: i64 = SKG_W; var dark_r_x1: i64 = 0; var dark_r_y0: i64 = SKG_H; var dark_r_y1: i64 = 0
437 var py: i64 = 0
438 while py < SKG_H { var px: i64 = 0; while px < SKG_W {
439 let po: i64 = (py * SKG_W + px) * SKG_RGB_BPP
440 if (rgb[po] as i64) < 40 { if (rgb[po + 1] as i64) < 40 { if (rgb[po + 2] as i64) < 40 {
441 if px < SKG_W / 2 { dark_l = dark_l + 1; if px < dark_l_x0 { dark_l_x0 = px }; if px > dark_l_x1 { dark_l_x1 = px }; if py < dark_l_y0 { dark_l_y0 = py }; if py > dark_l_y1 { dark_l_y1 = py } }
442 else { dark_r = dark_r + 1; if px < dark_r_x0 { dark_r_x0 = px }; if px > dark_r_x1 { dark_r_x1 = px }; if py < dark_r_y0 { dark_r_y0 = py }; if py > dark_r_y1 { dark_r_y1 = py } }
443 } } }
444 px = px + 1
445 } py = py + 1 }
446 gv_kv("dark_pixels_image_left" as *u8, dark_l); gv_kv("dark_pixels_image_right" as *u8, dark_r)
447 gv_kv("dark_left_x0" as *u8, dark_l_x0); gv_kv("dark_left_x1" as *u8, dark_l_x1); gv_kv("dark_right_x0" as *u8, dark_r_x0); gv_kv("dark_right_x1" as *u8, dark_r_x1)
448 let res: *i64 = sys_mmap(PM_R_N * SKG_I64) as *i64
449 var zi: i64 = 0
450 while zi < PM_R_N { res[zi] = 0; zi = zi + 1 }
451 let tm0: i64 = sys_now_ms()
452 let prc: i64 = pm_landmarks_auto(rgb, SKG_W, SKG_H, res, face_c, eye_c)
453 gv_kv("ruler_ms" as *u8, sys_now_ms() - tm0); gv_kv("ruler_rc" as *u8, prc); gv_kv("ruler_route" as *u8, res[PM_R_ROUTE])
454 gv_check("ruler-finds-a-face-on-the-skin-render" as *u8, (prc != PM_E_NOFACE) as i64, ctr)
455 gv_kv("face_box_x0" as *u8, res[PM_R_FX0]); gv_kv("face_box_y0" as *u8, res[PM_R_FY0]); gv_kv("face_box_x1" as *u8, res[PM_R_FX1]); gv_kv("face_box_y1" as *u8, res[PM_R_FY1])
456 gv_check("ruler-places-both-canthi-of-both-eyes" as *u8, ((prc != PM_E_NOEYES) as i64) * ((prc != PM_E_NOFACE) as i64) * ((res[PM_R_EYES] == 2) as i64), ctr)
457 gv_kv("eyes" as *u8, res[PM_R_EYES]); gv_kv("pair_fail" as *u8, res[PM_R_PAIR_FAIL])
458 gv_kv("canthal_tilt_R_deg10" as *u8, res[PM_R_TILT_R]); gv_kv("canthal_tilt_L_deg10" as *u8, res[PM_R_TILT_L])
459 gv_kv("eye_R_lateral_x" as *u8, res[PM_R_RLAT_X]); gv_kv("eye_R_medial_x" as *u8, res[PM_R_RMED_X]); gv_kv("eye_L_medial_x" as *u8, res[PM_R_LMED_X]); gv_kv("eye_L_lateral_x" as *u8, res[PM_R_LLAT_X])
460 gv_kv("eye_R_lateral_y" as *u8, res[PM_R_RLAT_Y]); gv_kv("eye_R_medial_y" as *u8, res[PM_R_RMED_Y]); gv_kv("eye_L_medial_y" as *u8, res[PM_R_LMED_Y]); gv_kv("eye_L_lateral_y" as *u8, res[PM_R_LLAT_Y])
461 gv_kv("eye_R_pixels" as *u8, res[PM_R_EYE_PIX_R]); gv_kv("eye_L_pixels" as *u8, res[PM_R_EYE_PIX_L]); gv_kv("eye_pinned_bits" as *u8, res[PM_R_PINNED])
462 gv_kv("eye_blob0_x0" as *u8, res[PM_R_TB]); gv_kv("eye_blob0_y0" as *u8, res[PM_R_TB + 1]); gv_kv("eye_blob0_x1" as *u8, res[PM_R_TB + 2]); gv_kv("eye_blob0_y1" as *u8, res[PM_R_TB + 3]); gv_kv("eye_blob0_cells" as *u8, res[PM_R_TB + 4])
463 gv_kv("eye_blob1_x0" as *u8, res[PM_R_TB + 5]); gv_kv("eye_blob1_y0" as *u8, res[PM_R_TB + 6]); gv_kv("eye_blob1_x1" as *u8, res[PM_R_TB + 7]); gv_kv("eye_blob1_y1" as *u8, res[PM_R_TB + 8]); gv_kv("eye_blob1_cells" as *u8, res[PM_R_TB + 9])
464 gv_kv("intercanthal_index_permil" as *u8, res[PM_R_ICI]); gv_kv("intercanthal_eq_fissure_permil" as *u8, res[PM_R_ICF])
465 let band: *i64 = sys_mmap(3 * SKG_I64) as *i64
466 let cb: i64 = skg_canon("intercanthal_index" as *u8, band)
467 gv_need("facecanon-intercanthal-band-readable" as *u8, cb, ctr)
468 if cb == 1 { if res[PM_R_EYES] == 2 {
469 // the band is widened by the RULER'S OWN measured precision (conf row ruler_canthus_nme_permil, the photomark
470 // referee's mean canthus NME as permil of the exocanthal span): endocanthal and exocanthal spans each carry two
471 // canthus errors, so the index moves by about 2 nme (1 + ici) -- a bar tighter than the ruler cannot be read
472 let nme: i64 = tfl_patch(T, "ruler_canthus_nme_permil" as *u8)
473 let ici: i64 = res[PM_R_ICI]
474 let itol: i64 = 2 * nme * (PM_PERMIL + ici) / PM_PERMIL
475 gv_check("intercanthal-index-inside-the-canon-band-within-the-ruler-precision" as *u8, ((ici >= band[1] - itol) as i64) * ((ici <= band[2] + itol) as i64), ctr)
476 gv_kv("canon_ici_lo" as *u8, band[1]); gv_kv("canon_ici_hi" as *u8, band[2]); gv_kv("ici_tolerance_from_ruler_nme" as *u8, itol)
477 } }
478 // neg-controls: the empty background frame carries no face (both routes refuse); a flat skin-coloured frame may
479 // pass the skin route's head box but can carry no eye pair
480 var fi: i64 = 0
481 while fi < SKG_W * SKG_H { fb[fi] = TFL_BG; fi = fi + 1 }
482 skg_fb_to_rgb(fb, SKG_W * SKG_H, rgb)
483 zi = 0
484 while zi < PM_R_N { res[zi] = 0; zi = zi + 1 }
485 let nrc: i64 = pm_landmarks_auto(rgb, SKG_W, SKG_H, res, face_c, eye_c)
486 gv_check_eq("neg-control-empty-background-frame-has-no-face" as *u8, nrc, PM_E_NOFACE, ctr)
487 fi = 0
488 while fi < SKG_W * SKG_H { fb[fi] = skin_col; fi = fi + 1 }
489 skg_fb_to_rgb(fb, SKG_W * SKG_H, rgb)
490 zi = 0
491 while zi < PM_R_N { res[zi] = 0; zi = zi + 1 }
492 let src: i64 = pm_landmarks_auto(rgb, SKG_W, SKG_H, res, face_c, eye_c)
493 gv_kv("blank_skin_frame_rc" as *u8, src)
494 gv_check("neg-control-blank-skin-frame-places-no-eye-pair" as *u8, ((src != PM_OK) as i64) * ((res[PM_R_EYES] != 2) as i64), ctr)
495 }
496 }
497 gv_values_head()
498 gv_kv("total_ms" as *u8, sys_now_ms() - t0); gv_kv("peak_rss_kb" as *u8, tfl_peak_rss_kb())
499 return gv_verdict("nx_skullskin_gate" as *u8, ctr, "the skin is an offset of the generated skull by a tissue-depth field whose kernel width, anchors and camera are derived from the skull, with the ruler's axes printed" as *u8)
500}