code wiki / _hdl_build / nx_face_expr_gate.nx

nx_face_expr_gate.nx source

↩ module page · 121 lines · 7047 B

1// nx_face_expr_gate.nx -- ★R-FACE EXPRESSIONS: muscle-driven facial expressions on the inside-out anatomical head 2// (the digital-human R3 face rung + the engine MetaHuman-axis face half). The anatstack already models the facial 3// muscles (zygomaticus/frontalis/orbicularis); an expression CONTRACTS one and the overlying fat/skin follows. The 4// proof is CAUSAL and PER-PART (as_part_sdf isolates the moved structure, not the smooth-union soup): a smile lifts 5// the malar fat (a point above the neutral cheek FLIPS inside); surprise drops the jaw (a low mouth point fills) and 6// raises the brow; a frown lowers the brow. Plus a whole-face render imdiff (the expression is VISIBLE) + evidence. 7// T1 build. T2 causal per-part flips. T3 visible (render imdiff). T4 determinism. T5 evidence strip. license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_png.nx" 10import "nx_anatstack.nx" 11 12func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func 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 } 14func imdiff(a: *i64, b: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { if a[i]!=b[i] { s=s+1 } i=i+1 } return s } 15 16const NPMAX: i64 = 64 17// render the face at expression e into a 512x512 buffer, then downscale-copy a 256-wide column into the strip at slot 18func face_col(P: *i64, gal: *i64, GW: i64, GH: i64, slot: i64, e: i64) -> i64 { 19 anat_set_expr(e, 1024) 20 let np: i64 = anatstack_build(P) 21 let fb: *i64 = sys_mmap(512*512*8) as *i64 22 anatstack_render(P, np, 0, 3, 2, 42, 236, 194, 176, fb) 23 // box-downsample 512->GH tall, 512->256 wide, front-centered 24 var y: i64 = 0 25 while y < GH { 26 var x: i64 = 0 27 while x < 190 { 28 let sxa: i64 = x*512/190 29 let sya: i64 = y*512/GH 30 gal[y*GW + slot*190 + x] = fb[sya*512 + sxa] 31 x = x + 1 32 } 33 y = y + 1 34 } 35 return np 36} 37 38func main() -> i64 { 39 hw("=== nx_face_expr_gate -- muscle-driven facial expressions (neutral/smile/surprise/frown) ===\n" as *u8) 40 var fails: i64 = 0 41 let P: *i64 = sys_mmap(NPMAX*AS_STRIDE*8) as *i64 42 43 // ---- T1 build ---- 44 anat_set_expr(0, 0) 45 let np: i64 = anatstack_build(P) 46 hw(" anatstack parts="); pn(np); hw(" (skeleton+muscle+fat, faceanat frame)\n" as *u8) 47 var t1: i64 = 0 48 if np >= 34 { t1 = 1 } 49 if t1 == 1 { hw("T1 PASS layered head built\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 50 51 // ---- T2 CAUSAL per-part muscle flips (as_part_sdf isolates the moved structure) ---- 52 // SMILE: malar fat (idx 25) lifts -> a point ABOVE the neutral cheek flips from outside(+) to inside(-) 53 anat_set_expr(0, 0); anatstack_build(P) 54 let smU: i64 = as_part_sdf(P, 25, 0-262, 70, 0-475) 55 anat_set_expr(1, 1024); anatstack_build(P) 56 let smS: i64 = as_part_sdf(P, 25, 0-262, 70, 0-475) 57 // SURPRISE: lower lip/jaw (idx 32) drops -> a LOW mouth point flips outside->inside 58 anat_set_expr(0, 0); anatstack_build(P) 59 let suU: i64 = as_part_sdf(P, 32, 0, 0-570, 0-585) 60 anat_set_expr(2, 1024); anatstack_build(P) 61 let suS: i64 = as_part_sdf(P, 32, 0, 0-570, 0-585) 62 // SURPRISE brow (idx 8) raises -> a HIGH brow point flips outside->inside 63 anat_set_expr(0, 0); anatstack_build(P) 64 let brU: i64 = as_part_sdf(P, 8, 0, 380, 0-470) 65 anat_set_expr(2, 1024); anatstack_build(P) 66 let brS: i64 = as_part_sdf(P, 8, 0, 380, 0-470) 67 // FROWN brow (idx 8) lowers -> a LOW brow point flips outside->inside 68 anat_set_expr(0, 0); anatstack_build(P) 69 let frU: i64 = as_part_sdf(P, 8, 0, 262, 0-470) 70 anat_set_expr(3, 1024); anatstack_build(P) 71 let frS: i64 = as_part_sdf(P, 8, 0, 262, 0-470) 72 hw(" smile malar: "); pn(smU); hw("->"); pn(smS); hw(" surprise jaw: "); pn(suU); hw("->"); pn(suS); hw(" surprise brow: "); pn(brU); hw("->"); pn(brS); hw(" frown brow: "); pn(frU); hw("->"); pn(frS); hw("\n" as *u8) 73 var t2: i64 = 0 74 if smU > 0 { if smS < 0 { if suU > 0 { if suS < 0 { if brU > 0 { if brS < 0 { if frU > 0 { if frS < 0 { t2 = 1 } } } } } } } } 75 if t2 == 1 { hw("T2 PASS CAUSAL: each expression's muscle move flips its own soft-tissue landmark (smile lifts cheek, surprise drops jaw + raises brow, frown lowers brow)\n" as *u8) } 76 else { fails=fails+1; hw("T2 FAIL\n" as *u8) } 77 78 // ---- T3 VISIBLE: each expression changes the rendered face ---- 79 let n2: i64 = 512*512 80 let fN: *i64 = sys_mmap(n2*8) as *i64 81 let fX: *i64 = sys_mmap(n2*8) as *i64 82 anat_set_expr(0, 0); anatstack_build(P); anatstack_render(P, np, 0, 3, 2, 42, 236,194,176, fN) 83 anat_set_expr(1, 1024); anatstack_build(P); anatstack_render(P, np, 0, 3, 2, 42, 236,194,176, fX) 84 let dSmile: i64 = imdiff(fN, fX, n2) 85 anat_set_expr(2, 1024); anatstack_build(P); anatstack_render(P, np, 0, 3, 2, 42, 236,194,176, fX) 86 let dSurp: i64 = imdiff(fN, fX, n2) 87 anat_set_expr(3, 1024); anatstack_build(P); anatstack_render(P, np, 0, 3, 2, 42, 236,194,176, fX) 88 let dFrown: i64 = imdiff(fN, fX, n2) 89 hw(" render imdiff vs neutral: smile="); pn(dSmile); hw(" surprise="); pn(dSurp); hw(" frown="); pn(dFrown); hw("\n" as *u8) 90 var t3: i64 = 0 91 if dSmile > 1500 { if dSurp > 3000 { if dFrown > 800 { t3 = 1 } } } 92 if t3 == 1 { hw("T3 PASS VISIBLE: every expression measurably re-shapes the rendered face (surprise the most -- jaw opens)\n" as *u8) } 93 else { fails=fails+1; hw("T3 FAIL\n" as *u8) } 94 95 // ---- T4 determinism ---- 96 anat_set_expr(1, 1024); anatstack_build(P) 97 let da: i64 = as_part_sdf(P, 25, 0-262, 70, 0-475) 98 anatstack_build(P) 99 let db: i64 = as_part_sdf(P, 25, 0-262, 70, 0-475) 100 var t4: i64 = 0 101 if da == db { t4 = 1 } 102 if t4 == 1 { hw("T4 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T4 FAIL\n" as *u8) } 103 104 // ---- T5 evidence strip: neutral | smile | surprise | frown ---- 105 let GW: i64 = 190*4 106 let GH: i64 = 250 107 let gal: *i64 = sys_mmap(GW*GH*8) as *i64 108 var i: i64 = 0 109 while i < GW*GH { gal[i] = 26 + 28*256 + 44*65536; i = i + 1 } 110 face_col(P, gal, GW, GH, 0, 0) 111 face_col(P, gal, GW, GH, 1, 1) 112 face_col(P, gal, GW, GH, 2, 2) 113 face_col(P, gal, GW, GH, 3, 3) 114 write_png(gal, GW, GH, "knowledge/nx_face_expr.png" as *u8) 115 hw("T5 evidence -> knowledge/nx_face_expr.png (neutral | smile | surprise | frown)\n" as *u8) 116 117 if fails == 0 { hw("FACE-EXPR-GATE GREEN -- R-face: muscle-driven expressions (smile/surprise/frown) on the inside-out head, each causally isolated to its own soft-tissue landmark + visible in render. Residual: FACS action-unit basis, learned blendshapes from capture, lip-sync visemes, asymmetric expressions.\n" as *u8); sys_exit(0); return 0 } 118 hw("FACE-EXPR-GATE RED fails="); pn(fails); hw("\n" as *u8) 119 sys_exit(1) 120 return 1 121}