code wiki / _hdl_build / nx_meshgen_connected_locations_t335.nx

nx_meshgen_connected_locations_t335.nx source

↩ module page · 183 lines · 9067 B

1// nx_meshgen_gate.nx -- prove the sovereign field->mesh extractor (surface nets) EMITS a valid closed mesh 2// from the pioneer SDF body. T1 emits verts+tris. T2 CLOSED-manifold (F ~= 2V, Euler genus-0). T3 all face 3// indices valid. T4 bounds match the body (tall y-span). T5 manifold vertex usage (>=90% verts used >=3x). 4// T6 writes knowledge/gates/connected-body-sdf-check-t335.stl (the EMITTED mesh, our own code). GREEN = the extractor works -> the bridge 5// from hand-placed SDF to an EMITTED mesh both i2mesh/t2mesh feed. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_sdfrender.nx" 8import "nx_meshgen.nx" 9import "nx_meshcheck.nx" 10import "nx_gltf_export.nx" 11import "nx_meshseg3d.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 hw2(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 15func pn(v: i64) -> i64 { 16 let b: *u8 = sys_mmap(32) as *u8 17 var x: i64 = v 18 var neg: i64 = 0 19 if x < 0 { neg = 1; x = 0 - x } 20 var i: i64 = 31 21 if x == 0 { b[i] = 48 as u8; i = i - 1 } 22 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 23 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 24 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 25 return 0 26} 27func iabs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 28 29// tiny ASCII-STL writer (integer fx1024 coords; units arbitrary) for the record/eyeball in any viewer. 30func stl_num(fd: i64, v: i64) -> i64 { 31 let b: *u8 = sys_mmap(32) as *u8 32 var x: i64 = v 33 var neg: i64 = 0 34 if x < 0 { neg = 1; x = 0 - x } 35 var i: i64 = 31 36 if x == 0 { b[i] = 48 as u8; i = i - 1 } 37 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 38 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 39 sys_write(fd, (b as i64 + i + 1) as *u8, 31 - i) 40 sys_write(fd, " " as *u8, 1) 41 return 0 42} 43func write_stl(vbuf: *i64, fbuf: *i64, nf: i64, path: *u8) -> i64 { 44 let fd: i64 = sys_openat_wr(path, 0x1a4) 45 if fd < 0 { return 0 - 1 } 46 hw2(fd, "solid nishi_emitted\n" as *u8) 47 var t: i64 = 0 48 while t < nf { 49 let a: i64 = fbuf[t * 3] 50 let b: i64 = fbuf[t * 3 + 1] 51 let c: i64 = fbuf[t * 3 + 2] 52 hw2(fd, "facet normal 0 0 0\nouter loop\nvertex " as *u8) 53 stl_num(fd, vbuf[a * 3]); stl_num(fd, vbuf[a * 3 + 1]); stl_num(fd, vbuf[a * 3 + 2]) 54 hw2(fd, "\nvertex " as *u8) 55 stl_num(fd, vbuf[b * 3]); stl_num(fd, vbuf[b * 3 + 1]); stl_num(fd, vbuf[b * 3 + 2]) 56 hw2(fd, "\nvertex " as *u8) 57 stl_num(fd, vbuf[c * 3]); stl_num(fd, vbuf[c * 3 + 1]); stl_num(fd, vbuf[c * 3 + 2]) 58 hw2(fd, "\nendloop\nendfacet\n" as *u8) 59 t = t + 1 60 } 61 hw2(fd, "endsolid nishi_emitted\n" as *u8) 62 sys_close(fd) 63 return 0 64} 65 66func main() -> i64 { 67 var fails: i64 = 0 68 let base: i64 = sys_mmap(sdf_bytes()) as i64 69 sdf_body(base) 70 let F: *i64 = sys_mmap((MG_N + 1) * (MG_N + 1) * (MG_N + 1) * 8) as *i64 71 let cubevi: *i64 = sys_mmap(MG_N * MG_N * MG_N * 8) as *i64 72 let vbuf: *i64 = sys_mmap(MG_MAXV * 3 * 8) as *i64 73 let fbuf: *i64 = sys_mmap(MG_MAXF * 3 * 8) as *i64 74 let out: *i64 = sys_mmap(16) as *i64 75 hw("=== nx_meshgen_gate -- EMIT a mesh from the pioneer SDF field (surface nets, our own code) ===\n" as *u8) 76 mg_build(base, F, cubevi, vbuf, fbuf, out) 77 let nv: i64 = out[0] 78 let nf: i64 = out[1] 79 hw("emitted: verts=" as *u8); pn(nv); hw(" tris=" as *u8); pn(nf); hw(" (grid " as *u8); pn(MG_N); hw("^3)\n" as *u8) 80 81 // T1 non-trivial mesh 82 if nv > 2000 { if nf > 4000 { hw("T1 PASS emits a real surface\n" as *u8) } else { hw("T1 FAIL tris low\n" as *u8); fails = fails + 1 } } 83 else { hw("T1 FAIL verts low\n" as *u8); fails = fails + 1 } 84 85 // T2 CLOSED manifold: F ~= 2V (Euler genus-0) 86 let lo: i64 = nv * 3 / 2 87 let hi: i64 = nv * 5 / 2 88 if nf >= lo { if nf <= hi { hw("T2 PASS closed-manifold: tris=" as *u8); pn(nf); hw(" ~= 2*verts=" as *u8); pn(nv * 2); hw(" (watertight genus-0)\n" as *u8) } else { hw("T2 FAIL tris>2.5V (holes/nonmanifold)\n" as *u8); fails = fails + 1 } } 89 else { hw("T2 FAIL tris<1.5V\n" as *u8); fails = fails + 1 } 90 91 // T3 all face indices valid + T5 vertex usage in one pass 92 let use: *i64 = sys_mmap(MG_MAXV * 8) as *i64 93 var vi: i64 = 0 94 while vi < nv { use[vi] = 0; vi = vi + 1 } 95 var t3ok: i64 = 1 96 var t: i64 = 0 97 while t < nf { 98 var e: i64 = 0 99 while e < 3 { 100 let idx: i64 = fbuf[t * 3 + e] 101 if idx < 0 { t3ok = 0 } else { if idx >= nv { t3ok = 0 } else { use[idx] = use[idx] + 1 } } 102 e = e + 1 103 } 104 t = t + 1 105 } 106 if t3ok == 1 { hw("T3 PASS all face indices valid\n" as *u8) } else { hw("T3 FAIL bad index\n" as *u8); fails = fails + 1 } 107 var used3: i64 = 0 108 vi = 0 109 while vi < nv { if use[vi] >= 3 { used3 = used3 + 1 } vi = vi + 1 } 110 if used3 * 100 / nv >= 90 { hw("T5 PASS manifold usage: " as *u8); pn(used3 * 100 / nv); hw("% of verts used >=3 tris\n" as *u8) } 111 else { hw("T5 FAIL usage " as *u8); pn(used3 * 100 / nv); hw("%\n" as *u8); fails = fails + 1 } 112 113 // T4 bounds: tall y-span (body), all within grid 114 var miny: i64 = 999999 115 var maxy: i64 = 0 - 999999 116 var oob: i64 = 0 117 vi = 0 118 while vi < nv { 119 let y: i64 = vbuf[vi * 3 + 1] 120 if y < miny { miny = y } 121 if y > maxy { maxy = y } 122 if iabs(vbuf[vi * 3]) > MG_GR + 100 { oob = 1 } 123 vi = vi + 1 124 } 125 if maxy - miny > 2000 { if oob == 0 { hw("T4 PASS bounds: y-span=" as *u8); pn(maxy - miny); hw(" fx1024 (full body), in-grid\n" as *u8) } else { hw("T4 FAIL out-of-grid vert\n" as *u8); fails = fails + 1 } } 126 else { hw("T4 FAIL y-span small=" as *u8); pn(maxy - miny); hw("\n" as *u8); fails = fails + 1 } 127 128 // T6 emit STL 129 write_stl(vbuf, fbuf, nf, "knowledge/gates/connected-body-sdf-check-t335.stl" as *u8) 130 hw("T6 PASS wrote knowledge/gates/connected-body-sdf-check-t335.stl (the EMITTED mesh, openable in any 3D viewer)\n" as *u8) 131 132 133 // Existing independent owners judge the exact emitted buffers; old F~=2V is only a proxy. 134 let co:*i64=sys_mmap(MC_O_SLOTS*MC_I64) as *i64 135 let cl:*i64=sys_mmap(mc_list_slots()*MC_I64) as *i64 136 let cr:i64=mc_check(vbuf,nv,fbuf,nf,co,cl) 137 hw("ACTUAL-MESH-CHECK rc=");pn(cr);hw(" watertight=");pn(mc_watertight(co));hw(" slots=") 138 var ci:i64=0;while ci<MC_O_SLOTS{pn(co[ci]);hw(" ");ci=ci+1};hw("\n") 139 140 var klass:i64=MC_L_DEGEN_AREA 141 while klass<=MC_L_NONMANIFOLD{ 142 if klass!=MC_L_LOOSE{ 143 hw("DEFECT-LOC class=");pn(klass);hw("\n") 144 var li:i64=0 145 while li<MC_LIST_MAX{ 146 let key:i64=mc_list_get(cl,klass,li);if key<0{break} 147 hw(" key=");pn(key) 148 if klass==MC_L_DEGEN_AREA{ 149 var j:i64=0;while j<3{let vi:i64=fbuf[key*3+j];hw(" v=");pn(vi);hw(" xyz=");pn(vbuf[vi*3]);hw(",");pn(vbuf[vi*3+1]);hw(",");pn(vbuf[vi*3+2]);j=j+1} 150 }else{ 151 let a:i64=key/nv;let b:i64=key%nv 152 hw(" a=");pn(a);hw(" xyz=");pn(vbuf[a*3]);hw(",");pn(vbuf[a*3+1]);hw(",");pn(vbuf[a*3+2]);hw(" b=");pn(b);hw(" xyz=");pn(vbuf[b*3]);hw(",");pn(vbuf[b*3+1]);hw(",");pn(vbuf[b*3+2]) 153 } 154 hw("\n");li=li+1 155 } 156 } 157 klass=klass+1 158 } 159 var components:i64=0-1 160 if nv<=M3_MAXV{if nf<=M3_MAXT{ 161 let mb:i64=sys_mmap(m3_bytes()) as i64;m3_init(mb) 162 ci=0;while ci<nv{m3_add_vert(mb,vbuf[ci*3],vbuf[ci*3+1],vbuf[ci*3+2]);ci=ci+1} 163 ci=0;while ci<nf{m3_add_tri(mb,fbuf[ci*3],fbuf[ci*3+1],fbuf[ci*3+2]);ci=ci+1} 164 let labels:*i64=sys_mmap(nf*8) as *i64 165 // Each of 3 unit-normal components is bounded by MTK_ONE. This threshold admits every adjacent pair. 166 // Raw FX1024 positions are unchanged: only topological adjacency is measured, no physical-unit conversion. 167 components=ms_segment(mb,0-3*MTK_ONE,labels) 168 let sizes:*i64=sys_mmap(components*8) as *i64;ms_seg_sizes(labels,nf,sizes,components) 169 hw("ACTUAL-COMPONENTS count=");pn(components);hw(" face_counts=") 170 ci=0;while ci<components{pn(sizes[ci]);hw(" ");ci=ci+1};hw("\n") 171 }} 172 let glbbytes:i64=write_glb(vbuf,fbuf,nv,nf,"knowledge/gates/connected-body-sdf-t335.glb") 173 hw("DIAGNOSTIC-GLB bytes=");pn(glbbytes);hw(" raw_positions=FX1024 node_scale=1/1024 anatomical_mm_registration=UNVERIFIED\n") 174 if cr!=MC_CLEAN{fails=fails+1} 175 if mc_watertight(co)!=1{fails=fails+1} 176 if components!=1{fails=fails+1} 177 if glbbytes<=0{fails=fails+1} 178 hw("AUTHORITATIVE-TOPOLOGY overrides earlier proxy claims; defects retained, geometry unchanged\n") 179 180 if fails == 0 { hw("GATE GREEN: sovereign surface-nets EMITS a valid closed mesh from an SDF field -- the pioneer SDF is now an EMITTED mesh; the extractor both i2mesh/t2mesh feed.\n" as *u8) } 181 else { hw("GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8) } 182 return fails 183}