code wiki / _hdl_build / nx_bodyatlas_mesh_gate.nx

nx_bodyatlas_mesh_gate.nx source

↩ module page · 137 lines · 7599 B

1// nx_bodyatlas_mesh_gate.nx -- ★MESHED ANATOMY with per-part MATERIAL colours (next ladder rung: the meshed being was one 2// skin colour; the being is HOLISTIC, so colour the mesh by anatomy). Polygonize a system-mask SDF (surface nets) then 3// colour each vertex by its NEAREST part -> a real triangle-mesh cutaway: ivory skeleton + coloured organs, smooth. 4// T1 skin mesh renders. T2 ★skeleton+viscera mesh has MULTIPLE materials (ivory bone AND red/pink organs both present). 5// T3 determinism. T4 gallery -> knowledge/nx_bodyatlas_mesh.png (skin mesh | anatomy mesh flat | anatomy mesh coloured). 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 } 17// reddish (organs): r noticeably > g and > b 18func reddish(fb: *i64, n: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { let p: i64=fb[i]; let r: i64=p&255; let g: i64=(p>>8)&255; let b: i64=(p>>16)&255; if r>g+25 { if r>b+25 { c=c+1 } } i=i+1 } return c } 19// pale/ivory (bone): all channels high and roughly balanced 20func pale(fb: *i64, n: i64) -> i64 { var c: i64=0; var i: i64=0; while i<n { let p: i64=fb[i]; let r: i64=p&255; let g: i64=(p>>8)&255; let b: i64=(p>>16)&255; if r>140 { if g>130 { if b>110 { c=c+1 } } } i=i+1 } return c } 21 22const BG: i64 = 24 + 26*256 + 34*65536 23const W: i64 = 300 24const H: i64 = 460 25const GX: i64 = 32 26const GY: i64 = 110 27const GZ: i64 = 20 28const GC: i64 = 18 29const OX: i64 = 0-288 30const OY: i64 = 0-990 31const OZ: i64 = 0-180 32 33static GRID: i64 34 35func fill_grid(mask: i64) -> i64 { 36 let g: *i64 = GRID as *i64 37 var i: i64 = 0 38 while i <= GX { 39 var j: i64 = 0 40 while j <= GY { 41 var k: i64 = 0 42 while k <= GZ { 43 g[(i*(GY+1)+j)*(GZ+1)+k] = ba_sdf(OX+i*GC, OY+j*GC, OZ+k*GC, mask) 44 k = k + 1 45 } 46 j = j + 1 47 } 48 i = i + 1 49 } 50 return 0 51} 52// colour every vertex by the nearest visible part (the material) 53func color_by_part(mask: i64) -> i64 { 54 let out: *i64 = sys_mmap(24) as *i64 55 var i: i64 = 0 56 let nv: i64 = tm_nv() 57 while i < nv { 58 tm_vpos(i, out) 59 let k: i64 = ba_nearest(out[0], out[1], out[2], mask) 60 if k >= 0 { tm_vcol(i, ba_part_color(k)) } else { tm_vcol(i, 200+200*256+200*65536) } 61 i = i + 1 62 } 63 return 0 64} 65 66func main() -> i64 { 67 hw("=== nx_bodyatlas_mesh_gate -- meshed anatomy with per-part material colours ===\n" as *u8) 68 var fails: i64 = 0 69 let npx: i64 = W*H 70 let fb: *i64 = sys_mmap(npx*8) as *i64 71 let zb: *i64 = sys_mmap(npx*8) as *i64 72 GRID = sys_mmap((GX+1)*(GY+1)*(GZ+1)*8) as i64 73 atlas_build(0); atlas_pose(0); atlas_sync_pose() 74 75 // ---- T1 skin mesh ---- 76 fill_grid(1) 77 tm_reset() 78 surface_nets(GRID as *i64, GX, GY, GZ, OX, OY, OZ, GC, 0, 216+172*256+152*65536) 79 color_by_part(1) 80 let skinV: i64 = tm_nv(); let skinT: i64 = tm_nt() 81 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-200, 2600, 520, 2) 82 let skinF: i64 = filled(fb, npx, BG) 83 hw(" skin-mesh: verts="); pn(skinV); hw(" tris="); pn(skinT); hw(" filled="); pn(skinF); hw("\n" as *u8) 84 var t1: i64 = 0 85 if skinT > 2000 { if skinF > npx/12 { t1 = 1 } } 86 if t1 == 1 { hw("T1 PASS skin meshed + per-vertex coloured\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 87 88 // ---- T2 ★VISCERA mesh (organs as their own cluster so they aren't buried under the skeleton union), per-part coloured ---- 89 fill_grid(120) 90 tm_reset() 91 surface_nets(GRID as *i64, GX, GY, GZ, OX, OY, OZ, GC, 0, 200+120*256+110*65536) 92 color_by_part(120) 93 let anV: i64 = tm_nv(); let anT: i64 = tm_nt() 94 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-200, 2600, 520, 2) 95 let red: i64 = reddish(fb, npx); let anF: i64 = filled(fb, npx, BG) 96 hw(" viscera-mesh: verts="); pn(anV); hw(" tris="); pn(anT); hw(" filled="); pn(anF); hw(" reddish(organs)="); pn(red); hw("\n" as *u8) 97 var t2: i64 = 0 98 if anT > 1500 { if red > 500 { if anF > npx/50 { t2 = 1 } } } // organs meshed + coloured (red heart/liver/lungs/aorta) 99 if t2 == 1 { hw("T2 PASS MESHED ORGANS: viscera polygonized into real triangle meshes with per-part material colours\n" as *u8) } else { fails=fails+1; hw("T2 FAIL red="); pn(red); hw(" filled="); pn(anF); hw("\n" as *u8) } 100 101 // ---- T3 determinism ---- 102 let f2: *i64 = sys_mmap(npx*8) as *i64 103 clearfb(f2, npx, BG); trimesh_zclear(zb, npx); trimesh_render(f2, zb, W, H, 2400, 0-200, 2600, 520, 2) 104 var diff: i64 = 0; var i: i64 = 0 105 while i < npx { if f2[i]!=fb[i] { diff=diff+1 } i=i+1 } 106 var t3: i64 = 0 107 if diff == 0 { t3 = 1 } 108 if t3 == 1 { hw("T3 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T3 FAIL diff="); pn(diff); hw("\n" as *u8) } 109 110 // ---- T4 gallery: skin mesh | anatomy mesh flat | anatomy mesh coloured ---- 111 let GW: i64 = W*3 112 let gal: *i64 = sys_mmap(GW*H*8) as *i64 113 clearfb(gal, GW*H, BG) 114 let cell: *i64 = sys_mmap(npx*8) as *i64 115 // cell 0: skin mesh coloured 116 fill_grid(1); tm_reset(); surface_nets(GRID as *i64, GX,GY,GZ, OX,OY,OZ, GC, 0, 216+172*256+152*65536); color_by_part(1) 117 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 2) 118 var y: i64 = 0 119 while y < H { var x: i64=0; while x<W { gal[y*GW+x]=cell[y*W+x]; x=x+1 } y=y+1 } 120 // cell 1: SKELETON mesh (mask 4), flat ivory -- shows the meshed bone with real facets 121 fill_grid(4); tm_reset(); surface_nets(GRID as *i64, GX,GY,GZ, OX,OY,OZ, GC, 0, 232+226*256+205*65536) 122 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 0) 123 y = 0 124 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 } 125 // cell 2: VISCERA mesh (mask 120), per-part coloured + smooth -- the meshed organs 126 fill_grid(120); tm_reset(); surface_nets(GRID as *i64, GX,GY,GZ, OX,OY,OZ, GC, 0, 200+120*256+110*65536); color_by_part(120) 127 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 2) 128 y = 0 129 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 } 130 write_png(gal, GW, H, "knowledge/nx_bodyatlas_mesh.png" as *u8) 131 hw("T4 gallery -> knowledge/nx_bodyatlas_mesh.png (skin mesh | anatomy mesh flat | anatomy mesh coloured)\n" as *u8) 132 133 if fails == 0 { hw("BODYATLAS-MESH-GATE GREEN -- the holistic being is now a real MULTI-MATERIAL triangle mesh: polygonized anatomy with per-part colours (ivory bone + coloured organs), smooth. Unified implicit->explicit + F3 shading, from the census. Residual: separate meshes per system for true peel + glTF export of the coloured mesh.\n" as *u8); sys_exit(0); return 0 } 134 hw("BODYATLAS-MESH-GATE RED fails="); pn(fails); hw("\n" as *u8) 135 sys_exit(1) 136 return 1 137}