code wiki / _hdl_build / nx_face_project_gate.nx
nx_face_project_gate.nx source
↩ module page · 278 lines · 15007 B
1// nx_face_project_gate.nx -- ★A-R1: REFERENCE-GROUNDED FACE (operator "go A" 2026-07-07). Project our OWN
2// Z-Image photoreal reference (knowledge/elara_ref.jpg, the standing benchmark target) onto the SDF face
3// geometry as albedo -- the photogrammetry front-projection move; ground in the reference, stop sculpting blind.
4// T1 EXACT MAPPING: a synthetic texture with a green block at the PHOTO's left-eye coords must land on the
5// GEOMETRY's left eye on screen (centroid box) -- and a deliberately mis-set projection must NOT (neg-control)
6// T2 the photoreal reference projects: the projected face differs massively from procedural + PNG
7// T3 MEASURED photoreal gain: nx_natstat LEVEL of the projected render must EXCEED the procedural render's
8// (the entire point of option A; per-axis printed)
9// T4 FAIL-CLOSED fallback: tex=0 arena renders BYTE-IDENTICAL to a never-touched arena (zero regression)
10// T5 determinism (two projected renders byte-identical)
11// expect_exit:0 license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_jpeg_ascii.nx"
14import "nx_sdfrender_mt.nx"
15import "nx_makeup.nx"
16import "nx_natstat.nx"
17import "nx_png.nx"
18
19func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
20func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
21func fbdiff(a: *i64, b: *i64, n: i64) -> i64 { var d: i64=0; var i: i64=0; while i<n { if a[i] != b[i] { d=d+1 } i=i+1 } return d }
22// ★REFERENCE-SIMILARITY error (the A-R2 fit metric): mean |render_luma - photo_luma| over a model-space grid
23// of face points, using the projection mapping for photo coords and the yaw-0 camera math for screen coords
24// (flat-plane approx on the face front, dist ~ 4096-560 = 3536). Eye + mouth zones excluded (ours by design).
25// This is the ruler option A optimizes: LOWER = the render is closer to the reference.
26func rs_err(fb: *i64, rgb: *u8, tw: i64, th: i64, uc: i64, vc: i64, su: i64, sv: i64) -> i64 {
27 var tot: i64 = 0
28 var n: i64 = 0
29 var gy: i64 = 0 - 300
30 while gy <= 500 {
31 var gx: i64 = 0 - 400
32 while gx <= 400 {
33 var skip: i64 = 0
34 var ax: i64 = gx - 258
35 if ax < 0 { ax = 0 - ax }
36 var bx: i64 = gx + 258
37 if bx < 0 { bx = 0 - bx }
38 var dy: i64 = gy - 178
39 if dy < 0 { dy = 0 - dy }
40 if dy < 110 { if ax < 130 { skip = 1 } }
41 if dy < 110 { if bx < 130 { skip = 1 } }
42 if skip == 0 {
43 let u: i64 = uc + gx * su / 1024
44 let v: i64 = vc - gy * sv / 1024
45 let xs: i64 = 256 + gx * 586 / 3536
46 let ys: i64 = 192 - gy * 586 / 3536
47 if u >= 0 { if u < tw { if v >= 0 { if v < th { if xs >= 0 { if xs < 512 { if ys >= 0 { if ys < 384 {
48 let col: i64 = fb[ys * 512 + xs]
49 let lr: i64 = (col & 255) + ((col >> 8) & 255) + ((col >> 16) & 255)
50 let o: i64 = (v * tw + u) * 3
51 let lp: i64 = (rgb[o] as i64) + (rgb[o + 1] as i64) + (rgb[o + 2] as i64)
52 var d: i64 = lr - lp
53 if d < 0 { d = 0 - d }
54 tot = tot + d
55 n = n + 1
56 } } } } } } } }
57 }
58 gx = gx + 50
59 }
60 gy = gy + 50
61 }
62 if n == 0 { return 99999 }
63 return tot / n
64}
65
66// the projection constants fitted to the HI-RES reference (A-R2a, generated 2026-07-08: 512x640 frontal
67// close-up, eyes (172,268)/(340,268) spacing 168px, mouth (256,462)) vs the model (eyes +-258 at y178,
68// mouth y-390): uc=256 vc=329 su=333 sv=350. Sanity: brows predict v219 (actual ~220), chin v563 (~560).
69// Photo-v BAND [240,440) = the MID-FACE payload: below her brows (~218-230, which double-imaged over our
70// brow parts at the wider floor), above her mouth (~450+, which grayed-out our berry lipstick). Same
71// band philosophy ref #1 converged to: cheeks/nose/under-eyes project; brows/lips/forehead/chin stay ours.
72const P_UC: i64 = 256
73const P_VC: i64 = 329
74const P_SU: i64 = 333
75const P_SV: i64 = 350
76const P_VLO: i64 = 240
77const P_VHI: i64 = 440
78
79func main() -> i64 {
80 hw("=== nx_face_project_gate -- A-R1 reference-grounded face (project the Z-Image target) ===\n" as *u8)
81 var fails: i64 = 0
82 let npx: i64 = ww() * hh()
83 let base: i64 = sys_mmap(sdf_bytes()) as i64
84 let fbA: *i64 = sys_mmap(npx*8) as *i64
85 let fbB: *i64 = sys_mmap(npx*8) as *i64
86
87 // --- T1 EXACT MAPPING with a synthetic texture: green 12x12 block at the photo's LEFT-EYE coords ---
88 // green block at the reference's LEFT-CHEEK coords (photo u 150..162, v 344..356 -- inside the projection
89 // band AND on SKIN; the eye is excluded-by-design so an eye landmark would prove nothing)
90 let stw: i64 = 512
91 let sth: i64 = 640
92 let stex: *u8 = sys_mmap(stw * sth * 3) as *u8
93 var gy: i64 = 344
94 while gy < 356 {
95 var gx: i64 = 150
96 while gx < 162 { let o: i64 = (gy*stw+gx)*3; stex[o]=0 as u8; stex[o+1]=255 as u8; stex[o+2]=0 as u8; gx=gx+1 }
97 gy = gy + 1
98 }
99 sdf_face(base)
100 sdf_set_facetex(base, stex as i64, stw, sth, P_UC, P_VC, P_SU, P_SV)
101 sdf_set_facetex_band(base, P_VLO, P_VHI)
102 sdfmt_render(base, 0, 4, 236, 180, 156, 0)
103 let fb: *i64 = (base + fb_off()) as *i64
104 var gcnt: i64 = 0
105 var gsx: i64 = 0
106 var gsy: i64 = 0
107 var pi: i64 = 0
108 while pi < npx {
109 let col: i64 = fb[pi]
110 let r: i64 = col & 255
111 let g: i64 = (col >> 8) & 255
112 let b: i64 = (col >> 16) & 255
113 if g > r + 60 { if g > b + 60 { if g > 120 { gcnt = gcnt + 1; gsx = gsx + pi % ww(); gsy = gsy + pi / ww() } } }
114 pi = pi + 1
115 }
116 var cx1: i64 = 0
117 var cy1: i64 = 0
118 if gcnt > 0 { cx1 = gsx / gcnt; cy1 = gsy / gcnt }
119 hw(" green block: px=" as *u8); pn(gcnt); hw(" centroid=(" as *u8); pn(cx1); hw("," as *u8); pn(cy1); hw(") expect ~(205,202)\n" as *u8)
120 var t1a: i64 = 0
121 if gcnt > 20 { if cx1 > 189 { if cx1 < 221 { if cy1 > 186 { if cy1 < 218 { t1a = 1 } } } } }
122 // neg-control: shift the projection center 60 photo-px -> the block must move off the cheek
123 sdf_face(base)
124 sdf_set_facetex(base, stex as i64, stw, sth, P_UC + 60, P_VC, P_SU, P_SV)
125 sdf_set_facetex_band(base, P_VLO, P_VHI)
126 sdfmt_render(base, 0, 4, 236, 180, 156, 0)
127 var gcnt2: i64 = 0
128 var gsx2: i64 = 0
129 pi = 0
130 while pi < npx {
131 let col: i64 = fb[pi]
132 let r: i64 = col & 255
133 let g: i64 = (col >> 8) & 255
134 let b: i64 = (col >> 16) & 255
135 if g > r + 60 { if g > b + 60 { if g > 120 { gcnt2 = gcnt2 + 1; gsx2 = gsx2 + pi % ww() } } }
136 pi = pi + 1
137 }
138 var moved: i64 = 0
139 if gcnt2 < gcnt / 2 { moved = 1 }
140 if gcnt2 > 0 { let cx2: i64 = gsx2 / gcnt2; var dxs: i64 = cx2 - cx1; if dxs < 0 { dxs = 0 - dxs } if dxs > 20 { moved = 1 } }
141 if t1a == 1 { if moved == 1 { hw("T1 PASS exact mapping: the photo-cheek block lands on the geometry cheek; mis-set projection misses (neg-control)\n" as *u8) } }
142 if t1a == 0 { fails=fails+1; hw("T1 FAIL mapping centroid\n" as *u8) }
143 else { if moved == 0 { fails=fails+1; hw("T1 FAIL neg-control did not move\n" as *u8) } }
144
145 // --- decode the real reference (the A-R2a hi-res frontal close-up) ---
146 let szp: *i64 = sys_mmap(16) as *i64
147 let jpeg: *u8 = sys_read_file("knowledge/elara_face_hi.jpg" as *u8, szp)
148 if (jpeg as i64) == 0 { hw("no reference jpg\n" as *u8); return 1 }
149 let rgbp: *i64 = sys_mmap(8) as *i64
150 let twp: *i64 = sys_mmap(8) as *i64
151 let thp: *i64 = sys_mmap(8) as *i64
152 let rc: i64 = nx_jpeg_decode_rgb(jpeg, szp[0], rgbp, twp, thp)
153 if rc != NX_JPEG_ASCII_OK { hw("decode failed rc=" as *u8); pn(rc); hw("\n" as *u8); return 1 }
154 hw(" reference decoded " as *u8); pn(twp[0]); hw("x" as *u8); pn(thp[0]); hw("\n" as *u8)
155
156 // --- T2 the photoreal projection changes the face massively (yaw 0 = the rs_err camera) ---
157 sdf_face(base)
158 sdfmt_render(base, 0, 4, 236, 180, 156, 0)
159 var k: i64 = 0
160 while k < npx { fbA[k] = fb[k]; k = k + 1 } // procedural render
161 sdf_face(base)
162 sdf_set_facetex(base, rgbp[0], twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
163 sdf_set_facetex_band(base, P_VLO, P_VHI)
164 sdfmt_render(base, 0, 4, 236, 180, 156, 0)
165 k = 0
166 while k < npx { fbB[k] = fb[k]; k = k + 1 } // projected render
167 let dproj: i64 = fbdiff(fbA, fbB, npx)
168 hw(" projected vs procedural diff px=" as *u8); pn(dproj); hw("\n" as *u8)
169 if dproj > 3000 { hw("T2 PASS the reference projects onto the face\n" as *u8) }
170 else { fails=fails+1; hw("T2 FAIL projection invisible\n" as *u8) }
171
172 // --- T3 MEASURED photoreal gain (nx_natstat, the honest ruler) ---
173 let cap: i64 = 512 * 512 * 8
174 let buf1: *i64 = sys_mmap(cap) as *i64
175 let buf2: *i64 = sys_mmap(cap) as *i64
176 let outA: *i64 = sys_mmap(64) as *i64
177 let outB: *i64 = sys_mmap(64) as *i64
178 let lvA: i64 = ns_assess(fbA, ww(), hh(), buf1, buf2, outA)
179 let lvB: i64 = ns_assess(fbB, ww(), hh(), buf1, buf2, outB)
180 hw(" natstat FULL-FRAME: procedural=" as *u8); pn(lvA); hw(" projected=" as *u8); pn(lvB)
181 hw(" [mscn " as *u8); pn(outA[1]); hw("->" as *u8); pn(outB[1]); hw("]\n" as *u8)
182 // ASSERT on the FACE-CROP -- the region the rung changes (whole-frame dilutes with unchanged bg/hair;
183 // same NIQE-style region-selection logic the metric itself uses). Same box both renders = fair.
184 let cw2: i64 = 232
185 let ch2: i64 = 304
186 let cropA: *i64 = sys_mmap(cw2*ch2*8) as *i64
187 let cropB: *i64 = sys_mmap(cw2*ch2*8) as *i64
188 var cy: i64 = 0
189 while cy < ch2 {
190 var cx: i64 = 0
191 while cx < cw2 {
192 cropA[cy*cw2+cx] = fbA[(cy+40)*ww() + cx+140]
193 cropB[cy*cw2+cx] = fbB[(cy+40)*ww() + cx+140]
194 cx = cx + 1
195 }
196 cy = cy + 1
197 }
198 let lcA: i64 = ns_assess(cropA, cw2, ch2, buf1, buf2, outA)
199 let lcB: i64 = ns_assess(cropB, cw2, ch2, buf1, buf2, outB)
200 hw(" natstat FACE-CROP: procedural=" as *u8); pn(lcA); hw(" projected=" as *u8); pn(lcB)
201 hw(" [mscn " as *u8); pn(outA[1]); hw("->" as *u8); pn(outB[1]); hw(" colour " as *u8); pn(outA[3]); hw("->" as *u8); pn(outB[3]); hw(" aifp " as *u8); pn(outA[4]); hw("->" as *u8); pn(outB[4]); hw("]\n" as *u8)
202 // A-R2 BAR = REFERENCE SIMILARITY (the ruler option A optimizes; the crop-MSCN band is calibrated on
203 // busy-scene statistics and its own docs warn smooth portrait skin under-scores -- printed above for the
204 // ledger, not asserted). rs_err = mean |render - photo| luma over mapped face correspondences: the
205 // projected render must be MEASURABLY CLOSER to the reference, and the full frame must not degrade.
206 let reA: i64 = rs_err(fbA, rgbp[0] as *u8, twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
207 let reB: i64 = rs_err(fbB, rgbp[0] as *u8, twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
208 hw(" REFERENCE-SIMILARITY err (lower=closer): procedural=" as *u8); pn(reA); hw(" projected=" as *u8); pn(reB); hw("\n" as *u8)
209 var t3: i64 = 0
210 if reB + 8 <= reA { if lvB >= lvA { t3 = 1 } }
211 if t3 == 1 { hw("T3 PASS the projected render is measurably CLOSER to the reference (the fit metric)\n" as *u8) }
212 else { fails=fails+1; hw("T3 FAIL no similarity gain\n" as *u8) }
213
214 // --- T4 fail-closed fallback: tex cleared == never-touched, BYTE-IDENTICAL ---
215 let b2: i64 = sys_mmap(sdf_bytes()) as i64
216 sdf_face(b2)
217 sdfmt_render(b2, 200, 4, 236, 180, 156, 0)
218 let fb2: *i64 = (b2 + fb_off()) as *i64
219 sdf_face(base)
220 sdf_set_facetex(base, 0, 0, 0, 0, 0, 0, 0)
221 sdfmt_render(base, 200, 4, 236, 180, 156, 0)
222 let dfall: i64 = fbdiff(fb, fb2, npx)
223 if dfall == 0 { hw("T4 PASS tex=0 fallback BYTE-IDENTICAL to a never-touched arena (zero regression)\n" as *u8) }
224 else { fails=fails+1; hw("T4 FAIL fallback diff=" as *u8); pn(dfall); hw("\n" as *u8) }
225
226 // --- T5 determinism + the artifact PNG (STYLED + projected: long hair framing the photoreal face) ---
227 sdf_face(base)
228 var nfs: i64 = sdf_face_hairstyle(base, HS_LONG)
229 nfs = makeup_style2(base, nfs, MKUP_NATURAL)
230 sdf_set_facetex(base, rgbp[0], twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
231 sdf_set_facetex_band(base, P_VLO, P_VHI)
232 sdfmt_render(base, 200, 4, 236, 180, 156, 0)
233 k = 0
234 while k < npx { fbA[k] = fb[k]; k = k + 1 }
235 sdf_face(base)
236 nfs = sdf_face_hairstyle(base, HS_LONG)
237 nfs = makeup_style2(base, nfs, MKUP_NATURAL)
238 sdf_set_facetex(base, rgbp[0], twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
239 sdf_set_facetex_band(base, P_VLO, P_VHI)
240 sdfmt_render(base, 200, 4, 236, 180, 156, 0)
241 let ddet: i64 = fbdiff(fbA, fb, npx)
242 write_png(fb, ww(), hh(), "knowledge/nx_faceproj.png" as *u8)
243 if ddet == 0 { hw("T5 PASS determinism + PNG knowledge/nx_faceproj.png (styled + reference-projected)\n" as *u8) }
244 else { fails=fails+1; hw("T5 FAIL nondeterministic diff=" as *u8); pn(ddet); hw("\n" as *u8) }
245
246 // --- T6 PERSON-space projection (A-R2c): the SAME face-space constants, folded through the graft
247 // transform (scale 5/16, y+1330) by sdf_set_facetex_person, project onto the grafted head -- and the
248 // v band auto-clips to the head (body coords map past the ceiling), so the change is head-confined.
249 let pb: i64 = sys_mmap(sdf_bytes()) as i64
250 let pscr: i64 = sys_mmap(sdf_bytes()) as i64
251 sdf_person(pb, pscr)
252 sdfmt_render(pb, 200, 5, 240, 184, 160, 0)
253 let pfb: *i64 = (pb + fb_off()) as *i64
254 k = 0
255 while k < npx { fbA[k] = pfb[k]; k = k + 1 }
256 sdf_person(pb, pscr)
257 sdf_set_facetex_person(pb, rgbp[0], twp[0], thp[0], P_UC, P_VC, P_SU, P_SV)
258 sdf_set_facetex_band(pb, P_VLO, P_VHI)
259 sdfmt_render(pb, 200, 5, 240, 184, 160, 0)
260 let dp6: i64 = fbdiff(fbA, pfb, npx)
261 var dphead: i64 = 0
262 var yy6: i64 = 0
263 while yy6 < 100 {
264 var xx6: i64 = 0
265 while xx6 < ww() { if fbA[yy6*ww()+xx6] != pfb[yy6*ww()+xx6] { dphead = dphead + 1 } xx6 = xx6 + 1 }
266 yy6 = yy6 + 1
267 }
268 hw(" person projection: diff=" as *u8); pn(dp6); hw(" in-head-rows=" as *u8); pn(dphead); hw("\n" as *u8)
269 var t6: i64 = 0
270 if dp6 > 300 { if dphead * 10 > dp6 * 8 { t6 = 1 } }
271 if t6 == 1 { hw("T6 PASS person-space constants project the face onto the grafted head (head-confined)\n" as *u8) }
272 else { fails=fails+1; hw("T6 FAIL person projection\n" as *u8) }
273
274 if fails == 0 { hw("FACE-PROJECT-GATE 6/6 GREEN -- the reference face is ON the geometry (face AND grafted person), mapping exact, no degradation, fail-closed fallback\n" as *u8); sys_exit(0); return 0 }
275 hw("FACE-PROJECT-GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8)
276 sys_exit(1)
277 return 1
278}