code wiki / _hdl_build / nx_procgen_human_gate.nx

nx_procgen_human_gate.nx source

↩ module page · 191 lines · 10191 B

1// nx_procgen_human_gate.nx -- Gx/PROCGEN: parametric human figures from the eye-keystone ANTHROPOMETRIC CANON 2// (phi/Vitruvian/Loomis). The companion half of "don't overfit to one human": each figure's measurements are 3// canon RATIOS of one keystone (head height), seed-varied WITHIN canon ranges -> diverse but always canon-valid. 4// 5 seeds side-by-side. Integer/sovereign/asset-free. HONEST: this is parametric-PROPORTION diversity (a clean 5// mannequin), NOT photoreal fidelity -- the same clay gap named on /world/graphics. It proves canon-valid variety. 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_png_write.nx" 8 9func ph_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func ph_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 } 11func ph_rng(seed: i64, salt: i64) -> i64 { var a: i64=(seed+1)*2654435761 + (salt+1)*40503; a=a%2147483647; if a<0{a=a+2147483647} a=(a*1103515245+12345)%2147483647; if a<0{a=a+2147483647} return a } 12// pick in [lo,hi] from seed/salt 13func ph_pick(seed: i64, salt: i64, lo: i64, hi: i64) -> i64 { let r: i64=ph_rng(seed,salt); return lo + r%(hi-lo+1) } 14 15func main() -> i64 { 16 ph_puts("=== nx_procgen_human_gate -- parametric canon-diverse human figures (companion, no overfit) ===\n" as *u8) 17 var fails: i64=0 18 let CW: i64=180; let W: i64=CW*5; let H: i64=280 19 let rgb: *u8=sys_mmap(W*H*3+16) 20 var i: i64=0 21 while i<W*H { let o: i64=i*3; rgb[o]=16 as u8; rgb[o+1]=18 as u8; rgb[o+2]=26 as u8; i=i+1 } 22 // per-figure canon params, stored for the gate 23 let pheads: *i64=sys_mmap(5*8) as *i64 24 let pshw: *i64=sys_mmap(5*8) as *i64 25 let pwsw: *i64=sys_mmap(5*8) as *i64 26 let phpw: *i64=sys_mmap(5*8) as *i64 27 let pnavel: *i64=sys_mmap(5*8) as *i64 28 var totallit: i64=0 29 var s: i64=0 30 while s<5 { 31 let seed: i64=s*1013 + 7 32 // CANON: total height = heads*head_h ; 1 head = 7 eye-widths ; measures = ratio_ew * head_h / 7 33 let heads10: i64=ph_pick(seed,1,70,80) // 7.0..8.0 heads 34 let Hfig: i64=228 35 let hh: i64=(Hfig*10)/heads10 // head height px 36 let ytop: i64=H-16-Hfig // top of head 37 let base: i64=H-16 // feet baseline 38 let cx: i64=s*CW + CW/2 39 // canon eye-width ratios (x10) -> px half-widths 40 let shoulder_ew10: i64=ph_pick(seed,2,130,180) // 13..18 ew 41 let waist_ew10: i64=ph_pick(seed,3,32,52) // 3.2..5.2 ew 42 let hip_ew10: i64=ph_pick(seed,4,46,66) // 4.6..6.6 ew 43 let sh_hw: i64=(shoulder_ew10*hh)/70/2 // (ew10/10 * hh/7)/2 44 var ws_hw: i64=(waist_ew10*hh)/70/2 45 let hp_hw: i64=(hip_ew10*hh)/70/2 46 if ws_hw<3 { ws_hw=3 } 47 pheads[s]=heads10; pshw[s]=sh_hw*2; pwsw[s]=ws_hw*2; phpw[s]=hp_hw*2 48 // key Y (from top, in head units) 49 let y_chin: i64=ytop+hh 50 let y_shoulder: i64=ytop+(hh*13)/10 51 let y_waist: i64=ytop+hh*3 52 let y_hip: i64=ytop+(hh*35)/10 53 pnavel[s]=(base-y_waist)*1000/Hfig // navel-from-ground / height *1000 (canon ~618) 54 // skin tone (seed-varied) 55 let tone: i64=ph_pick(seed,5,0,4) 56 var skr: i64=224; var skg: i64=172; var skb: i64=138 57 if tone==1 { skr=196; skg=140; skb=108 } 58 if tone==2 { skr=150; skg=100; skb=72 } 59 if tone==3 { skr=110; skg=72; skb=52 } 60 if tone==4 { skr=238; skg=200; skb=170 } 61 // ---- draw torso silhouette (shoulder -> waist -> hip), volume-shaded ---- 62 var py: i64=y_shoulder 63 while py<=y_hip { 64 var hw: i64=0 65 if py<=y_waist { let t: i64=((py-y_shoulder)*1000)/(y_waist-y_shoulder+1); hw=sh_hw+((ws_hw-sh_hw)*t)/1000 } 66 else { let t: i64=((py-y_waist)*1000)/(y_hip-y_waist+1); hw=ws_hw+((hp_hw-ws_hw)*t)/1000 } 67 var px: i64=cx-hw 68 while px<=cx+hw { 69 // simple center-lit volume shade 70 var d: i64=px-cx; if d<0 {d=0-d} 71 var sh: i64=255 - (d*90)/(hw+1) 72 if sh<120 {sh=120} 73 if px>=0 { if px<W { if py>=0 { if py<H { 74 let o: i64=(py*W+px)*3 75 rgb[o]=((skr*sh)/255) as u8; rgb[o+1]=((skg*sh)/255) as u8; rgb[o+2]=((skb*sh)/255) as u8 76 totallit=totallit+1 77 } } } } 78 px=px+1 79 } 80 py=py+1 81 } 82 // ---- legs: hip -> feet, two tapered bars ---- 83 var lg: i64=0 84 while lg<2 { 85 let lx0: i64=cx + (lg*2-1)*(hp_hw/2) 86 var yy: i64=y_hip 87 while yy<=base { 88 let t: i64=((yy-y_hip)*1000)/(base-y_hip+1) 89 let lw: i64=(hp_hw*6)/10 - ((hp_hw*4)/10 * t)/1000 // thigh -> ankle taper 90 var px: i64=lx0-lw 91 while px<=lx0+lw { 92 if px>=0 { if px<W { if yy>=0 { if yy<H { 93 var d: i64=px-lx0; if d<0 {d=0-d} 94 var sh: i64=255-(d*100)/(lw+1); if sh<115 {sh=115} 95 let o: i64=(yy*W+px)*3 96 rgb[o]=((skr*sh)/255) as u8; rgb[o+1]=((skg*sh)/255) as u8; rgb[o+2]=((skb*sh)/255) as u8 97 } } } } 98 px=px+1 99 } 100 yy=yy+1 101 } 102 lg=lg+1 103 } 104 // ---- arms: shoulder -> down the sides, two tapered bars ---- 105 var ar: i64=0 106 while ar<2 { 107 let axs: i64=cx + (ar*2-1)*sh_hw 108 let axe: i64=cx + (ar*2-1)*(hp_hw+ (hh*7)/10) 109 var yy: i64=y_shoulder 110 let armlen: i64=(hh*33)/10 111 let yend: i64=y_shoulder+armlen 112 while yy<=yend { 113 let t: i64=((yy-y_shoulder)*1000)/(armlen+1) 114 let ax: i64=axs + ((axe-axs)*t)/1000 115 let aw: i64=(hh*4)/10 - ((hh*25)/100 * t)/1000 116 var px: i64=ax-aw 117 while px<=ax+aw { 118 if px>=0 { if px<W { if yy>=0 { if yy<H { 119 var d: i64=px-ax; if d<0 {d=0-d} 120 var sh: i64=255-(d*100)/(aw+1); if sh<115 {sh=115} 121 let o: i64=(yy*W+px)*3 122 rgb[o]=((skr*sh)/255) as u8; rgb[o+1]=((skg*sh)/255) as u8; rgb[o+2]=((skb*sh)/255) as u8 123 } } } } 124 px=px+1 125 } 126 yy=yy+1 127 } 128 ar=ar+1 129 } 130 // ---- neck + head (ellipse: w=5ew, h=7ew=hh) ---- 131 let neckw: i64=(hh*13)/100 132 var ny: i64=y_chin-2 133 while ny<=y_shoulder { var px: i64=cx-neckw; while px<=cx+neckw { if px>=0 { if px<W { let o: i64=(ny*W+px)*3; rgb[o]=((skr*220)/255) as u8; rgb[o+1]=((skg*220)/255) as u8; rgb[o+2]=((skb*220)/255) as u8 } } px=px+1 } ny=ny+1 } 134 let hrx: i64=(hh*5)/14; let hry: i64=hh/2; let hcx: i64=cx; let hcy: i64=ytop+hry 135 var ey: i64=hcy-hry 136 while ey<=hcy+hry { 137 var ex: i64=hcx-hrx 138 while ex<=hcx+hrx { 139 let dxv: i64=ex-hcx; let dyv: i64=ey-hcy 140 if (dxv*dxv)*(hry*hry) + (dyv*dyv)*(hrx*hrx) <= (hrx*hrx)*(hry*hry) { 141 if ex>=0 { if ex<W { if ey>=0 { if ey<H { 142 var d: i64=ex-hcx; if d<0 {d=0-d} 143 var sh: i64=255-(d*80)/(hrx+1); if sh<140 {sh=140} 144 let o: i64=(ey*W+ex)*3 145 rgb[o]=((skr*sh)/255) as u8; rgb[o+1]=((skg*sh)/255) as u8; rgb[o+2]=((skb*sh)/255) as u8 146 totallit=totallit+1 147 } } } } 148 } 149 ex=ex+1 150 } 151 ey=ey+1 152 } 153 ph_puts(" fig "); ph_pn(s); ph_puts(": heads="); ph_pn(heads10); ph_puts(" shoulder_ew="); ph_pn(shoulder_ew10); ph_puts(" waist_ew="); ph_pn(waist_ew10); ph_puts(" navel/h*1000="); ph_pn(pnavel[s]); ph_puts("\n" as *u8) 154 s=s+1 155 } 156 nx_png_write_rgb("knowledge/nx_procgen_human.png\x00" as *u8, rgb, W, H) 157 ph_puts(" wrote knowledge/nx_procgen_human.png\n" as *u8) 158 // T1 rendered 159 var t1: i64=0 160 if totallit>3000 { t1=1 } 161 if t1==1 { ph_puts("T1 PASS 5 figures rendered\n" as *u8) } else { fails=fails+1; ph_puts("T1 FAIL\n" as *u8) } 162 // T2 canon-valid: heads in [70,80]; navel/height in golden band [560,680] (~1/phi=618); shoulder>waist 163 var t2: i64=1 164 var k: i64=0 165 while k<5 { 166 if pheads[k]<70 { t2=0 } if pheads[k]>80 { t2=0 } 167 if pnavel[k]<540 { t2=0 } if pnavel[k]>700 { t2=0 } 168 if pshw[k]<=pwsw[k] { t2=0 } 169 k=k+1 170 } 171 ph_puts(" canon check: navel-golden-band + heads 7-8 + shoulder>waist\n" as *u8) 172 if t2==1 { ph_puts("T2 PASS all figures CANON-VALID (phi navel, 7-8 heads, taper)\n" as *u8) } else { fails=fails+1; ph_puts("T2 FAIL a figure off-canon\n" as *u8) } 173 // T3 diversity 174 var dmax: i64=0; var dmin: i64=9999 175 k=0 176 while k<5 { if pshw[k]>dmax {dmax=pshw[k]} if pshw[k]<dmin {dmin=pshw[k]} k=k+1 } 177 ph_puts(" shoulder-width spread="); ph_pn(dmax-dmin); ph_puts("\n" as *u8) 178 var t3: i64=0 179 if dmax-dmin>10 { t3=1 } 180 if t3==1 { ph_puts("T3 PASS figures DIVERSE (parametric, no overfit to one human)\n" as *u8) } else { fails=fails+1; ph_puts("T3 FAIL not diverse\n" as *u8) } 181 // T4 png 182 let szp: *i64=sys_mmap(16) as *i64 183 let rb: *u8=sys_read_file("knowledge/nx_procgen_human.png\x00" as *u8, szp) 184 var t4: i64=0 185 if (rb as i64)!=0 { if szp[0]>1000 { t4=1 } } 186 if t4==1 { ph_puts("T4 PASS PNG ("); ph_pn(szp[0]); ph_puts(" bytes)\n" as *u8) } else { fails=fails+1; ph_puts("T4 FAIL png\n" as *u8) } 187 if fails==0 { ph_puts("PROCGEN-HUMAN verdict=GREEN -- canon-valid parametric human diversity (companion, no overfit), sovereign integer\n" as *u8); sys_exit(0); return 0 } 188 ph_puts("PROCGEN-HUMAN RED fails="); ph_pn(fails); ph_puts("\n" as *u8) 189 sys_exit(1) 190 return 1 191}