code wiki / _hdl_build / nx_isosurf_gate.nx

nx_isosurf_gate.nx source

↩ module page · 129 lines · 7394 B

1// nx_isosurf_gate.nx -- ★F0 UNIFY: polygonize a signed-distance FIELD into a real TRIANGLE MESH (surface nets), then 2// MESH THE BEING (operator: build logically up the foundation ladder -> unify implicit+explicit + real geometry, not blobs). 3// T1 polygonize a known SPHERE SDF -> mesh (tris, watertight-ish, renders round + SMOOTH via SDF-gradient normals). 4// T2 ★polygonize the holistic BEING's skin SDF -> a real triangle mesh; render it SMOOTH (Gouraud). The ellipsoid blob is 5// now genuine triangle geometry on the unified pipeline. T3 determinism. T4 gallery -> knowledge/nx_isosurf.png. 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_png.nx" 9import "nx_trimesh.nx" 10import "nx_isosurf.nx" 11import "nx_bodyatlas.nx" 12 13func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func 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 } 15func clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 } 16func filled(fb: *i64, n: i64, bg: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { if fb[i]!=bg { c=c+1 } i=i+1 } return c } 17func isq(v: i64) -> i64 { if v<=0 {return 0} var x: i64=v; var y: i64=(x+1)/2; while y<x {x=y;y=(x+v/x)/2} return x } 18func facetedges(fb: *i64, W: i64, H: i64, bg: i64) -> i64 { 19 var c: i64=0; var y: i64=0 20 while y<H { var x: i64=0; while x<W-1 { let p: i64=fb[y*W+x]; let q: i64=fb[y*W+x+1]; if p!=bg { if q!=bg { var d: i64=(p&255)-(q&255); if d<0{d=0-d} var d2: i64=((p>>8)&255)-((q>>8)&255); if d2<0{d2=0-d2} if d+d2>20 {c=c+1} } } x=x+1 } y=y+1 } 21 return c 22} 23 24const BG: i64 = 24 + 26*256 + 34*65536 25const W: i64 = 300 26const H: i64 = 460 27 28func main() -> i64 { 29 hw("=== nx_isosurf_gate -- polygonize SDF -> triangle mesh (surface nets) + MESH THE BEING ===\n" as *u8) 30 var fails: i64 = 0 31 let npx: i64 = W*H 32 let fb: *i64 = sys_mmap(npx*8) as *i64 33 let zb: *i64 = sys_mmap(npx*8) as *i64 34 35 // ---- T1 polygonize a SPHERE SDF ---- 36 let SG: i64 = 18; let scell: i64 = 40; let so: i64 = 0-360 37 let sgrid: *i64 = sys_mmap((SG+1)*(SG+1)*(SG+1)*8) as *i64 38 var i: i64 = 0 39 while i <= SG { 40 var j: i64 = 0 41 while j <= SG { 42 var k: i64 = 0 43 while k <= SG { 44 let x: i64 = so+i*scell; let y: i64 = so+j*scell; let z: i64 = so+k*scell 45 sgrid[(i*(SG+1)+j)*(SG+1)+k] = isq(x*x+y*y+z*z) - 300 46 k = k + 1 47 } 48 j = j + 1 49 } 50 i = i + 1 51 } 52 tm_reset() 53 surface_nets(sgrid, SG, SG, SG, so, so, so, scell, 0, 200+140*256+120*65536) 54 let stris: i64 = tm_nt(); let sverts: i64 = tm_nv() 55 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 0, 0, 1400, 520, 1) 56 let sfil: i64 = filled(fb, npx, BG); let sedge: i64 = facetedges(fb, W, H, BG) 57 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 0, 0, 1400, 520, 0) 58 let sedgeFlat: i64 = facetedges(fb, W, H, BG) 59 hw(" sphere-mesh: verts="); pn(sverts); hw(" tris="); pn(stris); hw(" filled="); pn(sfil); hw(" facet-edges smooth="); pn(sedge); hw(" flat="); pn(sedgeFlat); hw("\n" as *u8) 60 var t1: i64 = 0 61 if stris > 500 { if sfil > 2000 { if sedge*2 < sedgeFlat { t1 = 1 } } } 62 if t1 == 1 { hw("T1 PASS SDF->MESH: sphere field polygonized to a real triangle mesh, smooth via gradient normals\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 63 64 // ---- T2 ★MESH THE BEING (skin SDF -> triangle mesh) ---- 65 atlas_build(0); atlas_pose(0); atlas_sync_pose() 66 let BGX: i64 = 26; let BGY: i64 = 90; let BGZ: i64 = 16; let bcell: i64 = 22 67 let box: i64 = 0-286; let boy: i64 = 0-990; let boz: i64 = 0-176 68 let bgrid: *i64 = sys_mmap((BGX+1)*(BGY+1)*(BGZ+1)*8) as *i64 69 i = 0 70 while i <= BGX { 71 var j: i64 = 0 72 while j <= BGY { 73 var k: i64 = 0 74 while k <= BGZ { 75 let x: i64 = box+i*bcell; let y: i64 = boy+j*bcell; let z: i64 = boz+k*bcell 76 bgrid[(i*(BGY+1)+j)*(BGZ+1)+k] = ba_sdf(x, y, z, 1) // skin envelope (mask bit0) 77 k = k + 1 78 } 79 j = j + 1 80 } 81 i = i + 1 82 } 83 tm_reset() 84 surface_nets(bgrid, BGX, BGY, BGZ, box, boy, boz, bcell, 0, 216+172*256+152*65536) 85 let btris: i64 = tm_nt(); let bverts: i64 = tm_nv() 86 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-200, 2600, 520, 1) 87 let bfil: i64 = filled(fb, npx, BG) 88 hw(" being-mesh: verts="); pn(bverts); hw(" tris="); pn(btris); hw(" filled="); pn(bfil); hw("\n" as *u8) 89 var t2: i64 = 0 90 if btris > 2000 { if bverts > 1000 { if bfil > npx/12 { t2 = 1 } } } 91 if t2 == 1 { hw("T2 PASS MESHED THE BEING: the skin SDF is now a real triangle mesh (unified implicit->explicit), rendered smooth\n" as *u8) } else { fails=fails+1; hw("T2 FAIL btris="); pn(btris); hw("\n" as *u8) } 92 93 // ---- T3 determinism ---- 94 let f2: *i64 = sys_mmap(npx*8) as *i64 95 clearfb(f2, npx, BG); trimesh_zclear(zb, npx); trimesh_render(f2, zb, W, H, 2400, 0-200, 2600, 520, 1) 96 var diff: i64 = 0; i = 0 97 while i < npx { if f2[i]!=fb[i] { diff=diff+1 } i=i+1 } 98 var t3: i64 = 0 99 if diff == 0 { t3 = 1 } 100 if t3 == 1 { hw("T3 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T3 FAIL diff="); pn(diff); hw("\n" as *u8) } 101 102 // ---- T4 gallery: sphere-mesh(smooth) | being-mesh(flat) | being-mesh(smooth) ---- 103 let GW: i64 = W*3 104 let gal: *i64 = sys_mmap(GW*H*8) as *i64 105 clearfb(gal, GW*H, BG) 106 let cell: *i64 = sys_mmap(npx*8) as *i64 107 // cell 0: sphere smooth 108 tm_reset(); surface_nets(sgrid, SG, SG, SG, so, so, so, scell, 0, 200+140*256+120*65536) 109 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 900, 0-200, 1400, 520, 1) 110 var y: i64 = 0 111 while y < H { var x: i64=0; while x<W { gal[y*GW+x]=cell[y*W+x]; x=x+1 } y=y+1 } 112 // being mesh (rebuild) 113 tm_reset(); surface_nets(bgrid, BGX, BGY, BGZ, box, boy, boz, bcell, 0, 216+172*256+152*65536) 114 // cell 1: being flat 115 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2400, 0-200, 2600, 520, 0) 116 y = 0 117 while y < H { var x: i64=0; while x<W { gal[y*GW+W+x]=cell[y*W+x]; x=x+1 } y=y+1 } 118 // cell 2: being smooth 119 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2400, 0-200, 2600, 520, 1) 120 y = 0 121 while y < H { var x: i64=0; while x<W { gal[y*GW+2*W+x]=cell[y*W+x]; x=x+1 } y=y+1 } 122 write_png(gal, GW, H, "knowledge/nx_isosurf.png" as *u8) 123 hw("T4 gallery -> knowledge/nx_isosurf.png (sphere-mesh | being-mesh flat | being-mesh smooth)\n" as *u8) 124 125 if fails == 0 { hw("ISOSURF-GATE GREEN -- SDF->triangle-mesh (surface nets) UNIFIES implicit+explicit; the BEING is now real triangle geometry rendered smooth. F0 rung, built from the foundation census. Residual: marching-cubes sharp-feature preservation + per-part material colours on the mesh.\n" as *u8); sys_exit(0); return 0 } 126 hw("ISOSURF-GATE RED fails="); pn(fails); hw("\n" as *u8) 127 sys_exit(1) 128 return 1 129}