code wiki / (root) / nx_body_gen_skin_candidate_t345.nx

nx_body_gen_skin_candidate_t345.nx source

↩ module page · 1304 lines · 82269 B

1// nx_body_gen.nx -- ★THE PRODUCT: a SOVEREIGN PROCEDURAL human body, generated from the first byte up. 2// No scanned mesh, no third-party asset: the body is EMITTED from anthropometric parameters (fractions of 3// stature, per-mille). BodyParts3D is the ORACLE we measure against -- never the product. Competitor 4// platforms are benchmarked on CAPABILITY only; none of their assets are used. 5// 6// v2 (GX-23): the body is now built from CONTINUOUS RING PROFILES, not disjoint cylinders. Each part 7// (torso, arm, leg) is ONE tube threaded through control rings and sub-sampled, so there are no seams 8// where segments used to butt together -- that was the largest silhouette defect vs the oracle. Hands and 9// feet are ring extensions of the arm/leg tubes, so they attach continuously. Normals include the taper 10// slope, so shading follows the true surface rather than a stack of cylinders. 11// nx_body_gen <out.nxmesh> <height> [radial] [sub] [relief] [canonpath] [fat] [prof] [?] [hf] 12// ★★THE CANON PATH IS argv[6] AND IT WAS UNDOCUMENTED (seq1377). It is the single most important input -- 13// this emitter carries NO geometry literals, so WHICH canon you hand it is the whole product -- and its 14// absence from this line is why the nx_skullgen -> mesh chain LOOKED non-composable and cost a 15// measurement cycle. Defaults: relief/RLF argv[5], canon argv[6] (knowledge/canon_male.dat), fat argv[7], 16// prof argv[8], hf argv[10]. ★A usage line that omits a positional does not merely under-document it -- 17// it makes every LATER positional unreachable, because you cannot skip one. 18// license_tier: ORIGINAL expect_exit: 0 19import "nx_syscalls.nx" 20import "nx_body_skin_cut.nx" 21import "nx_body_rest_registration.nx" 22import "nx_fio.nx" 23import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 24const BG_SMOOTHSTEP_3_PERMIL: i64 = 3000 25const BG_PERMIL_CUBE: i64 = 1000000000 26const BG_PERMIL_SQ: i64 = 1000000 27const BG_CATMULL_DEN: i64 = 2000000 28const BG_F32_MANT_SCALE: i64 = 8388608 29const BG_F32_MANT_MASK: i64 = 8388607 30const BG_DEFAULT_STATURE_MM: i64 = 1750 31 32const BG_Q14: i64 = 16384 33// Bhaskara I's sine approximation (7th century): sin(x deg) ~= 4x(180-x) / (40500 - x(180-x)). 34// 40500 is that method's published constant, not a tuning knob -- the old name BG_MAGIC_40500 hid 35// the fact that this line implements a named algorithm at all. 36const BG_BHASKARA_K: i64 = 40500 37// raised for the face-parts program (F1083): 21 parts at radial 52/sub 8 reached 393,432 of the old 400k, 38// i.e. 6.5k from SILENT truncation of the skin head. Headroom + a truncated flag are now reported every run. 39// ★RAISED AGAIN 2026-08-10, measured binding LIVE: radial 96/sub 24 on even the 5-part canon truncated at 40// 599,998/600,000 (truncated:1) -- 600k sat at MetaHuman-ENTRY density while the ladder (ZBrush-class 41// sculpt density, 32 teeth, 200 eyelashes) needs millions. 2^23 tris = 1.41GB emit buffer of ADDRESS SPACE 42// (lazily mmap'd, resident only what is touched) + ~700MB NXMSH2 write buffer at FULL cap; the renderer 43// (nx_anat_sov) streams ntris from the header with NO cap of its own. Still a loud threshold, and the 44// manifest now reports tris_offered (demand) so a capped emit can never again pass as a complete one. 45const BG_MAXTRI: i64 = 8388608 46// ★★CAPS RAISED TO WHAT THE HARDWARE SUPPORTS, NOT TO A NUMBER SOMEBODY ONCE TYPED. 256 rings and 48 parts 47// were never measured against anything -- and the anatomy ladder ahead (32 teeth, 20 nails, 200 eyelashes, 48// each its own part with its own rings) walks straight through both. The arrays are lazily mmap'd, so a 49// larger cap costs address space rather than resident memory: 8192 rings across 5 arrays is 320KB of 50// reservation. ★The cap is now a REFUSAL THRESHOLD, not a silent truncation point -- which is only safe 51// because the guards above landed in the same change. Raising a bound without a check would have converted 52// a near-miss into a much larger silent overwrite. 53const BG_MAXRING: i64 = 8192 54const BG_MAXFEAT: i64 = 4096 55// ★PART CAP, named + guarded (was a bare 16 in six mmap sizes with NO bound check on the parser's write: 56// a canon with a 17th P row wrote past a 128-byte allocation -- an unguarded array write driven by an 57// EXTERNAL file, which is the classic overflow class and silently "worked" only because mmap rounds to a 58// page. The canon is external input to this emitter, so it is a boundary and must be defensive. Raised to 59// 48 because the face-geometry program needs real feature PARTS (nose, lips, ears) and the canon already 60// carried exactly 16. 61const BG_MAXPART: i64 = 2048 62// surface points are kept at BG_PSUB sub-units so the central-difference tangents keep their precision; 63// integer rounding of a raw-unit point would be ~3% of a one-step tangent and swamp the relief signal 64const BG_PSUB: i64 = 512 65const BG_MAXRS: i64 = 256 66// components are halved down to this before squaring -- keeps enx*enx+... far inside i64 67const BG_NCAP: i64 = 1000000 68// ★LAYER STACK -- the body is built from the first cell up: a bone core, a muscle envelope on the bone, and 69// the skin as the outer envelope of muscle+fat. Each is a real NXMSH2 layer, so it renders/peels separately 70// and the skin's SHAPE EMERGES from the layers beneath instead of being painted on a shell. 71// ★MEASURED SECTION PROFILES (GX-31). Every ring was an ELLIPSE modulated by relief bumps; a real human 72// cross-section is not an ellipse (flat back, scapular bulges, sternal hollow, iliac flare). nx_profile_fit 73// measures those sections off the oracle and emits a DIMENSIONLESS per-angle ratio (1000 = on the ellipse); 74// this emitter applies the ratio to whatever size the rule engine asked for, so the prior is shape-only and 75// the body stays procedural. Same idea as Infinigen taking creature profiles from real references. 76// the prior's angular resolution is carried IN the data file ('N <bins>'), never duplicated as a const in 77// two organs -- a silent bin-count mismatch would misread every row as a rotated body. 78const BG_NBMAX: i64 = 96 79// ★512 -> 2048 (2026-07-30, ws=neuro). A profile carrying every canon part needs parts x stations rows: 80// the skull canon alone is 7 x 167 = 1169, so 512 silently dropped 47 percent of a 973-row file and the 81// emitter reported prof_rows:512 as though that WERE the data. sR is BG_MAXPROF*BG_NBMAX*8 = 1.5MB at 2048. 82const BG_MAXPROF: i64 = 2048 83const BG_PROFCLAMP: i64 = 300 // a single bad measured row can never deform the body more than +/-30% 84// ★per-mille-of-stature band at each part's data extremes over which the prior fades to neutral, so a 85// per-part modulation can never pull two parts apart at the boundary the canon built them to share. 86const BG_PROFEDGE: i64 = 45 87const BG_NLAYER: i64 = 3 88const BG_BONEF: i64 = 400 // bone core radius = 40% of the skin radius (the skeletal armature) 89const BG_MUSCF: i64 = 820 // muscle envelope = 82% of the skin radius (muscle bellies fill toward skin) 90 91// ★★★ONE PLACEMENT ROTATION, USED BY ALL FOUR TRANSFORM SITES. The Z->X->Y chain was written out FOUR 92// TIMES -- position, normal, cap normal, cap centre -- as twenty-four hand-copied lines. That is the 93// duplication class this programme keeps finding the hard way: when rotY was added, the same six lines had 94// to be inserted four times and any one of them could have been missed or transposed silently, because a 95// body with three correct sites and one wrong one still renders. 96// ★A TRANSFORM APPLIED IN FOUR PLACES IS ONE RULE WITH THREE CHANCES TO DRIFT. 97// Extracting it also makes the chain TESTABLE: bg_rot3 is a pure function of nine integers, so the gate can 98// assert the composition order and the mirror convention directly instead of inferring them from a mesh. 99// ORDER IS LOAD-BEARING: rotZ turns the cross-section in its own plane, rotX pitches the part, and rotY -- 100// applied LAST -- is a true world AZIMUTH. Applied first it would merely spin the cross-section. 101func bg_rot3(x: i64, y: i64, z: i64, pzc: i64, pzs: i64, prc: i64, prs: i64, pyc: i64, pys: i64, o: *i64) -> i64 { 102 let zx: i64 = (x*pzc - y*pzs)/BG_Q14 103 let zy: i64 = (x*pzs + y*pzc)/BG_Q14 104 let ry: i64 = (zy*prc - z*prs)/BG_Q14 105 let rz: i64 = (zy*prs + z*prc)/BG_Q14 106 o[2] = (rz*pyc - zx*pys)/BG_Q14 107 o[0] = (rz*pys + zx*pyc)/BG_Q14 108 o[1] = ry 109 return 0 110} 111func bg_hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 112// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 113// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 114// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 115// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 116func bg_pn(v: i64) -> i64 { nxi_out(v); return 0 } 117func bg_satoi(s: *u8) -> i64 { var i: i64=0; var n: i64=0 118 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 } return n } 119func bg_sin_fill(t: *i64) -> i64 { 120 var d: i64=0 121 while d<180 { let P: i64=d*(180-d); t[d]=BG_Q14*4*P/(BG_BHASKARA_K-P); t[d+180]=0-t[d]; d=d+1 } 122 return 0 123} 124func bg_wrap(d: i64) -> i64 { var x: i64=d%360; if x<0 { x=x+360 } return x } 125// shortest angular separation, 0..180 126func bg_angsep(a: i64, b: i64) -> i64 { 127 var d: i64 = bg_wrap(a) - bg_wrap(b) 128 if d < 0 { d = 0-d } 129 if d > 180 { d = 360-d } 130 return d 131} 132// ★ANATOMICAL SURFACE RELIEF. Until now every ring was a plain ellipse, so the body had correct OUTLINE but a 133// featureless surface -- which is exactly what the normal-field judge was penalising (it became the binding 134// constraint at 749 once the silhouettes were fixed). Real anatomy modulates the radius with ANGLE as well as 135// height: pectorals and abdominal bulges at the front, the spinal groove and scapulae at the back, glutes, 136// quadriceps, calves. Each feature is a (part, height, angle, extent, amplitude) tuple with quadratic falloff, 137// summed -- data, not hand-carved geometry. theta: 0=+X right, 90=+Z front, 180=-X left, 270=-Z back. 138func bg_relief(part: i64, ymil: i64, th: i64, fP: *i64, fY: *i64, fYw: *i64, fT: *i64, fTw: *i64, fA: *i64, nf: i64) -> i64 { 139 var s: i64 = 0 140 var k: i64 = 0 141 while k < nf { 142 if fP[k] == part { 143 var dy: i64 = ymil - fY[k] 144 if dy < 0 { dy = 0-dy } 145 if dy < fYw[k] { 146 let dt: i64 = bg_angsep(th, fT[k]) 147 if dt < fTw[k] { 148 let wy: i64 = 1000 - dy*1000/fYw[k] 149 let wt: i64 = 1000 - dt*1000/fTw[k] 150 s = s + fA[k]*wy/1000*wt/1000 151 } 152 } 153 } 154 k = k + 1 155 } 156 return s 157} 158// ANALYTIC SHAPE BASIS (GX-41). displacement = sum of beta_i * basis_i(coord): the body as a mean shape 159// plus weighted deformation functions, the genome being the coefficients. Two properties earned it this slot: 160// (1) REGISTRATION-FREE BY CONSTRUCTION -- the basis is evaluated in OUR body's own parameter space, so the 161// misregistration that killed measured detail transfer (residual sampled at the oracle's coordinates) 162// cannot occur here; 163// (2) it creates surface that faces UP and DOWN. A smooth vertical body has ny approx 0 everywhere, which is 164// why the environment light had nothing to differentiate and why the detail judge sees no busyness. What 165// matters is the GRADIENT dr/dy, not the amplitude -- so the bases vary in HEIGHT, not just in angle, 166// unlike the relief-feature table that measured as worth 1 permil. 167// Anatomical, not noise: intercostal banding over the ribcage, the clavicle trough, the inframammary fold. 168const BG_WK_RAMP: i64 = 1 169const BG_WK_TRI: i64 = 2 170const BG_WK_RIDGE: i64 = 3 171const BG_YK_FLAT: i64 = 1 172const BG_YK_WAVE_RIB: i64 = 2 173const BG_YK_WAVE_SPAN: i64 = 3 174const BG_HALFTURN_DEG: i64 = 180 175// ★THE ROW TAGS AND THE LINE TERMINATOR, NAMED. The walkers below tested `== 66` and `== 10` -- ASCII 176// 'B' and newline written as bare integers. Those two values ARE the grammar of the canon file, and a 177// reader cannot tell a row tag from a threshold at a glance when both are just numbers. 178const BG_ROW_BASIS: i64 = 66 // 'B' -- a shape-basis feature row 179const BG_ROW_LAYER: i64 = 76 // 'L' -- a layer palette row 180const BG_LF: i64 = 10 // end of line 181// walks the basis file for 'B' rows. Same independent-walker shape as bg_parse_open, for the same reason: 182// bg_parse_canon already carries 24 arguments and widening it is the wrong direction. 183func bg_parse_basis(b: *u8, len: i64, kP: *i64, kYlo: *i64, kYhi: *i64, kTh: *i64, kWk: *i64, kW0: *i64, kW1: *i64, kW2: *i64, kAmp: *i64, kYk: *i64, kP0: *i64, kP1: *i64, kP2: *i64, cnt: *i64) -> i64 { 184 let pos: *i64 = sys_mmap(16) as *i64 185 var n: i64 = 0 186 var i: i64 = 0 187 var bol: i64 = 1 188 while i < len { 189 if bol == 1 { 190 if (b[i] as i64) == BG_ROW_BASIS { 191 if n < BG_MAXFEAT { 192 pos[0] = i+1 193 kP[n]=bg_rdint(b,pos,len); kYlo[n]=bg_rdint(b,pos,len); kYhi[n]=bg_rdint(b,pos,len) 194 kTh[n]=bg_rdint(b,pos,len); kWk[n]=bg_rdint(b,pos,len) 195 kW0[n]=bg_rdint(b,pos,len); kW1[n]=bg_rdint(b,pos,len); kW2[n]=bg_rdint(b,pos,len) 196 kAmp[n]=bg_rdint(b,pos,len); kYk[n]=bg_rdint(b,pos,len) 197 kP0[n]=bg_rdint(b,pos,len); kP1[n]=bg_rdint(b,pos,len); kP2[n]=bg_rdint(b,pos,len) 198 i = pos[0] 199 n = n + 1 200 } 201 } 202 } 203 if (b[i] as i64) == BG_LF { bol = 1 } else { bol = 0 } 204 i = i + 1 205 } 206 cnt[0] = n 207 return n 208} 209// ★THE LAYER PALETTE IS DATA TOO. The three layer colours were an IF-LADDER inside the emit loop -- 210// skin 880/700/620, muscle 760/286/262, bone 928/918/872 -- while the radius scales sitting on the SAME 211// lines (BG_MUSCF, BG_BONEF) were already named consts. Half a table named and half raw is the signature 212// of an oversight, not a decision. And as an if-ladder a FOURTH layer could not be expressed at all 213// without editing the emit loop -- the same defect anat_sov's 5-row anchor ladder had. 214// Rows are 'L <layer> <r> <g> <b>' in the SAME canon file, so there is still one file, one read, one 215// walker shape. A row naming a layer outside 0..BG_NLAYER-1 is skipped rather than writing past the array. 216func bg_parse_layers(b: *u8, len: i64, lR: *i64, lG: *i64, lB: *i64, cnt: *i64) -> i64 { 217 let pos: *i64 = sys_mmap(16) as *i64 218 var n: i64 = 0 219 var i: i64 = 0 220 var bol: i64 = 1 221 while i < len { 222 if bol == 1 { 223 if (b[i] as i64) == BG_ROW_LAYER { 224 pos[0] = i+1 225 let ly: i64 = bg_rdint(b,pos,len) 226 if ly >= 0 { if ly < BG_NLAYER { 227 lR[ly]=bg_rdint(b,pos,len); lG[ly]=bg_rdint(b,pos,len); lB[ly]=bg_rdint(b,pos,len) 228 n = n + 1 229 } } 230 i = pos[0] 231 } 232 } 233 if (b[i] as i64) == BG_LF { bol = 1 } else { bol = 0 } 234 i = i + 1 235 } 236 cnt[0] = n 237 return n 238} 239// ★THE SHAPE-BASIS CANON IS DATA (2026-08-14). Every band edge, angular window and amplitude below used to 240// be a literal here: the anatomy this body claims to have, written into the emitter where it could not be 241// revised or varied without a rebuild. nx_magic at threshold 2 surfaced them; at its DEFAULT threshold they 242// all sit under the bar and the organ reports clean. They now live in knowledge/body_shape_basis.conf. 243// ★★THE ARITHMETIC ORDER OF EACH SHAPE IS PRESERVED EXACTLY, because integer division is not associative: 244// amp*(z*1000/half)/1000 and amp*z*1000/half/1000 are DIFFERENT expressions. The acceptance proof is a 245// BIT-IDENTICAL mesh against the banked pre-refactor artifact, so a shape rewritten "equivalently" fails it. 246func bg_shape_basis(part: i64, ymil: i64, th: i64, sinT: *i64, kP: *i64, kYlo: *i64, kYhi: *i64, kTh: *i64, kWk: *i64, kW0: *i64, kW1: *i64, kW2: *i64, kAmp: *i64, kYk: *i64, kP0: *i64, kP1: *i64, kP2: *i64, nk: i64) -> i64 { 247 var s: i64 = 0 248 var k: i64 = 0 249 while k < nk { 250 var hit: i64 = 0 251 if part == kP[k] { if ymil > kYlo[k] { if ymil < kYhi[k] { hit = 1 } } } 252 if hit == 1 { 253 let w: i64 = bg_angsep(th, kTh[k]) 254 let amp: i64 = kAmp[k] 255 if kWk[k] == BG_WK_RAMP { if kYk[k] == BG_YK_FLAT { 256 if w < kW1[k] { s = s + amp*(1000 - w*1000/kW1[k])/1000 } 257 } } 258 if kWk[k] == BG_WK_RAMP { if kYk[k] == BG_YK_WAVE_SPAN { 259 if w < kW1[k] { 260 let ph2: i64 = (ymil-kYlo[k])*1000/(kYhi[k]-kYlo[k]) 261 s = s + amp*sinT[bg_wrap(ph2*BG_HALFTURN_DEG/1000)]/BG_Q14*(1000 - w*1000/kW1[k])/1000 262 } 263 } } 264 if kWk[k] == BG_WK_TRI { if kYk[k] == BG_YK_FLAT { 265 if w > kW0[k] { if w < kW0[k]+2*kW1[k] { 266 var z: i64 = w - kW0[k] 267 if z > kW1[k] { z = 2*kW1[k] - z } 268 if z > 0 { s = s + amp*z*1000/kW1[k]/1000 } 269 } } 270 } } 271 if kWk[k] == BG_WK_RIDGE { if kYk[k] == BG_YK_WAVE_RIB { 272 let dy: i64 = ymil - kYlo[k] 273 let ph: i64 = (dy*kP0[k] - dy*dy/kP1[k] + w*kP2[k]) % 360 274 var aw: i64 = 0 275 if w > kW0[k] { if w < kW1[k] { aw = (w-kW0[k])*1000/(kW1[k]-kW0[k]) } } 276 if w >= kW1[k] { if w < kW2[k] { aw = (kW2[k]-w)*1000/(kW2[k]-kW1[k]) } } 277 if aw > 0 { s = s + amp*sinT[bg_wrap(ph)]/BG_Q14*aw/1000 } 278 } } 279 } 280 k = k + 1 281 } 282 return s 283} 284// ⚠⚠DEAD CODE -- NOTHING CALLS THIS. 2026-08-14: the shape-basis canon moved OUT of the emitter and into 285// knowledge/body_shape_basis.conf, and the live implementation is bg_shape_basis() above, which is table 286// driven. This copy is kept only because a sibling seat was mid-edit in it when the move landed (the 287// zyg_lo/zyg_span/szy_half hoists below are theirs, and they are hoists to NAMED LOCALS -- which satisfies 288// a magic-number ratchet while leaving the policy in code; the numbers are now DATA one function up). 289// ★IF YOU ARE EDITING THIS FUNCTION, YOUR CHANGE HAS NO EFFECT ON ANY EMITTED MESH. Edit the .conf. 290// Delete this whole function once the sibling's lane has landed; the mesh is bit-identical without it 291// (proven: sha f318ade9 both sides, 3,784,792 B). 292// bg_shape_basis_retired REMOVED 2026-08-14: 92 lines, ZERO call sites (grep coverage_complete=1, 293// corpus_complete=1 over 8,769 files -- the only match was its own definition). Its 8 anatomical 294// features now live as B rows in knowledge/body_shape_basis.conf, read by the live bg_shape_basis, 295// which derives every span from the data ((kW1[k]-kW0[k])) instead of carrying a typed copy. 296// ★A RETIRED FUNCTION IS A MEASUREMENT LIABILITY, NOT INERT WEIGHT. nx_magic scans SOURCE, so this 297// fossil's ~30 hardcoded per-mille constants kept being counted as live defects in this organ: the 298// headline "510 magic-number sites in nx_body_gen" was mostly THIS, and a reader diagnosing the organ 299// (2026-08-14) fixed the retired copy believing it was the shipping one. Deleting the body is what 300// makes the count honest -- a _retired SUFFIX IS NOT A GUARD, because a name is read only after you 301// have already decided what the file says. Only the call-site query is a control. 302func bg_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 } 303// ★CATMULL-ROM spline through control values p1..p2 (neighbours p0,p3), t in [0,1000]; returns value*1000. 304// THE Infinigen-gap fix: linear interpolation between sparse rings makes every part read as stacked cones with 305// a crease at each ring -- a cubic through the SAME rings gives C1-smooth organic curvature everywhere. 306// Integer-exact at the ends: t=0 -> p1*1000, t=1000 -> p2*1000. 307func bg_cmr(p0: i64, p1: i64, p2: i64, p3: i64, t: i64) -> i64 { 308 let a: i64 = 2*p1 309 let b: i64 = p2 - p0 310 let c: i64 = 2*p0 - 5*p1 + 4*p2 - p3 311 let d: i64 = 3*p1 - p0 + p3 - 3*p2 312 return (a*BG_PERMIL_CUBE + b*t*BG_PERMIL_SQ + c*t*t*1000 + d*t*t*t)/BG_CATMULL_DEN 313} 314// integer IEEE-754 float32 encoder (NishiLang has no float type -- we pack the bits ourselves) 315func bg_f32(v: i64, scale: i64) -> i64 { 316 if v == 0 { return 0 } 317 var neg: i64 = 0 318 var m: i64 = v 319 if m < 0 { neg = 1; m = 0-m } 320 var e: i64 = 0 321 var num: i64 = m 322 var den: i64 = scale 323 while num >= den*2 { den = den*2; e = e+1 } 324 while num < den { num = num*2; e = e-1 } 325 let frac: i64 = ((num - den)*BG_F32_MANT_SCALE)/den 326 var bits: i64 = ((e+127) << 23) | (frac & BG_F32_MANT_MASK) 327 if neg == 1 { bits = bits | (1<<31) } 328 return bits 329} 330func bg_wr32(b: *u8, o: i64, v: i64) -> i64 { 331 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 332 return 0 333} 334 335// ---- CANON PARSER: the emitter reads its geometry from DATA, it carries none. ---- 336// signed-integer token reader: skips leading non-digit/non-minus within [pos,end), reads, advances pos. 337func bg_rdint(b: *u8, pos: *i64, end: i64) -> i64 { 338 var i: i64 = pos[0] 339 var go: i64 = 1 340 while go == 1 { 341 if i >= end { go = 0 } else { 342 let c: i64 = b[i] as i64 343 if c == 45 { go = 0 } else { 344 if c >= 48 { if c <= 57 { go = 0 } else { i = i+1 } } else { i = i+1 } 345 } 346 } 347 } 348 var sg: i64 = 1 349 if i < end { if (b[i] as i64) == 45 { sg = 0-1; i = i+1 } } 350 var v: i64 = 0 351 var g2: i64 = 1 352 while g2 == 1 { 353 if i >= end { g2 = 0 } else { 354 let c: i64 = b[i] as i64 355 if c >= 48 { if c <= 57 { v = v*10 + (c-48); i = i+1 } else { g2 = 0 } } else { g2 = 0 } 356 } 357 } 358 pos[0] = i 359 return v*sg 360} 361// parse a canon buffer: P (part header) / R (control ring) / F (relief feature) lines; '#' = comment. 362// ---- ★★★OPENINGS: THE MECHANISM RELIEF STRUCTURALLY CANNOT PROVIDE (seq1387/seq908) ---- 363// An orbit and a nasal aperture are HOLES. Relief modulates a RADIUS, and a radius modulation can dent a 364// surface but can never remove it -- which is why the skull's six declared hollows moved the mesh and 365// moved nx_bodybench by NOTHING (headline 45, detail_head 14). An 'O' row declares a region where the 366// shell is simply ABSENT: the quad is not emitted, so the surface is genuinely gone and what lies behind 367// it is visible. Same geometry fields as an F row (part, y, y-halfwidth, theta, theta-halfwidth) minus 368// the amplitude, because there is no amount -- the surface is there or it is not. 369// ★PARSED SEPARATELY ON PURPOSE: bg_parse_canon already takes 24 arguments, and widening it further is 370// the wrong shape (that row wants a context array on its next touch, D001). This walks the same buffer 371// independently, so the existing signature and all its callers are untouched. 372// ★★ADDITIVE BY CONSTRUCTION: a canon carrying no O rows yields count 0 and every test below is inert, 373// so every existing canon must emit BIT-IDENTICAL output. That is the acceptance proof, not an opinion. 374func bg_parse_open(b: *u8, len: i64, oP: *i64, oY: *i64, oYw: *i64, oT: *i64, oTw: *i64, cnt: *i64) -> i64 { 375 let pos: *i64 = sys_mmap(16) as *i64 376 var n: i64 = 0 377 var i: i64 = 0 378 var bol: i64 = 1 379 while i < len { 380 if bol == 1 { 381 if (b[i] as i64) == 79 { 382 if n < BG_MAXFEAT { 383 pos[0] = i+1 384 oP[n] = bg_rdint(b, pos, len) 385 oY[n] = bg_rdint(b, pos, len) 386 oYw[n] = bg_rdint(b, pos, len) 387 oT[n] = bg_rdint(b, pos, len) 388 oTw[n] = bg_rdint(b, pos, len) 389 i = pos[0] 390 n = n + 1 391 } 392 } 393 } 394 if (b[i] as i64) == 10 { bol = 1 } else { bol = 0 } 395 i = i + 1 396 } 397 cnt[0] = n 398 return n 399} 400// is this (part, station, angle) inside a declared opening? bg_angsep handles the 0/360 wrap. 401func bg_in_open(part: i64, ymil: i64, thd: i64, oP: *i64, oY: *i64, oYw: *i64, oT: *i64, oTw: *i64, n: i64) -> i64 { 402 var k: i64 = 0 403 while k < n { 404 if oP[k] == part { 405 var dy: i64 = ymil - oY[k] 406 if dy < 0 { dy = 0 - dy } 407 if dy <= oYw[k] { 408 if bg_angsep(thd, oT[k]) <= oTw[k] { return 1 } 409 } 410 } 411 k = k + 1 412 } 413 return 0 414} 415 416func bg_parse_canon(b: *u8, len: i64, ry: *i64, rx0: *i64, rz0: *i64, ra: *i64, rb: *i64, 417 pStart: *i64, pCount: *i64, pMirror: *i64, pRot: *i64, pOx: *i64, pOy: *i64, pOz: *i64, 418 pMat: *i64, pRotZ: *i64, pRotY: *i64, fP: *i64, fY: *i64, fYw: *i64, fT: *i64, fTw: *i64, fA: *i64, counts: *i64) -> i64 { 419 var np: i64 = 0; var n: i64 = 0; var nf: i64 = 0 420 let pos: *i64 = sys_mmap(8) as *i64 421 var i: i64 = 0 422 while i < len { 423 var j: i64 = i 424 var sk: i64 = 1 425 while sk == 1 { 426 if j >= len { sk = 0 } else { 427 let c: i64 = b[j] as i64 428 if c == 32 { j = j+1 } else { if c == 9 { j = j+1 } else { sk = 0 } } 429 } 430 } 431 var tag: i64 = 0 432 if j < len { tag = b[j] as i64 } 433 var e: i64 = i 434 var fe: i64 = 1 435 while fe == 1 { if e >= len { fe = 0 } else { if (b[e] as i64) == 10 { fe = 0 } else { e = e+1 } } } 436 if tag == 80 { // 'P' -- part header 437 // GUARD: refuse LOUD rather than write past the part arrays. The canon is EXTERNAL input, so 438 // this is a boundary; an unguarded write here silently overflowed for every canon past the cap. 439 if np >= BG_MAXPART { 440 bg_hw("CANON-REFUSED part count exceeds BG_MAXPART\n" as *u8) 441 sys_exit(4) 442 } 443 if np > 0 { pCount[np-1] = n - pStart[np-1] } 444 pos[0] = j+1 445 pMirror[np] = bg_rdint(b,pos,e); pRot[np] = bg_rdint(b,pos,e) 446 pOx[np] = bg_rdint(b,pos,e); pOy[np] = bg_rdint(b,pos,e); pOz[np] = bg_rdint(b,pos,e) 447 pMat[np] = bg_rdint(b,pos,e) // optional 6th field: material (0=flesh, 1=eye) 448 // ★optional 7th field: ROTATION ABOUT Z, degrees (F1084 prereq 3). The mechanism gap the face 449 // work hit head-on: a brow ridge and a lip run HORIZONTALLY, but a part could only stack rings 450 // along Y and rotate about X, so every horizontal feature came out a vertical blob. rotZ turns 451 // the canonical Y-tube sideways IN the face plane; absent field parses as 0 = exact identity, 452 // so every existing canon is bit-identical (proven by golden md5 on the standing canon). 453 pRotZ[np] = bg_rdint(b,pos,e) 454 // ★optional 8th field: ROTATION ABOUT Y = AZIMUTH, degrees (S1). rotZ turns a part sideways in 455 // its own plane and rotX pitches it; NEITHER can answer "which compass direction does this limb 456 // point". That is the whole reason a wing, a branching antler and a spine-following ribcage were 457 // inexpressible: parts could only fan within one plane. Applied LAST of the three, so it is a 458 // true world azimuth rather than a spin of the cross-section. ZXY is a complete Euler 459 // parameterisation, so any orientation is now reachable. Absent field parses 0 -> pyc=Q14, 460 // pys=0, and x*Q14/Q14 is integer-exact => every existing canon stays BIT-IDENTICAL. 461 pRotY[np] = bg_rdint(b,pos,e) 462 pStart[np] = n; np = np+1 463 } 464 if tag == 82 { // 'R' -- control ring 465 // ★★THE GUARD THAT WAS MISSING FOR AS LONG AS THE PART GUARD HAS EXISTED. The 'P' branch above 466 // was hardened after an unguarded write silently overflowed -- and the fix was applied to ONE of 467 // the three branches. R and F kept writing past their arrays. ★A HARDENING APPLIED TO ONE 468 // BRANCH OF THREE IS NOT A HARDENING; it is a note saying somebody once knew about the problem. 469 // ⚠WHY IT NEVER SHOWED: the ring pool is GLOBAL across parts (256 entries for the whole body) 470 // and the canon has used 110 of them, so the overflow was always one anatomy rung away rather 471 // than absent. A 32-tooth arch at 4 rings each is 128 rings ON ITS OWN. mmap rounds to a page, 472 // so the first few hundred over-writes would have "worked" -- a wrong body with a clean exit. 473 if n >= BG_MAXRING { 474 bg_hw("CANON-REFUSED ring count exceeds BG_MAXRING\n" as *u8) 475 sys_exit(5) 476 } 477 pos[0] = j+1 478 ry[n] = bg_rdint(b,pos,e); rx0[n] = bg_rdint(b,pos,e); rz0[n] = bg_rdint(b,pos,e) 479 ra[n] = bg_rdint(b,pos,e); rb[n] = bg_rdint(b,pos,e); n = n+1 480 } 481 if tag == 70 { // 'F' -- relief feature 482 // the F pool was a bare unnamed sys_mmap(128*8) with no constant and no check -- it now has both 483 if nf >= BG_MAXFEAT { 484 bg_hw("CANON-REFUSED feature count exceeds BG_MAXFEAT\n" as *u8) 485 sys_exit(6) 486 } 487 pos[0] = j+1 488 fP[nf] = bg_rdint(b,pos,e); fY[nf] = bg_rdint(b,pos,e); fYw[nf] = bg_rdint(b,pos,e) 489 fT[nf] = bg_rdint(b,pos,e); fTw[nf] = bg_rdint(b,pos,e); fA[nf] = bg_rdint(b,pos,e); nf = nf+1 490 } 491 i = e+1 492 } 493 if np > 0 { pCount[np-1] = n - pStart[np-1] } 494 counts[0] = np; counts[1] = n; counts[2] = nf 495 return 0 496} 497 498// parse the measured-profile file: 'S <part> <ymil> <ra> <rb> <24 ratios>' rows, ';' comments. 499// ★DECLARED BIN COUNT, READ BEFORE ALLOCATION (2026-08-23) so the PRIOR'S OWN RESOLUTION sizes the 500// array instead of a compile-time ceiling deciding it. Mirrors bg_parse_prof's own line walk so the 501// two can never disagree about what an 'N' row is. Returns 0 when the file declares none. 502func bg_scan_nb(b: *u8, len: i64) -> i64 { 503 let pos: *i64 = sys_mmap(8) as *i64 504 var i: i64 = 0 505 while i < len { 506 var j: i64 = i 507 var sk: i64 = 1 508 while sk == 1 { 509 if j >= len { sk = 0 } else { 510 let c: i64 = b[j] as i64 511 if c == 32 { j = j+1 } else { if c == 9 { j = j+1 } else { sk = 0 } } 512 } 513 } 514 var e: i64 = i 515 var fe: i64 = 1 516 while fe == 1 { if e >= len { fe = 0 } else { if (b[e] as i64) == 10 { fe = 0 } else { e = e+1 } } } 517 if j < len { if (b[j] as i64) == 78 { pos[0] = j+1; return bg_rdint(b,pos,e) } } 518 i = e+1 519 } 520 return 0 521} 522 523func bg_parse_prof(b: *u8, len: i64, sP: *i64, sY: *i64, sR: *i64, nbo: *i64, stride: i64) -> i64 { 524 var ns: i64 = 0 525 var nb: i64 = 0 526 let pos: *i64 = sys_mmap(8) as *i64 527 var i: i64 = 0 528 while i < len { 529 var j: i64 = i 530 var sk: i64 = 1 531 while sk == 1 { 532 if j >= len { sk = 0 } else { 533 let c: i64 = b[j] as i64 534 if c == 32 { j = j+1 } else { if c == 9 { j = j+1 } else { sk = 0 } } 535 } 536 } 537 var tag: i64 = 0 538 if j < len { tag = b[j] as i64 } 539 var e: i64 = i 540 var fe: i64 = 1 541 while fe == 1 { if e >= len { fe = 0 } else { if (b[e] as i64) == 10 { fe = 0 } else { e = e+1 } } } 542 if tag == 78 { // 'N' -- bins per section 543 pos[0] = j+1 544 nb = bg_rdint(b,pos,e) 545 // ⚠THIS USED TO READ `if nb > BG_NBMAX { nb = 0 }` -- a prior declaring MORE bins than the 546 // compile-time ceiling was SILENTLY DISCARDED WHOLE: nb=0 means every S row is skipped, so 547 // the body emitted with NO measured prior at all and said nothing. A cap that deletes the 548 // data it cannot hold, without a word, is the worst shape a cap can take. The stride is now 549 // DERIVED from this same file's declared N, so for a well-formed prior this is unreachable; 550 // it remains as a fail-loud guard and RECORDS the offered count so the caller can announce. 551 if nb > stride { nbo[2] = nb; nb = 0 } 552 } 553 if tag == 83 { // 'S' 554 // ★COUNT WHAT WAS OFFERED, NOT ONLY WHAT FIT. nbo[1] is the row count PRESENT in the file; the 555 // caller compares it to the accepted count so a cap can never pass itself off as the data. 556 if nb > 0 { nbo[1] = nbo[1] + 1 } 557 if ns < BG_MAXPROF { if nb > 0 { 558 pos[0] = j+1 559 sP[ns] = bg_rdint(b,pos,e); sY[ns] = bg_rdint(b,pos,e) 560 bg_rdint(b,pos,e); bg_rdint(b,pos,e) // measured ra/rb are diagnostics; SIZE stays procedural 561 var k: i64 = 0 562 while k < nb { sR[ns*stride+k] = bg_rdint(b,pos,e); k = k+1 } 563 ns = ns+1 564 }} 565 } 566 i = e+1 567 } 568 nbo[0] = nb 569 return ns 570} 571// look up the shape prior for (part, height, parameter angle): linear in height between the two bracketing 572// measured stations, linear in angle between the two bracketing bins, clamped. Parts with no measured rows 573// (the foot, which runs forward and is measured in its own frame) return 1000 = unchanged. 574// ★HIGH-FREQUENCY RESIDUAL (hf != 0). Four measured attempts agreed that transferring the reference's 575// LOW-frequency shape improves silhouettes and WORSENS surface busyness -- and busyness is the binding 576// judge. The information the detail judge wants is the part of the real section that a smooth shape does 577// NOT explain: the residual against the profile's own smoothed self. Same measured data, opposite band. 578// ★stride is nb, not a ceiling: the array is exactly as wide as the prior declared itself to be. 579func bg_prof_at(sR: *i64, row: i64, b: i64, nb: i64) -> i64 { return sR[row*nb + ((b%nb)+nb)%nb] } 580func bg_prof_smooth(sR: *i64, row: i64, b: i64, nb: i64) -> i64 { 581 var acc: i64 = 0 582 var k: i64 = 0-3 583 while k <= 3 { acc = acc + bg_prof_at(sR,row,b+k,nb); k = k+1 } 584 return acc/7 585} 586func bg_prof(part: i64, ymil: i64, dq: i64, sP: *i64, sY: *i64, sR: *i64, ns: i64, nb: i64, hf: i64) -> i64 { 587 var lo: i64 = 0-1 588 var hi: i64 = 0-1 589 var k: i64 = 0 590 // ★★★PART-EDGE FEATHER (seq1466). The prior is looked up PER PART, so two parts that MEET can be 591 // perturbed to DIFFERENT radii at their shared boundary and stop meeting -- the skin then shows a hard 592 // step. RENDERED AND CONFIRMED: at PROF=1000 layer 0 alone (not a layer-superposition artifact -- that 593 // hypothesis was tested and refuted) carries a collar at the neck, a waist band, a knee seam and a 594 // capped head, and PROF=0 is clean, so the prior introduces them. 595 // ★The canon's parts are built to meet EXACTLY; the prior only has to stop pulling them apart. Fading 596 // its influence to zero at each part's own data extremes preserves the join by construction while 597 // leaving the part's INTERIOR fully modulated, which is where the detail lives. 598 let edge: i64 = BG_PROFEDGE 599 var pmin: i64 = BG_PERMIL_SQ 600 var pmax: i64 = 0 - BG_PERMIL_SQ 601 while k < ns { 602 if sP[k] == part { 603 if sY[k] < pmin { pmin = sY[k] } 604 if sY[k] > pmax { pmax = sY[k] } 605 if sY[k] <= ymil { if lo < 0 { lo = k } else { if sY[k] > sY[lo] { lo = k } } } 606 if sY[k] >= ymil { if hi < 0 { hi = k } else { if sY[k] < sY[hi] { hi = k } } } 607 } 608 k = k+1 609 } 610 if lo < 0 { if hi < 0 { return 1000 } lo = hi } 611 if hi < 0 { hi = lo } 612 let a: i64 = bg_wrap(dq) 613 let fb: i64 = a*nb*1000/360 614 let b0: i64 = (fb/1000) % nb 615 let b1: i64 = (b0+1) % nb 616 let bm: i64 = (b0+nb-1) % nb 617 let b2: i64 = (b0+2) % nb 618 let ft: i64 = fb % 1000 619 // ★CIRCULAR CATMULL-ROM around the ring, not linear between bins. Linear interpolation of the prior puts 620 // a CREASE at every one of the 24 bin boundaries -- a normal discontinuity every ~2 vertices at radial 44 621 // -- and the detail judge punishes busy-where-the-oracle-is-smooth exactly as it punishes smooth-where- 622 // detailed, so a C0 prior scored WORSE than no prior at all (measured: detail 360 -> 333). A cubic through 623 // the same measured bins is C1 by construction. Same fix that removed the axial banding in the lofting. 624 let v0: i64 = bg_cmr(sR[lo*nb+bm], sR[lo*nb+b0], sR[lo*nb+b1], sR[lo*nb+b2], ft)/1000 625 let v1: i64 = bg_cmr(sR[hi*nb+bm], sR[hi*nb+b0], sR[hi*nb+b1], sR[hi*nb+b2], ft)/1000 626 var w: i64 = 0 627 if sY[hi] != sY[lo] { w = (ymil - sY[lo])*1000/(sY[hi]-sY[lo]) } 628 if w < 0 { w = 0 } 629 if w > 1000 { w = 1000 } 630 // ★★★THE RINGING, FIXED WITH THE FIX ALREADY IN THIS FUNCTION -- ONE AXIS OVER. 631 // The comment above records killing a CREASE AT EVERY BIN BOUNDARY in the THETA direction by replacing 632 // linear interpolation with a circular Catmull-Rom, because a C0 prior scored WORSE than no prior at 633 // all (360 -> 333). ***THE AXIAL BLEND WAS STILL LINEAR***, so the prior was C1 AROUND the body and C0 634 // ALONG it: the tangent jumps at EVERY one of the 156 profile rows, and at strong PROF those creases 635 // read as hard horizontal bands. RENDERED AND CONFIRMED at PROF=1000: a pronounced collar at the neck 636 // plus seams at waist and knees -- which is exactly the 'it rings the neck' the default-off rationale 637 // cited, and it was never a property of the DATA, only of how the data was blended. 638 // ★SMOOTHSTEP makes dw/dy vanish at w=0 and w=1, so both sides of every row meet with equal tangent = 639 // C1 across the row. Exact at the ends by construction: w=0 -> 0, w=1000 -> 1000, w=500 -> 500. 640 w = w*w*(BG_SMOOTHSTEP_3_PERMIL - 2*w)/BG_PERMIL_SQ 641 var v: i64 = (v0*(1000-w) + v1*w)/1000 642 if hf != 0 { 643 // subtract the profile's own smoothed shape -> keep ONLY what the smooth shape cannot explain 644 let s0: i64 = bg_prof_smooth(sR, lo, b0, nb) 645 let s1: i64 = bg_prof_smooth(sR, hi, b0, nb) 646 let sv: i64 = (s0*(1000-w) + s1*w)/1000 647 // hf is a FLAG here, not a scale -- the caller applies the scale. Using it as a per-mille multiplier 648 // made (v-sv)*1/1000 truncate to exactly ZERO, so the whole band was a silent no-op that still built 649 // and still benched identically. Match the units of every factor before believing a null result. 650 v = 1000 + (v - sv) 651 } 652 // ★APPLY THE EDGE FEATHER: within `edge` of this part's own data extremes, ramp the modulation back 653 // toward neutral so the part meets its neighbour exactly as the canon built it. Linear in distance is 654 // enough here because the endpoints are what must agree; the axial blend is already C1 (smoothstep). 655 if pmax > pmin { 656 var d: i64 = ymil - pmin 657 let d2: i64 = pmax - ymil 658 if d2 < d { d = d2 } 659 if d < 0 { d = 0 } 660 // ⚠FEATHER REMOVED FROM THE PATH 2026-07-30 (my first disable was WRONG and the bench caught it: 661 // writing (v-1000)*0/edge forces v=1000 near the extremes, which is the MAXIMUM feather, not none -- 662 // it read headline 332, worse than both the feather 340 and the clean 354. Match the units of every 663 // factor before believing a null result -- this file says exactly that 40 lines down about hf.) 664 // ⚠DISABLED 2026-07-30: feather LOST ON BOTH INSTRUMENTS -- headline 354->340, detail_head 309->288 at PROF=1000, AND the render was visually UNCHANGED (same collar, waist band, knee seam). A change that loses the number AND shows nothing to the eye has no case. Kept as a zeroed term rather than deleted so the next diagnosis can see what was tried. THREE hypotheses now refuted BY LOOKING: axial C0 creasing (smoothstep helped numbers, changed nothing visible), layer superposition (layer 0 ALONE is identical), and this. STOP GUESSING: dump radius-vs-y at fixed theta for PROF 0 vs 1000, diff, and read off the exact stations where the step is. 665 } 666 if v > 1000+BG_PROFCLAMP { v = 1000+BG_PROFCLAMP } 667 if v < 1000-BG_PROFCLAMP { v = 1000-BG_PROFCLAMP } 668 return v 669} 670 671func bg_generate_cut(argc: i64, argv: *i64, join:*BgCutJoin) -> i64 { 672 let outp: *u8 = argv[1] as *u8 673 var H: i64 = BG_DEFAULT_STATURE_MM 674 if argc > 2 { H = bg_satoi(argv[2] as *u8) } 675 var RS: i64 = 28 676 if argc > 3 { RS = bg_satoi(argv[3] as *u8) } 677 var SUB: i64 = 4 678 if argc > 4 { SUB = bg_satoi(argv[4] as *u8) } 679 // relief amplitude scale, per-mille -- makes the anatomical surface relief a CONTROLLED VARIABLE 680 // measurable against the oracle, rather than a change assumed to help (1000 = as authored) 681 var RLF: i64 = 1000 682 if argc > 5 { RLF = bg_satoi(argv[5] as *u8) } 683 // ★SUBCUTANEOUS FAT (argv[7], per-mille): the layer between muscle and skin. It attenuates how much of 684 // the muscle definition reaches the SKIN surface -- lean (fat 0) shows the abs, heavy (fat high) smooths 685 // them -- so "abs hidden under fat" is EMERGENT from the layer stack, not a hack. Default lean-ish. 686 var FAT: i64 = 250 687 if argc > 7 { FAT = bg_satoi(argv[7] as *u8) } 688 // ★PROF (argv[8], per-mille): how much of the MEASURED section prior to apply. A CONTROLLED VARIABLE -- 689 // 0 reproduces the pre-GX-31 elliptical body exactly, so the A/B is the same code path and any change in 690 // the bench is attributable to the profiles alone (the relief-scale knob earned this pattern in GX-24). 691 // ★DEFAULT OFF, and it stays off until the prior earns it: MEASURED, applying the section prior LOWERS 692 // the honest headline (361 -> 335) because it transfers low-frequency form while flattening the back, 693 // and it rings the neck. Shipping it on by default would have been a silent regression for every other 694 // caller. It stays available as an opt-in so the next rung can build on the measurement, not repeat it. 695 // ★★★DEFAULT 0 -> 250, EARNED 2026-07-30 BY BOTH INSTRUMENTS (seq1441/1448/1451). The rationale above 696 // was correct FOR THE BODY IT WAS WRITTEN AGAINST -- but that body measured 361 baseline and this one 697 // measures 319, so the emitter moved underneath the decision. Re-swept vs the cadaver oracle 698 // knowledge/skin.nxmesh (self_iou=self_shape=self_detail=1000 every run): 699 // PROF 0 -> headline 319 detail_head 155 shape 934 700 // PROF 250 -> headline 360 detail_head 164 shape 934 <-- INTERIOR OPTIMUM, +41, NO shape cost 701 // PROF 500 -> headline 343 detail_head 196 shape 934 702 // PROF 750 -> headline 338 detail_head 242 shape 923 703 // PROF 1000 -> headline 347 detail_head 289 shape 923 704 // ★★AND THE NECK RINGING IS REAL -- I RENDERED IT AND LOOKED, which is the half a bench cannot do. 705 // At PROF=1000 the body shows a pronounced COLLAR AT THE NECK plus hard seams at waist and knees and 706 // a bulbous seamed head: the original author's word 'rings' is exactly right, and their refusal to 707 // ship it on was CORRECT. At PROF=250 those artifacts are ABSENT and the render is INDISTINGUISHABLE 708 // from PROF=0 by eye, while scoring +41. ★So 250 is number-up AND eye-neutral; 1000 was number-up and 709 // eye-DOWN, which is the Goodhart this lane refuses. A rising number is not permission to ship -- a 710 // rising number WITH AN UNCHANGED EYE is. 711 // ⚠The ringing at high prior is an unfixed defect, not a reason to avoid the prior: fixing it is what 712 // would let the strong end of this curve (detail_head 289, +134) be taken. That is the next rung. 713 var PROF: i64 = 250 714 if argc > 8 { PROF = bg_satoi(argv[8] as *u8) } 715 // ★HF (argv[10], per-mille): apply only the measured HIGH-FREQUENCY residual. Independent of PROF so the 716 // two bands are never confounded -- the whole point of the four measurements that led here. 717 var HF: i64 = 0 718 if argc > 10 { HF = bg_satoi(argv[10] as *u8) } 719 // BAS (argv[11], per-mille): analytic shape-basis weight -- a controlled variable like every knob here 720 var BAS: i64 = 0 721 if argc > 11 { BAS = bg_satoi(argv[11] as *u8) } 722 // ★DEC (argv[12], per-mille): measured-prior vs authored-relief DECONFLICTION. Where a part carries 723 // measured prof rows, attenuate the AUTHORED relief by DEC so the two stop double-modulating the same 724 // radius (triangulated 2026-08-10: head-scale A/Bs 226/249/166 -- each prior helped exactly where its 725 // data was faithful and fought the hand relief elsewhere). DEC=0 -> byte-identical output. 726 var DEC: i64 = 0 727 if argc > 12 { DEC = bg_satoi(argv[12] as *u8) } 728 let sinT: *i64 = sys_mmap(400*8) as *i64 729 bg_sin_fill(sinT) 730 731 // ---- CONTROL RINGS, per-mille of stature: y, xoff, zoff, rx, rz ---- 732 let ry: *i64 = sys_mmap(BG_MAXRING*8) as *i64 733 let rx0: *i64 = sys_mmap(BG_MAXRING*8) as *i64 734 let rz0: *i64 = sys_mmap(BG_MAXRING*8) as *i64 735 let ra: *i64 = sys_mmap(BG_MAXRING*8) as *i64 736 let rb: *i64 = sys_mmap(BG_MAXRING*8) as *i64 737 // three consecutive station rings (slot 1 = the station being normalled) -> TRUE surface normals 738 // by CENTRAL difference in both surface parameters 739 let sx: *i64 = sys_mmap(3*BG_MAXRS*8) as *i64 740 let sz: *i64 = sys_mmap(3*BG_MAXRS*8) as *i64 741 let sy: *i64 = sys_mmap(4*8) as *i64 742 let scx: *i64 = sys_mmap(4*8) as *i64 743 let scz: *i64 = sys_mmap(4*8) as *i64 744 // vertex rings: c = current station, pp = previous station (quads are emitted between them) 745 let cpx: *i64 = sys_mmap(BG_MAXRS*8) as *i64 746 let cpy: *i64 = sys_mmap(BG_MAXRS*8) as *i64 747 let cpz: *i64 = sys_mmap(BG_MAXRS*8) as *i64 748 let ppy: *i64 = sys_mmap(BG_MAXRS*8) as *i64 749 let cnx: *i64 = sys_mmap(BG_MAXRS*8) as *i64 750 let cny: *i64 = sys_mmap(BG_MAXRS*8) as *i64 751 let cnz: *i64 = sys_mmap(BG_MAXRS*8) as *i64 752 let ppx: *i64 = sys_mmap(BG_MAXRS*8) as *i64 753 let ppz: *i64 = sys_mmap(BG_MAXRS*8) as *i64 754 let pnx: *i64 = sys_mmap(BG_MAXRS*8) as *i64 755 let pny: *i64 = sys_mmap(BG_MAXRS*8) as *i64 756 let pnz: *i64 = sys_mmap(BG_MAXRS*8) as *i64 757 let pStart: *i64 = sys_mmap(BG_MAXPART*8) as *i64 758 let pCount: *i64 = sys_mmap(BG_MAXPART*8) as *i64 759 let pMirror: *i64 = sys_mmap(BG_MAXPART*8) as *i64 760 // ★PART PLACEMENT. Every part is authored in its own canonical frame as a tube stacked along +Y, then 761 // ROTATED about X and offset into place. Without this a part can only run vertically, which is why the 762 // foot had to be faked as two forward-stretched rings on the end of the leg tube (it read as a blunt 763 // cone, and the error map put the feet among the worst regions). A real foot runs FORWARD, so it is a 764 // Y-tube rotated 90 degrees. Same mechanism gives fingers and toes. Existing parts use rot=0, off=0 765 // and are bit-identical to before. 766 let pRot: *i64 = sys_mmap(BG_MAXPART*8) as *i64 767 let pOx: *i64 = sys_mmap(BG_MAXPART*8) as *i64 768 let pOy: *i64 = sys_mmap(BG_MAXPART*8) as *i64 769 let pOz: *i64 = sys_mmap(BG_MAXPART*8) as *i64 770 let pMat: *i64 = sys_mmap(BG_MAXPART*8) as *i64 771 let pRotZ: *i64 = sys_mmap(BG_MAXPART*8) as *i64 772 let pRotY: *i64 = sys_mmap(BG_MAXPART*8) as *i64 773 let tf3: *i64 = sys_mmap(64) as *i64 774 var pz: i64 = 0 775 while pz < BG_MAXPART { pRot[pz]=0; pOx[pz]=0; pOy[pz]=0; pOz[pz]=0; pMat[pz]=0; pRotZ[pz]=0; pRotY[pz]=0; pz=pz+1 } 776 var n: i64 = 0 777 var np: i64 = 0 778 779 // ---- LOAD THE CANON (DATA-DRIVEN: the emitter carries NO geometry literals; the anthropometric canon -- 780 // every ring and every relief feature -- lives in a data file. A different archetype (male, female, 781 // anime, monster) is a different canon fed to the SAME emit algorithm, and the canon itself is produced 782 // by a PROCEDURAL generator, not typed by hand.) ---- 783 let fP: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 784 let fY: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 785 let fYw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 786 let fT: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 787 let fTw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 788 let fA: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 789 var canonp: *u8 = "knowledge/canon_male.dat" as *u8 790 if argc > 6 { canonp = argv[6] as *u8 } 791 let clen: *i64 = sys_mmap(16) as *i64 792 let cbuf: *u8 = sys_read_file(canonp, clen) 793 // ★FIXED 2026-07-30: this literal had UNESCAPED QUOTES -- bg_hw("{"error":...") -- so nx_cc read 794 // `error` as a bare identifier and REFUSED the whole module. nx_body_gen, the emitter behind every 795 // body AND the skull mesh, was NOT BUILDABLE FROM ITS OWN SOURCE; the live binary predates the line. 796 // The file already had the right idiom 350 lines further down (\x22), it just was not used here. 797 if (cbuf as i64) == 0 { bg_hw("{\x22error\x22:\x22cannot read canon file\x22}\n" as *u8); return 3 } 798 let counts: *i64 = sys_mmap(32) as *i64 799 bg_parse_canon(cbuf, clen[0], ry,rx0,rz0,ra,rb, pStart,pCount,pMirror,pRot,pOx,pOy,pOz, pMat, pRotZ, pRotY, fP,fY,fYw,fT,fTw,fA, counts) 800 // ★OPENINGS (seq1387): parsed from the SAME buffer by its own walker, so bg_parse_canon's 24-arg 801 // signature and every caller of it stay untouched. Zero O rows => nopen[0]=0 => provably inert. 802 let oP: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 803 let oY: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 804 let oYw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 805 let oT: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 806 let oTw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 807 let nopen: *i64 = sys_mmap(16) as *i64 808 nopen[0] = 0 809 bg_parse_open(cbuf, clen[0], oP, oY, oYw, oT, oTw, nopen) 810 // ★SHAPE-BASIS CANON, loaded as DATA. Applies to every canon exactly as the hardcoded table did, so it 811 // is one file rather than a copy per canon. REFUSES if absent: a body emitted with no basis rows would 812 // be silently smooth-torsoed and browless, which reads as a rendering fault rather than a missing file. 813 let kP: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 814 let kYlo: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 815 let kYhi: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 816 let kTh: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 817 let kWk: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 818 let kW0: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 819 let kW1: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 820 let kW2: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 821 let kAmp: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 822 let kYk: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 823 let kP0: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 824 let kP1: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 825 let kP2: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64 826 let nbasis: *i64 = sys_mmap(16) as *i64 827 nbasis[0] = 0 828 let blen: *i64 = sys_mmap(16) as *i64 829 let bbuf: *u8 = sys_read_file("knowledge/body_shape_basis.conf" as *u8, blen) 830 if (bbuf as i64) == 0 { bg_hw("{\x22error\x22:\x22cannot read knowledge/body_shape_basis.conf -- the shape-basis canon is REQUIRED; refusing to emit a featureless body\x22}\n" as *u8); return 7 } 831 bg_parse_basis(bbuf, blen[0], kP,kYlo,kYhi,kTh,kWk,kW0,kW1,kW2,kAmp,kYk,kP0,kP1,kP2, nbasis) 832 if nbasis[0] < 1 { bg_hw("{\x22error\x22:\x22body_shape_basis.conf declares no B rows -- refusing to emit a featureless body\x22}\n" as *u8); return 7 } 833 // ★LAYER PALETTE, read from L rows in the SAME buffer -- one file, one read, no second config path. 834 // REFUSES on an incomplete palette for the same reason the B rows do: a body emitted with an unpainted 835 // layer looks like a renderer fault, not a missing row, and would be debugged in the wrong organ. 836 let lyR: *i64 = sys_mmap(BG_NLAYER*8) as *i64 837 let lyG: *i64 = sys_mmap(BG_NLAYER*8) as *i64 838 let lyB: *i64 = sys_mmap(BG_NLAYER*8) as *i64 839 let nlay: *i64 = sys_mmap(16) as *i64 840 nlay[0] = 0 841 bg_parse_layers(bbuf, blen[0], lyR, lyG, lyB, nlay) 842 if nlay[0] < BG_NLAYER { bg_hw("{\x22error\x22:\x22body_shape_basis.conf must declare one L row per layer -- refusing to emit a body with an unpainted layer\x22}\n" as *u8); return 7 } 843 np = counts[0]; n = counts[1]; var nf: i64 = counts[2] 844 845 // ---- MEASURED SECTION PROFILES (optional: absent file -> ns 0 -> every lookup returns 1000) ---- 846 let sP: *i64 = sys_mmap(BG_MAXPROF*8) as *i64 847 let sY: *i64 = sys_mmap(BG_MAXPROF*8) as *i64 848 let nbp: *i64 = sys_mmap(32) as *i64 849 nbp[0] = 0 850 nbp[2] = 0 851 var ns: i64 = 0 852 var profp: *u8 = "knowledge/profile_human.dat" as *u8 853 if argc > 9 { profp = argv[9] as *u8 } 854 let plen: *i64 = sys_mmap(16) as *i64 855 let pbuf: *u8 = sys_read_file(profp, plen) 856 // ★★★STRIDE DERIVED FROM THE PRIOR'S OWN DECLARED N (2026-08-23) -- the ceiling is REMOVED, not 857 // raised. BG_NBMAX=96 was a compile-time bound on how finely the measured prior could be carried, 858 // and it was the binding cap the moment PF_NB=48 was lifted in the producer the same day. Worse, 859 // a file declaring MORE bins was silently discarded whole (see bg_parse_prof). Sizing the array 860 // from the file's own N means the consumer can carry ANY resolution the producer can measure, and 861 // there is no number here for anyone to pick. BG_MAXPROF still bounds the ROW count (a different 862 // axis, still a cap, named for the census). 863 var nbdecl: i64 = 0 864 if (pbuf as i64) != 0 { nbdecl = bg_scan_nb(pbuf, plen[0]) } 865 var stride: i64 = nbdecl 866 if stride < 1 { stride = 1 } 867 let sR: *i64 = sys_mmap(BG_MAXPROF*stride*8) as *i64 868 if (pbuf as i64) != 0 { ns = bg_parse_prof(pbuf, plen[0], sP, sY, sR, nbp, stride) } 869 let nbins: i64 = nbp[0] 870 if nbins < 1 { ns = 0 } 871 // per-part prof coverage: which parts the measured prior actually speaks for (drives DEC) 872 let pcov: *i64 = sys_mmap(BG_MAXPART*8) as *i64 873 var pcf: i64 = 0 874 while pcf < ns { if sP[pcf] >= 0 { if sP[pcf] < BG_MAXPART { pcov[sP[pcf]] = 1 } } pcf = pcf + 1 } 875 876 // ---- emit ---- 877 let join_rc:i64=bjc_init(join,RS,np,pCount,pMirror,SUB) 878 if join_rc<0{return bg_join_refuse(join,"input" as *u8,join_rc)} 879 let tri: *i64 = sys_mmap(BG_MAXTRI*21*8) as *i64 880 // ★per-triangle MATERIAL colour (0-1000 each). Lets a feature (eyes) carry its OWN colour instead of the 881 // flat per-layer flesh -- the reason eyes were invisible skin-bumps. Written per tri at emit, read at write. 882 let tcR: *i64 = sys_mmap(BG_MAXTRI*8) as *i64 883 let tcG: *i64 = sys_mmap(BG_MAXTRI*8) as *i64 884 let tcB: *i64 = sys_mmap(BG_MAXTRI*8) as *i64 885 var nt: i64 = 0 886 // demand counter: every tri the geometry WANTED to emit, counted whether or not the cap admitted it. 887 // ntoff > nt is the truncation proof; mirrors the prof_rows_offered pattern below. 888 var ntoff: i64 = 0 889 let layCnt: *i64 = sys_mmap(BG_NLAYER*8) as *i64 890 891 // ★LAYER LOOP -- emit the body once per anatomical layer (bone, muscle, skin), nested inside each other. 892 var Lyr: i64 = 0 893 while Lyr < BG_NLAYER { 894 let lstart: i64 = nt 895 // per-layer radius scale + how much of the muscle relief this layer carries 896 // ★SKIN IS LAYER 0 so the renderer's skin shading (wrap-diffuse SSS + sheen, keyed on layer 0) lands 897 // on it; muscle=1, bone=2 sit inside. Intact z-buffers to the outermost (skin) regardless of order. 898 var rmul: i64 = 1000; var relmul: i64 = 1000 899 // ★COLOUR IS INDEXED, NOT LADDERED -- a new layer needs an L row in the canon, not a new branch 900 // here. rmul and relmul stay as branches deliberately: they are SHAPE, not palette (already-named 901 // consts plus a fat-derived value), and folding them into the same table would put two unrelated 902 // kinds of thing in one row. 903 var lcR: i64 = lyR[Lyr]; var lcG: i64 = lyG[Lyr]; var lcB: i64 = lyB[Lyr] 904 if Lyr == 0 { rmul = 1000; relmul = 1000 - FAT } // SKIN: relief attenuated by fat 905 if Lyr == 1 { rmul = BG_MUSCF; relmul = 1000 } // MUSCLE 906 if Lyr == 2 { rmul = BG_BONEF; relmul = 0 } // BONE 907 if relmul < 0 { relmul = 0 } 908 var p: i64 = 0 909 while p < np { 910 var side: i64 = 0 911 var nsides: i64 = 1 912 if pMirror[p] == 1 { nsides = 2 } 913 while side < nsides { 914 var sgn: i64 = 1 915 if side == 1 { sgn = 0-1 } 916 // ★ONE GLOBAL STATION INDEX. Normals are CENTRAL differences in BOTH surface parameters -- 917 // across stations as well as around the ring -- so they are shared by the quads either side 918 // and vary continuously. The previous one-sided within-band difference made the vertical 919 // tilt CONSTANT per band and step at every boundary: true normals, but visible bandaging. 920 let maxst: i64 = (pCount[p]-1)*SUB 921 let prc: i64 = sinT[bg_wrap(pRot[p]+90)] 922 let prs: i64 = sinT[bg_wrap(pRot[p])] 923 // ★Z-rotation (F1084): applied BEFORE the X-rotation at every transform site. The MIRRORED side 924 // gets the NEGATED angle so a left feature is the true mirror of the right one (a brow that 925 // tilts up-and-out on the right must tilt up-and-out on the left too). rotZ=0 gives pzc=Q14, 926 // pzs=0, and x*Q14/Q14 is integer-exact, so unrotated parts are BIT-IDENTICAL by construction. 927 let pzc: i64 = sinT[bg_wrap(sgn*pRotZ[p]+90)] 928 let pzs: i64 = sinT[bg_wrap(sgn*pRotZ[p])] 929 // ★AZIMUTH IS NEGATED ON THE MIRRORED SIDE, exactly as rotZ is -- mirroring flips X, so a limb 930 // swept BACK on the right must sweep BACK on the left, not forward. rotX is deliberately NOT 931 // negated: a forward pitch stays forward on both sides. Getting this wrong yields a body whose 932 // two halves rotate opposite ways, which reads as a twist rather than as a mirror. 933 let pyc: i64 = sinT[bg_wrap(sgn*pRotY[p]+90)] 934 let pys: i64 = sinT[bg_wrap(sgn*pRotY[p])] 935 var st: i64 = 0 936 while st <= maxst { 937 // evaluate the station ring at st-1, st, st+1 (clamped at the caps) into slots 0,1,2 938 var q: i64 = 0 939 while q < 3 { 940 var stq: i64 = st + q - 1 941 if stq < 0 { stq = 0 } 942 if stq > maxst { stq = maxst } 943 var cq: i64 = stq/SUB 944 if cq > pCount[p]-2 { cq = pCount[p]-2 } 945 let j0: i64 = pStart[p]+cq 946 let j1: i64 = j0+1 947 let fq: i64 = (stq - cq*SUB)*1000/SUB 948 // ★SUB-UNIT SAMPLING. Every station coordinate is computed to BG_PSUB sub-units with ONE 949 // division, instead of rounding to raw units first. The old code rounded y to whole units 950 // before the central-difference tangent, so on a SMALL part (the head is ~1/8 the body) 951 // where dy/station is only a few units, the rounding made uy alternate 6,8,6,8 -> the 952 // vertical tilt oscillated -> horizontal BANDING (proven in the normal map, and immune to 953 // relief and to raising PSUB, because y never went through PSUB). yri etc are per-mille*1000. 954 // Catmull-Rom through the ring and its neighbours (clamped at part ends) -- see bg_cmr. 955 var jm: i64 = j0-1 956 if jm < pStart[p] { jm = pStart[p] } 957 var jp: i64 = j1+1 958 if jp > pStart[p]+pCount[p]-1 { jp = pStart[p]+pCount[p]-1 } 959 let yri: i64 = bg_cmr(ry[jm], ry[j0], ry[j1], ry[jp], fq) 960 let xri: i64 = sgn*bg_cmr(rx0[jm], rx0[j0], rx0[j1], rx0[jp], fq) 961 let zri: i64 = bg_cmr(rz0[jm], rz0[j0], rz0[j1], rz0[jp], fq) 962 var ari: i64 = bg_cmr(ra[jm], ra[j0], ra[j1], ra[jp], fq) 963 var bri: i64 = bg_cmr(rb[jm], rb[j0], rb[j1], rb[jp], fq) 964 if ari < 0 { ari = 0 } // cubic overshoot must never go negative 965 if bri < 0 { bri = 0 } 966 let ymq: i64 = yri/1000 967 let xqs: i64 = xri*H*BG_PSUB/BG_PERMIL_SQ 968 let zqs: i64 = zri*H*BG_PSUB/BG_PERMIL_SQ 969 sy[q] = yri*H*BG_PSUB/BG_PERMIL_SQ 970 scx[q] = xqs 971 scz[q] = zqs 972 let aqs: i64 = ari*rmul/1000*H*BG_PSUB/BG_PERMIL_SQ 973 let bqs: i64 = bri*rmul/1000*H*BG_PSUB/BG_PERMIL_SQ 974 var i: i64 = 0 975 while i < RS { 976 let dq: i64 = bg_wrap(i*360/RS) 977 let cw: i64 = sinT[bg_wrap(dq+90)]; let sw: i64 = sinT[dq] 978 // anatomical relief modulates the radius per ANGLE as well as height, so the 979 // surface carries real features instead of being an ellipse of revolution. relmul is 980 // the fraction of that relief THIS layer carries (bone 0, muscle full, skin fat-attenuated). 981 var relmul2: i64 = relmul 982 if DEC > 0 { if pcov[p] == 1 { relmul2 = relmul*(1000-DEC)/1000 } } 983 var rq: i64 = 1000 + bg_relief(p, ymq, dq, fP,fY,fYw,fT,fTw,fA, nf)*RLF/1000*relmul2/1000 984 if BAS != 0 { rq = rq + bg_shape_basis(p, ymq, dq, sinT, kP,kYlo,kYhi,kTh,kWk,kW0,kW1,kW2,kAmp,kYk,kP0,kP1,kP2, nbasis[0])*BAS/1000*relmul2/1000 } 985 // ★measured section prior. On a MIRRORED part the ring itself is not mirrored (only its 986 // centre offset is), so theta 0 stays +X and would put the limb's LATERAL profile on the 987 // medial side; flip the angle for side 1 so left and right are true mirrors. 988 if ns > 0 { 989 var pdq: i64 = dq 990 if sgn < 0 { pdq = bg_wrap(180-dq) } 991 if PROF != 0 { 992 let pv: i64 = bg_prof(p, ymq, pdq, sP, sY, sR, ns, nbins, 0) 993 rq = rq*(1000 + (pv-1000)*PROF/1000)/1000 994 } 995 if HF != 0 { 996 let hv: i64 = bg_prof(p, ymq, pdq, sP, sY, sR, ns, nbins, 1) 997 rq = rq*(1000 + (hv-1000)*HF/1000)/1000 998 } 999 } 1000 sx[q*BG_MAXRS+i] = xqs + (aqs*rq/1000)*cw/BG_Q14 1001 sz[q*BG_MAXRS+i] = zqs + (bqs*rq/1000)*sw/BG_Q14 1002 i = i+1 1003 } 1004 q = q+1 1005 } 1006 let uy: i64 = sy[2]-sy[0] 1007 var i: i64 = 0 1008 while i < RS { 1009 let ip: i64 = (i+1)%RS 1010 let im: i64 = (i+RS-1)%RS 1011 // dP/dtheta and dP/dstation, both central 1012 let vx: i64 = sx[BG_MAXRS+ip]-sx[BG_MAXRS+im] 1013 let vz: i64 = sz[BG_MAXRS+ip]-sz[BG_MAXRS+im] 1014 let ux: i64 = sx[2*BG_MAXRS+i]-sx[i] 1015 let uz: i64 = sz[2*BG_MAXRS+i]-sz[i] 1016 var enx: i64 = uy*vz 1017 var eny: i64 = uz*vx - ux*vz 1018 var enz: i64 = 0-uy*vx 1019 // keep the components squareable without throwing away the fine detail 1020 var mx: i64 = enx; if mx < 0 { mx = 0-mx } 1021 var m2: i64 = eny; if m2 < 0 { m2 = 0-m2 } 1022 if m2 > mx { mx = m2 } 1023 m2 = enz; if m2 < 0 { m2 = 0-m2 } 1024 if m2 > mx { mx = m2 } 1025 while mx > BG_NCAP { enx=enx/2; eny=eny/2; enz=enz/2; mx=mx/2 } 1026 // outward = agrees with the ring-centre-to-vertex direction (relief included) 1027 let ox: i64 = sx[BG_MAXRS+i]-scx[1] 1028 let oz: i64 = sz[BG_MAXRS+i]-scz[1] 1029 if (enx*ox + enz*oz) < 0 { enx = 0-enx; eny = 0-eny; enz = 0-enz } 1030 var enl: i64 = bg_isqrt(enx*enx + eny*eny + enz*enz) 1031 if enl < 1 { enl = 1 } 1032 var wnx: i64 = enx*BG_Q14/enl 1033 var wny: i64 = eny*BG_Q14/enl 1034 var wnz: i64 = enz*BG_Q14/enl 1035 var wpx: i64 = sx[BG_MAXRS+i]/BG_PSUB 1036 var wpy: i64 = sy[1]/BG_PSUB 1037 var wpz: i64 = sz[BG_MAXRS+i]/BG_PSUB 1038 // place the part: rotate about Z, then about X, then offset (see PART PLACEMENT above) 1039 bg_rot3(wpx,wpy,wpz, pzc,pzs,prc,prs,pyc,pys, tf3) 1040 wpx = tf3[0]; wpy = tf3[1]; wpz = tf3[2] 1041 wpy = wpy + pOy[p]*H/1000 1042 wpz = wpz + pOz[p]*H/1000 1043 wpx = wpx + sgn*pOx[p]*H/1000 1044 bg_rot3(wnx,wny,wnz, pzc,pzs,prc,prs,pyc,pys, tf3) 1045 wnx = tf3[0]; wny = tf3[1]; wnz = tf3[2] 1046 cnx[i] = wnx; cny[i] = wny; cnz[i] = wnz 1047 cpx[i] = wpx; cpy[i] = wpy; cpz[i] = wpz 1048 i = i+1 1049 } 1050 if Lyr==0{let rc:i64=bjc_ring(join,p,side,st,cpx,cpy,cpz,cnx,cny,cnz);if rc<0{return bg_join_refuse(join,"ring" as *u8,rc)}} 1051 let cy: i64 = sy[1]/BG_PSUB 1052 // ★CAP THE TUBE ENDS. An open tube lets the camera see its own inner wall -- that was the 1053 // bright flat trapezoid at the crotch: the torso tube's open bottom, viewed from inside. 1054 // The cap faces along the tube AXIS (from the station-centre difference), so it works for 1055 // the arm and leg tubes too, whose parameter runs downward rather than up. 1056 var capend: i64 = 0 1057 if st == 0 { capend = 1 } 1058 if st == maxst { capend = 2 } 1059 if Lyr==0&&capend>0{if bjc_keep(join,p,side,st,capend)==0{capend=0;if (join as i64)!=0{join.removed=join.removed+RS}}} 1060 if capend > 0 { 1061 var kx: i64 = scx[1]-scx[2]; var ky: i64 = sy[1]-sy[2]; var kz: i64 = scz[1]-scz[2] 1062 if capend == 2 { kx = scx[1]-scx[0]; ky = sy[1]-sy[0]; kz = scz[1]-scz[0] } 1063 var kl: i64 = bg_isqrt(kx*kx+ky*ky+kz*kz) 1064 if kl < 1 { kl = 1 } 1065 kx = kx*BG_Q14/kl; ky = ky*BG_Q14/kl; kz = kz*BG_Q14/kl 1066 // a station ring is planar in xz, so its face normal is purely +/-y: pick the winding 1067 // that agrees with the axis direction. Decided in the CANONICAL frame, before the 1068 // placement rotation -- a rotation preserves orientation, so the choice still holds. 1069 let kyc: i64 = ky 1070 bg_rot3(kx,ky,kz, pzc,pzs,prc,prs,pyc,pys, tf3) 1071 kx = tf3[0]; ky = tf3[1]; kz = tf3[2] 1072 var ccx: i64 = scx[1]/BG_PSUB 1073 var ccy: i64 = cy 1074 var ccz: i64 = scz[1]/BG_PSUB 1075 bg_rot3(ccx,ccy,ccz, pzc,pzs,prc,prs,pyc,pys, tf3) 1076 ccx = tf3[0]; ccy = tf3[1]; ccz = tf3[2] 1077 ccy = ccy + pOy[p]*H/1000 1078 ccz = ccz + pOz[p]*H/1000 1079 ccx = ccx + sgn*pOx[p]*H/1000 1080 var e2: i64 = 0 1081 while e2 < RS { 1082 var v0: i64 = (e2+1)%RS 1083 var v1: i64 = e2 1084 if kyc < 0 { v0 = e2; v1 = (e2+1)%RS } 1085 ntoff = ntoff + 1 1086 if nt < BG_MAXTRI-2 { 1087 let t3: i64 = nt*21 1088 tri[t3]=ccx; tri[t3+1]=ccy; tri[t3+2]=ccz 1089 tri[t3+3]=cpx[v0]; tri[t3+4]=cpy[v0]; tri[t3+5]=cpz[v0] 1090 tri[t3+6]=cpx[v1]; tri[t3+7]=cpy[v1]; tri[t3+8]=cpz[v1] 1091 tri[t3+9]=kx; tri[t3+10]=ky; tri[t3+11]=kz 1092 tri[t3+12]=kx; tri[t3+13]=ky; tri[t3+14]=kz 1093 tri[t3+15]=kx; tri[t3+16]=ky; tri[t3+17]=kz 1094 if Lyr==0{let rc:i64=bjc_face(join,p,side,bjc_words(tri,t3));if rc<0{return bg_join_refuse(join,"witness" as *u8,rc)}} 1095 var cr3: i64 = lcR; var cg3: i64 = lcG; var cb3: i64 = lcB 1096 if pMat[p] >= 1 { if pMat[p] <= 4 { cr3 = 958; cg3 = 954; cb3 = 942 } } // eye cap = sclera 1097 tcR[nt]=cr3; tcG[nt]=cg3; tcB[nt]=cb3 1098 nt = nt+1 1099 } 1100 e2 = e2+1 1101 } 1102 } 1103 if st > 0 { 1104 var e: i64 = 0 1105 while e < RS { 1106 let ep: i64 = (e+1)%RS 1107 // per-quad material colour (flat per-layer, unless a feature material) 1108 var qr: i64 = lcR; var qg: i64 = lcG; var qb: i64 = lcB 1109 if pMat[p] >= 1 { if pMat[p] <= 4 { // EYE (material = iris colour 1..4) 1110 qr = 958; qg = 954; qb = 942 // sclera white 1111 var af: i64 = e*360/RS - 90; if af < 0 { af = 0-af } // 0 at the FRONT (+Z) 1112 var sf: i64 = st - maxst/2; if sf < 0 { sf = 0-sf } 1113 sf = sf*100/maxst // 0 at the eye equator 1114 if af < 34 { if sf < 17 { 1115 qr = 120; qg = 78; qb = 45 // 1 = brown (default) 1116 if pMat[p] == 2 { qr = 86; qg = 122; qb = 170 } // 2 = blue 1117 if pMat[p] == 3 { qr = 92; qg = 134; qb = 92 } // 3 = green 1118 if pMat[p] == 4 { qr = 150; qg = 116; qb = 66 } // 4 = amber/hazel 1119 }} 1120 if af < 14 { if sf < 7 { qr = 24; qg = 22; qb = 24 } } // pupil (near-black) 1121 }} 1122 if pMat[p] == 5 { // FACE (head): colour by region 1123 let ymil: i64 = cy*1000/H 1124 var af2: i64 = e*360/RS - 90; if af2 < 0 { af2 = 0-af2 } // 0 at the FRONT (+Z) 1125 // ★LIP PAINT RETIRED (F1093), same reasoning as the brow: the lips are now two 1126 // real Z-rotated parts with a genuine groove between them, and a colour band 1127 // calibrated for the HEAD's angular frame lands arbitrarily across them. The 1128 // mouth is now a shadow cast by anatomy, not a rectangle drawn on an ovoid. 1129 // ★BROW PAINT RETIRED (F1084 prereq 2). The brow is now a real Z-ROTATED PART, and 1130 // paint fights geometry: this band is calibrated for the HEAD's angular frame, so 1131 // on a small brow part it landed as black bars that read as goggles. A feature is 1132 // either geometry or paint; once it is geometry the paint is a lie on top of it. 1133 } 1134 // ★★THE OPENING TEST. Skipping the quad removes the surface OUTRIGHT -- this is the 1135 // thing a radius modulation structurally cannot do. Written as a flag rather than a 1136 // wrapping block so the brace structure is UNCHANGED (a stray brace here would be a 1137 // silent scoping change in the emitter's hottest loop). 1138 var emit: i64 = 1 1139 if Lyr==0{if bjc_keep(join,p,side,st,0)==0{emit=0;if (join as i64)!=0{join.removed=join.removed+2}}} 1140 if nopen[0] > 0 { if bg_in_open(p, cy*1000/H, e*360/RS, oP,oY,oYw,oT,oTw, nopen[0]) == 1 { emit = 0 } } 1141 // an OPENING is intentional absence -- only cap refusal counts as truncation demand 1142 if emit == 1 { ntoff = ntoff + 2 } 1143 if nt >= BG_MAXTRI-2 { emit = 0 } 1144 if emit == 1 { 1145 let t1: i64 = nt*21 1146 tri[t1]=ppx[e]; tri[t1+1]=ppy[e]; tri[t1+2]=ppz[e] 1147 tri[t1+3]=cpx[e]; tri[t1+4]=cpy[e]; tri[t1+5]=cpz[e] 1148 tri[t1+6]=cpx[ep];tri[t1+7]=cpy[ep]; tri[t1+8]=cpz[ep] 1149 tri[t1+9]=pnx[e]; tri[t1+10]=pny[e]; tri[t1+11]=pnz[e] 1150 tri[t1+12]=cnx[e]; tri[t1+13]=cny[e]; tri[t1+14]=cnz[e] 1151 tri[t1+15]=cnx[ep];tri[t1+16]=cny[ep]; tri[t1+17]=cnz[ep] 1152 if Lyr==0{let rc:i64=bjc_face(join,p,side,bjc_words(tri,t1));if rc<0{return bg_join_refuse(join,"witness" as *u8,rc)}} 1153 tcR[nt]=qr; tcG[nt]=qg; tcB[nt]=qb 1154 nt = nt+1 1155 let t2: i64 = nt*21 1156 tri[t2]=ppx[e]; tri[t2+1]=ppy[e]; tri[t2+2]=ppz[e] 1157 tri[t2+3]=cpx[ep]; tri[t2+4]=cpy[ep]; tri[t2+5]=cpz[ep] 1158 tri[t2+6]=ppx[ep]; tri[t2+7]=ppy[ep]; tri[t2+8]=ppz[ep] 1159 tri[t2+9]=pnx[e]; tri[t2+10]=pny[e]; tri[t2+11]=pnz[e] 1160 tri[t2+12]=cnx[ep]; tri[t2+13]=cny[ep]; tri[t2+14]=cnz[ep] 1161 tri[t2+15]=pnx[ep]; tri[t2+16]=pny[ep]; tri[t2+17]=pnz[ep] 1162 if Lyr==0{let rc:i64=bjc_face(join,p,side,bjc_words(tri,t2));if rc<0{return bg_join_refuse(join,"witness" as *u8,rc)}} 1163 tcR[nt]=qr; tcG[nt]=qg; tcB[nt]=qb 1164 nt = nt+1 1165 } 1166 e = e+1 1167 } 1168 } 1169 var g: i64 = 0 1170 while g < RS { 1171 ppx[g]=cpx[g]; ppy[g]=cpy[g]; ppz[g]=cpz[g] 1172 pnx[g]=cnx[g]; pny[g]=cny[g]; pnz[g]=cnz[g] 1173 g = g+1 1174 } 1175 st = st+1 1176 } 1177 side = side+1 1178 } 1179 p = p+1 1180 } 1181 if Lyr==0&&(join as i64)!=0{ 1182 let joined:i64=bjc_finish(join,tri,tcR,tcG,tcB,nt,BG_MAXTRI,lcR,lcG,lcB) 1183 if joined<0{return bg_join_refuse(join,"bridge" as *u8,joined)} 1184 if join.added!=2*RS||joined!=nt+join.added||join.removed<=0{return bg_join_refuse(join,"bridge-count" as *u8,BJ_BOUNDARY)} 1185 nt=joined;ntoff=ntoff+join.added 1186 } 1187 layCnt[Lyr] = nt - lstart 1188 Lyr = Lyr+1 1189 } 1190 1191 // (the head is now PART 4 above -- the ellipsoid-of-revolution cranium is gone, replaced by a ring-tube 1192 // that inherits true normals + the facial relief table + the crown cap, just like every other part.) 1193 1194 // ---- write NXMSH2 (3 LAYERS: bone, muscle, skin -- contiguous tri ranges) ---- 1195 let hdr: i64 = 16 + BG_NLAYER*24 1196 let bytes: i64 = hdr + nt*84 + nt*4 1197 let buf: *u8 = sys_mmap(bytes + 64) 1198 buf[0]=78 as u8; buf[1]=88 as u8; buf[2]=77 as u8; buf[3]=83 as u8 1199 buf[4]=72 as u8; buf[5]=50 as u8; buf[6]=0 as u8; buf[7]=0 as u8 1200 bg_wr32(buf, 8, BG_NLAYER); bg_wr32(buf, 12, nt) 1201 // layer table: name[16] off4 cnt4, in emit order 0=bone 1=muscle 2=skin 1202 var lo: i64 = 0; var Lw: i64 = 0 1203 while Lw < BG_NLAYER { 1204 let lb: i64 = 16 + Lw*24 1205 var q: i64 = 0 1206 while q < 16 { buf[lb+q]=0 as u8; q=q+1 } 1207 if Lw==0 { buf[lb]=115 as u8; buf[lb+1]=107 as u8; buf[lb+2]=105 as u8; buf[lb+3]=110 as u8 } // "skin" 1208 if Lw==1 { buf[lb]=109 as u8; buf[lb+1]=117 as u8; buf[lb+2]=115 as u8; buf[lb+3]=99 as u8 } // "musc" 1209 if Lw==2 { buf[lb]=98 as u8; buf[lb+1]=111 as u8; buf[lb+2]=110 as u8; buf[lb+3]=101 as u8 } // "bone" 1210 bg_wr32(buf, lb+16, lo); bg_wr32(buf, lb+20, layCnt[Lw]) 1211 lo = lo + layCnt[Lw] 1212 Lw = Lw+1 1213 } 1214 // per-tri geometry + per-TRIANGLE material colour (from the emit; carries feature colours like the eyes). 1215 let l0: i64 = layCnt[0]; let l1: i64 = layCnt[0]+layCnt[1] 1216 var t: i64 = 0 1217 while t < nt { 1218 let o: i64 = hdr + t*84 1219 var j: i64 = 0 1220 while j < 9 { bg_wr32(buf, o+j*4, bg_f32(tri[t*21+j], 1)); j=j+1 } 1221 while j < 18 { bg_wr32(buf, o+j*4, bg_f32(tri[t*21+j], BG_Q14)); j=j+1 } 1222 bg_wr32(buf, o+72, bg_f32(tcR[t], 1000)); bg_wr32(buf, o+76, bg_f32(tcG[t], 1000)); bg_wr32(buf, o+80, bg_f32(tcB[t], 1000)) 1223 t = t+1 1224 } 1225 // per-tri layer id (0=skin, 1=muscle, 2=bone) 1226 var z: i64 = 0 1227 while z < nt { 1228 var lid: i64 = 2 1229 if z < l0 { lid = 0 } else { if z < l1 { lid = 1 } } 1230 bg_wr32(buf, hdr + nt*84 + z*4, lid); z=z+1 1231 } 1232 let fd: i64 = sys_openat_wr(outp, MODE_0644) 1233 sys_write(fd, buf, bytes) 1234 sys_close(fd) 1235 1236 bg_hw("{\x22organ\x22:\x22nx_body_gen\x22,\x22v\x22:2,\x22source\x22:\x22SOVEREIGN PROCEDURAL -- anthropometric canon, continuous ring profiles, no scanned asset\x22" as *u8) 1237 bg_hw(",\x22height\x22:" as *u8); bg_pn(H) 1238 bg_hw(",\x22parts\x22:" as *u8); bg_pn(np) 1239 bg_hw(",\x22control_rings\x22:" as *u8); bg_pn(n) 1240 bg_hw(",\x22radial\x22:" as *u8); bg_pn(RS) 1241 bg_hw(",\x22sub\x22:" as *u8); bg_pn(SUB) 1242 bg_hw(",\x22tris\x22:" as *u8); bg_pn(nt) 1243 bg_hw(",\x22tris_offered\x22:" as *u8); bg_pn(ntoff) 1244 // ★TRUNCATION MUST BE LOUD (F1083). The two raster guards silently STOP emitting at BG_MAXTRI, which is 1245 // the documented root of the \x22red face\x22 bug: the skin layer is emitted LAST, so a full budget drops the 1246 // head/hands and they render as the muscle layer underneath -- a wrong body with a clean exit code. Same 1247 // silent-failure class as the unguarded part array. Report the headroom always, and FAIL LOUD at the cap. 1248 bg_hw(",\x22tri_cap\x22:" as *u8); bg_pn(BG_MAXTRI) 1249 bg_hw(",\x22tri_headroom\x22:" as *u8); bg_pn(BG_MAXTRI - nt) 1250 var trunc: i64 = 0 1251 if nt >= BG_MAXTRI - 4 { trunc = 1 } 1252 if ntoff > nt { trunc = 1 } // demand-based: ANY refused tri marks the mesh truncated, not just a full budget 1253 bg_hw(",\x22truncated\x22:" as *u8); bg_pn(trunc) 1254 bg_hw(",\x22prof_rows\x22:" as *u8); bg_pn(ns) 1255 // ★OFFERED vs ACCEPTED. Equal = the whole file loaded. Greater = the cap ate rows and every number below 1256 // is measured on PARTIAL data -- publish it rather than let a truncated prior look like a measured one. 1257 bg_hw(",\x22prof_rows_offered\x22:" as *u8); bg_pn(nbp[1]) 1258 var proftrunc: i64 = 0 1259 if nbp[1] > ns { proftrunc = 1 } 1260 bg_hw(",\x22prof_truncated\x22:" as *u8); bg_pn(proftrunc) 1261 bg_hw(",\x22prof_scale\x22:" as *u8); bg_pn(PROF) 1262 bg_hw(",\x22dec\x22:" as *u8); bg_pn(DEC) 1263 if (join as i64)!=0{bg_hw(",\x22attachment_removed\x22:" as *u8);bg_pn(join.removed);bg_hw(",\x22attachment_added\x22:" as *u8);bg_pn(join.added)} 1264 bg_hw("}\n" as *u8) 1265 return 0 1266} 1267 1268func bg_join_refuse(j:*BgCutJoin,stage:*u8,code:i64)->i64{ 1269 bg_hw("{\x22error\x22:\x22SKIN_ATTACHMENT_REFUSED\x22,\x22stage\x22:\x22" as *u8);bg_hw(stage);bg_hw("\x22,\x22code\x22:" as *u8);bg_pn(code) 1270 if (j as i64)!=0{bg_hw(",\x22part_a\x22:" as *u8);bg_pn(j.part_a);bg_hw(",\x22station_a\x22:" as *u8);bg_pn(j.station_a);bg_hw(",\x22part_b\x22:" as *u8);bg_pn(j.part_b);bg_hw(",\x22station_b\x22:" as *u8);bg_pn(j.station_b)} 1271 bg_hw("}\n" as *u8);bjc_release(j);return 7 1272} 1273// Optional recipe is versioned and bounded independently of the launcher argument limit. 1274func bg_recipe_error(stage:*u8,code:i64,e:*i64)->i64{ 1275 bg_hw("{\x22error\x22:\x22ATTACHMENT_RECIPE_REFUSED\x22,\x22stage\x22:\x22" as *u8);bg_hw(stage);bg_hw("\x22,\x22code\x22:" as *u8);bg_pn(code) 1276 bg_hw(",\x22expected_version\x22:" as *u8);bg_pn(BJC_RECIPE_VERSION);bg_hw(",\x22expected_tokens\x22:" as *u8);bg_pn(BJC_RECIPE_TOKENS) 1277 if (e as i64)!=0{bg_hw(",\x22token_index\x22:" as *u8);bg_pn(e[1]);bg_hw(",\x22byte_offset\x22:" as *u8);bg_pn(e[2]);bg_hw(",\x22observed_version\x22:" as *u8);bg_pn(e[3]);bg_hw(",\x22input_bytes\x22:" as *u8);bg_pn(e[4])};bg_hw("}\n" as *u8);return 7 1278} 1279func main(argc:i64,argv:*i64)->i64{ 1280 if argc<2||argc>14{return bg_recipe_error("arguments" as *u8,BR_SCHEMA,0 as *i64)} 1281 if argc<=13{return bg_generate_cut(argc,argv,0 as *BgCutJoin)} 1282 let bytes:i64=bjc_join_bytes()+BJC_ERROR_WORDS*8+__size_of(NxFileReadRegion);let arena:*u8=sys_mmap_try(bytes) 1283 if (arena as i64)<=0{return bg_recipe_error("allocation" as *u8,NX_BO_ALLOCATION,0 as *i64)} 1284 let j:*BgCutJoin=arena as *BgCutJoin;let e:*i64=((arena as i64)+bjc_join_bytes()) as *i64 1285 let region:*NxFileReadRegion=((e as i64)+BJC_ERROR_WORDS*8) as *NxFileReadRegion;fio_region_init(region) 1286 var rc:i64=0;var data:*u8=0 as *u8;var extent:i64=0;let opened:i64=fio_region_open(argv[13] as *u8,region) 1287 if opened<0{rc=bg_recipe_error(region.stage,opened,e)}else{ 1288 extent=region.total;e[4]=extent 1289 if extent<=0{rc=bg_recipe_error("empty-file" as *u8,BR_SCHEMA,e)}else{ 1290 data=sys_mmap_try(extent) 1291 if (data as i64)<=0{rc=bg_recipe_error("file-allocation" as *u8,NX_BO_ALLOCATION,e)}else{ 1292 let got:i64=fio_region_next(region,data,extent) 1293 if got!=extent||region.read_bytes!=extent||region.code!=0{rc=bg_recipe_error(region.stage,region.code,e)}else{ 1294 let parsed:i64=bjc_recipe_parse(data,extent,j,e) 1295 if parsed<0{rc=bg_recipe_error("schema" as *u8,parsed,e)}else{rc=bg_generate_cut(argc,argv,j)} 1296 } 1297 } 1298 } 1299 } 1300 let closed:i64=fio_region_close(region);if closed<0&&rc==0{rc=bg_recipe_error("close" as *u8,closed,e)} 1301 if (data as i64)>0{let freed_data:i64=sys_munmap_direct(data,extent);if freed_data<0{rc=bg_recipe_error("file-release" as *u8,NX_BO_RELEASE,e)}} 1302 let released:i64=bjc_release(j);if released<0{rc=bg_recipe_error("release" as *u8,released,e)} 1303 let freed:i64=sys_munmap_direct(arena,bytes);if freed<0{return bg_recipe_error("release-arena" as *u8,NX_BO_RELEASE,0 as *i64)};return rc 1304}