code wiki / _hdl_build / nx_profile_fit.nx

nx_profile_fit.nx source

↩ module page · 754 lines · 38757 B

1// nx_profile_fit.nx -- ★MEASURED CROSS-SECTION PROFILES FROM A REAL ANATOMICAL REFERENCE. 2// This is the Infinigen method, done sovereignly: their creature lofting threads NURBS surfaces through 3// profile sections taken from REAL references (nurbs_data). Ours threaded ELLIPSES -- and a human cross 4// section is not an ellipse (flat back, spinal furrow, sternal hollow, deltoid shelf, iliac flare). This 5// organ SLICES an oracle mesh at every station height, clusters each slice into torso / arm / leg / head, 6// fits the section's bounding ellipse, and emits the DIMENSIONLESS deviation of the real outline from that 7// ellipse as a per-angle ratio. 8// 9// ★WHAT IS AND IS NOT TAKEN FROM THE ORACLE: only the SHAPE PRIOR (a per-mille ratio per angle, 1000 = on 10// the ellipse). Every SIZE stays procedural -- the generator still decides stature, breadth, build and 11// dimorphism. So this cannot become "ship the scanned body": it is a measured shape rule the emitter applies 12// to whatever body the rule engine asks for, exactly as Infinigen applies profiles from refs to a genome. 13// Oracle provenance/licence is written into the emitted file header by the caller's manifest. 14// 15// nx_profile_fit <oracle.nxmesh> <out.dat> [step_permil] [canon_out.dat] [part_id] 16// ORGAN MODE: supply part_id and the oracle is declared to BE that single part -- no band gate, no limb 17// clustering. Absent it, the body path is unchanged. 18// license_tier: ORIGINAL expect_exit: 0 19import "nx_syscalls.nx" 20 21const PF_Q14: i64 = 16384 22const PF_BIG: i64 = 2000000000 23const PF_M8388607: i64 = 8388607 24const PF_M8388608: i64 = 8388608 25const PF_POSQ0: i64 = 4096 26const PF_TARGET: i64 = 200000 27const PF_MAGIC_40500: i64 = 40500 28// ★ANGULAR RESOLUTION OF THE PRIOR. 24 bins (15 deg) proved too coarse to be worth anything: it smoothed 29// the back instead of carrying the scapular ridges, and measured WORSE than no prior. The prior can only 30// carry structure the emitter's ring can represent, so keep bins <= the emitter's radial segment count. 31const PF_NB: i64 = 48 32const PF_MAXST: i64 = 256 // station slices 33const PF_MAXPT: i64 = 4096 // section points held per station 34const PF_XBINS: i64 = 128 // x-histogram bins used to separate torso from limbs 35const PF_GAP: i64 = 2 // empty x-bins that separate two clusters 36const PF_MAXRUN: i64 = 8 37const PF_MAXPARTS: i64 = 8 38const PF_MAXK: i64 = 12 // control rings kept per part after factorisation 39// ★262144 -> 1048576: emitting a row under N canon parts multiplies the output by N. 139 rows x ~223B is 40// 31KB for one part, but x7 for the skull canon is ~217KB -- 83 percent of the old cap, with NO bounds check 41// in pf_puts/pf_putint, so a slightly finer STEP would have silently corrupted memory instead of erroring. 42const PF_OUTCAP: i64 = 1048576 // output text buffer 43// part indices must match the canon: 0 torso, 1 arm, 2 leg, 4 head 44const PF_PTORSO: i64 = 0 45const PF_PARM: i64 = 1 46const PF_PLEG: i64 = 2 47const PF_PHEAD: i64 = 4 48// anatomical band limits, per-mille of stature (canon ring extents, not tuned constants) 49const PF_TORSO_LO: i64 = 430 50const PF_TORSO_HI: i64 = 908 51// ★BAND FLOORS ARE ANATOMY, NOT TUNING: below the wrist the outer cluster is the HAND and below the ankle 52// it is the FOOT (a forward-running part in the canon, so its section is not the leg tube's section). 53// Profiling those heights would feed hand/foot outlines into the arm/leg tubes. 54const PF_ARM_LO: i64 = 470 55const PF_ARM_HI: i64 = 838 56const PF_LEG_LO: i64 = 60 57const PF_LEG_HI: i64 = 452 58const PF_HEAD_LO: i64 = 852 59 60func pf_hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n+1 } sys_write(1, s, n); return 0 } 61func pf_pn(v: i64) -> i64 { 62 let b: *u8 = sys_mmap(32); var x: i64 = v; var ng: i64 = 0 63 if x < 0 { ng = 1; x = 0-x } 64 var i: i64 = 31 65 if x == 0 { b[i] = 48 as u8; i = i-1 } 66 while x > 0 { b[i] = (48 + x%10) as u8; x = x/10; i = i-1 } 67 if ng == 1 { b[i] = 45 as u8; i = i-1 } 68 sys_write(1, (b as i64 + i + 1) as *u8, 31-i); return 0 69} 70func pf_satoi(s: *u8) -> i64 { 71 var i: i64 = 0; var n: i64 = 0 72 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 } 73 return n 74} 75func pf_rdbits(b: *u8, o: i64) -> i64 { 76 return (b[o] as i64) | ((b[o+1] as i64)<<8) | ((b[o+2] as i64)<<16) | ((b[o+3] as i64)<<24) 77} 78func pf_f32mul(b: *u8, o: i64, mul: i64) -> i64 { 79 let bits: i64 = pf_rdbits(b, o) 80 let sign: i64 = (bits>>31) & 1 81 let exp: i64 = (bits>>23) & 255 82 let mant: i64 = bits & PF_M8388607 83 if exp == 0 { return 0 } 84 let m: i64 = (mant | PF_M8388608) * mul 85 var e: i64 = exp - 127 - 23 86 var v: i64 = 0 87 if e >= 0 { v = m << e } else { let sh: i64 = 0-e; v = (m + (1 << (sh-1))) >> sh } 88 if sign == 1 { v = 0-v } 89 return v 90} 91func pf_isqrt(v: i64) -> i64 { if v <= 0 { return 0 } var x: i64 = v; var y: i64 = (x+1)/2; while y < x { x = y; y = (x + v/x)/2 } return x } 92func pf_wrap(d: i64) -> i64 { var x: i64 = d % 360; if x < 0 { x = x + 360 } return x } 93// Bhaskara-I degree sine in Q14 -- our own integer trig, exact at 0/30/90/150/180 94func pf_sin_fill(t: *i64) -> i64 { 95 var d: i64 = 0 96 while d < 180 { let P: i64 = d*(180-d); t[d] = PF_Q14*4*P/(PF_MAGIC_40500-P); t[d+180] = 0-t[d]; d = d+1 } 97 return 0 98} 99// append a decimal integer plus one separator byte to the output buffer 100func pf_putint(buf: *u8, pos: *i64, v: i64, sep: i64) -> i64 { 101 var p: i64 = pos[0] 102 var x: i64 = v 103 if x < 0 { buf[p] = 45 as u8; p = p+1; x = 0-x } 104 let tmp: *u8 = sys_mmap(32) 105 var i: i64 = 31 106 if x == 0 { tmp[i] = 48 as u8; i = i-1 } 107 while x > 0 { tmp[i] = (48 + x%10) as u8; x = x/10; i = i-1 } 108 var k: i64 = i+1 109 while k < 32 { buf[p] = tmp[k]; p = p+1; k = k+1 } 110 buf[p] = sep as u8; p = p+1 111 pos[0] = p 112 return 0 113} 114func pf_puts(buf: *u8, pos: *i64, s: *u8) -> i64 { 115 var p: i64 = pos[0] 116 var i: i64 = 0 117 while s[i] != (0 as u8) { buf[p] = s[i]; p = p+1; i = i+1 } 118 pos[0] = p 119 return 0 120} 121 122// ============================ SECTION FIT ============================ 123// Fit one clustered slice: bounding ellipse (centre + semi-axes from the section's own extent), then the 124// per-angle support radius measured along the ELLIPSE PARAMETER directions the emitter actually uses -- 125// the emitter places a vertex at (ra*cos t, rb*sin t), so the ratio must be measured along that same ray or 126// it would not compose with the emitter's parameterisation. 127// Writes ra,rb into fit[0..1] and PF_NB ratios into rat[]; returns 1 on success, 0 if the section is degenerate. 128func pf_fit_section(px: *i64, pz: *i64, idx: *i64, cnt: i64, sinT: *i64, fit: *i64, rat: *i64) -> i64 { 129 if cnt < PF_NB { return 0 } 130 var xmn: i64 = PF_BIG; var xmx: i64 = 0-PF_BIG; var zmn: i64 = PF_BIG; var zmx: i64 = 0-PF_BIG 131 var k: i64 = 0 132 while k < cnt { 133 let j: i64 = idx[k] 134 let x: i64 = px[j]; let z: i64 = pz[j] 135 if x < xmn { xmn = x } 136 if x > xmx { xmx = x } 137 if z < zmn { zmn = z } 138 if z > zmx { zmx = z } 139 k = k+1 140 } 141 let xc: i64 = (xmn+xmx)/2; let zc: i64 = (zmn+zmx)/2 142 var ra: i64 = (xmx-xmn)/2; var rb: i64 = (zmx-zmn)/2 143 if ra < 1 { return 0 } 144 if rb < 1 { return 0 } 145 fit[0] = ra; fit[1] = rb; fit[2] = xc; fit[3] = zc 146 // ellipse-parameter directions, unit (Q14) plus their true length 147 let ux: *i64 = sys_mmap(PF_NB*8) as *i64 148 let uz: *i64 = sys_mmap(PF_NB*8) as *i64 149 let ul: *i64 = sys_mmap(PF_NB*8) as *i64 150 let best: *i64 = sys_mmap(PF_NB*8) as *i64 151 var i: i64 = 0 152 while i < PF_NB { 153 let dg: i64 = pf_wrap(i*360/PF_NB) 154 let dx: i64 = ra*sinT[pf_wrap(dg+90)]/PF_Q14 155 let dz: i64 = rb*sinT[dg]/PF_Q14 156 var l: i64 = pf_isqrt(dx*dx + dz*dz) 157 if l < 1 { l = 1 } 158 ux[i] = dx*PF_Q14/l; uz[i] = dz*PF_Q14/l; ul[i] = l 159 best[i] = 0-PF_BIG 160 i = i+1 161 } 162 // ★MEAN RADIUS PER BIN, NOT THE MAXIMUM. Taking the max projection in each direction is the section's 163 // SUPPORT FUNCTION, and a support function describes the CONVEX HULL -- every concavity (the spinal 164 // furrow, the groove between the erector columns, the armpit, the popliteal hollow) is erased by 165 // construction. Measured consequence: the convexified prior transferred fine on the mostly-convex FRONT 166 // (detail 348->372) and wrecked the BACK (374->281), because it flattened our back without carrying the 167 // structure that makes a real back busy. Averaging the points that fall in a bin keeps concavities. 168 let bsum: *i64 = sys_mmap(PF_NB*8) as *i64 169 let bcnt: *i64 = sys_mmap(PF_NB*8) as *i64 170 i = 0 171 while i < PF_NB { bsum[i] = 0; bcnt[i] = 0; i = i+1 } 172 k = 0 173 while k < cnt { 174 let j: i64 = idx[k] 175 let vx: i64 = px[j]-xc; let vz: i64 = pz[j]-zc 176 var bi: i64 = 0; var bd: i64 = 0-PF_BIG 177 i = 0 178 while i < PF_NB { 179 let d: i64 = (vx*ux[i] + vz*uz[i])/PF_Q14 180 if d > bd { bd = d; bi = i } 181 i = i+1 182 } 183 bsum[bi] = bsum[bi] + bd; bcnt[bi] = bcnt[bi] + 1 184 if bd > best[bi] { best[bi] = bd } 185 k = k+1 186 } 187 // dimensionless ratio vs the fitted ellipse; empty bins filled from the nearest occupied neighbour 188 i = 0 189 while i < PF_NB { 190 if bcnt[i] < 1 { rat[i] = 0 } else { rat[i] = (bsum[i]/bcnt[i])*1000/ul[i] } 191 i = i+1 192 } 193 var filled: i64 = 0 194 i = 0 195 while i < PF_NB { if rat[i] > 0 { filled = filled+1 } i = i+1 } 196 if filled < PF_NB/2 { return 0 } 197 var pass: i64 = 0 198 while pass < PF_NB { 199 i = 0 200 while i < PF_NB { 201 if rat[i] == 0 { 202 let a: i64 = rat[(i+1)%PF_NB] 203 let b: i64 = rat[(i+PF_NB-1)%PF_NB] 204 if a > 0 { if b > 0 { rat[i] = (a+b)/2 } else { rat[i] = a } } else { if b > 0 { rat[i] = b } } 205 } 206 i = i+1 207 } 208 pass = pass+1 209 } 210 return 1 211} 212 213// ★SAGITTAL SYMMETRISATION for the midline parts (torso, head). A cadaver is not perfectly symmetric and a 214// slice picks up scan noise; a GENERATED body is mirrored about x=0, so an asymmetric prior would apply one 215// side's noise to both. Averaging theta with 180-theta keeps the anatomy (flat back, sternal hollow) and 216// cancels the asymmetry we could not honestly reproduce anyway. Limb profiles are left as measured: their 217// asymmetry (medial vs lateral) is real and the emitter flips the angle for the mirrored side. 218func pf_symmetrize(rat: *i64) -> i64 { 219 let tmp: *i64 = sys_mmap(PF_NB*8) as *i64 220 var i: i64 = 0 221 while i < PF_NB { tmp[i] = rat[i]; i = i+1 } 222 i = 0 223 while i < PF_NB { 224 let m: i64 = (PF_NB/2 - i + PF_NB) % PF_NB 225 rat[i] = (tmp[i] + tmp[m])/2 226 i = i+1 227 } 228 return 0 229} 230 231 232// ★FACTORISE MEASURED STATIONS INTO CONTROL HANDLES -- the Infinigen step we had been skipping. Their part 233// templates carry handles factorised from real reference data; ours were TYPED. Given every measured station 234// of a part, choose the K stations that reconstruct the whole run best: start from the two ends and greedily 235// insert whichever station deviates most from the straight line between its selected neighbours. 236// ★THE POINT: ring density then follows WHERE THE SHAPE CHANGES, instead of being uniform. GX-34 proved a 237// uniform-linear generator scores WORSE than the typed table precisely because the table encoded dense rings 238// at the shoulder and sparse ones down the forearm. Here that density is measured, not authored. 239func pf_factorise(cY: *i64, cRA: *i64, cRB: *i64, cXC: *i64, cZC: *i64, base: i64, n: i64, K: i64, sel: *i64) -> i64 { 240 var i: i64 = 0 241 while i < n { sel[i] = 0; i = i+1 } 242 if n < 2 { if n == 1 { sel[0] = 1 } return n } 243 sel[0] = 1; sel[n-1] = 1 244 var have: i64 = 2 245 while have < K { 246 var bi: i64 = 0-1 247 var be: i64 = 0-1 248 var a: i64 = 0 249 while a < n-1 { 250 if sel[a] == 1 { 251 var b: i64 = a+1 252 var go: i64 = 1 253 while go == 1 { if b >= n-1 { go = 0 } else { if sel[b] == 1 { go = 0 } else { b = b+1 } } } 254 // every unselected station between the selected pair (a,b): error vs the linear reconstruction 255 var m: i64 = a+1 256 while m < b { 257 var w: i64 = 0 258 if cY[base+b] != cY[base+a] { w = (cY[base+m]-cY[base+a])*1000/(cY[base+b]-cY[base+a]) } 259 var e: i64 = 0 260 var d1: i64 = cRA[base+m] - (cRA[base+a] + (cRA[base+b]-cRA[base+a])*w/1000) 261 if d1 < 0 { d1 = 0-d1 } 262 var d2: i64 = cRB[base+m] - (cRB[base+a] + (cRB[base+b]-cRB[base+a])*w/1000) 263 if d2 < 0 { d2 = 0-d2 } 264 var d3: i64 = cXC[base+m] - (cXC[base+a] + (cXC[base+b]-cXC[base+a])*w/1000) 265 if d3 < 0 { d3 = 0-d3 } 266 var d4: i64 = cZC[base+m] - (cZC[base+a] + (cZC[base+b]-cZC[base+a])*w/1000) 267 if d4 < 0 { d4 = 0-d4 } 268 e = d1+d2+d3+d4 269 if e > be { be = e; bi = m } 270 m = m+1 271 } 272 a = b 273 } else { a = a+1 } 274 } 275 if bi < 0 { have = K } else { sel[bi] = 1; have = have+1 } 276 } 277 return have 278} 279// emit one factorised part as canon P/R rows. Midline parts force xoff 0 (a generated body is bilaterally 280// symmetric; the cadaver's own asymmetry is not something we could honestly reproduce anyway). 281func pf_emit_part(buf: *u8, pos: *i64, cY: *i64, cRA: *i64, cRB: *i64, cXC: *i64, cZC: *i64, 282 base: i64, n: i64, sel: *i64, mir: i64, mat: i64, zref: i64) -> i64 { 283 pf_puts(buf, pos, "P " as *u8) 284 pf_putint(buf, pos, mir, 32); pf_putint(buf, pos, 0, 32); pf_putint(buf, pos, 0, 32) 285 pf_putint(buf, pos, 0, 32); pf_putint(buf, pos, 0, 32); pf_putint(buf, pos, mat, 10) 286 var i: i64 = 0 287 while i < n { 288 if sel[i] == 1 { 289 var xo: i64 = cXC[base+i] 290 if mir == 0 { xo = 0 } 291 pf_puts(buf, pos, "R " as *u8) 292 pf_putint(buf, pos, cY[base+i], 32) 293 pf_putint(buf, pos, xo, 32) 294 // ★z is measured ABSOLUTE to the oracle's own origin, so every part came out fitted at its own 295 // depth and the parts stopped agreeing with each other -- measured as the side silhouette 296 // collapsing 813 -> 678 while the front hit its best ever 886. One global reference subtracted 297 // keeps the RELATIVE depths (arms behind the chest plane is real anatomy) and removes the shift. 298 pf_putint(buf, pos, cZC[base+i]-zref, 32) 299 pf_putint(buf, pos, cRA[base+i], 32) 300 pf_putint(buf, pos, cRB[base+i], 10) 301 } 302 i = i+1 303 } 304 return 0 305} 306 307// ANATOMICAL LANDMARKS -- the registration primitive. kind 1 = height of MAX radius in the band, 0 = MIN. 308func pf_landmark(cY: *i64, cR: *i64, base: i64, n: i64, lo: i64, hi: i64, kind: i64) -> i64 { 309 var bi: i64 = 0-1 310 var bv: i64 = 0 311 var i: i64 = 0 312 while i < n { 313 let y: i64 = cY[base+i] 314 if y >= lo { if y <= hi { 315 let v: i64 = cR[base+i] 316 if bi < 0 { bi = i; bv = v } else { 317 if kind == 1 { if v > bv { bv = v; bi = i } } else { if v < bv { bv = v; bi = i } } 318 } 319 }} 320 i = i+1 321 } 322 if bi < 0 { return 0 } 323 return cY[base+bi] 324} 325 326func main(argc: i64, argv: *i64) -> i64 { 327 if argc < 3 { pf_hw("{\x22error\x22:\x22usage: nx_profile_fit <oracle.nxmesh> <out.dat> [step_permil]\x22}\n" as *u8); return 2 } 328 let orap: *u8 = argv[1] as *u8 329 let outp: *u8 = argv[2] as *u8 330 var STEP: i64 = 6 331 if argc > 3 { STEP = pf_satoi(argv[3] as *u8) } 332 if STEP < 2 { STEP = 2 } 333 // ★ORGAN MODE (argv[5]). The band table is BODY anatomy expressed in per-mille of the oracle's OWN AABB 334 // height, so aiming this organ at a SINGLE-ORGAN oracle silently misclassifies it: for a skull the 335 // mandible and maxilla fall in the LEG band, the midface in TORSO, and only the top 148 permil reads as 336 // HEAD. Worse, the two-fused-legs recovery fires whenever nrun==1 inside the leg band and SAWS THE SKULL 337 // DOWN ITS MIDLINE to fit the +x half as a limb. That is measured, not feared: debt 1785438981 records 338 // that the 156 rows in profile_human.dat are body sections, and feeding them to a skull lifted the front 339 // (+5 headline) while DEGRADING side_iou 559->531 and quarter_iou 665->627 -- right mechanism, wrong data. 340 // Declaring the part makes every station belong to it. ORGANON==0 leaves the body path byte-identical. 341 var ORGAN: i64 = 0-1 342 var ORGANON: i64 = 0 343 if argc > 5 { ORGANON = 1; ORGAN = pf_satoi(argv[5] as *u8) } 344 // the head band spills a duplicate torso row where the two overlap; one organ has no such overlap 345 var spillHi: i64 = PF_TORSO_HI 346 if ORGANON == 1 { spillHi = 0-1 } 347 // ★TARGET Y-BAND (argv[6],argv[7], per-mille of STATURE). Identity by default, so nothing changes for a 348 // whole-body oracle. See the frame-mapping note in the station loop for why a single-organ oracle needs it. 349 var YLO: i64 = 0 350 var YHI: i64 = 1000 351 if argc > 7 { YLO = pf_satoi(argv[6] as *u8); YHI = pf_satoi(argv[7] as *u8) } 352 if YHI <= YLO { YLO = 0; YHI = 1000 } 353 // ★HIGHEST CANON PART ID to also emit each row under (argv[8]). Default -1 = emit for `part` only, so every 354 // existing caller is byte-identical. See the emission site for the measured reason this exists. 355 var PIDHI: i64 = 0-1 356 if argc > 8 { PIDHI = pf_satoi(argv[8] as *u8) } 357 let sinT: *i64 = sys_mmap(400*8) as *i64 358 pf_sin_fill(sinT) 359 360 let ln: *i64 = sys_mmap(16) as *i64 361 let mb: *u8 = sys_read_file(orap, ln) 362 if (mb as i64) == 0 { pf_hw("{\x22error\x22:\x22cannot read oracle mesh\x22}\n" as *u8); return 3 } 363 let nl: i64 = pf_rdbits(mb, 8) 364 let nt: i64 = pf_rdbits(mb, 12) 365 let tb: i64 = 16 + nl*24 366 367 // pass 0: scale-invariant working precision (a metre-authored mesh must not collapse to zero) 368 var q0mn: i64 = PF_BIG; var q0mx: i64 = 0-PF_BIG 369 var t: i64 = 0 370 while t < nt { 371 let o0: i64 = tb + t*84 372 var c0: i64 = 0 373 while c0 < 3 { let vq: i64 = pf_f32mul(mb, o0 + c0*4, PF_POSQ0); if vq < q0mn { q0mn = vq } if vq > q0mx { q0mx = vq } c0 = c0+1 } 374 t = t+1 375 } 376 var span0: i64 = q0mx - q0mn 377 if span0 < 1 { span0 = 1 } 378 var posq: i64 = PF_POSQ0 * PF_TARGET / span0 379 if posq < 1 { posq = 1 } 380 381 // pass 1: AABB -> stature and body midline 382 var mnx: i64 = PF_BIG; var mny: i64 = PF_BIG; var mnz: i64 = PF_BIG 383 var mxx: i64 = 0-PF_BIG; var mxy: i64 = 0-PF_BIG; var mxz: i64 = 0-PF_BIG 384 t = 0 385 while t < nt { 386 var v: i64 = 0 387 while v < 3 { 388 let o: i64 = tb + t*84 + v*12 389 let x: i64 = pf_f32mul(mb,o,posq); let y: i64 = pf_f32mul(mb,o+4,posq); let z: i64 = pf_f32mul(mb,o+8,posq) 390 if x<mnx {mnx=x} if x>mxx {mxx=x} if y<mny {mny=y} if y>mxy {mxy=y} if z<mnz {mnz=z} if z>mxz {mxz=z} 391 v = v+1 392 } 393 t = t+1 394 } 395 var stature: i64 = mxy - mny 396 if stature < 1 { stature = 1 } 397 let cxmid: i64 = (mnx+mxx)/2 398 let nst: i64 = 1000/STEP + 1 399 if nst > PF_MAXST { pf_hw("{\x22error\x22:\x22step too small for station table\x22}\n" as *u8); return 4 } 400 401 // pass 2: slice. ONE pass over triangles; each triangle contributes to the few stations it spans. 402 let spx: *i64 = sys_mmap(PF_MAXST*PF_MAXPT*8) as *i64 403 let spz: *i64 = sys_mmap(PF_MAXST*PF_MAXPT*8) as *i64 404 let scn: *i64 = sys_mmap(PF_MAXST*8) as *i64 405 var s: i64 = 0 406 while s < nst { scn[s] = 0; s = s+1 } 407 let vx: *i64 = sys_mmap(3*8) as *i64 408 let vy: *i64 = sys_mmap(3*8) as *i64 409 let vz: *i64 = sys_mmap(3*8) as *i64 410 t = 0 411 while t < nt { 412 var v: i64 = 0 413 var ymn: i64 = PF_BIG; var ymx: i64 = 0-PF_BIG 414 while v < 3 { 415 let o: i64 = tb + t*84 + v*12 416 vx[v] = pf_f32mul(mb,o,posq); vy[v] = pf_f32mul(mb,o+4,posq); vz[v] = pf_f32mul(mb,o+8,posq) 417 if vy[v] < ymn { ymn = vy[v] } 418 if vy[v] > ymx { ymx = vy[v] } 419 v = v+1 420 } 421 var s0: i64 = (ymn - mny)*1000/stature/STEP 422 var s1: i64 = (ymx - mny)*1000/stature/STEP + 1 423 if s0 < 0 { s0 = 0 } 424 if s1 > nst-1 { s1 = nst-1 } 425 var st: i64 = s0 426 while st <= s1 { 427 let Y: i64 = mny + st*STEP*stature/1000 428 var e: i64 = 0 429 while e < 3 { 430 let a: i64 = e; let b: i64 = (e+1)%3 431 var lo: i64 = a; var hi: i64 = b 432 if vy[a] > vy[b] { lo = b; hi = a } 433 if vy[lo] <= Y { if vy[hi] > Y { 434 var den: i64 = vy[hi]-vy[lo] 435 if den < 1 { den = 1 } 436 let f: i64 = (Y - vy[lo])*1000/den 437 let ix: i64 = vx[lo] + (vx[hi]-vx[lo])*f/1000 438 let iz: i64 = vz[lo] + (vz[hi]-vz[lo])*f/1000 439 let c: i64 = scn[st] 440 if c < PF_MAXPT { spx[st*PF_MAXPT+c] = ix; spz[st*PF_MAXPT+c] = iz; scn[st] = c+1 } 441 }} 442 e = e+1 443 } 444 st = st+1 445 } 446 t = t+1 447 } 448 449 // pass 3: per station, cluster on x, assign clusters to canon parts, fit each section 450 let obuf: *u8 = sys_mmap(PF_OUTCAP) 451 let opos: *i64 = sys_mmap(16) as *i64 452 opos[0] = 0 453 pf_puts(obuf, opos, "; Nishi measured cross-section profiles -- DIMENSIONLESS shape prior (1000 = on the fitted\n" as *u8) 454 pf_puts(obuf, opos, "; ellipse). Sizes stay procedural; only the per-angle deviation of a real human section from an\n" as *u8) 455 pf_puts(obuf, opos, "; ellipse is taken. Source oracle: BodyParts3D, (c) The Database Center for Life Science,\n" as *u8) 456 pf_puts(obuf, opos, "; licensed under CC Attribution-Share Alike 2.1 Japan. Measured by nx_profile_fit.\n" as *u8) 457 pf_puts(obuf, opos, "; S <part> <ymil> <ra_permil> <rb_permil> <ratio x N, theta 0=+X lateral, 90=+Z front>\n" as *u8) 458 // the bin count travels WITH the data, so the consumer can never assume a different resolution 459 pf_puts(obuf, opos, "N " as *u8) 460 pf_putint(obuf, opos, PF_NB, 10) 461 462 let hist: *i64 = sys_mmap(PF_XBINS*8) as *i64 463 let runLo: *i64 = sys_mmap(PF_MAXRUN*8) as *i64 464 let runHi: *i64 = sys_mmap(PF_MAXRUN*8) as *i64 465 let sel: *i64 = sys_mmap(PF_MAXPT*8) as *i64 466 let fit: *i64 = sys_mmap(8*8) as *i64 467 let rat: *i64 = sys_mmap(PF_NB*8) as *i64 468 var rows: i64 = 0 469 var devsum: i64 = 0; var devcnt: i64 = 0 470 // measured control-handle tables, per part (the raw material the canon is factorised from) 471 let cN: *i64 = sys_mmap(PF_MAXPARTS*8) as *i64 472 let cY: *i64 = sys_mmap(PF_MAXPARTS*PF_MAXST*8) as *i64 473 let cRA: *i64 = sys_mmap(PF_MAXPARTS*PF_MAXST*8) as *i64 474 let cRB: *i64 = sys_mmap(PF_MAXPARTS*PF_MAXST*8) as *i64 475 let cXC: *i64 = sys_mmap(PF_MAXPARTS*PF_MAXST*8) as *i64 476 let cZC: *i64 = sys_mmap(PF_MAXPARTS*PF_MAXST*8) as *i64 477 var pz0: i64 = 0 478 while pz0 < PF_MAXPARTS { cN[pz0] = 0; pz0 = pz0+1 } 479 s = 0 480 while s < nst { 481 let ymil: i64 = s*STEP 482 // ★FRAME MAPPING. ymil is per-mille of the ORACLE'S OWN height, but the consumer looks the prior up in 483 // per-mille of STATURE (nx_body_gen: ymq = yri/1000, the canon's own R-row units). For a whole-body 484 // oracle those two frames coincide, which is why nothing needed this before. For a SINGLE-ORGAN oracle 485 // they do NOT: nx_skullgen emits R rows spanning y 872..1000, so a skull profile written at 0..1000 486 // would be queried ONLY over its top 128 per-mille -- every part of the skull modulated by the CROWN's 487 // cross-section, and silently, because the rows exist and the lookup succeeds. Map the oracle's own 488 // extent onto the band the canon actually occupies. 489 var yout: i64 = ymil 490 if ORGANON == 1 { yout = YLO + ymil*(YHI-YLO)/1000 } 491 let cnt: i64 = scn[s] 492 if cnt >= PF_NB { 493 var xmn: i64 = PF_BIG; var xmx: i64 = 0-PF_BIG 494 var k: i64 = 0 495 while k < cnt { 496 let x: i64 = spx[s*PF_MAXPT+k] 497 if x < xmn { xmn = x } 498 if x > xmx { xmx = x } 499 k = k+1 500 } 501 var xsp: i64 = xmx - xmn 502 if xsp < 1 { xsp = 1 } 503 var h: i64 = 0 504 while h < PF_XBINS { hist[h] = 0; h = h+1 } 505 k = 0 506 while k < cnt { 507 var bi: i64 = (spx[s*PF_MAXPT+k] - xmn)*(PF_XBINS-1)/xsp 508 if bi < 0 { bi = 0 } 509 if bi > PF_XBINS-1 { bi = PF_XBINS-1 } 510 hist[bi] = hist[bi] + 1 511 k = k+1 512 } 513 // contiguous runs of occupied bins, split where PF_GAP or more bins are empty 514 var nrun: i64 = 0 515 var inrun: i64 = 0 516 var gap: i64 = 0 517 h = 0 518 while h < PF_XBINS { 519 if hist[h] > 0 { 520 if inrun == 0 { if nrun < PF_MAXRUN { runLo[nrun] = h; runHi[nrun] = h; nrun = nrun+1; inrun = 1 } } 521 else { runHi[nrun-1] = h } 522 gap = 0 523 } else { 524 if inrun == 1 { gap = gap+1; if gap >= PF_GAP { inrun = 0 } else { runHi[nrun-1] = h } } 525 } 526 h = h+1 527 } 528 // pick the centre run (torso/head) and the outermost run (arm/leg) 529 var ic: i64 = 0-1; var io: i64 = 0-1 530 var bestc: i64 = PF_BIG; var besto: i64 = 0-1 531 var r: i64 = 0 532 while r < nrun { 533 let rc: i64 = xmn + (runLo[r]+runHi[r])*xsp/(2*(PF_XBINS-1)) 534 var dc: i64 = rc - cxmid 535 if dc < 0 { dc = 0-dc } 536 if dc < bestc { bestc = dc; ic = r } 537 if rc > besto { besto = rc; io = r } 538 r = r+1 539 } 540 // ---- centre run -> torso and/or head ---- 541 if ic >= 0 { 542 let lo: i64 = xmn + runLo[ic]*xsp/(PF_XBINS-1) - 1 543 let hi: i64 = xmn + runHi[ic]*xsp/(PF_XBINS-1) + 1 544 var nsel: i64 = 0 545 k = 0 546 while k < cnt { 547 let x: i64 = spx[s*PF_MAXPT+k] 548 if x >= lo { if x <= hi { if nsel < PF_MAXPT { sel[nsel] = s*PF_MAXPT+k; nsel = nsel+1 } } } 549 k = k+1 550 } 551 if pf_fit_section(spx, spz, sel, nsel, sinT, fit, rat) == 1 { 552 pf_symmetrize(rat) 553 var part: i64 = 0-1 554 if ymil >= PF_TORSO_LO { if ymil <= PF_TORSO_HI { part = PF_PTORSO } } 555 if ymil >= PF_HEAD_LO { part = PF_PHEAD } 556 if ORGANON == 1 { part = ORGAN } 557 if part >= 0 { 558 // ★DO NOT RECORD A HANDLE WHERE THE SECTION IS NOT MEASURABLE. Through the arm band the 559 // arms touch the torso, so the x-clustering returns ONE run and the "torso" section 560 // silently includes both arms -- measured as ra jumping 88 -> 148 at shoulder height. 561 // A handle fitted there is an artifact, and the greedy factoriser will faithfully 562 // select it BECAUSE it is the largest change. Skip it and let the spline interpolate 563 // across the gap: an honest hole beats a confident wrong number. 564 var meas: i64 = 1 565 if nrun < 2 { if ymil >= PF_ARM_LO { if ymil <= PF_ARM_HI { meas = 0 } } } 566 if ORGANON == 1 { meas = 1 } 567 if meas == 1 { if cN[part] < PF_MAXST { 568 let ci: i64 = part*PF_MAXST + cN[part] 569 cY[ci]=yout; cRA[ci]=fit[0]*1000/stature; cRB[ci]=fit[1]*1000/stature 570 cXC[ci]=fit[2]*1000/stature; cZC[ci]=fit[3]*1000/stature 571 cN[part] = cN[part]+1 572 }} 573 // ★★★EMIT UNDER EVERY CANON PART THAT SPANS THIS HEIGHT. MEASURED 2026-07-30: with rows for 574 // part 0 ONLY, the vault moved at full strength while supraorbital and zygomatic -- which 575 // both span canon y958 -- had no rows for their id, returned 1000, and STOOD STILL. 576 // nx_meshprofile caught it as a NEW radius jump at station 671 that PROF=0 and PROF=250 577 // do not have. A per-part EDGE feather cannot fix that: y958 is the vault's INTERIOR, 578 // exactly where an edge rule is designed not to act. LAW: a prior on ONE part but not the 579 // parts it OVERLAPS steps worst in that part's interior. Correspondence stays honest -- 580 // the rows span the whole canon band, so part p reads the section measured at p's own y. 581 var pend: i64 = part 582 if PIDHI > part { pend = PIDHI } 583 var pid: i64 = part 584 while pid <= pend { 585 pf_puts(obuf, opos, "S " as *u8) 586 pf_putint(obuf, opos, pid, 32) 587 pf_putint(obuf, opos, yout, 32) 588 pf_putint(obuf, opos, fit[0]*1000/stature, 32) 589 pf_putint(obuf, opos, fit[1]*1000/stature, 32) 590 var i2: i64 = 0 591 while i2 < PF_NB { 592 var sepc: i64 = 32 593 if i2 == PF_NB-1 { sepc = 10 } 594 pf_putint(obuf, opos, rat[i2], sepc) 595 var d2: i64 = rat[i2]-1000 596 if d2 < 0 { d2 = 0-d2 } 597 devsum = devsum + d2; devcnt = devcnt + 1 598 i2 = i2+1 599 } 600 rows = rows+1 601 // the head band also feeds the torso tube where they overlap, so the neck keeps a profile 602 if part == PF_PHEAD { if ymil <= spillHi { 603 pf_puts(obuf, opos, "S " as *u8) 604 pf_putint(obuf, opos, PF_PTORSO, 32) 605 pf_putint(obuf, opos, ymil, 32) 606 pf_putint(obuf, opos, fit[0]*1000/stature, 32) 607 pf_putint(obuf, opos, fit[1]*1000/stature, 32) 608 i2 = 0 609 while i2 < PF_NB { 610 var sepd: i64 = 32 611 if i2 == PF_NB-1 { sepd = 10 } 612 pf_putint(obuf, opos, rat[i2], sepd) 613 i2 = i2+1 614 } 615 rows = rows+1 616 }} 617 pid = pid + 1 618 } 619 } 620 } 621 } 622 // ---- outermost run -> arm (above the crotch) or leg (below it) ---- 623 // TWO FUSED LEGS ARE A KNOWN GEOMETRY, NOT AN UNMEASURABLE ONE. Below mid-thigh the legs 624 // converge, the x-clustering returns ONE run, and the not-measurable guard refused to record 625 // anything -- which is why leg stations stopped at 282 permil and calf/ankle landmarks read 626 // zero. An arm fused to a torso is genuinely unrecoverable; two legs are not, because we know 627 // the seam is the midline. Split the single run at its x-midpoint and take the +x half. 628 var io2: i64 = io 629 var forceLo: i64 = 0 630 var forceHi: i64 = 0 631 var forced: i64 = 0 632 if nrun == 1 { if ymil <= PF_LEG_HI { if ymil >= PF_LEG_LO { 633 let rlo: i64 = xmn + runLo[0]*xsp/(PF_XBINS-1) 634 let rhi: i64 = xmn + runHi[0]*xsp/(PF_XBINS-1) 635 forceLo = (rlo+rhi)/2 636 forceHi = rhi + 1 637 forced = 1 638 io2 = 0 639 }}} 640 if forced == 1 { io = io2 } 641 if io >= 0 { if io != ic { forced = forced } else { if forced == 0 { io = 0-1 } } 642 if io >= 0 { 643 var lo2: i64 = xmn + runLo[io]*xsp/(PF_XBINS-1) - 1 644 var hi2: i64 = xmn + runHi[io]*xsp/(PF_XBINS-1) + 1 645 if forced == 1 { lo2 = forceLo; hi2 = forceHi } 646 var nsel2: i64 = 0 647 k = 0 648 while k < cnt { 649 let x: i64 = spx[s*PF_MAXPT+k] 650 if x >= lo2 { if x <= hi2 { if nsel2 < PF_MAXPT { sel[nsel2] = s*PF_MAXPT+k; nsel2 = nsel2+1 } } } 651 k = k+1 652 } 653 if pf_fit_section(spx, spz, sel, nsel2, sinT, fit, rat) == 1 { 654 var part2: i64 = 0-1 655 if ymil >= PF_ARM_LO { if ymil <= PF_ARM_HI { part2 = PF_PARM } } 656 if ymil <= PF_LEG_HI { if ymil >= PF_LEG_LO { part2 = PF_PLEG } else { part2 = 0-1 } } 657 // a single-organ oracle has no limbs: the outer run IS the organ, already taken above 658 if ORGANON == 1 { part2 = 0-1 } 659 if part2 >= 0 { 660 if cN[part2] < PF_MAXST { 661 let c2: i64 = part2*PF_MAXST + cN[part2] 662 cY[c2]=ymil; cRA[c2]=fit[0]*1000/stature; cRB[c2]=fit[1]*1000/stature 663 cXC[c2]=fit[2]*1000/stature; cZC[c2]=fit[3]*1000/stature 664 cN[part2] = cN[part2]+1 665 } 666 pf_puts(obuf, opos, "S " as *u8) 667 pf_putint(obuf, opos, part2, 32) 668 pf_putint(obuf, opos, ymil, 32) 669 pf_putint(obuf, opos, fit[0]*1000/stature, 32) 670 pf_putint(obuf, opos, fit[1]*1000/stature, 32) 671 var i3: i64 = 0 672 while i3 < PF_NB { 673 var sepe: i64 = 32 674 if i3 == PF_NB-1 { sepe = 10 } 675 pf_putint(obuf, opos, rat[i3], sepe) 676 var d3: i64 = rat[i3]-1000 677 if d3 < 0 { d3 = 0-d3 } 678 devsum = devsum + d3; devcnt = devcnt + 1 679 i3 = i3+1 680 } 681 rows = rows+1 682 } 683 } 684 }} 685 } 686 s = s+1 687 } 688 689 let fd: i64 = sys_openat_wr(outp, 420) 690 sys_write(fd, obuf, opos[0]) 691 sys_close(fd) 692 693 // ★FACTORISED CANON (argv[4]): control handles derived from the reference, not typed. This is the 694 // Infinigen construction end to end -- measure a real reference, factorise it into a small set of 695 // handles, and let the genome scale them. The emitter is UNCHANGED: these are ordinary canon rows. 696 var kept: i64 = 0 697 if argc > 4 { 698 let kbuf: *u8 = sys_mmap(PF_OUTCAP) 699 let kpos: *i64 = sys_mmap(16) as *i64 700 kpos[0] = 0 701 pf_puts(kbuf, kpos, "# Nishi canon FACTORISED FROM A REFERENCE by nx_profile_fit -- control handles are 702" as *u8) 703 pf_puts(kbuf, kpos, "# measured and greedily selected where the shape changes, never typed. Source oracle: 704" as *u8) 705 pf_puts(kbuf, kpos, "# BodyParts3D, (c) The Database Center for Life Science, CC Attribution-Share Alike 2.1 Japan. 706" as *u8) 707 let sel: *i64 = sys_mmap(PF_MAXST*8) as *i64 708 // global depth reference = the torso's middle handle 709 var zref: i64 = 0 710 if cN[PF_PTORSO] > 0 { zref = cZC[PF_PTORSO*PF_MAXST + cN[PF_PTORSO]/2] } 711 var pp: i64 = 0 712 while pp < PF_MAXPARTS { 713 if cN[pp] > 2 { 714 var mir: i64 = 0 715 var mat: i64 = 0 716 if pp == PF_PARM { mir = 1 } 717 if pp == PF_PLEG { mir = 1 } 718 if pp == PF_PHEAD { mat = 5 } 719 let hv: i64 = pf_factorise(cY,cRA,cRB,cXC,cZC, pp*PF_MAXST, cN[pp], PF_MAXK, sel) 720 pf_emit_part(kbuf,kpos, cY,cRA,cRB,cXC,cZC, pp*PF_MAXST, cN[pp], sel, mir, mat, zref) 721 kept = kept + hv 722 } 723 pp = pp+1 724 } 725 let kfd: i64 = sys_openat_wr(argv[4] as *u8, 420) 726 sys_write(kfd, kbuf, kpos[0]) 727 sys_close(kfd) 728 } 729 730 if devcnt < 1 { devcnt = 1 } 731 pf_hw("{\x22organ\x22:\x22nx_profile_fit\x22,\x22tris\x22:" as *u8); pf_pn(nt) 732 pf_hw(",\x22stations\x22:" as *u8); pf_pn(nst) 733 pf_hw(",\x22rows\x22:" as *u8); pf_pn(rows) 734 pf_hw(",\x22bytes\x22:" as *u8); pf_pn(opos[0]) 735 // ★NON-VACUITY: how far a real human section actually is from the ellipse the emitter used to assume. 736 // Near zero here would mean the whole rung is pointless -- publish it either way. 737 pf_hw(",\x22mean_abs_dev_permil\x22:" as *u8); pf_pn(devsum/devcnt) 738 // landmark block: measured on THIS mesh in per-mille of its OWN stature, so two meshes become comparable. 739 // Five measured-transfer attempts failed because the reference anatomy at a coordinate is not OUR anatomy 740 // at that coordinate; these points ARE that correspondence, and every one is an extremum of the per-station 741 // series this organ already measured and was discarding. 742 pf_hw(" lm_acromion=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PTORSO*PF_MAXST, cN[PF_PTORSO], 760, 908, 1)) 743 pf_hw(" lm_waist=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PTORSO*PF_MAXST, cN[PF_PTORSO], 500, 660, 0)) 744 pf_hw(" lm_iliac=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PTORSO*PF_MAXST, cN[PF_PTORSO], 430, 520, 1)) 745 pf_hw(" lm_neck=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PTORSO*PF_MAXST, cN[PF_PTORSO], 860, 940, 0)) 746 pf_hw(" lm_wrist=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PARM*PF_MAXST, cN[PF_PARM], 470, 560, 0)) 747 pf_hw(" lm_elbow=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PARM*PF_MAXST, cN[PF_PARM], 580, 700, 0)) 748 pf_hw(" lm_trochanter=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PLEG*PF_MAXST, cN[PF_PLEG], 380, 452, 1)) 749 pf_hw(" lm_knee=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PLEG*PF_MAXST, cN[PF_PLEG], 170, 300, 0)) 750 pf_hw(" lm_calf=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PLEG*PF_MAXST, cN[PF_PLEG], 110, 220, 1)) 751 pf_hw(" lm_ankle=" as *u8); pf_pn(pf_landmark(cY,cRA, PF_PLEG*PF_MAXST, cN[PF_PLEG], 60, 120, 0)) 752 pf_hw(",\x22note\x22:\x22dimensionless shape prior only; sizes stay procedural\x22}\n" as *u8) 753 return 0 754}