code wiki / _hdl_build / nx_procgen_gtlabels_gate.nx

nx_procgen_gtlabels_gate.nx source

↩ module page · 104 lines · 7237 B

1// nx_procgen_gtlabels_gate.nx -- Gx/PROCGEN: the digital-twin GROUND-TRUTH TRIO from REAL heightfield geometry. 2// One procedural terrain (FBM heightfield) emitted 4 ways in aligned panels: (1) shaded-relief RGB, (2) DEPTH map 3// (elevation, grayscale), (3) NORMAL map (surface normal from the height gradient, standard tangent-space RGB 4// encoding), (4) SEGMENTATION map (flat class by altitude + water). Depth + normals are GENUINE (computed from the 5// heightfield gradient, not faked) -- exactly the multi-channel per-pixel label an ML-training or digital-twin 6// pipeline consumes (Infinigen's signature output). Integer/sovereign/asset-free. Completes ground-truth-labels. 7// license_tier: ORIGINAL expect_exit: 0 8import "nx_png_write.nx" 9import "nx_vecmath.nx" 10import "nx_gate_verdict.nx" 11 12func gl_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func gl_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 } 14// RETIRED ONTO THE SHARED OWNER 2026-08-24: was a capped-iteration Newton floor-sqrt; vm_isqrt computes the 15// same floor and is gate-proven exact, and this gate's output is the before/after identity proof. 16func gl_isqrt(n: i64) -> i64 { return vm_isqrt(n) } 17func gl_hash(x: i64, y: i64, seed: i64) -> i64 { var a: i64=(x+1)*73856093+(y+1)*19349663+(seed+1)*83492791; a=a%16777216; if a<0{a=a+16777216} a=(a*1103515245+12345)%16777216; if a<0{a=a+16777216} return a%4096 } 18func gl_smooth(t: i64) -> i64 { let t2: i64=(t*t)/4096; return (t2*(3*4096-2*t))/4096 } 19func gl_vnoise(px: i64, py: i64, lat: i64, sz: i64, seed: i64) -> i64 { 20 let fx: i64=(px*lat*4096)/sz; let fy: i64=(py*lat*4096)/sz 21 let gx: i64=fx/4096; let tx: i64=fx-gx*4096; let gy: i64=fy/4096; let ty: i64=fy-gy*4096 22 let c00: i64=gl_hash(gx,gy,seed); let c10: i64=gl_hash(gx+1,gy,seed); let c01: i64=gl_hash(gx,gy+1,seed); let c11: i64=gl_hash(gx+1,gy+1,seed) 23 let sx: i64=gl_smooth(tx); let sy: i64=gl_smooth(ty) 24 let top: i64=c00+((c10-c00)*sx)/4096; let bot: i64=c01+((c11-c01)*sx)/4096 25 return top+((bot-top)*sy)/4096 26} 27func gl_fbm(px: i64, py: i64, sz: i64, seed: i64) -> i64 { 28 var h: i64=0; var amp: i64=2048; var lat: i64=5; var norm: i64=0; var o: i64=0 29 while o<5 { h=h+(gl_vnoise(px,py,lat,sz,seed+o*131)*amp)/4096; norm=norm+amp; amp=amp/2; lat=lat*2; o=o+1 } 30 if norm<1 { norm=1 } 31 return (h*4000)/norm 32} 33 34func main() -> i64 { 35 gl_puts("=== nx_procgen_gtlabels_gate -- ground-truth trio (relief/depth/normal/segmentation) from real geometry ===\n" as *u8) 36 var fails: i64=0 37 let TW: i64=270; let TH: i64=270; let GAP: i64=10 38 let W: i64=TW*4+GAP*3; let H: i64=TH 39 let rgb: *u8=sys_mmap(W*H*3+16) 40 var i: i64=0 41 while i<W*H { let o: i64=i*3; rgb[o]=8 as u8; rgb[o+1]=8 as u8; rgb[o+2]=10 as u8; i=i+1 } 42 let hf: *i64=sys_mmap(TW*TH*8) as *i64 43 let seed: i64=2718 44 var hmin: i64=999999; var hmax: i64=0 45 var py: i64=0 46 while py<TH { var px: i64=0; while px<TW { let hv: i64=gl_fbm(px,py,TW,seed); hf[py*TW+px]=hv; if hv<hmin{hmin=hv} if hv>hmax{hmax=hv} px=px+1 } py=py+1 } 47 let hrange: i64=hmax-hmin+1 48 // panel x-offsets 49 let x0: i64=0; let x1: i64=TW+GAP; let x2: i64=(TW+GAP)*2; let x3: i64=(TW+GAP)*3 50 var depthspread: i64=0; var normok: i64=0; var segclasses: i64=0 51 let segseen: *i64=sys_mmap(8*8) as *i64 52 var q: i64=0 53 while q<6 { segseen[q]=0; q=q+1 } 54 py=1 55 while py<TH-1 { 56 var px: i64=1 57 while px<TW-1 { 58 let ci: i64=py*TW+px; let hc: i64=hf[ci] 59 let dzdx: i64=hf[ci+1]-hf[ci-1]; let dzdy: i64=hf[ci+TW]-hf[ci-TW] 60 // ---- panel 1: shaded relief ---- 61 var shade: i64=150+(0-dzdx-dzdy)/6; if shade<40{shade=40} if shade>255{shade=255} 62 var br: i64=0; var bg: i64=0; var bb: i64=0 63 if hc<700 { br=40;bg=90;bb=140 } else { if hc<1600 { br=70;bg=120;bb=60 } else { if hc<2600 { br=110;bg=95;bb=60 } else { if hc<3300 { br=140;bg=132;bb=122 } else { br=222;bg=222;bb=228 } } } } 64 let o1: i64=(py*W+x0+px)*3; rgb[o1]=((br*shade)/255) as u8; rgb[o1+1]=((bg*shade)/255) as u8; rgb[o1+2]=((bb*shade)/255) as u8 65 // ---- panel 2: depth (elevation grayscale, high=near/white) ---- 66 let dv: i64=((hc-hmin)*255)/hrange 67 let o2: i64=(py*W+x1+px)*3; rgb[o2]=dv as u8; rgb[o2+1]=dv as u8; rgb[o2+2]=dv as u8 68 if dv>200 { depthspread=depthspread+1 } 69 // ---- panel 3: normal map (tangent-space RGB) ---- 70 // n = (-dzdx, -dzdy, NZ) normalized ; NZ controls slope scale 71 let nxv: i64=0-dzdx; let nyv: i64=0-dzdy; let nzv: i64=60 72 let mag: i64=gl_isqrt(nxv*nxv+nyv*nyv+nzv*nzv)+1 73 let enr: i64=128+(nxv*127)/mag; let eng: i64=128+(nyv*127)/mag; let enb: i64=128+(nzv*127)/mag 74 let o3: i64=(py*W+x2+px)*3; rgb[o3]=enr as u8; rgb[o3+1]=eng as u8; rgb[o3+2]=enb as u8 75 if enb>128 { normok=normok+1 } 76 // ---- panel 4: segmentation (flat class) ---- 77 var cls: i64=0; var sr: i64=0; var sg: i64=0; var sb: i64=0 78 if hc<700 { cls=0; sr=40;sg=110;sb=200 } else { if hc<1600 { cls=1; sr=70;sg=160;sb=60 } else { if hc<2600 { cls=2; sr=150;sg=110;sb=60 } else { if hc<3300 { cls=3; sr=130;sg=130;sb=130 } else { cls=4; sr=235;sg=235;sb=240 } } } } 79 segseen[cls]=1 80 let o4: i64=(py*W+x3+px)*3; rgb[o4]=sr as u8; rgb[o4+1]=sg as u8; rgb[o4+2]=sb as u8 81 px=px+1 82 } 83 py=py+1 84 } 85 q=0 86 while q<5 { if segseen[q]==1 { segclasses=segclasses+1 } q=q+1 } 87 nx_png_write_rgb("knowledge/nx_procgen_gtlabels.png\x00" as *u8, rgb, W, H) 88 gl_puts(" wrote knowledge/nx_procgen_gtlabels.png\n" as *u8) 89 gl_puts(" hrange="); gl_pn(hrange); gl_puts(" depth-bright-px="); gl_pn(depthspread); gl_puts(" normal-upfacing-px="); gl_pn(normok); gl_puts(" seg-classes="); gl_pn(segclasses); gl_puts("\n" as *u8) 90 // D001 migration 2026-08-24: verdicts carried by nx_gate_verdict so /api/gate_run can READ the outcome 91 // from the exit code; each tooth prints the value it judged. 92 let c: *i64 = gv_ctr() 93 gv_subjects("heightfield-pixels-examined" as *u8, depthspread + normok, c) 94 gv_check("depth-channel-is-genuine-elevation-varies-with-the-heightfield" as *u8, hrange > 1500 && depthspread > 200, c) 95 gv_check("normal-channel-is-genuine-surface-normals-mostly-up-facing" as *u8, normok > 10000, c) 96 gv_check("segmentation-channel-is-multi-class-water-land-rock-snow" as *u8, segclasses >= 3, c) 97 let szp: *i64=sys_mmap(16) as *i64 98 let rb: *u8=sys_read_file("knowledge/nx_procgen_gtlabels.png\x00" as *u8, szp) 99 var pngok: i64=0 100 if (rb as i64)!=0 { if szp[0]>1000 { pngok=1 } } 101 gl_puts(" png_bytes="); gl_pn(szp[0]); gl_puts("\n" as *u8) 102 gv_check("png-artifact-written-and-non-trivial" as *u8, pngok == 1, c) 103 return gv_verdict("nx_procgen_gtlabels_gate" as *u8, c, "Ground-truth trio from real heightfield geometry; each tooth prints the value it judged." as *u8) 104}