code wiki / _hdl_build / nx_bodyatlas_mesh_gate.nx
nx_bodyatlas_mesh_gate.nx source
↩ module page · 140 lines · 7417 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_gate_verdict.nx"
10import "nx_trimesh.nx"
11import "nx_isosurf.nx"
12import "nx_bodyatlas.nx"
13
14func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func 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 }
16func clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 }
17func 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 }
18// reddish (organs): r noticeably > g and > b
19func 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 }
20// pale/ivory (bone): all channels high and roughly balanced
21func 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 }
22
23const BG: i64 = 24 + 26*256 + 34*65536
24const W: i64 = 300
25const H: i64 = 460
26const GX: i64 = 32
27const GY: i64 = 110
28const GZ: i64 = 20
29const GC: i64 = 18
30const OX: i64 = 0-288
31const OY: i64 = 0-990
32const OZ: i64 = 0-180
33
34static GRID: i64
35
36func fill_grid(mask: i64) -> i64 {
37 let g: *i64 = GRID as *i64
38 var i: i64 = 0
39 while i <= GX {
40 var j: i64 = 0
41 while j <= GY {
42 var k: i64 = 0
43 while k <= GZ {
44 g[(i*(GY+1)+j)*(GZ+1)+k] = ba_sdf(OX+i*GC, OY+j*GC, OZ+k*GC, mask)
45 k = k + 1
46 }
47 j = j + 1
48 }
49 i = i + 1
50 }
51 return 0
52}
53// colour every vertex by the nearest visible part (the material)
54func color_by_part(mask: i64) -> i64 {
55 let out: *i64 = sys_mmap(24) as *i64
56 var i: i64 = 0
57 let nv: i64 = tm_nv()
58 while i < nv {
59 tm_vpos(i, out)
60 let k: i64 = ba_nearest(out[0], out[1], out[2], mask)
61 if k >= 0 { tm_vcol(i, ba_part_color(k)) } else { tm_vcol(i, 200+200*256+200*65536) }
62 i = i + 1
63 }
64 return 0
65}
66
67func main() -> i64 {
68 hw("=== nx_bodyatlas_mesh_gate -- meshed anatomy with per-part material colours ===\n" as *u8)
69 var fails: i64 = 0
70 let npx: i64 = W*H
71 let fb: *i64 = sys_mmap(npx*8) as *i64
72 let zb: *i64 = sys_mmap(npx*8) as *i64
73 GRID = sys_mmap((GX+1)*(GY+1)*(GZ+1)*8) as i64
74 atlas_build(0); atlas_pose(0); atlas_sync_pose()
75
76 // ---- T1 skin mesh ----
77 fill_grid(1)
78 tm_reset()
79 surface_nets(GRID as *i64, GX, GY, GZ, OX, OY, OZ, GC, 0, 216+172*256+152*65536)
80 color_by_part(1)
81 let skinV: i64 = tm_nv(); let skinT: i64 = tm_nt()
82 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-200, 2600, 520, 2)
83 let skinF: i64 = filled(fb, npx, BG)
84 hw(" skin-mesh: verts="); pn(skinV); hw(" tris="); pn(skinT); hw(" filled="); pn(skinF); hw("\n" as *u8)
85 var t1: i64 = 0
86 if skinT > 2000 { if skinF > npx/12 { t1 = 1 } }
87 if t1 == 1 { hw("T1 PASS skin meshed + per-vertex coloured\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) }
88
89 // ---- T2 ★VISCERA mesh (organs as their own cluster so they aren't buried under the skeleton union), per-part coloured ----
90 fill_grid(120)
91 tm_reset()
92 surface_nets(GRID as *i64, GX, GY, GZ, OX, OY, OZ, GC, 0, 200+120*256+110*65536)
93 color_by_part(120)
94 let anV: i64 = tm_nv(); let anT: i64 = tm_nt()
95 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 2400, 0-200, 2600, 520, 2)
96 let red: i64 = reddish(fb, npx); let anF: i64 = filled(fb, npx, BG)
97 hw(" viscera-mesh: verts="); pn(anV); hw(" tris="); pn(anT); hw(" filled="); pn(anF); hw(" reddish(organs)="); pn(red); hw("\n" as *u8)
98 var t2: i64 = 0
99 if anT > 1500 { if red > 500 { if anF > npx/50 { t2 = 1 } } } // organs meshed + coloured (red heart/liver/lungs/aorta)
100 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) }
101
102 // ---- T3 determinism ----
103 let f2: *i64 = sys_mmap(npx*8) as *i64
104 clearfb(f2, npx, BG); trimesh_zclear(zb, npx); trimesh_render(f2, zb, W, H, 2400, 0-200, 2600, 520, 2)
105 var diff: i64 = 0; var i: i64 = 0
106 while i < npx { if f2[i]!=fb[i] { diff=diff+1 } i=i+1 }
107 var t3: i64 = 0
108 if diff == 0 { t3 = 1 }
109 if t3 == 1 { hw("T3 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T3 FAIL diff="); pn(diff); hw("\n" as *u8) }
110
111 // ---- T4 gallery: skin mesh | anatomy mesh flat | anatomy mesh coloured ----
112 let GW: i64 = W*3
113 let gal: *i64 = sys_mmap(GW*H*8) as *i64
114 clearfb(gal, GW*H, BG)
115 let cell: *i64 = sys_mmap(npx*8) as *i64
116 // cell 0: skin mesh coloured
117 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)
118 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 2)
119 var y: i64 = 0
120 while y < H { var x: i64=0; while x<W { gal[y*GW+x]=cell[y*W+x]; x=x+1 } y=y+1 }
121 // cell 1: SKELETON mesh (mask 4), flat ivory -- shows the meshed bone with real facets
122 fill_grid(4); tm_reset(); surface_nets(GRID as *i64, GX,GY,GZ, OX,OY,OZ, GC, 0, 232+226*256+205*65536)
123 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 0)
124 y = 0
125 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 }
126 // cell 2: VISCERA mesh (mask 120), per-part coloured + smooth -- the meshed organs
127 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)
128 clearfb(cell, npx, BG); trimesh_zclear(zb, npx); trimesh_render(cell, zb, W, H, 2100, 0-200, 2600, 520, 2)
129 y = 0
130 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 }
131 write_png(gal, GW, H, "knowledge/nx_bodyatlas_mesh.png" as *u8)
132 hw("T4 gallery -> knowledge/nx_bodyatlas_mesh.png (skin mesh | anatomy mesh flat | anatomy mesh coloured)\n" as *u8)
133
134 let ctr: *i64 = gv_ctr()
135 ctr[0] = 4 - fails
136 ctr[1] = 4
137 let rc: i64 = gv_verdict("BODYATLAS-MESH-GATE" as *u8, ctr, "meshed anatomy with per-part skin, bone and viscera materials; deterministic gallery preserved" as *u8)
138 sys_exit(rc)
139 return rc
140}