code wiki / _hdl_build / nx_gsplat_elara_gate.nx

nx_gsplat_elara_gate.nx source

↩ module page · 153 lines · 7798 B

1// nx_gsplat_elara_gate.nx -- ★FIT GAUSSIANS TO THE ACTUAL ELARA PHOTO (operator correction 2026-07-08: "that's 2// not the elara photo, that's the clay thing" -- my earlier photofit target was our SDF CLAY render, not the 3// real photograph). Here the TARGET is the real Elara photo (elara_face_hi.jpg) directly. A cloud of Gaussians 4// starts GREY and the differentiable-splatting optimizer fits their colours to REPRODUCE THE PHOTO -> the splat 5// looks like Elara (not clay). HONEST BOUNDARY: this is a SINGLE-VIEW image fit (a 3D-Gaussian representation of 6// the 2D photo) -- it reproduces Elara from the front; true 3D-consistent Elara from one photo needs 7// single-image-3D reconstruction (the neural frontier method), a trained net. But the SPLAT + OPTIMIZER are ours. 8// T1 the optimizer reproduces the ELARA PHOTO: L1(render, photo) drops hard from the grey start (< 12%) 9// T2 close match (final per-pixel error small) = the splat looks like the photo 10// T3 PNG knowledge/nx_gsplat_elara.png (elara photo | grey start | fitted) + determinism 11// license_tier: ORIGINAL expect_exit: 0 12import "nx_syscalls.nx" 13import "nx_jpeg_ascii.nx" 14import "nx_gsplat.nx" 15import "nx_png.nx" 16 17func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func 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 } 19func l1(a: *i64, b: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { var dr: i64=(a[i]&255)-(b[i]&255); if dr<0{dr=0-dr} var dg: i64=((a[i]>>8)&255)-((b[i]>>8)&255); if dg<0{dg=0-dg} var db: i64=((a[i]>>16)&255)-((b[i]>>16)&255); if db<0{db=0-db} s=s+dr+dg+db; i=i+1 } return s } 20 21const R: i64 = 4096 // camz 4 * 1024 22const FOC: i64 = 586 23const CROP_Y0: i64 = 110 // crop the 512x640 photo to the 512x384 face region 24 25func main() -> i64 { 26 hw("=== nx_gsplat_elara_gate -- fit Gaussians to the ACTUAL Elara photo (not the clay render) ===\n" as *u8) 27 var fails: i64 = 0 28 let W: i64 = gs_w() 29 let H: i64 = gs_h() 30 let npx: i64 = W*H 31 32 // decode the real Elara photo 33 let szp: *i64 = sys_mmap(16) as *i64 34 let jpeg: *u8 = sys_read_file("knowledge/elara_face_hi.jpg" as *u8, szp) 35 if (jpeg as i64) == 0 { hw("no elara photo\n" as *u8); return 1 } 36 let rp: *i64 = sys_mmap(8) as *i64; let wp: *i64 = sys_mmap(8) as *i64; let hp: *i64 = sys_mmap(8) as *i64 37 if nx_jpeg_decode_rgb(jpeg, szp[0], rp, wp, hp) != NX_JPEG_ASCII_OK { hw("decode fail\n" as *u8); return 1 } 38 let rgb: *u8 = rp[0] as *u8 39 let tw: i64 = wp[0] 40 let th: i64 = hp[0] 41 42 // TARGET = the Elara photo, face region, resampled into the 512x384 render frame 43 let target: *i64 = sys_mmap(npx*8) as *i64 44 var y: i64 = 0 45 while y < H { 46 var x: i64 = 0 47 while x < W { 48 var sx: i64 = x*tw/W 49 var sy: i64 = (y*384/H)+CROP_Y0 50 if sx>=tw {sx=tw-1} 51 if sy>=th {sy=th-1} 52 let o: i64 = (sy*tw+sx)*3 53 target[y*W+x] = (rgb[o] as i64 &255) + (rgb[o+1] as i64 &255)*256 + (rgb[o+2] as i64 &255)*65536 54 x = x + 1 55 } 56 y = y + 1 57 } 58 59 // a GRID of Gaussians (one per 4x4 screen block) on the z=0 plane; start GREY 60 let NGX: i64 = 13000 61 let gauss: *i64 = sys_mmap(NGX*8*8) as *i64 62 var ng: i64 = 0 63 var gy: i64 = 0 64 while gy < 96 { 65 var gx: i64 = 0 66 while gx < 128 { 67 let sx: i64 = gx*4+2 68 let sy: i64 = gy*4+2 69 let mx: i64 = (sx-256)*R/FOC 70 let my: i64 = (192-sy)*R/FOC 71 gs_set(gauss, ng, mx, my, 0, 21, 128, 128, 128, 255) 72 ng = ng + 1 73 gx = gx + 1 74 } 75 gy = gy + 1 76 } 77 78 // scratch 79 let fb: *i64 = sys_mmap(npx*8) as *i64 80 let acc: *i64 = sys_mmap(npx*3*8) as *i64 81 let trans: *i64 = sys_mmap(npx*8) as *i64 82 let depth: *i64 = sys_mmap(NGX*8) as *i64 83 let sxb: *i64 = sys_mmap(NGX*8) as *i64 84 let syb: *i64 = sys_mmap(NGX*8) as *i64 85 let sigb: *i64 = sys_mmap(NGX*8) as *i64 86 let order: *i64 = sys_mmap(NGX*8) as *i64 87 let count: *i64 = sys_mmap((gs_nb()+2)*8) as *i64 88 let explut: *i64 = sys_mmap(gs_expn()*8) as *i64 89 let gradbuf: *i64 = sys_mmap(NGX*3*8) as *i64 90 let wnorm: *i64 = sys_mmap(NGX*8) as *i64 91 gs_build_explut(explut) 92 93 // GREY start render + loss 94 gs_render(gauss, ng, 0, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, 26, 28, 44) 95 let grey: *i64 = sys_mmap(npx*8) as *i64 96 var k: i64 = 0 97 while k < npx { grey[k]=fb[k]; k=k+1 } 98 let loss0: i64 = l1(fb, target, npx) 99 100 // OPTIMIZE colours to reproduce the Elara photo 101 hw(" fitting "); pn(ng); hw(" Gaussians to the Elara photo:\n" as *u8) 102 var lossN: i64 = 0 103 var it: i64 = 0 104 while it < 30 { 105 lossN = gs_color_grad_step(gauss, ng, target, 0, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, gradbuf, wnorm) 106 if it < 3 { hw(" iter "); pn(it); hw(" L1="); pn(lossN); hw("\n" as *u8) } 107 it = it + 1 108 } 109 hw(" L1(render, ELARA PHOTO): grey-start="); pn(loss0); hw(" fitted="); pn(lossN); hw(" (drop to "); pn(lossN*100/loss0); hw("%)\n" as *u8) 110 111 var t1: i64 = 0 112 if lossN*100 < loss0*12 { t1 = 1 } 113 if t1 == 1 { hw("T1 PASS the optimizer REPRODUCES the Elara photo (splat fits the real photograph, not clay)\n" as *u8) } 114 else { fails=fails+1; hw("T1 FAIL insufficient fit\n" as *u8) } 115 116 let perpx: i64 = lossN/npx 117 hw(" final per-pixel L1 (/3 ch): "); pn(perpx); hw("\n" as *u8) 118 var t2: i64 = 0 119 if perpx < 24 { t2 = 1 } 120 if t2 == 1 { hw("T2 PASS close match -- the Gaussian splat looks like the Elara photo\n" as *u8) } 121 else { fails=fails+1; hw("T2 FAIL match too loose\n" as *u8) } 122 123 // T3 PNG (elara photo | grey | fitted) + determinism 124 gs_render(gauss, ng, 0, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, 26, 28, 44) 125 let W3: i64 = W*3 126 let combo: *i64 = sys_mmap(W3*H*8) as *i64 127 var yy: i64 = 0 128 while yy < H { 129 var xx: i64 = 0 130 while xx < W { combo[yy*W3+xx]=target[yy*W+xx]; combo[yy*W3+W+xx]=grey[yy*W+xx]; combo[yy*W3+W*2+xx]=fb[yy*W+xx]; xx=xx+1 } 131 yy = yy + 1 132 } 133 write_png(combo, W3, H, "knowledge/nx_gsplat_elara.png" as *u8) 134 // determinism: re-fit fresh (g2 hoisted to *i64 -- subscripting a cast expression miscompiles) 135 let g2: *i64 = sys_mmap(NGX*8*8) as *i64 136 var n2: i64 = 0 137 gy = 0 138 while gy < 96 { var gx2: i64=0; while gx2<128 { let sx: i64=gx2*4+2; let sy: i64=gy*4+2; gs_set(g2, n2, (sx-256)*R/FOC, (192-sy)*R/FOC, 0, 21, 128,128,128,255); n2=n2+1; gx2=gx2+1 } gy=gy+1 } 139 it = 0 140 while it < 30 { gs_color_grad_step(g2, ng, target, 0, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, gradbuf, wnorm); it=it+1 } 141 var cdiff: i64 = 0 142 k = 0 143 while k < ng { if gauss[k*8+4]!=g2[k*8+4] {cdiff=cdiff+1} k=k+1 } 144 var t3: i64 = 0 145 if cdiff == 0 { t3 = 1 } 146 if t3 == 1 { hw("T3 PASS deterministic + PNG knowledge/nx_gsplat_elara.png (Elara photo | grey | fitted)\n" as *u8) } 147 else { fails=fails+1; hw("T3 FAIL nondeterministic cdiff="); pn(cdiff); hw("\n" as *u8) } 148 149 if fails == 0 { hw("GSPLAT-ELARA-GATE 3/3 GREEN -- the sovereign optimizer fits Gaussians to the REAL Elara photo (L1 "); pn(loss0); hw("->"); pn(lossN); hw(") = the splat reproduces Elara, not clay (single-view; 3D needs neural single-image reconstruction)\n" as *u8); sys_exit(0); return 0 } 150 hw("GSPLAT-ELARA-GATE RED fails="); pn(fails); hw("\n" as *u8) 151 sys_exit(1) 152 return 1 153}