code wiki / _hdl_build / nx_facemark.nx
nx_facemark.nx source
↩ module page · 647 lines · 33115 B
1// nx_facemark.nx -- F1084 prereq 1: THE JUDGE THAT FAILS WRONG ANATOMY (beyond-metahuman program).
2// WHY THIS EXISTS (measured, 2026-07-25): the detail judge scores LOCAL NORMAL VARIANCE and cannot ask
3// whether that variance is anatomy, so a face carrying brow-flanges, a duckbill chin and no visible lips
4// scored 357 against a cleaner face's 288. Three times now a variance-style judge has ranked wrong work
5// above right work (FBM noise over a real render; a bell-shaped body over a good one; facial bumps over a
6// clean face). A judge that counts busyness will always do this. So this one scores PLACEMENT.
7//
8// THE METHOD, purely mechanistic (no learned model, no rendering, no third-party): a human face read down
9// its MIDLINE is a fixed sequence of alternating depth extrema --
10// brow(max) -> nasion(min) -> pronasale(max) -> subnasale(min) -> upper lip(max)
11// -> mouth groove(min) -> lower lip(max) -> mentolabial(min) -> pogonion(max)
12// Extract that profile straight from mesh geometry (per y-band, the most-forward vertex near the midline),
13// find its extrema by prominence, and score ours against the oracle's on WHERE the features are.
14// ANTI-SPAM IS THE POINT: the score multiplies recall (did we find the oracle's features, in the right
15// places) by PRECISION (what fraction of OUR extrema correspond to a real one). A corrugated face full of
16// bumps scores near zero because precision collapses -- which is exactly what the variance judge could not do.
17// nx_facemark profile <mesh.nxmesh> -> the midline profile + detected extrema
18// nx_facemark cmp <ours.nxmesh> <oracle.nxmesh> -> placement score + per-landmark table
19// nx_facemark selftest -> gv gate
20// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
21import "nx_gate_verdict.nx"
22import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
23
24const FM_M8388607: i64 = 8388607
25const FM_M8388608: i64 = 8388608
26const FM_HDR: i64 = 16
27const FM_LAYENT: i64 = 24
28const FM_TRI: i64 = 84
29const FM_LID: i64 = 4
30// ★seq864: THE JUDGE WAS NOT ANATOMICAL AND THE CAUSE WAS MEASURED, NOT GUESSED. On the real oracle it
31// resolved 2 landmarks where a face has 7+. The oracle's own midline profile showed why: the nose swings
32// ~100 per-mille of whole-head depth while the brow is a 901->906 rise -- a swing of FIVE -- so a fixed
33// prominence of 28 could only ever see the nose. Three fixes, all principled rather than dialled:
34// 1. FM_MIDDIV 14 -> 48: a MIDLINE profile must be a midline. Taking the most-forward vertex across a
35// swath of +/-1/14 of head width means that near the nose the profile follows the NOSE across the
36// whole swath, smearing the brow and lip signal into it. A narrow slice is the actual profile.
37// 2. FM_NB 48 -> 96: a 48-band profile puts only ~5 bands across a lip. Features need bands to exist in.
38// 3. the prominence threshold is DERIVED FROM THE PROFILE'S OWN NOISE FLOOR (median adjacent |diff|),
39// not a constant -- this lane's standing law that you measure a ruler's noise floor before trusting
40// its silence. A threshold that is a magic number is a threshold that is wrong on the next mesh.
41const FM_NB: i64 = 96
42const FM_MAXEX: i64 = 64
43const FM_PERMIL: i64 = 1000
44const FM_MM: i64 = 1000
45const FM_MIDDIV: i64 = 48
46const FM_SWING: i64 = 28
47const FM_NF_MULT: i64 = 2
48const FM_NF_FLOOR: i64 = 4
49// ★★★OFF-MIDLINE LANES (F1091/seq873). The midline judge scored the best face this program has built the
50// SAME as no face at all, because a brow ridge lives LATERALLY and its midline point is the glabella DIP --
51// its lowest part. A one-dimensional midline slice is blind to laterally-distributed anatomy BY
52// CONSTRUCTION, and worse, it had been rewarding stray buried geometry that happened to intrude into the
53// midline band. So the profile becomes a SET of parallel lanes across the face: the midline plus offsets
54// either side, each yielding its own landmark sequence and its own score. Cheekbones, the brow tails, the
55// jaw line and the nasolabial fold all live in these lanes and were previously unmeasurable.
56const FM_LANES: i64 = 5
57const FM_LANESTEP: i64 = 10
58const FM_LANEMID: i64 = 2
59const FM_TOL: i64 = 55
60const FM_SCRATCH: i64 = 4096
61const FM_STATB: i64 = 160
62// coverage floor: below this fraction of bands carrying REAL surface, a placement score would be measuring
63// the representation rather than the face, so the judge refuses to emit one (nx_capaxes grounding-0 rule)
64const FM_COV_FLOOR: i64 = 500
65
66func fm_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1, s, n); return 0 }
67// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
68// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
69// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
70// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
71func fm_pn(v: i64) -> i64 { nxi_out(v); return 0 }
72func fm_rd32(b: *u8, o: i64) -> i64 {
73 return (b[o] as i64) | ((b[o+1] as i64)<<8) | ((b[o+2] as i64)<<16) | ((b[o+3] as i64)<<24)
74}
75func fm_f32mul(b: *u8, o: i64, mul: i64) -> i64 {
76 let bits: i64 = fm_rd32(b, o)
77 let sign: i64 = (bits>>31) & 1
78 let exp: i64 = (bits>>23) & 255
79 let mant: i64 = bits & FM_M8388607
80 if exp == 0 { return 0 }
81 let m: i64 = (mant | FM_M8388608) * mul
82 var e: i64 = exp - 127 - 23
83 var v: i64 = 0
84 if e >= 0 { v = m << e } else { let sh: i64 = 0 - e; v = (m + (1 << (sh-1))) >> sh }
85 if sign == 1 { v = 0 - v }
86 return v
87}
88func fm_streq(a: *u8, b: *u8) -> i64 {
89 var i: i64=0; var go: i64=1; var eq: i64=1
90 while go==1 { if a[i]!=b[i] { eq=0; go=0 } else { if a[i]==(0 as u8) { go=0 } else { i=i+1 } } }
91 return eq
92}
93// ---- THE PROFILE: per y-band, the most-forward (max z) vertex near the midline. Pure geometry.
94// prof[] is filled with z in per-mille of the mesh's own depth span; -1 marks a band with no midline
95// surface (an honest hole, never a fabricated zero -- the GX-35 lesson).
96// Returns 1 ok, 0 refused.
97// ADD A VERB, NEVER WIDEN: fm_profile keeps its exact signature and delegates to the midline lane, so every
98// existing caller and every gate tooth is untouched. lane 0..FM_LANES-1, FM_LANEMID = the midline.
99func fm_profile_lane(path: *u8, prof: *i64, lane: i64) -> i64 {
100 let szp: *i64 = sys_mmap(16) as *i64
101 let mb: *u8 = sys_read_file(path, szp)
102 if (mb as i64) == 0 { return 0 }
103 let sz: i64 = szp[0]
104 if sz < FM_HDR { return 0 }
105 if mb[0]!=(78 as u8) { return 0 }
106 if mb[1]!=(88 as u8) { return 0 }
107 let nlay: i64 = fm_rd32(mb, 8)
108 let nt: i64 = fm_rd32(mb, 12)
109 if nlay <= 0 { return 0 }
110 if nt <= 0 { return 0 }
111 let hdr: i64 = FM_HDR + nlay*FM_LAYENT
112 if hdr + nt*FM_TRI + nt*FM_LID > sz { return 0 }
113 // pass 1: AABB over x, y, z
114 var xmn: i64 = 0; var xmx: i64 = 0; var ymn: i64 = 0
115 var ymx: i64 = 0; var zmn: i64 = 0; var zmx: i64 = 0
116 var first: i64 = 1
117 var t: i64 = 0
118 while t < nt {
119 let o: i64 = hdr + t*FM_TRI
120 var k: i64 = 0
121 while k < 3 {
122 let x: i64 = fm_f32mul(mb, o + k*12 + 0, FM_MM)
123 let y: i64 = fm_f32mul(mb, o + k*12 + 4, FM_MM)
124 let z: i64 = fm_f32mul(mb, o + k*12 + 8, FM_MM)
125 if first == 1 { xmn=x; xmx=x; ymn=y; ymx=y; zmn=z; zmx=z; first=0 } else {
126 if x<xmn { xmn=x } if x>xmx { xmx=x }
127 if y<ymn { ymn=y } if y>ymx { ymx=y }
128 if z<zmn { zmn=z } if z>zmx { zmx=z }
129 }
130 k = k + 1
131 }
132 t = t + 1
133 }
134 let yspan: i64 = ymx - ymn
135 let zspan: i64 = zmx - zmn
136 if yspan <= 0 { return 0 }
137 if zspan <= 0 { return 0 }
138 // the lane's sampling centre: the midline shifted laterally by (lane - mid) steps of the face width
139 let xspan: i64 = xmx - xmn
140 let xc: i64 = (xmn + xmx) / 2 + (lane - FM_LANEMID) * xspan / FM_LANESTEP
141 var halfw: i64 = xspan / FM_MIDDIV
142 if halfw < 1 { halfw = 1 }
143 var i: i64 = 0
144 while i < FM_NB { prof[i] = 0 - 1; i = i + 1 }
145 // pass 2: per band, keep the max z among near-midline verts
146 var t2: i64 = 0
147 while t2 < nt {
148 let o2: i64 = hdr + t2*FM_TRI
149 var k2: i64 = 0
150 while k2 < 3 {
151 let x2: i64 = fm_f32mul(mb, o2 + k2*12 + 0, FM_MM)
152 var dx: i64 = x2 - xc
153 if dx < 0 { dx = 0 - dx }
154 if dx <= halfw {
155 let y2: i64 = fm_f32mul(mb, o2 + k2*12 + 4, FM_MM)
156 let z2: i64 = fm_f32mul(mb, o2 + k2*12 + 8, FM_MM)
157 var bnd: i64 = (y2 - ymn) * FM_NB / yspan
158 if bnd < 0 { bnd = 0 }
159 if bnd >= FM_NB { bnd = FM_NB - 1 }
160 let zp: i64 = (z2 - zmn) * FM_PERMIL / zspan
161 if prof[bnd] < zp { prof[bnd] = zp }
162 }
163 k2 = k2 + 1
164 }
165 t2 = t2 + 1
166 }
167 return 1
168}
169func fm_profile(path: *u8, prof: *i64) -> i64 {
170 return fm_profile_lane(path, prof, FM_LANEMID)
171}
172// ---- EXTREMA by prominence. Walk the profile tracking direction; when it reverses and the swing since
173// the last recorded extremum exceeds FM_SWING, record one. ex[] gets band index, ty[] gets 1 max / 0 min.
174// Returns the count. A gentle curve yields none; a real face yields the alternating landmark sequence.
175// ★THE NOISE FLOOR, MEASURED FROM THE PROFILE ITSELF: the MEDIAN adjacent-band |diff|. Median not mean,
176// because a nose is a genuine outlier and a mean would be dragged up by it until it hid every smaller
177// landmark -- the signal must never set the noise estimate. Returns the prominence a swing must beat.
178func fm_noisefloor(prof: *i64) -> i64 {
179 let d: *i64 = sys_mmap(FM_SCRATCH) as *i64
180 var n: i64 = 0
181 var prev: i64 = 0 - 1
182 var i: i64 = 0
183 while i < FM_NB {
184 if prof[i] >= 0 {
185 if prev >= 0 {
186 var v: i64 = prof[i] - prev
187 if v < 0 { v = 0 - v }
188 if n < FM_MAXEX { d[n] = v; n = n + 1 }
189 }
190 prev = prof[i]
191 }
192 i = i + 1
193 }
194 if n == 0 { return FM_NF_FLOOR }
195 var a: i64 = 1
196 while a < n {
197 let key: i64 = d[a]
198 var b: i64 = a - 1
199 var go: i64 = 1
200 while go == 1 {
201 if b < 0 { go = 0 } else {
202 if d[b] > key { d[b+1] = d[b]; b = b - 1 } else { go = 0 }
203 }
204 }
205 d[b+1] = key
206 a = a + 1
207 }
208 var th: i64 = d[n/2] * FM_NF_MULT
209 if th < FM_NF_FLOOR { th = FM_NF_FLOOR }
210 return th
211}
212func fm_extrema(prof: *i64, ex: *i64, ty: *i64) -> i64 {
213 let swingth: i64 = fm_noisefloor(prof)
214 var n: i64 = 0
215 var lastI: i64 = 0 - 1
216 var lastV: i64 = 0
217 var dir: i64 = 0
218 var pI: i64 = 0 - 1
219 var pV: i64 = 0
220 var i: i64 = 0
221 while i < FM_NB {
222 let v: i64 = prof[i]
223 if v >= 0 {
224 if pI < 0 { pI = i; pV = v; lastI = i; lastV = v } else {
225 var d: i64 = 0
226 if v > pV { d = 1 }
227 if v < pV { d = 0 - 1 }
228 if d != 0 {
229 if dir == 0 { dir = d } else {
230 if d != dir {
231 // direction reversed at pI: pI is a candidate extremum
232 var swing: i64 = pV - lastV
233 if swing < 0 { swing = 0 - swing }
234 if swing >= swingth {
235 if n < FM_MAXEX {
236 ex[n] = pI
237 if dir > 0 { ty[n] = 1 } else { ty[n] = 0 }
238 n = n + 1
239 lastI = pI; lastV = pV
240 }
241 }
242 dir = d
243 }
244 }
245 }
246 pI = i; pV = v
247 }
248 }
249 i = i + 1
250 }
251 return n
252}
253// ---- THE SCORE. recall = for each ORACLE extremum, did we place a same-type one nearby (credit decays
254// with distance); precision = what fraction of OURS matched something real. score = recall*precision/1000.
255// ★THE ANTI-GOODHART PROPERTY: spraying extra features cannot help, because every unmatched extremum of
256// ours cuts precision. This is precisely the tooth the variance judge lacked.
257// out[0]=score out[1]=recall out[2]=precision out[3]=n_ours out[4]=n_oracle out[5]=matched
258func fm_score(exA: *i64, tyA: *i64, nA: i64, exB: *i64, tyB: *i64, nB: i64, out: *i64) -> i64 {
259 out[0]=0; out[1]=0; out[2]=0; out[3]=nA; out[4]=nB; out[5]=0
260 if nB <= 0 { return 0 }
261 let usedA: *i64 = sys_mmap(FM_SCRATCH) as *i64
262 var z: i64 = 0
263 while z < FM_MAXEX { usedA[z] = 0; z = z + 1 }
264 var credit: i64 = 0
265 var matched: i64 = 0
266 var b: i64 = 0
267 while b < nB {
268 var bestJ: i64 = 0 - 1
269 var bestD: i64 = FM_TOL + 1
270 var a: i64 = 0
271 while a < nA {
272 if usedA[a] == 0 { if tyA[a] == tyB[b] {
273 var d: i64 = exA[a] - exB[b]
274 if d < 0 { d = 0 - d }
275 let dp: i64 = d * FM_PERMIL / FM_NB
276 if dp < bestD { bestD = dp; bestJ = a }
277 } }
278 a = a + 1
279 }
280 if bestJ >= 0 { if bestD <= FM_TOL {
281 usedA[bestJ] = 1
282 matched = matched + 1
283 credit = credit + (FM_PERMIL - bestD*FM_PERMIL/FM_TOL)
284 } }
285 b = b + 1
286 }
287 let recall: i64 = credit / nB
288 var prec: i64 = 0
289 if nA > 0 { prec = matched * FM_PERMIL / nA }
290 out[0] = recall * prec / FM_PERMIL
291 out[1] = recall
292 out[2] = prec
293 out[5] = matched
294 return 0
295}
296func fm_emit_prof(tag: *u8, prof: *i64, ex: *i64, ty: *i64, n: i64) -> i64 {
297 fm_puts("\x22" as *u8); fm_puts(tag); fm_puts("\x22:{\x22profile\x22:[" as *u8)
298 var i: i64 = 0
299 while i < FM_NB {
300 if i > 0 { fm_puts("," as *u8) }
301 fm_pn(prof[i])
302 i = i + 1
303 }
304 fm_puts("],\x22extrema\x22:[" as *u8)
305 var k: i64 = 0
306 while k < n {
307 if k > 0 { fm_puts("," as *u8) }
308 fm_puts("{\x22band\x22:" as *u8); fm_pn(ex[k])
309 fm_puts(",\x22y_permil\x22:" as *u8); fm_pn(ex[k]*FM_PERMIL/FM_NB)
310 fm_puts(",\x22kind\x22:\x22" as *u8)
311 if ty[k]==1 { fm_puts("max" as *u8) } else { fm_puts("min" as *u8) }
312 fm_puts("\x22}" as *u8)
313 k = k + 1
314 }
315 fm_puts("],\x22n\x22:" as *u8); fm_pn(n)
316 // DECLARE the derived threshold and the noise floor it came from -- a judge that hides the number it
317 // thresholded on is unauditable, and this one is derived per-mesh so it MUST travel with the result.
318 fm_puts(",\x22noise_floor_swing\x22:" as *u8); fm_pn(fm_noisefloor(prof))
319 fm_puts("}" as *u8)
320 return 0
321}
322// ★MULTI-LANE COMPARE (F1091): score every lane, publish them all, and take the MIN as the headline --
323// this lane's standing law that you hold several judges and publish the minimum. A face that is correct on
324// the midline and wrong at the cheek can no longer hide behind one good slice.
325func fm_cmp_lanes(oursp: *u8, orap: *u8) -> i64 {
326 let pA: *i64 = sys_mmap(FM_SCRATCH) as *i64
327 let pB: *i64 = sys_mmap(FM_SCRATCH) as *i64
328 let exA: *i64 = sys_mmap(FM_SCRATCH) as *i64
329 let tyA: *i64 = sys_mmap(FM_SCRATCH) as *i64
330 let exB: *i64 = sys_mmap(FM_SCRATCH) as *i64
331 let tyB: *i64 = sys_mmap(FM_SCRATCH) as *i64
332 let out: *i64 = sys_mmap(FM_SCRATCH) as *i64
333 fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22v\x22:2,\x22judge\x22:\x22MULTI-LANE landmark placement (midline + off-midline)\x22,\x22lanes\x22:[" as *u8)
334 var worst: i64 = FM_PERMIL
335 var sum: i64 = 0
336 var nl: i64 = 0
337 var L: i64 = 0
338 while L < FM_LANES {
339 var sc: i64 = 0 - 1
340 if fm_profile_lane(oursp, pA, L) == 1 {
341 if fm_profile_lane(orap, pB, L) == 1 {
342 let nA: i64 = fm_extrema(pA, exA, tyA)
343 let nB: i64 = fm_extrema(pB, exB, tyB)
344 if nB > 0 {
345 fm_score(exA, tyA, nA, exB, tyB, nB, out)
346 sc = out[0]
347 }
348 }
349 }
350 if L > 0 { fm_puts("," as *u8) }
351 fm_puts("{\x22lane\x22:" as *u8); fm_pn(L - FM_LANEMID)
352 fm_puts(",\x22score\x22:" as *u8); fm_pn(sc)
353 if sc >= 0 {
354 fm_puts(",\x22recall\x22:" as *u8); fm_pn(out[1])
355 fm_puts(",\x22precision\x22:" as *u8); fm_pn(out[2])
356 fm_puts(",\x22n_oracle\x22:" as *u8); fm_pn(out[4])
357 if sc < worst { worst = sc }
358 sum = sum + sc
359 nl = nl + 1
360 }
361 fm_puts("}" as *u8)
362 L = L + 1
363 }
364 var mean: i64 = 0
365 if nl > 0 { mean = sum / nl }
366 if nl == 0 { worst = 0 - 1 }
367 fm_puts("],\x22headline\x22:" as *u8); fm_pn(worst)
368 fm_puts(",\x22mean\x22:" as *u8); fm_pn(mean)
369 fm_puts(",\x22lanes_scored\x22:" as *u8); fm_pn(nl)
370 fm_puts(",\x22rule\x22:\x22headline = the WORST lane. lane 0 is the midline; negative and positive lanes step laterally across the face. A brow ridge, a cheekbone and a jaw line live OFF the midline and were invisible to a single-slice judge -- which scored the best face this program has built the same as no face at all.\x22}\n" as *u8)
371 return 0
372}
373func fm_cmp(oursp: *u8, orap: *u8) -> i64 {
374 let pA: *i64 = sys_mmap(FM_SCRATCH) as *i64
375 let pB: *i64 = sys_mmap(FM_SCRATCH) as *i64
376 if fm_profile(oursp, pA) == 0 { fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22rc\x22:-1,\x22err\x22:\x22cannot profile ours\x22}\n" as *u8); return 1 }
377 if fm_profile(orap, pB) == 0 { fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22rc\x22:-2,\x22err\x22:\x22cannot profile oracle\x22}\n" as *u8); return 1 }
378 let exA: *i64 = sys_mmap(FM_SCRATCH) as *i64
379 let tyA: *i64 = sys_mmap(FM_SCRATCH) as *i64
380 let exB: *i64 = sys_mmap(FM_SCRATCH) as *i64
381 let tyB: *i64 = sys_mmap(FM_SCRATCH) as *i64
382 let nA: i64 = fm_extrema(pA, exA, tyA)
383 let nB: i64 = fm_extrema(pB, exB, tyB)
384 let out: *i64 = sys_mmap(FM_SCRATCH) as *i64
385 fm_score(exA, tyA, nA, exB, tyB, nB, out)
386 // ***COVERAGE GATE. A profile band holds -1 when no surface lies in the midline lane, and this judge
387 // has always been honest about that -- but it then scored anyway. MEASURED 2026-08-01: the shipped
388 // ring-lofted head returns a profile that is roughly 60 PERCENT holes, because control rings sit at
389 // DISCRETE HEIGHTS and between them there is no surface near a lane of half-width width/48. It scored
390 // 0. I regenerated at SIX TIMES the density (radial 24->96, head crop 2,448->14,976 tris) and it still
391 // scored 0, because raising radial adds points AROUND rings, not BETWEEN them.
392 // ***SO A 0 FROM THIS JUDGE COULD MEAN 'WRONG ANATOMY' OR 'WRONG REPRESENTATION' AND NOTHING
393 // DISTINGUISHED THEM. That silently penalises every loft against every marching-surface mesh, and it
394 // means this lane's banked 8-percent-of-a-real-face figure and its 69/92/138 series -- all measured on
395 // lofts -- are self-consistent but NOT comparable across representations.
396 // ***THE FIX IS THE nx_capaxes GROUNDING-0 DISCIPLINE: report COVERAGE, and REFUSE to emit a score the
397 // profile cannot ground. A number computed from mostly holes is not a measurement of a face.
398 var covA: i64 = 0
399 var covB: i64 = 0
400 var bi: i64 = 0
401 while bi < FM_NB {
402 if pA[bi] >= 0 { covA = covA + 1 }
403 if pB[bi] >= 0 { covB = covB + 1 }
404 bi = bi + 1
405 }
406 let cpA: i64 = covA*FM_PERMIL/FM_NB
407 let cpB: i64 = covB*FM_PERMIL/FM_NB
408 fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22v\x22:1,\x22judge\x22:\x22midline-profile LANDMARK PLACEMENT (not variance)\x22" as *u8)
409 fm_puts(",\x22coverage_permil_ours\x22:" as *u8); fm_pn(cpA)
410 fm_puts(",\x22coverage_permil_oracle\x22:" as *u8); fm_pn(cpB)
411 fm_puts(",\x22coverage_floor_permil\x22:" as *u8); fm_pn(FM_COV_FLOOR)
412 if cpA < FM_COV_FLOOR {
413 fm_puts(",\x22score\x22:-1,\x22verdict\x22:\x22REFUSED-LOW-COVERAGE\x22,\x22why\x22:\x22our midline profile is mostly holes, so a score would measure the REPRESENTATION not the face -- a ring loft has no surface between control rings and raising radial density does not help. Re-emit through a marching-surface path, or raise the ring count, before quoting a placement number.\x22" as *u8)
414 fm_puts(",\x22n_ours\x22:" as *u8); fm_pn(nA)
415 fm_puts(",\x22n_oracle\x22:" as *u8); fm_pn(nB)
416 fm_puts(",\x22profile_units\x22:\x22z in per-mille of the mesh own depth span, per y-band; -1 = no midline surface in that band\x22}\n" as *u8)
417 return 0
418 }
419 fm_puts(",\x22score\x22:" as *u8); fm_pn(out[0])
420 fm_puts(",\x22recall\x22:" as *u8); fm_pn(out[1])
421 fm_puts(",\x22precision\x22:" as *u8); fm_pn(out[2])
422 fm_puts(",\x22matched\x22:" as *u8); fm_pn(out[5])
423 fm_puts(",\x22n_ours\x22:" as *u8); fm_pn(nA)
424 fm_puts(",\x22n_oracle\x22:" as *u8); fm_pn(nB)
425 fm_puts("," as *u8); fm_emit_prof("ours" as *u8, pA, exA, tyA, nA)
426 fm_puts("," as *u8); fm_emit_prof("oracle" as *u8, pB, exB, tyB, nB)
427 fm_puts(",\x22rule\x22:\x22score = recall x precision. recall credits each ORACLE landmark found near its true place (credit decays with distance, zero past tolerance); precision is the fraction of OUR extrema that matched a real one, so spurious features CUT the score. A face cannot win by being bumpy.\x22" as *u8)
428 fm_puts(",\x22profile_units\x22:\x22z in per-mille of the mesh own depth span, per y-band; -1 = no midline surface in that band (an honest hole, never a fabricated zero)\x22}\n" as *u8)
429 return 0
430}
431// f32 encode of a small positive integer (same helper pattern as the headcrop fixture)
432func fm_f32of(v: i64) -> i64 {
433 if v <= 0 { return 0 }
434 var ex: i64 = 0
435 var tv: i64 = v
436 while tv >= 2 { tv = tv/2; ex = ex + 1 }
437 var frac: i64 = 0
438 if ex > 0 { frac = (v - (1 << ex)) * (1 << 23) / (1 << ex) }
439 return ((127 + ex) << 23) | frac
440}
441func fm_wr32(b: *u8, o: i64, v: i64) -> i64 {
442 b[o]=(v & 255) as u8; b[o+1]=((v>>8) & 255) as u8; b[o+2]=((v>>16) & 255) as u8; b[o+3]=((v>>24) & 255) as u8
443 return 0
444}
445// ★THE FIXTURE THAT PROVES THE POINT: a flat wall spanning the width, plus a BUMP placed only at one SIDE.
446// The midline lane must see a flat profile; a lateral lane must see the bump. If lanes were cosmetic, both
447// would read the same -- so this is the tooth that makes off-midline sampling real rather than asserted.
448func fm_mkfix(path: *u8) -> i64 {
449 let nt: i64 = 8
450 let hdr: i64 = FM_HDR + FM_LAYENT
451 let bytes: i64 = hdr + nt*FM_TRI + nt*FM_LID
452 let b: *u8 = sys_mmap(bytes + 64)
453 var z: i64 = 0
454 while z < bytes { b[z] = 0 as u8; z = z + 1 }
455 b[0]=78 as u8; b[1]=88 as u8; b[2]=77 as u8; b[3]=83 as u8
456 b[4]=72 as u8; b[5]=50 as u8
457 fm_wr32(b, 8, 1); fm_wr32(b, 12, nt)
458 fm_wr32(b, FM_HDR+16, 0); fm_wr32(b, FM_HDR+20, nt)
459 // ⚠The lane windows are NARROW by design (halfw = width/48), so a fixture must place geometry AT the
460 // lane centres or both lanes correctly see nothing -- which is how the first cut of this tooth failed.
461 // With x spanning 0..100 the midline lane samples x=50 and the outermost lane samples x=70.
462 // tri 0 sets the x AABB; tris 1-5 are the flat wall ON the midline; tris 6-7 are the bump at x=70.
463 var t: i64 = 0
464 while t < nt {
465 let o: i64 = hdr + t*FM_TRI
466 var xv: i64 = 50
467 var zv: i64 = 20
468 let yv: i64 = 10 + t*12
469 if t >= 6 { xv = 70; zv = 200 } // the BUMP tris stand proud, and ONLY off the midline
470 var k: i64 = 0
471 while k < 3 {
472 var xx: i64 = xv
473 if t == 0 { if k == 0 { xx = 0 } if k == 1 { xx = 100 } } // AABB spanners
474 fm_wr32(b, o + k*12 + 0, fm_f32of(xx))
475 fm_wr32(b, o + k*12 + 4, fm_f32of(yv + k))
476 fm_wr32(b, o + k*12 + 8, fm_f32of(zv))
477 k = k + 1
478 }
479 fm_wr32(b, hdr + nt*FM_TRI + t*FM_LID, 0)
480 t = t + 1
481 }
482 let fd: i64 = sys_openat_wr(path, 420)
483 if fd < 0 { return 0 - 1 }
484 sys_write(fd, b, bytes)
485 sys_close(fd)
486 return 0
487}
488func fm_gate() -> i64 {
489 let ctr: *i64 = gv_ctr()
490 gv_head("nx_facemark selftest -- a judge that scores PLACEMENT and cannot be gamed by bumps" as *u8)
491 // synthetic ORACLE profile: the real alternating facial sequence (brow, nasion, nose, subnasale, lip...)
492 let po: *i64 = sys_mmap(FM_SCRATCH) as *i64
493 var i: i64 = 0
494 while i < FM_NB { po[i] = 500; i = i + 1 }
495 po[8]=560; po[9]=600; po[10]=560 // brow ridge (max at 9)
496 po[13]=470; po[14]=440; po[15]=470 // nasion (min at 14)
497 po[19]=700; po[20]=760; po[21]=700 // pronasale (max at 20)
498 po[24]=480; po[25]=450; po[26]=480 // subnasale (min at 25)
499 po[29]=560; po[30]=600; po[31]=560 // upper lip (max at 30)
500 po[34]=490; po[35]=460; po[36]=490 // mouth groove (min at 35)
501 po[39]=570; po[40]=610; po[41]=570 // chin (max at 40)
502 let exo: *i64 = sys_mmap(FM_SCRATCH) as *i64
503 let tyo: *i64 = sys_mmap(FM_SCRATCH) as *i64
504 let no: i64 = fm_extrema(po, exo, tyo)
505 var t1: i64 = 0
506 if no >= 6 { t1 = 1 }
507 gv_check("T1 the facial landmark sequence is detected (>=6 extrema)" as *u8, t1, ctr)
508 let out: *i64 = sys_mmap(FM_SCRATCH) as *i64
509 fm_score(exo, tyo, no, exo, tyo, no, out)
510 var t2: i64 = 0
511 if out[0] == FM_PERMIL { t2 = 1 }
512 gv_check("T2 non-vacuity: a profile against ITSELF scores 1000" as *u8, t2, ctr)
513 // ★THE ANTI-GOODHART TOOTH: a CORRUGATED profile -- bumps everywhere, more "detail" than the oracle by
514 // any variance measure -- must score LOW, because almost none of its extrema are real landmarks.
515 let pc: *i64 = sys_mmap(FM_SCRATCH) as *i64
516 var c: i64 = 0
517 while c < FM_NB {
518 var v: i64 = 500
519 if c % 2 == 0 { v = 560 } else { v = 440 }
520 pc[c] = v
521 c = c + 1
522 }
523 let exc: *i64 = sys_mmap(FM_SCRATCH) as *i64
524 let tyc: *i64 = sys_mmap(FM_SCRATCH) as *i64
525 let nc: i64 = fm_extrema(pc, exc, tyc)
526 let outc: *i64 = sys_mmap(FM_SCRATCH) as *i64
527 fm_score(exc, tyc, nc, exo, tyo, no, outc)
528 // ★T3 RESTATED AFTER seq864, and it got SHARPER not looser. Under the old fixed prominence a corrugated
529 // profile produced MANY extrema and was rejected on PRECISION. Under the noise-floor threshold it is
530 // rejected one step earlier and more honestly: uniform corrugation IS noise, its own median adjacent
531 // diff sets the floor ABOVE its swing, and it yields no landmarks at all. Both routes end at a low
532 // score, so the anti-Goodhart property is unchanged -- but the assertion now names the MECHANISM.
533 var t3: i64 = 0
534 if outc[0] < out[0]/2 { t3 = 1 }
535 gv_check("T3 ANTI-GOODHART: a corrugated profile scores under half the true one" as *u8, t3, ctr)
536 var t3b: i64 = 0
537 if fm_noisefloor(pc) > fm_noisefloor(po) { if nc == 0 { t3b = 1 } }
538 gv_check("T3b the MECHANISM: corrugation raises its own noise floor and yields no landmarks" as *u8, t3b, ctr)
539 // a SMOOTH profile (our egg) must score near zero -- the judge must see absence, not forgive it
540 let ps: *i64 = sys_mmap(FM_SCRATCH) as *i64
541 var s: i64 = 0
542 while s < FM_NB { ps[s] = 500; s = s + 1 }
543 let exs: *i64 = sys_mmap(FM_SCRATCH) as *i64
544 let tys: *i64 = sys_mmap(FM_SCRATCH) as *i64
545 let ns: i64 = fm_extrema(ps, exs, tys)
546 let outs: *i64 = sys_mmap(FM_SCRATCH) as *i64
547 fm_score(exs, tys, ns, exo, tyo, no, outs)
548 var t4: i64 = 0
549 if outs[0] == 0 { t4 = 1 }
550 gv_check("T4 a featureless smooth profile scores 0 (absence is seen, not forgiven)" as *u8, t4, ctr)
551 // ★PLACEMENT IS SCORED, NOT JUST COUNT: shift every landmark and the score must FALL while the
552 // extremum COUNT is identical. This is the property the variance judge structurally lacked.
553 let pd: *i64 = sys_mmap(FM_SCRATCH) as *i64
554 var d: i64 = 0
555 while d < FM_NB { pd[d] = 500; d = d + 1 }
556 // ★ONE band of shift = 21 per-mille, INSIDE the 55 tolerance, so this tests the CREDIT DECAY.
557 // (The gate first ran RED here with a 3-band shift: 62 per-mille is BEYOND tolerance, so scoring zero
558 // was the judge behaving correctly and the TEST was wrong. Rather than loosen the tolerance to get a
559 // green -- which would have blunted the instrument -- the case was split into two teeth: decay inside
560 // tolerance here, and a hard zero outside it in T5b. The gate caught a real specification question.)
561 pd[9]=560; pd[10]=600; pd[11]=560
562 pd[14]=470; pd[15]=440; pd[16]=470
563 pd[20]=700; pd[21]=760; pd[22]=700
564 pd[25]=480; pd[26]=450; pd[27]=480
565 pd[30]=560; pd[31]=600; pd[32]=560
566 pd[35]=490; pd[36]=460; pd[37]=490
567 pd[40]=570; pd[41]=610; pd[42]=570
568 let exd: *i64 = sys_mmap(FM_SCRATCH) as *i64
569 let tyd: *i64 = sys_mmap(FM_SCRATCH) as *i64
570 let nd: i64 = fm_extrema(pd, exd, tyd)
571 let outd: *i64 = sys_mmap(FM_SCRATCH) as *i64
572 fm_score(exd, tyd, nd, exo, tyo, no, outd)
573 var t5: i64 = 0
574 if nd == no { if outd[0] < out[0] { if outd[0] > 0 { t5 = 1 } } }
575 gv_check("T5 PLACEMENT scored: same count, shifted 1 band, strictly lower but credited" as *u8, t5, ctr)
576 // T5b: past the tolerance a landmark is simply NOT the landmark -- no partial credit for being nearby.
577 let pf: *i64 = sys_mmap(FM_SCRATCH) as *i64
578 var f: i64 = 0
579 while f < FM_NB { pf[f] = 500; f = f + 1 }
580 pf[15]=560; pf[16]=600; pf[17]=560
581 pf[20]=470; pf[21]=440; pf[22]=470
582 pf[26]=700; pf[27]=760; pf[28]=700
583 pf[31]=480; pf[32]=450; pf[33]=480
584 pf[36]=560; pf[37]=600; pf[38]=560
585 pf[41]=490; pf[42]=460; pf[43]=490
586 let exf: *i64 = sys_mmap(FM_SCRATCH) as *i64
587 let tyf: *i64 = sys_mmap(FM_SCRATCH) as *i64
588 let nf: i64 = fm_extrema(pf, exf, tyf)
589 let outf: *i64 = sys_mmap(FM_SCRATCH) as *i64
590 fm_score(exf, tyf, nf, exo, tyo, no, outf)
591 var t5b: i64 = 0
592 if outf[0] < outd[0] { t5b = 1 }
593 gv_check("T5b a landmark shifted PAST tolerance earns less than one inside it" as *u8, t5b, ctr)
594 let pz: *i64 = sys_mmap(FM_SCRATCH) as *i64
595 var t6: i64 = 0
596 if fm_profile("/tmp/nx_fm_absent_zz.nxmesh" as *u8, pz) == 0 { t6 = 1 }
597 gv_check("T6 missing input refused, not silently empty" as *u8, t6, ctr)
598 // ★★T7/T8 (F1091): OFF-MIDLINE SAMPLING IS REAL. Same mesh, two lanes: a bump placed only at one side
599 // must be INVISIBLE to the midline lane and VISIBLE to the lateral one. This is the exact blindness
600 // that scored a real brow ridge the same as no brow, so it gets a standing tooth.
601 var t7: i64 = 0
602 if fm_mkfix("/tmp/nx_fm_fix.nxmesh" as *u8) == 0 { t7 = 1 }
603 gv_check("T7 lateral-bump fixture written" as *u8, t7, ctr)
604 let pmid: *i64 = sys_mmap(FM_SCRATCH) as *i64
605 let plat: *i64 = sys_mmap(FM_SCRATCH) as *i64
606 let okm: i64 = fm_profile_lane("/tmp/nx_fm_fix.nxmesh" as *u8, pmid, FM_LANEMID)
607 let okl: i64 = fm_profile_lane("/tmp/nx_fm_fix.nxmesh" as *u8, plat, FM_LANES-1)
608 var maxm: i64 = 0
609 var maxl: i64 = 0
610 var q: i64 = 0
611 while q < FM_NB {
612 if pmid[q] > maxm { maxm = pmid[q] }
613 if plat[q] > maxl { maxl = plat[q] }
614 q = q + 1
615 }
616 var t8: i64 = 0
617 if okm == 1 { if okl == 1 { if maxl > maxm { t8 = 1 } } }
618 gv_check("T8 the lateral lane SEES a bump the midline lane cannot" as *u8, t8, ctr)
619 return gv_verdict("FACEMARK-GATE" as *u8, ctr, "placement-scored; bumps cannot buy a score" as *u8)
620}
621func main(argc: i64, argv: *i64) -> i64 {
622 if argc >= 2 {
623 if fm_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return fm_gate() }
624 if fm_streq(argv[1] as *u8, "profile" as *u8) == 1 {
625 if argc < 3 { fm_puts("usage: nx_facemark profile <mesh.nxmesh>\n" as *u8); return 2 }
626 let p: *i64 = sys_mmap(FM_SCRATCH) as *i64
627 if fm_profile(argv[2] as *u8, p) == 0 { fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22rc\x22:-1}\n" as *u8); return 1 }
628 let ex: *i64 = sys_mmap(FM_SCRATCH) as *i64
629 let ty: *i64 = sys_mmap(FM_SCRATCH) as *i64
630 let n: i64 = fm_extrema(p, ex, ty)
631 fm_puts("{\x22organ\x22:\x22nx_facemark\x22,\x22v\x22:1," as *u8)
632 fm_emit_prof("mesh" as *u8, p, ex, ty, n)
633 fm_puts("}\n" as *u8)
634 return 0
635 }
636 if fm_streq(argv[1] as *u8, "cmp" as *u8) == 1 {
637 if argc < 4 { fm_puts("usage: nx_facemark cmp <ours.nxmesh> <oracle.nxmesh>\n" as *u8); return 2 }
638 return fm_cmp(argv[2] as *u8, argv[3] as *u8)
639 }
640 if fm_streq(argv[1] as *u8, "lanes" as *u8) == 1 {
641 if argc < 4 { fm_puts("usage: nx_facemark lanes <ours.nxmesh> <oracle.nxmesh>\n" as *u8); return 2 }
642 return fm_cmp_lanes(argv[2] as *u8, argv[3] as *u8)
643 }
644 }
645 fm_puts("usage: nx_facemark profile <mesh> | cmp <ours> <oracle> | selftest\n" as *u8)
646 return 2
647}