code wiki / _hdl_build / nx_headcrop.nx

nx_headcrop.nx source

↩ module page · 325 lines · 13859 B

1// nx_headcrop.nx -- P1 FACE rung 1, THE INSTRUMENT (beyond-metahuman program). 2// The face is the measured WORST region (head judge 227) but every judge so far scores it inside a 3// WHOLE-BODY frame, where a head is a few percent of the pixels -- GX-48 proved a head-sized change 4// moves a body-average by exactly zero. This organ crops an NXMSH2 to its own TOP y-band, so ours and 5// the oracle can be benched HEAD-TO-HEAD at full render resolution by the existing nx_bodybench 6// (its height-fit framing then normalises on the CROP's height = the head fills the frame). 7// Band is per-mille of the mesh's OWN height => scale-invariant, works on any mesh, any species. 8// A tri is kept only if ALL 3 verts are in the band (clean boundary); every count is DECLARED. 9// nx_headcrop <in.nxmesh> <out.nxmesh> [lo_permil=870] [hi_permil=1000] 10// nx_headcrop selftest 11// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26). 12import "nx_gate_verdict.nx" 13import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 14 15const HC_MAGIC_8388607: i64 = 8388607 16const HC_MAGIC_8388608: i64 = 8388608 17const HC_HDR: i64 = 16 18const HC_LAYENT: i64 = 24 19const HC_TRI: i64 = 84 20const HC_LID: i64 = 4 21const HC_MAXLAY: i64 = 64 22const HC_PERMIL: i64 = 1000 23const HC_DEF_LO: i64 = 870 24const HC_DEF_HI: i64 = 1000 25const HC_MM: i64 = 1000 26const HC_MODE: i64 = 420 27const HC_SELFN: i64 = 12 28const HC_JBUF: i64 = 8192 29const HC_MAXBYTES: i64 = 268435456 30 31func hc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1, s, n); return 0 } 32// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 33// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 34// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 35// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 36func hc_pn(v: i64) -> i64 { nxi_out(v); return 0 } 37func hc_rd32(b: *u8, o: i64) -> i64 { 38 return (b[o] as i64) | ((b[o+1] as i64)<<8) | ((b[o+2] as i64)<<16) | ((b[o+3] as i64)<<24) 39} 40func hc_wr32(b: *u8, o: i64, v: i64) -> i64 { 41 b[o]=(v & 255) as u8; b[o+1]=((v>>8) & 255) as u8; b[o+2]=((v>>16) & 255) as u8; b[o+3]=((v>>24) & 255) as u8 42 return 0 43} 44// decode IEEE-754 float32 at o, return round(value*mul) as integer (the canonical sovereign decoder) 45func hc_f32mul(b: *u8, o: i64, mul: i64) -> i64 { 46 let bits: i64 = hc_rd32(b, o) 47 let sign: i64 = (bits>>31) & 1 48 let exp: i64 = (bits>>23) & 255 49 let mant: i64 = bits & HC_MAGIC_8388607 50 if exp == 0 { return 0 } 51 let m: i64 = (mant | HC_MAGIC_8388608) * mul 52 var e: i64 = exp - 127 - 23 53 var v: i64 = 0 54 if e >= 0 { v = m << e } else { let sh: i64 = 0 - e; v = (m + (1 << (sh-1))) >> sh } 55 if sign == 1 { v = 0 - v } 56 return v 57} 58func hc_streq(a: *u8, b: *u8) -> i64 { 59 var i: i64=0; var go: i64=1; var eq: i64=1 60 while go==1 { 61 if a[i]!=b[i] { eq=0; go=0 } else { if a[i]==(0 as u8) { go=0 } else { i=i+1 } } 62 } 63 return eq 64} 65func hc_atoi(s: *u8) -> i64 { 66 var v: i64=0; var i: i64=0 67 while s[i]!=(0 as u8) { let c: i64 = s[i] as i64; if c>=48 { if c<=57 { v=v*10+(c-48) } } i=i+1 } 68 return v 69} 70// THE CROP. Returns 0 ok, negative on refusal (never writes a partial mesh). 71// outcnt[0]=tris_in outcnt[1]=tris_out outcnt[2]=ymin_mm outcnt[3]=ymax_mm outcnt[4]=cut_mm outcnt[5]=nlayer 72func hc_crop(inp: *u8, outp: *u8, lo: i64, hi: i64, outcnt: *i64) -> i64 { 73 let szp: *i64 = sys_mmap(16) as *i64 74 let mb: *u8 = sys_read_file(inp, szp) 75 if (mb as i64) == 0 { return 0 - 1 } 76 let sz: i64 = szp[0] 77 if sz < HC_HDR { return 0 - 2 } 78 if mb[0]!=(78 as u8) { return 0 - 3 } 79 if mb[1]!=(88 as u8) { return 0 - 3 } 80 if mb[2]!=(77 as u8) { return 0 - 3 } 81 if mb[3]!=(83 as u8) { return 0 - 3 } 82 let nlay: i64 = hc_rd32(mb, 8) 83 let nt: i64 = hc_rd32(mb, 12) 84 if nlay <= 0 { return 0 - 4 } 85 if nlay > HC_MAXLAY { return 0 - 4 } 86 if nt <= 0 { return 0 - 5 } 87 let hdr: i64 = HC_HDR + nlay*HC_LAYENT 88 let need: i64 = hdr + nt*HC_TRI + nt*HC_LID 89 if need > sz { return 0 - 6 } 90 // pass 1: mesh AABB on y (all three verts of every tri) 91 var ymin: i64 = 0 92 var ymax: i64 = 0 93 var first: i64 = 1 94 var t: i64 = 0 95 while t < nt { 96 let o: i64 = hdr + t*HC_TRI 97 var k: i64 = 0 98 while k < 3 { 99 let y: i64 = hc_f32mul(mb, o + k*12 + 4, HC_MM) 100 if first == 1 { ymin = y; ymax = y; first = 0 } else { 101 if y < ymin { ymin = y } 102 if y > ymax { ymax = y } 103 } 104 k = k + 1 105 } 106 t = t + 1 107 } 108 let span: i64 = ymax - ymin 109 if span <= 0 { return 0 - 7 } 110 let ylo: i64 = ymin + span*lo/HC_PERMIL 111 let yhi: i64 = ymin + span*hi/HC_PERMIL 112 // pass 2: keep-mask (ALL 3 verts inside the band) + per-layer counts 113 let keep: *i64 = sys_mmap(nt*8 + 64) as *i64 114 let lcnt: *i64 = sys_mmap(HC_MAXLAY*8 + 64) as *i64 115 var li: i64 = 0 116 while li < HC_MAXLAY { lcnt[li] = 0; li = li + 1 } 117 var kept: i64 = 0 118 var t2: i64 = 0 119 while t2 < nt { 120 let o2: i64 = hdr + t2*HC_TRI 121 var ok: i64 = 1 122 var k2: i64 = 0 123 while k2 < 3 { 124 let y2: i64 = hc_f32mul(mb, o2 + k2*12 + 4, HC_MM) 125 if y2 < ylo { ok = 0 } 126 if y2 > yhi { ok = 0 } 127 k2 = k2 + 1 128 } 129 keep[t2] = ok 130 if ok == 1 { 131 kept = kept + 1 132 let lid: i64 = hc_rd32(mb, hdr + nt*HC_TRI + t2*HC_LID) 133 if lid >= 0 { if lid < HC_MAXLAY { lcnt[lid] = lcnt[lid] + 1 } } 134 } 135 t2 = t2 + 1 136 } 137 outcnt[0] = nt 138 outcnt[1] = kept 139 outcnt[2] = ymin 140 outcnt[3] = ymax 141 outcnt[4] = ylo 142 outcnt[5] = nlay 143 if kept <= 0 { return 0 - 8 } 144 // pass 3: write, tris GROUPED BY LAYER so the layer table stays contiguous (the format's contract) 145 let ohdr: i64 = HC_HDR + nlay*HC_LAYENT 146 let obytes: i64 = ohdr + kept*HC_TRI + kept*HC_LID 147 if obytes > HC_MAXBYTES { return 0 - 9 } 148 let ob: *u8 = sys_mmap(obytes + 64) 149 ob[0]=78 as u8; ob[1]=88 as u8; ob[2]=77 as u8; ob[3]=83 as u8 150 ob[4]=72 as u8; ob[5]=50 as u8; ob[6]=0 as u8; ob[7]=0 as u8 151 hc_wr32(ob, 8, nlay) 152 hc_wr32(ob, 12, kept) 153 var off: i64 = 0 154 var lw: i64 = 0 155 while lw < nlay { 156 let src: i64 = HC_HDR + lw*HC_LAYENT 157 let dst: i64 = HC_HDR + lw*HC_LAYENT 158 var q: i64 = 0 159 while q < 16 { ob[dst+q] = mb[src+q]; q = q + 1 } 160 hc_wr32(ob, dst+16, off) 161 hc_wr32(ob, dst+20, lcnt[lw]) 162 off = off + lcnt[lw] 163 lw = lw + 1 164 } 165 var wi: i64 = 0 166 var lp: i64 = 0 167 while lp < nlay { 168 var t3: i64 = 0 169 while t3 < nt { 170 if keep[t3] == 1 { 171 let lid3: i64 = hc_rd32(mb, hdr + nt*HC_TRI + t3*HC_LID) 172 if lid3 == lp { 173 let so: i64 = hdr + t3*HC_TRI 174 let doff: i64 = ohdr + wi*HC_TRI 175 var c: i64 = 0 176 while c < HC_TRI { ob[doff+c] = mb[so+c]; c = c + 1 } 177 hc_wr32(ob, ohdr + kept*HC_TRI + wi*HC_LID, lp) 178 wi = wi + 1 179 } 180 } 181 t3 = t3 + 1 182 } 183 lp = lp + 1 184 } 185 if wi != kept { return 0 - 10 } 186 let fd: i64 = sys_openat_wr(outp, HC_MODE) 187 if fd < 0 { return 0 - 11 } 188 sys_write(fd, ob, obytes) 189 sys_close(fd) 190 return 0 191} 192func hc_emit(inp: *u8, outp: *u8, lo: i64, hi: i64, c: *i64, rc: i64) -> i64 { 193 hc_puts("{\x22organ\x22:\x22nx_headcrop\x22,\x22v\x22:1,\x22in\x22:\x22" as *u8); hc_puts(inp) 194 hc_puts("\x22,\x22out\x22:\x22" as *u8); hc_puts(outp) 195 hc_puts("\x22,\x22band_permil\x22:[" as *u8); hc_pn(lo); hc_puts("," as *u8); hc_pn(hi) 196 hc_puts("],\x22rc\x22:" as *u8); hc_pn(rc) 197 hc_puts(",\x22tris_in\x22:" as *u8); hc_pn(c[0]) 198 hc_puts(",\x22tris_out\x22:" as *u8); hc_pn(c[1]) 199 var ratio: i64 = 0 200 if c[0] > 0 { ratio = c[1]*HC_PERMIL/c[0] } 201 hc_puts(",\x22kept_permil\x22:" as *u8); hc_pn(ratio) 202 hc_puts(",\x22y_min_mm\x22:" as *u8); hc_pn(c[2]) 203 hc_puts(",\x22y_max_mm\x22:" as *u8); hc_pn(c[3]) 204 hc_puts(",\x22cut_mm\x22:" as *u8); hc_pn(c[4]) 205 hc_puts(",\x22layers\x22:" as *u8); hc_pn(c[5]) 206 hc_puts(",\x22rule\x22:\x22a tri is kept only if ALL 3 verts lie in the band; band is per-mille of the mesh OWN height so the crop is scale- and species-invariant; layer table rebuilt contiguous\x22" as *u8) 207 hc_puts(",\x22why\x22:\x22the face is the measured worst region but a whole-body judge averages it into invisibility (GX-48); crop both meshes, then bench HEAD-TO-HEAD at full render resolution\x22}\n" as *u8) 208 return 0 209} 210// build a tiny synthetic 2-layer mesh in memory and write it: 1 tri LOW, 1 tri HIGH. 211// f32 encode of small whole numbers: sign 0, exp 127+k, mantissa shifted. 212func hc_f32of(v: i64) -> i64 { 213 if v <= 0 { return 0 } 214 var ex: i64 = 0 215 var tv: i64 = v 216 while tv >= 2 { tv = tv/2; ex = ex + 1 } 217 var frac: i64 = 0 218 if ex > 0 { frac = (v - (1 << ex)) * (1 << 23) / (1 << ex) } 219 return ((127 + ex) << 23) | frac 220} 221func hc_mktest(path: *u8) -> i64 { 222 let nlay: i64 = 2 223 let nt: i64 = 2 224 let hdr: i64 = HC_HDR + nlay*HC_LAYENT 225 let bytes: i64 = hdr + nt*HC_TRI + nt*HC_LID 226 let b: *u8 = sys_mmap(bytes + 64) 227 var z: i64 = 0 228 while z < bytes { b[z] = 0 as u8; z = z + 1 } 229 b[0]=78 as u8; b[1]=88 as u8; b[2]=77 as u8; b[3]=83 as u8 230 b[4]=72 as u8; b[5]=50 as u8 231 hc_wr32(b, 8, nlay); hc_wr32(b, 12, nt) 232 hc_wr32(b, HC_HDR+16, 0); hc_wr32(b, HC_HDR+20, 1) 233 hc_wr32(b, HC_HDR+HC_LAYENT+16, 1); hc_wr32(b, HC_HDR+HC_LAYENT+20, 1) 234 // tri 0: all y=1 (LOW, must be cut). tri 1: all y=100 (HIGH, must survive) 235 var t: i64 = 0 236 while t < nt { 237 let o: i64 = hdr + t*HC_TRI 238 var yv: i64 = 1 239 if t == 1 { yv = 100 } 240 var k: i64 = 0 241 while k < 3 { 242 hc_wr32(b, o + k*12 + 0, hc_f32of(1)) 243 hc_wr32(b, o + k*12 + 4, hc_f32of(yv)) 244 hc_wr32(b, o + k*12 + 8, hc_f32of(1)) 245 k = k + 1 246 } 247 hc_wr32(b, hdr + nt*HC_TRI + t*HC_LID, t) 248 t = t + 1 249 } 250 let fd: i64 = sys_openat_wr(path, HC_MODE) 251 if fd < 0 { return 0 - 1 } 252 sys_write(fd, b, bytes) 253 sys_close(fd) 254 return 0 255} 256func hc_gate() -> i64 { 257 let ctr: *i64 = gv_ctr() 258 gv_head("nx_headcrop selftest -- the head-scale instrument is correct by construction" as *u8) 259 let c: *i64 = sys_mmap(128) as *i64 260 var t0: i64 = 0 261 if hc_mktest("/tmp/nx_hc_t.nxmesh" as *u8) == 0 { t0 = 1 } 262 gv_check("T0 synthetic 2-layer fixture written" as *u8, t0, ctr) 263 // band 500..1000 of a mesh spanning y 1..100 => cut at ~50 => the y=1 tri dies, the y=100 tri lives 264 let rc: i64 = hc_crop("/tmp/nx_hc_t.nxmesh" as *u8, "/tmp/nx_hc_o.nxmesh" as *u8, 500, HC_DEF_HI, c) 265 var t1: i64 = 0 266 if rc == 0 { if c[0] == 2 { if c[1] == 1 { t1 = 1 } } } 267 gv_check("T1 crop keeps the HIGH tri and drops the LOW one" as *u8, t1, ctr) 268 var t2: i64 = 0 269 if c[2] == HC_MM { if c[3] == 100*HC_MM { t2 = 1 } } 270 gv_check("T2 AABB measured from the mesh itself (y 1..100)" as *u8, t2, ctr) 271 // re-read the output: header must be well-formed and layer counts must sum to the tri count 272 let sp: *i64 = sys_mmap(16) as *i64 273 let ob: *u8 = sys_read_file("/tmp/nx_hc_o.nxmesh" as *u8, sp) 274 var t3: i64 = 0 275 if (ob as i64) != 0 { 276 if ob[0]==(78 as u8) { if ob[1]==(88 as u8) { 277 let onl: i64 = hc_rd32(ob, 8) 278 let ont: i64 = hc_rd32(ob, 12) 279 var sum: i64 = 0 280 var li: i64 = 0 281 while li < onl { sum = sum + hc_rd32(ob, HC_HDR + li*HC_LAYENT + 20); li = li + 1 } 282 if ont == 1 { if sum == ont { t3 = 1 } } 283 } } 284 } 285 gv_check("T3 output is valid NXMSH2 and layer counts sum to the tri count" as *u8, t3, ctr) 286 // ANTI-VACUITY: a full-range band must keep EVERYTHING (a cropper that always returns 1 tri fails here) 287 let c2: *i64 = sys_mmap(128) as *i64 288 let rc2: i64 = hc_crop("/tmp/nx_hc_t.nxmesh" as *u8, "/tmp/nx_hc_f.nxmesh" as *u8, 0, HC_DEF_HI, c2) 289 var t4: i64 = 0 290 if rc2 == 0 { if c2[1] == 2 { t4 = 1 } } 291 gv_check("T4 anti-vacuity: band 0-1000 keeps every tri" as *u8, t4, ctr) 292 // REFUSAL: an empty band commits nothing and reports it (never a silent zero-tri mesh) 293 let c3: *i64 = sys_mmap(128) as *i64 294 let rc3: i64 = hc_crop("/tmp/nx_hc_t.nxmesh" as *u8, "/tmp/nx_hc_e.nxmesh" as *u8, 998, 999, c3) 295 var t5: i64 = 0 296 if rc3 < 0 { if c3[1] == 0 { t5 = 1 } } 297 gv_check("T5 empty band REFUSES loudly, writes nothing" as *u8, t5, ctr) 298 var t6: i64 = 0 299 let c4: *i64 = sys_mmap(128) as *i64 300 if hc_crop("/tmp/nx_hc_absent_zz.nxmesh" as *u8, "/tmp/nx_hc_x.nxmesh" as *u8, 500, HC_DEF_HI, c4) < 0 { t6 = 1 } 301 gv_check("T6 missing input refused, not silently empty" as *u8, t6, ctr) 302 return gv_verdict("HEADCROP-GATE" as *u8, ctr, "band-crop exact, refusals loud, anti-vacuity held" as *u8) 303} 304func main(argc: i64, argv: *i64) -> i64 { 305 if argc >= 2 { 306 if hc_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return hc_gate() } 307 } 308 if argc < 3 { 309 hc_puts("usage: nx_headcrop <in.nxmesh> <out.nxmesh> [lo_permil=870] [hi_permil=1000] | selftest\n" as *u8) 310 return 2 311 } 312 let inp: *u8 = argv[1] as *u8 313 let outp: *u8 = argv[2] as *u8 314 var lo: i64 = HC_DEF_LO 315 var hi: i64 = HC_DEF_HI 316 if argc > 3 { lo = hc_atoi(argv[3] as *u8) } 317 if argc > 4 { hi = hc_atoi(argv[4] as *u8) } 318 if lo < 0 { lo = 0 } 319 if hi > HC_PERMIL { hi = HC_PERMIL } 320 let c: *i64 = sys_mmap(128) as *i64 321 let rc: i64 = hc_crop(inp, outp, lo, hi, c) 322 hc_emit(inp, outp, lo, hi, c, rc) 323 if rc < 0 { return 1 } 324 return 0 325}