code wiki / _hdl_build / nx_skinwrap.nx

nx_skinwrap.nx source

↩ module page · 351 lines · 16921 B

1// nx_skinwrap.nx -- SKIN AS AN ENVELOPE OVER BONE, offset by FORENSIC SOFT-TISSUE DEPTH. 2// 3// ★WHY: pipeline stage 4. This program's skin has always been emitted INDEPENDENTLY of the layers under 4// it, which is why brow/cheek/jaw/chin had to be modelled as parts STUCK ON an ovoid and why three tuned 5// chin attempts all rendered as stacked lobes. The operator named the root cause -- "you aren't building 6// from the inside out" -- and the fix is not another feature part: it is to DERIVE the surface from the 7// bone beneath. With a generated skull now sitting in the body's bone layer, that is finally possible. 8// 9// ★THE RULE IS MEASURED, NOT TASTED. Forensic facial reconstruction (Rhine / De Greef class) publishes the 10// millimetres of soft tissue over bone AT EACH LANDMARK. This codebase already owns that table -- it was 11// ported once into knowledge/face_tissue.dat and its first two values alone (brow at the glabella, eye 12// line) moved the binding number 92 -> 138 without a line of new geometry. Those depths are ABSOLUTE 13// physical measurements, which is exactly the kind of value that TRANSFERS between heads where a 14// y-position does not. 15// 16// ★AUDIT RAN FIRST, and it changed nothing but is why this is a new organ rather than an edit: 17// nx_meshthick MEASURES wall thickness by ray-casting and is built on nx_mesh3, which is capped at 4096 18// verts and cannot hold a 43k-triangle skull. nx_mesh_sculpt has an inflate brush that pushes verts along 19// their normal, but it is RADIUS-LIMITED around a brush point on a different mesh kernel. Neither does a 20// global, per-landmark-depth offset on NXMSH2. The "push along the normal" idiom is borrowed from 21// nx_mesh_sculpt deliberately, so this is consistent with the ecosystem rather than a second dialect. 22// 23// nx_skinwrap <out.nxmesh> <bone.nxmesh> [depth_scale_permil] 24// nx_skinwrap selftest 25// depth_scale_permil scales the whole table (1000 = the published depths; 0 = a pure copy of the bone). 26// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26). 27import "nx_syscalls.nx" 28import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 29const SW_MAGIC_8388607: i64 = 8388607 30const SW_MAGIC_8388608: i64 = 8388608 31const SW_MAGIC_3003: i64 = 3003 32 33const SW_HDRB: i64 = 16 34const SW_LREC: i64 = 24 35const SW_TRIB: i64 = 84 36const SW_IDXB: i64 = 4 37const SW_LENSLOT: i64 = 16 38// the mesh format stores normals as f32 at this scale (surface_nets emits gradient normals at 4096 and 39// nx_skullsdf divides by that before writing, so a stored normal is ~1.0 in f32 terms) 40const SW_NORM_ONE: i64 = 1000 41 42static SW_QUIET: i64 43func sw_puts(s: *u8) -> i64 { if SW_QUIET == 1 { return 0 } var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 44func sw_say(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 45// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 46// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 47// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 48// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 49func sw_pn(v: i64) -> i64 { nxi_out(v); return 0 } 50// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 51// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 52// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 53// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 54func sw_sn(v: i64) -> i64 { nxi_out(v); return 0 } 55func sw_streq(a: *u8, b: *u8) -> i64 { 56 var i: i64=0; var go: i64=1; var eq: i64=1 57 while go==1 { if a[i]!=b[i] { eq=0; go=0 } else { if a[i]==(0 as u8) { go=0 } else { i=i+1 } } } 58 return eq 59} 60func sw_atoi(s: *u8) -> i64 { 61 var i: i64=0; var n: i64=0; var sg: i64=1 62 if s[0]==(45 as u8) { sg=0-1; i=1 } 63 while s[i]!=(0 as u8) { let c: i64=s[i] as i64; if c>=48 { if c<=57 { n=n*10+(c-48) } } i=i+1 } 64 return n*sg 65} 66func sw_rd32(b: *u8, o: i64) -> i64 { 67 return (b[o] as i64) | ((b[o+1] as i64)<<8) | ((b[o+2] as i64)<<16) | ((b[o+3] as i64)<<24) 68} 69func sw_wr32(b: *u8, o: i64, v: i64) -> i64 { 70 b[o] = (v & 255) as u8 71 b[o+1] = ((v >> 8) & 255) as u8 72 b[o+2] = ((v >> 16) & 255) as u8 73 b[o+3] = ((v >> 24) & 255) as u8 74 return 0 75} 76// f32 <-> integer at a caller-chosen scale, so a normal can be read at 1000 (milli-units) and a 77// coordinate at 1 (whole units) with the same helper. 78func sw_r_f32(bits: i64, scale: i64) -> i64 { 79 let s: i64 = (bits >> 31) & 1 80 let e: i64 = (bits >> 23) & 255 81 let m: i64 = bits & SW_MAGIC_8388607 82 if e == 0 { return 0 } 83 if e == 255 { return 0 } 84 let mant: i64 = SW_MAGIC_8388608 | m 85 let sh: i64 = e - 127 - 23 86 var v: i64 = 0 87 if sh >= 0 { if sh > 30 { return 0 } v = (mant*scale) << sh } 88 else { let rs: i64 = 0 - sh; if rs > 62 { return 0 } v = (mant*scale) >> rs } 89 if s == 1 { return 0 - v } 90 return v 91} 92func sw_f32(v: i64, scale: i64) -> i64 { 93 if v == 0 { return 0 } 94 var neg: i64 = 0 95 var m: i64 = v 96 if m < 0 { neg = 1; m = 0 - m } 97 var e: i64 = 127 98 var num: i64 = m 99 var den: i64 = scale 100 while num >= den*2 { den = den*2; e = e + 1 } 101 while num < den { num = num*2; e = e - 1 } 102 let frac: i64 = ((num - den) * SW_MAGIC_8388608) / den 103 var bits: i64 = (e << 23) | (frac & SW_MAGIC_8388607) 104 if neg == 1 { bits = bits | (1 << 31) } 105 return bits 106} 107func sw_is_nxmsh2(b: *u8) -> i64 { 108 if b[0]!=(78 as u8) { return 0 } 109 if b[1]!=(88 as u8) { return 0 } 110 if b[2]!=(77 as u8) { return 0 } 111 if b[3]!=(83 as u8) { return 0 } 112 if b[4]!=(72 as u8) { return 0 } 113 if b[5]!=(50 as u8) { return 0 } 114 return 1 115} 116 117// ★★THE FORENSIC TABLE. Depth in MILLIMETRES over bone, keyed by per-mille of the mesh's OWN height so it 118// is scale- and individual-invariant exactly as nx_headcrop's band is. Values are the published 119// landmark depths this codebase already carries: chin 11, jaw 9, lower lip 11, upper lip 10, cheekbone 7, 120// nasal 6, nasion 3, brow/glabella 7, forehead 4.5 (integer 5 here -- see the honest note below). 121// ⚠HONEST v1 LIMITATION, declared in the organ rather than discovered later: this indexes by HEIGHT ONLY. 122// A real reconstruction is per-LANDMARK in three dimensions -- the temple is 3 mm while the cheekbone at 123// nearly the same height is 7 -- so a height-banded table cannot express a lateral difference. That is a 124// known and stated approximation, not an oversight, and it is why this is v1. The nasion at 3 mm sitting 125// between brow 7 and nasal 6 is the important part: it is the DIP that makes a nose read as a nose, and a 126// height table CAN express it. 127func sw_depth(ypermil: i64) -> i64 { 128 if ypermil < 200 { return 11 } // mental protuberance / chin 129 if ypermil < 300 { return 9 } // jaw / gonion 130 if ypermil < 380 { return 11 } // lower lip 131 if ypermil < 450 { return 10 } // upper lip 132 if ypermil < 620 { return 7 } // maxilla / cheekbone 133 if ypermil < 700 { return 6 } // nasal 134 if ypermil < 780 { return 3 } // nasion -- the DIP; shallowest point on the face 135 if ypermil < 860 { return 7 } // brow ridge / glabella 136 return 5 // forehead / vault (4.5 published, integer here) 137} 138 139func sw_wrap(outp: *u8, bonep: *u8, dscale: i64) -> i64 { 140 let ln: *i64 = sys_mmap(SW_LENSLOT) as *i64 141 let B: *u8 = sys_read_file(bonep, ln) 142 if (B as i64) == 0 { sw_puts("{\x22error\x22:\x22cannot read bone mesh\x22}\n" as *u8); return 0-3 } 143 if sw_is_nxmsh2(B) == 0 { sw_puts("{\x22error\x22:\x22bone is not NXMSH2\x22}\n" as *u8); return 0-4 } 144 let bn: i64 = sw_rd32(B, 8) 145 let nt: i64 = sw_rd32(B, 12) 146 if nt <= 0 { sw_puts("{\x22error\x22:\x22bone mesh has no triangles\x22}\n" as *u8); return 0-5 } 147 let bhdr: i64 = SW_HDRB + bn*SW_LREC 148 // pass 1: the mesh's own vertical extent, so the band table is scale-invariant 149 var lo: i64 = 0 150 var hi: i64 = 0 151 var first: i64 = 1 152 var t: i64 = 0 153 while t < nt { 154 var j: i64 = 0 155 while j < 3 { 156 let y: i64 = sw_r_f32(sw_rd32(B, bhdr + t*SW_TRIB + j*12 + 4), 1) 157 if first == 1 { lo=y; hi=y; first=0 } else { if y<lo {lo=y} if y>hi {hi=y} } 158 j = j + 1 159 } 160 t = t + 1 161 } 162 var span: i64 = hi - lo 163 if span < 1 { span = 1 } 164 // pass 2: offset every vertex along its own normal by the depth for its height band 165 let obytes: i64 = bhdr + nt*SW_TRIB + nt*SW_IDXB 166 let O: *u8 = sys_mmap(obytes + 64) 167 var q: i64 = 0 168 while q < obytes { O[q] = B[q]; q = q + 1 } 169 var moved: i64 = 0 170 var dmin: i64 = 0 171 var dmax: i64 = 0 172 var dfirst: i64 = 1 173 t = 0 174 while t < nt { 175 var j: i64 = 0 176 while j < 3 { 177 let po: i64 = bhdr + t*SW_TRIB + j*12 178 let no: i64 = bhdr + t*SW_TRIB + 36 + j*12 179 let px: i64 = sw_r_f32(sw_rd32(B, po), 1) 180 let py: i64 = sw_r_f32(sw_rd32(B, po+4), 1) 181 let pz: i64 = sw_r_f32(sw_rd32(B, po+8), 1) 182 // normals are read at SW_NORM_ONE so a unit normal is ~1000 and the offset stays integer 183 let nx: i64 = sw_r_f32(sw_rd32(B, no), SW_NORM_ONE) 184 let ny: i64 = sw_r_f32(sw_rd32(B, no+4), SW_NORM_ONE) 185 let nz: i64 = sw_r_f32(sw_rd32(B, no+8), SW_NORM_ONE) 186 let yp: i64 = (py - lo) * 1000 / span 187 let d: i64 = sw_depth(yp) * dscale / 1000 188 if dfirst == 1 { dmin=d; dmax=d; dfirst=0 } else { if d<dmin {dmin=d} if d>dmax {dmax=d} } 189 let ox: i64 = px + nx*d/SW_NORM_ONE 190 let oy: i64 = py + ny*d/SW_NORM_ONE 191 let oz: i64 = pz + nz*d/SW_NORM_ONE 192 if ox != px { moved = moved + 1 } else { if oy != py { moved = moved + 1 } else { if oz != pz { moved = moved + 1 } } } 193 sw_wr32(O, po, sw_f32(ox, 1)) 194 sw_wr32(O, po+4, sw_f32(oy, 1)) 195 sw_wr32(O, po+8, sw_f32(oz, 1)) 196 j = j + 1 197 } 198 t = t + 1 199 } 200 let fd: i64 = sys_openat_wr(outp, 420) 201 sys_write(fd, O, obytes) 202 sys_close(fd) 203 sw_puts("{\x22organ\x22:\x22nx_skinwrap\x22,\x22tris\x22:" as *u8); sw_pn(nt) 204 sw_puts(",\x22verts_offset\x22:" as *u8); sw_pn(moved) 205 sw_puts(",\x22verts_total\x22:" as *u8); sw_pn(nt*3) 206 sw_puts(",\x22bone_span\x22:" as *u8); sw_pn(span) 207 sw_puts(",\x22depth_min_mm\x22:" as *u8); sw_pn(dmin) 208 sw_puts(",\x22depth_max_mm\x22:" as *u8); sw_pn(dmax) 209 sw_puts(",\x22depth_scale_permil\x22:" as *u8); sw_pn(dscale) 210 sw_puts(",\x22method\x22:\x22per-vertex offset along the stored normal by the forensic soft-tissue depth for that height band\x22}\n" as *u8) 211 return moved 212} 213 214// ---- TEETH. Same discipline as nx_meshmerge: build a REAL NXMSH2 fixture on disk and run the SHIPPED 215// wrap over it, so no tooth can pass against a reimplementation. 216func sw_fixture(path: *u8, nt: i64) -> i64 { 217 let hdr: i64 = SW_HDRB + 1*SW_LREC 218 let bytes: i64 = hdr + nt*SW_TRIB + nt*SW_IDXB 219 let B: *u8 = sys_mmap(bytes + 64) 220 B[0]=78 as u8; B[1]=88 as u8; B[2]=77 as u8; B[3]=83 as u8 221 B[4]=72 as u8; B[5]=50 as u8; B[6]=0 as u8; B[7]=0 as u8 222 sw_wr32(B, 8, 1); sw_wr32(B, 12, nt) 223 var t: i64 = 0 224 while t < nt { 225 var c: i64 = 0 226 while c < 21 { sw_wr32(B, hdr + t*SW_TRIB + c*4, 0); c = c + 1 } 227 // y climbs with t so the fixture spans the whole height, exercising every band 228 var j: i64 = 0 229 while j < 3 { 230 sw_wr32(B, hdr + t*SW_TRIB + j*12, sw_f32(100, 1)) 231 sw_wr32(B, hdr + t*SW_TRIB + j*12+4, sw_f32(t, 1)) 232 sw_wr32(B, hdr + t*SW_TRIB + j*12+8, sw_f32(0, 1)) 233 // unit normal pointing +x, so the offset is exactly the depth on x and zero elsewhere 234 sw_wr32(B, hdr + t*SW_TRIB + 36 + j*12, sw_f32(1000, SW_NORM_ONE)) 235 sw_wr32(B, hdr + t*SW_TRIB + 36 + j*12+4, 0) 236 sw_wr32(B, hdr + t*SW_TRIB + 36 + j*12+8, 0) 237 j = j + 1 238 } 239 sw_wr32(B, hdr + nt*SW_TRIB + t*SW_IDXB, 0) 240 t = t + 1 241 } 242 var k: i64 = 0 243 while k < 16 { B[SW_HDRB + k] = 0 as u8; k = k + 1 } 244 sw_wr32(B, SW_HDRB + 16, 0) 245 sw_wr32(B, SW_HDRB + 20, nt) 246 let fd: i64 = sys_openat_wr(path, 420) 247 sys_write(fd, B, bytes) 248 sys_close(fd) 249 return nt 250} 251func sw_x_of(path: *u8, t: i64) -> i64 { 252 let ln: *i64 = sys_mmap(SW_LENSLOT) as *i64 253 let B: *u8 = sys_read_file(path, ln) 254 if (B as i64) == 0 { return 0-1 } 255 let n: i64 = sw_rd32(B, 8) 256 let hdr: i64 = SW_HDRB + n*SW_LREC 257 return sw_r_f32(sw_rd32(B, hdr + t*SW_TRIB), 1) 258} 259func sw_selftest() -> i64 { 260 var pass: i64 = 0 261 var total: i64 = 0 262 let bone: *u8 = "_offc/sw_t_bone.nxmesh" as *u8 263 let out: *u8 = "_offc/sw_t_out.nxmesh" as *u8 264 let bad: *u8 = "_offc/sw_t_bad.bin" as *u8 265 sw_fixture(bone, 1001) 266 SW_QUIET = 1 267 let r: i64 = sw_wrap(out, bone, 1000) 268 SW_QUIET = 0 269 270 // T1 -- every vertex moved. The fixture's normal is +x everywhere and every band has a nonzero depth. 271 total = total + 1 272 var t1: i64 = 0 273 if r == SW_MAGIC_3003 { t1 = 1 } 274 if t1 == 1 { pass = pass + 1 } 275 sw_say("T1 every_vertex_offset=" as *u8); sw_sn(t1); sw_say("\n" as *u8) 276 277 // T2 -- THE DEPTH IS THE PUBLISHED ONE, checked at a specific band. Triangle 0 sits at the very bottom 278 // (chin band, 11 mm) and the fixture's x is 100, so the wrapped x must be exactly 111. 279 total = total + 1 280 var t2: i64 = 0 281 if sw_x_of(out, 0) == 111 { t2 = 1 } 282 if t2 == 1 { pass = pass + 1 } 283 sw_say("T2 chin_band_depth_11mm=" as *u8); sw_sn(t2); sw_say("\n" as *u8) 284 285 // T3 -- THE NASION DIP EXISTS AND IS THE SHALLOWEST POINT. This is the tooth that matters: a table that 286 // returned one constant would pass T1 and T2 for the wrong reason. Triangle 750 lands in the 700-780 287 // nasion band at 3 mm, and it must be strictly shallower than the brow band above it (t=800 -> 7 mm). 288 total = total + 1 289 var t3: i64 = 0 290 let nas: i64 = sw_x_of(out, 750) 291 let brw: i64 = sw_x_of(out, 800) 292 if nas == 103 { if brw == 107 { t3 = 1 } } 293 if t3 == 1 { pass = pass + 1 } 294 sw_say("T3 nasion_dip_shallower_than_brow=" as *u8); sw_sn(t3); sw_say("\n" as *u8) 295 296 // T4 -- NON-VACUITY OF THE TABLE: the wrap must NOT be uniform. Compare three bands and require at 297 // least two distinct depths, or a constant-depth balloon would satisfy everything above. 298 total = total + 1 299 var t4: i64 = 0 300 let chin: i64 = sw_x_of(out, 0) 301 if chin != nas { if nas != brw { t4 = 1 } } 302 if t4 == 1 { pass = pass + 1 } 303 sw_say("T4 depth_varies_by_band=" as *u8); sw_sn(t4); sw_say("\n" as *u8) 304 305 // T5 -- SCALE 0 IS AN EXACT PASS-THROUGH. The honest identity: asking for zero tissue must return the 306 // bone unchanged, which proves the offset is driven by the argument and not baked in. 307 total = total + 1 308 SW_QUIET = 1 309 let r0: i64 = sw_wrap(out, bone, 0) 310 SW_QUIET = 0 311 var t5: i64 = 0 312 if r0 == 0 { if sw_x_of(out, 0) == 100 { t5 = 1 } } 313 if t5 == 1 { pass = pass + 1 } 314 sw_say("T5 zero_depth_is_passthrough=" as *u8); sw_sn(t5); sw_say("\n" as *u8) 315 316 // T6 -- a non-NXMSH2 input REFUSES rather than wrapping arbitrary bytes. 317 total = total + 1 318 let junk: *u8 = sys_mmap(256) 319 var j: i64 = 0 320 while j < 256 { junk[j] = 65 as u8; j = j + 1 } 321 let jfd: i64 = sys_openat_wr(bad, 420) 322 sys_write(jfd, junk, 256) 323 sys_close(jfd) 324 SW_QUIET = 1 325 let rb: i64 = sw_wrap(out, bad, 1000) 326 SW_QUIET = 0 327 var t6: i64 = 0 328 if rb < 0 { t6 = 1 } 329 if t6 == 1 { pass = pass + 1 } 330 sw_say("T6 non_nxmsh2_refuses=" as *u8); sw_sn(t6); sw_say("\n" as *u8) 331 332 sw_say("{\x22organ\x22:\x22nx_skinwrap\x22,\x22verb\x22:\x22selftest\x22,\x22pass\x22:" as *u8); sw_sn(pass) 333 sw_say(",\x22total\x22:" as *u8); sw_sn(total) 334 sw_say(",\x22verdict\x22:\x22" as *u8) 335 if pass == total { sw_say("GREEN" as *u8) } else { sw_say("RED" as *u8) } 336 sw_say("\x22}\n" as *u8) 337 if pass == total { return 0 } 338 return 1 339} 340 341func main(argc: i64, argv: *i64) -> i64 { 342 if argc < 2 { sw_puts("usage: nx_skinwrap <out.nxmesh> <bone.nxmesh> [depth_scale_permil] | selftest\n" as *u8); return 2 } 343 if sw_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return sw_selftest() } 344 if argc < 3 { sw_puts("usage: nx_skinwrap <out.nxmesh> <bone.nxmesh> [depth_scale_permil]\n" as *u8); return 2 } 345 var ds: i64 = 1000 346 if argc > 3 { ds = sw_atoi(argv[3] as *u8) } 347 if ds < 0 { sw_puts("{\x22error\x22:\x22depth_scale_permil must not be negative\x22}\n" as *u8); return 2 } 348 let r: i64 = sw_wrap(argv[1] as *u8, argv[2] as *u8, ds) 349 if r < 0 { return 0 - r } 350 return 0 351}