code wiki / _hdl_build / nx_body_gen.nx
nx_body_gen.nx source
↩ module page · 1070 lines · 66009 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_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
21const BG_MAGIC_3000: i64 = 3000
22const BG_MAGIC_1000000000: i64 = 1000000000
23const BG_MAGIC_1000000: i64 = 1000000
24const BG_MAGIC_2000000: i64 = 2000000
25const BG_MAGIC_8388608: i64 = 8388608
26const BG_MAGIC_8388607: i64 = 8388607
27const BG_MAGIC_1750: i64 = 1750
28
29const BG_Q14: i64 = 16384
30const BG_MAGIC_40500: i64 = 40500
31// raised for the face-parts program (F1083): 21 parts at radial 52/sub 8 reached 393,432 of the old 400k,
32// i.e. 6.5k from SILENT truncation of the skin head. Headroom + a truncated flag are now reported every run.
33const BG_MAXTRI: i64 = 600000
34// ★★CAPS RAISED TO WHAT THE HARDWARE SUPPORTS, NOT TO A NUMBER SOMEBODY ONCE TYPED. 256 rings and 48 parts
35// were never measured against anything -- and the anatomy ladder ahead (32 teeth, 20 nails, 200 eyelashes,
36// each its own part with its own rings) walks straight through both. The arrays are lazily mmap'd, so a
37// larger cap costs address space rather than resident memory: 8192 rings across 5 arrays is 320KB of
38// reservation. ★The cap is now a REFUSAL THRESHOLD, not a silent truncation point -- which is only safe
39// because the guards above landed in the same change. Raising a bound without a check would have converted
40// a near-miss into a much larger silent overwrite.
41const BG_MAXRING: i64 = 8192
42const BG_MAXFEAT: i64 = 4096
43// ★PART CAP, named + guarded (was a bare 16 in six mmap sizes with NO bound check on the parser's write:
44// a canon with a 17th P row wrote past a 128-byte allocation -- an unguarded array write driven by an
45// EXTERNAL file, which is the classic overflow class and silently "worked" only because mmap rounds to a
46// page. The canon is external input to this emitter, so it is a boundary and must be defensive. Raised to
47// 48 because the face-geometry program needs real feature PARTS (nose, lips, ears) and the canon already
48// carried exactly 16.
49const BG_MAXPART: i64 = 2048
50// surface points are kept at BG_PSUB sub-units so the central-difference tangents keep their precision;
51// integer rounding of a raw-unit point would be ~3% of a one-step tangent and swamp the relief signal
52const BG_PSUB: i64 = 512
53const BG_MAXRS: i64 = 256
54// components are halved down to this before squaring -- keeps enx*enx+... far inside i64
55const BG_NCAP: i64 = 1000000
56// ★LAYER STACK -- the body is built from the first cell up: a bone core, a muscle envelope on the bone, and
57// the skin as the outer envelope of muscle+fat. Each is a real NXMSH2 layer, so it renders/peels separately
58// and the skin's SHAPE EMERGES from the layers beneath instead of being painted on a shell.
59// ★MEASURED SECTION PROFILES (GX-31). Every ring was an ELLIPSE modulated by relief bumps; a real human
60// cross-section is not an ellipse (flat back, scapular bulges, sternal hollow, iliac flare). nx_profile_fit
61// measures those sections off the oracle and emits a DIMENSIONLESS per-angle ratio (1000 = on the ellipse);
62// this emitter applies the ratio to whatever size the rule engine asked for, so the prior is shape-only and
63// the body stays procedural. Same idea as Infinigen taking creature profiles from real references.
64// the prior's angular resolution is carried IN the data file ('N <bins>'), never duplicated as a const in
65// two organs -- a silent bin-count mismatch would misread every row as a rotated body.
66const BG_NBMAX: i64 = 96
67// ★512 -> 2048 (2026-07-30, ws=neuro). A profile carrying every canon part needs parts x stations rows:
68// the skull canon alone is 7 x 167 = 1169, so 512 silently dropped 47 percent of a 973-row file and the
69// emitter reported prof_rows:512 as though that WERE the data. sR is BG_MAXPROF*BG_NBMAX*8 = 1.5MB at 2048.
70const BG_MAXPROF: i64 = 2048
71const BG_PROFCLAMP: i64 = 300 // a single bad measured row can never deform the body more than +/-30%
72// ★per-mille-of-stature band at each part's data extremes over which the prior fades to neutral, so a
73// per-part modulation can never pull two parts apart at the boundary the canon built them to share.
74const BG_PROFEDGE: i64 = 45
75const BG_NLAYER: i64 = 3
76const BG_BONEF: i64 = 400 // bone core radius = 40% of the skin radius (the skeletal armature)
77const BG_MUSCF: i64 = 820 // muscle envelope = 82% of the skin radius (muscle bellies fill toward skin)
78
79// ★★★ONE PLACEMENT ROTATION, USED BY ALL FOUR TRANSFORM SITES. The Z->X->Y chain was written out FOUR
80// TIMES -- position, normal, cap normal, cap centre -- as twenty-four hand-copied lines. That is the
81// duplication class this programme keeps finding the hard way: when rotY was added, the same six lines had
82// to be inserted four times and any one of them could have been missed or transposed silently, because a
83// body with three correct sites and one wrong one still renders.
84// ★A TRANSFORM APPLIED IN FOUR PLACES IS ONE RULE WITH THREE CHANCES TO DRIFT.
85// Extracting it also makes the chain TESTABLE: bg_rot3 is a pure function of nine integers, so the gate can
86// assert the composition order and the mirror convention directly instead of inferring them from a mesh.
87// ORDER IS LOAD-BEARING: rotZ turns the cross-section in its own plane, rotX pitches the part, and rotY --
88// applied LAST -- is a true world AZIMUTH. Applied first it would merely spin the cross-section.
89func bg_rot3(x: i64, y: i64, z: i64, pzc: i64, pzs: i64, prc: i64, prs: i64, pyc: i64, pys: i64, o: *i64) -> i64 {
90 let zx: i64 = (x*pzc - y*pzs)/BG_Q14
91 let zy: i64 = (x*pzs + y*pzc)/BG_Q14
92 let ry: i64 = (zy*prc - z*prs)/BG_Q14
93 let rz: i64 = (zy*prs + z*prc)/BG_Q14
94 o[2] = (rz*pyc - zx*pys)/BG_Q14
95 o[0] = (rz*pys + zx*pyc)/BG_Q14
96 o[1] = ry
97 return 0
98}
99func 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 }
100// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
101// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
102// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
103// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
104func bg_pn(v: i64) -> i64 { nxi_out(v); return 0 }
105func bg_satoi(s: *u8) -> i64 { var i: i64=0; var n: i64=0
106 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 }
107func bg_sin_fill(t: *i64) -> i64 {
108 var d: i64=0
109 while d<180 { let P: i64=d*(180-d); t[d]=BG_Q14*4*P/(BG_MAGIC_40500-P); t[d+180]=0-t[d]; d=d+1 }
110 return 0
111}
112func bg_wrap(d: i64) -> i64 { var x: i64=d%360; if x<0 { x=x+360 } return x }
113// shortest angular separation, 0..180
114func bg_angsep(a: i64, b: i64) -> i64 {
115 var d: i64 = bg_wrap(a) - bg_wrap(b)
116 if d < 0 { d = 0-d }
117 if d > 180 { d = 360-d }
118 return d
119}
120// ★ANATOMICAL SURFACE RELIEF. Until now every ring was a plain ellipse, so the body had correct OUTLINE but a
121// featureless surface -- which is exactly what the normal-field judge was penalising (it became the binding
122// constraint at 749 once the silhouettes were fixed). Real anatomy modulates the radius with ANGLE as well as
123// height: pectorals and abdominal bulges at the front, the spinal groove and scapulae at the back, glutes,
124// quadriceps, calves. Each feature is a (part, height, angle, extent, amplitude) tuple with quadratic falloff,
125// summed -- data, not hand-carved geometry. theta: 0=+X right, 90=+Z front, 180=-X left, 270=-Z back.
126func bg_relief(part: i64, ymil: i64, th: i64, fP: *i64, fY: *i64, fYw: *i64, fT: *i64, fTw: *i64, fA: *i64, nf: i64) -> i64 {
127 var s: i64 = 0
128 var k: i64 = 0
129 while k < nf {
130 if fP[k] == part {
131 var dy: i64 = ymil - fY[k]
132 if dy < 0 { dy = 0-dy }
133 if dy < fYw[k] {
134 let dt: i64 = bg_angsep(th, fT[k])
135 if dt < fTw[k] {
136 let wy: i64 = 1000 - dy*1000/fYw[k]
137 let wt: i64 = 1000 - dt*1000/fTw[k]
138 s = s + fA[k]*wy/1000*wt/1000
139 }
140 }
141 }
142 k = k + 1
143 }
144 return s
145}
146// ANALYTIC SHAPE BASIS (GX-41). displacement = sum of beta_i * basis_i(coord): the body as a mean shape
147// plus weighted deformation functions, the genome being the coefficients. Two properties earned it this slot:
148// (1) REGISTRATION-FREE BY CONSTRUCTION -- the basis is evaluated in OUR body's own parameter space, so the
149// misregistration that killed measured detail transfer (residual sampled at the oracle's coordinates)
150// cannot occur here;
151// (2) it creates surface that faces UP and DOWN. A smooth vertical body has ny approx 0 everywhere, which is
152// why the environment light had nothing to differentiate and why the detail judge sees no busyness. What
153// matters is the GRADIENT dr/dy, not the amplitude -- so the bases vary in HEIGHT, not just in angle,
154// unlike the relief-feature table that measured as worth 1 permil.
155// Anatomical, not noise: intercostal banding over the ribcage, the clavicle trough, the inframammary fold.
156func bg_shape_basis(part: i64, ymil: i64, th: i64, sinT: *i64) -> i64 {
157 var s: i64 = 0
158 if part == 0 {
159 if ymil > 600 { if ymil < 764 {
160 // INTERCOSTALS, SHAPED LIKE RIBS INSTEAD OF LIKE CORRUGATION. The first pass banded the chest
161 // with horizontal rings of constant spacing and constant amplitude: it was visible, it moved the
162 // judge, and it read as a ribbed sweater. Three anatomical facts fix that, and each one is a term:
163 // 1. ribs run DOWN AND LATERAL, so the band phase must shift with angle, not just height;
164 // 2. spacing WIDENS toward the lower ribs, so the phase rate falls as we descend;
165 // 3. amplitude is ZERO at the sternum (a flat plate, no bands), peaks mid-lateral, and fades
166 // again at the flank -- so the window is a ridge, not a front-weighted ramp.
167 let dy: i64 = ymil - 600
168 let w1: i64 = bg_angsep(th, 90)
169 let ph: i64 = (dy*10 - dy*dy/60 + w1*7) % 360
170 var aw: i64 = 0
171 if w1 > 18 { if w1 < 55 { aw = (w1-18)*1000/37 } }
172 if w1 >= 55 { if w1 < 110 { aw = (110-w1)*1000/55 } }
173 if aw > 0 { s = s + 26*sinT[bg_wrap(ph)]/BG_Q14*aw/1000 }
174 }}
175 if ymil > 776 { if ymil < 814 {
176 // clavicle trough just under the shoulder line
177 let w2: i64 = bg_angsep(th, 90)
178 if w2 < 84 { s = s - 30*(1000 - w2*1000/84)/1000 }
179 }}
180 if ymil > 636 { if ymil < 668 {
181 // inframammary / pectoral fold: a down-facing surface under the chest mass
182 let w3: i64 = bg_angsep(th, 90)
183 if w3 < 70 { s = s - 22*(1000 - w3*1000/70)/1000 }
184 }}
185 }
186 if part == 4 {
187 // FACIAL STRUCTURE BASES. The operator's axis list names the exact defect: standard anime eyes sit
188 // FLUSH with the face, and ours do -- spheres set into a smooth head, no orbital depth, no ridge
189 // above them. That is why the face reads as a mask. All four terms below are GEOMETRY, and all four
190 // create DOWN-facing surface, which is precisely what the environment light can now reveal and what
191 // a front-flat body never had. theta 90 = front.
192 let wf: i64 = bg_angsep(th, 90)
193 // 1. BROW RIDGE -- a projecting shelf; without it there is nothing to cast the shadow that makes
194 // eyes read as deep-set rather than painted on.
195 if ymil > 944 { if ymil < 959 { if wf < 58 { s = s + 72*(1000 - wf*1000/58)/1000 } } }
196 // 2. ORBITAL RECESSION -- the eye sits BENEATH that ridge. Negative radius, sharp gradient.
197 if ymil > 921 { if ymil < 943 { if wf < 50 { s = s - 92*(1000 - wf*1000/50)/1000 } } }
198 // 3. ZYGOMATIC PROJECTION -- cheekbone pushed out, lateral of the nose.
199 if ymil > 899 { if ymil < 919 { if wf > 18 { if wf < 74 {
200 var z1: i64 = wf - 18
201 if z1 > 28 { z1 = 56 - z1 }
202 if z1 > 0 { s = s + 64*z1*1000/28/1000 }
203 } } } }
204 // 4. SUB-ZYGOMATIC HOLLOW -- the indent UNDER the cheekbone. This is the one that makes light carve
205 // a diagonal across the face instead of washing it flat; it is a gradient feature, not a texture.
206 if ymil > 877 { if ymil < 899 { if wf > 24 { if wf < 70 {
207 var z2: i64 = wf - 24
208 if z2 > 23 { z2 = 46 - z2 }
209 if z2 > 0 { s = s - 58*z2*1000/23/1000 }
210 } } } }
211 }
212 if part == 2 {
213 if ymil > 120 { if ymil < 300 {
214 // gastrocnemius belly: a real bulge with a real gradient down the calf
215 let ph2: i64 = (ymil-120)*1000/180
216 let w4: i64 = bg_angsep(th, 270)
217 if w4 < 90 { s = s + 24*sinT[bg_wrap(ph2*180/1000)]/BG_Q14*(1000 - w4*1000/90)/1000 }
218 }}
219 }
220 return s
221}
222func 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 }
223// ★CATMULL-ROM spline through control values p1..p2 (neighbours p0,p3), t in [0,1000]; returns value*1000.
224// THE Infinigen-gap fix: linear interpolation between sparse rings makes every part read as stacked cones with
225// a crease at each ring -- a cubic through the SAME rings gives C1-smooth organic curvature everywhere.
226// Integer-exact at the ends: t=0 -> p1*1000, t=1000 -> p2*1000.
227func bg_cmr(p0: i64, p1: i64, p2: i64, p3: i64, t: i64) -> i64 {
228 let a: i64 = 2*p1
229 let b: i64 = p2 - p0
230 let c: i64 = 2*p0 - 5*p1 + 4*p2 - p3
231 let d: i64 = 3*p1 - p0 + p3 - 3*p2
232 return (a*BG_MAGIC_1000000000 + b*t*BG_MAGIC_1000000 + c*t*t*1000 + d*t*t*t)/BG_MAGIC_2000000
233}
234// integer IEEE-754 float32 encoder (NishiLang has no float type -- we pack the bits ourselves)
235func bg_f32(v: i64, scale: i64) -> i64 {
236 if v == 0 { return 0 }
237 var neg: i64 = 0
238 var m: i64 = v
239 if m < 0 { neg = 1; m = 0-m }
240 var e: i64 = 0
241 var num: i64 = m
242 var den: i64 = scale
243 while num >= den*2 { den = den*2; e = e+1 }
244 while num < den { num = num*2; e = e-1 }
245 let frac: i64 = ((num - den)*BG_MAGIC_8388608)/den
246 var bits: i64 = ((e+127) << 23) | (frac & BG_MAGIC_8388607)
247 if neg == 1 { bits = bits | (1<<31) }
248 return bits
249}
250func bg_wr32(b: *u8, o: i64, v: i64) -> i64 {
251 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
252 return 0
253}
254
255// ---- CANON PARSER: the emitter reads its geometry from DATA, it carries none. ----
256// signed-integer token reader: skips leading non-digit/non-minus within [pos,end), reads, advances pos.
257func bg_rdint(b: *u8, pos: *i64, end: i64) -> i64 {
258 var i: i64 = pos[0]
259 var go: i64 = 1
260 while go == 1 {
261 if i >= end { go = 0 } else {
262 let c: i64 = b[i] as i64
263 if c == 45 { go = 0 } else {
264 if c >= 48 { if c <= 57 { go = 0 } else { i = i+1 } } else { i = i+1 }
265 }
266 }
267 }
268 var sg: i64 = 1
269 if i < end { if (b[i] as i64) == 45 { sg = 0-1; i = i+1 } }
270 var v: i64 = 0
271 var g2: i64 = 1
272 while g2 == 1 {
273 if i >= end { g2 = 0 } else {
274 let c: i64 = b[i] as i64
275 if c >= 48 { if c <= 57 { v = v*10 + (c-48); i = i+1 } else { g2 = 0 } } else { g2 = 0 }
276 }
277 }
278 pos[0] = i
279 return v*sg
280}
281// parse a canon buffer: P (part header) / R (control ring) / F (relief feature) lines; '#' = comment.
282// ---- ★★★OPENINGS: THE MECHANISM RELIEF STRUCTURALLY CANNOT PROVIDE (seq1387/seq908) ----
283// An orbit and a nasal aperture are HOLES. Relief modulates a RADIUS, and a radius modulation can dent a
284// surface but can never remove it -- which is why the skull's six declared hollows moved the mesh and
285// moved nx_bodybench by NOTHING (headline 45, detail_head 14). An 'O' row declares a region where the
286// shell is simply ABSENT: the quad is not emitted, so the surface is genuinely gone and what lies behind
287// it is visible. Same geometry fields as an F row (part, y, y-halfwidth, theta, theta-halfwidth) minus
288// the amplitude, because there is no amount -- the surface is there or it is not.
289// ★PARSED SEPARATELY ON PURPOSE: bg_parse_canon already takes 24 arguments, and widening it further is
290// the wrong shape (that row wants a context array on its next touch, D001). This walks the same buffer
291// independently, so the existing signature and all its callers are untouched.
292// ★★ADDITIVE BY CONSTRUCTION: a canon carrying no O rows yields count 0 and every test below is inert,
293// so every existing canon must emit BIT-IDENTICAL output. That is the acceptance proof, not an opinion.
294func bg_parse_open(b: *u8, len: i64, oP: *i64, oY: *i64, oYw: *i64, oT: *i64, oTw: *i64, cnt: *i64) -> i64 {
295 let pos: *i64 = sys_mmap(16) as *i64
296 var n: i64 = 0
297 var i: i64 = 0
298 var bol: i64 = 1
299 while i < len {
300 if bol == 1 {
301 if (b[i] as i64) == 79 {
302 if n < BG_MAXFEAT {
303 pos[0] = i+1
304 oP[n] = bg_rdint(b, pos, len)
305 oY[n] = bg_rdint(b, pos, len)
306 oYw[n] = bg_rdint(b, pos, len)
307 oT[n] = bg_rdint(b, pos, len)
308 oTw[n] = bg_rdint(b, pos, len)
309 i = pos[0]
310 n = n + 1
311 }
312 }
313 }
314 if (b[i] as i64) == 10 { bol = 1 } else { bol = 0 }
315 i = i + 1
316 }
317 cnt[0] = n
318 return n
319}
320// is this (part, station, angle) inside a declared opening? bg_angsep handles the 0/360 wrap.
321func bg_in_open(part: i64, ymil: i64, thd: i64, oP: *i64, oY: *i64, oYw: *i64, oT: *i64, oTw: *i64, n: i64) -> i64 {
322 var k: i64 = 0
323 while k < n {
324 if oP[k] == part {
325 var dy: i64 = ymil - oY[k]
326 if dy < 0 { dy = 0 - dy }
327 if dy <= oYw[k] {
328 if bg_angsep(thd, oT[k]) <= oTw[k] { return 1 }
329 }
330 }
331 k = k + 1
332 }
333 return 0
334}
335
336func bg_parse_canon(b: *u8, len: i64, ry: *i64, rx0: *i64, rz0: *i64, ra: *i64, rb: *i64,
337 pStart: *i64, pCount: *i64, pMirror: *i64, pRot: *i64, pOx: *i64, pOy: *i64, pOz: *i64,
338 pMat: *i64, pRotZ: *i64, pRotY: *i64, fP: *i64, fY: *i64, fYw: *i64, fT: *i64, fTw: *i64, fA: *i64, counts: *i64) -> i64 {
339 var np: i64 = 0; var n: i64 = 0; var nf: i64 = 0
340 let pos: *i64 = sys_mmap(8) as *i64
341 var i: i64 = 0
342 while i < len {
343 var j: i64 = i
344 var sk: i64 = 1
345 while sk == 1 {
346 if j >= len { sk = 0 } else {
347 let c: i64 = b[j] as i64
348 if c == 32 { j = j+1 } else { if c == 9 { j = j+1 } else { sk = 0 } }
349 }
350 }
351 var tag: i64 = 0
352 if j < len { tag = b[j] as i64 }
353 var e: i64 = i
354 var fe: i64 = 1
355 while fe == 1 { if e >= len { fe = 0 } else { if (b[e] as i64) == 10 { fe = 0 } else { e = e+1 } } }
356 if tag == 80 { // 'P' -- part header
357 // GUARD: refuse LOUD rather than write past the part arrays. The canon is EXTERNAL input, so
358 // this is a boundary; an unguarded write here silently overflowed for every canon past the cap.
359 if np >= BG_MAXPART {
360 bg_hw("CANON-REFUSED part count exceeds BG_MAXPART\n" as *u8)
361 sys_exit(4)
362 }
363 if np > 0 { pCount[np-1] = n - pStart[np-1] }
364 pos[0] = j+1
365 pMirror[np] = bg_rdint(b,pos,e); pRot[np] = bg_rdint(b,pos,e)
366 pOx[np] = bg_rdint(b,pos,e); pOy[np] = bg_rdint(b,pos,e); pOz[np] = bg_rdint(b,pos,e)
367 pMat[np] = bg_rdint(b,pos,e) // optional 6th field: material (0=flesh, 1=eye)
368 // ★optional 7th field: ROTATION ABOUT Z, degrees (F1084 prereq 3). The mechanism gap the face
369 // work hit head-on: a brow ridge and a lip run HORIZONTALLY, but a part could only stack rings
370 // along Y and rotate about X, so every horizontal feature came out a vertical blob. rotZ turns
371 // the canonical Y-tube sideways IN the face plane; absent field parses as 0 = exact identity,
372 // so every existing canon is bit-identical (proven by golden md5 on the standing canon).
373 pRotZ[np] = bg_rdint(b,pos,e)
374 // ★optional 8th field: ROTATION ABOUT Y = AZIMUTH, degrees (S1). rotZ turns a part sideways in
375 // its own plane and rotX pitches it; NEITHER can answer "which compass direction does this limb
376 // point". That is the whole reason a wing, a branching antler and a spine-following ribcage were
377 // inexpressible: parts could only fan within one plane. Applied LAST of the three, so it is a
378 // true world azimuth rather than a spin of the cross-section. ZXY is a complete Euler
379 // parameterisation, so any orientation is now reachable. Absent field parses 0 -> pyc=Q14,
380 // pys=0, and x*Q14/Q14 is integer-exact => every existing canon stays BIT-IDENTICAL.
381 pRotY[np] = bg_rdint(b,pos,e)
382 pStart[np] = n; np = np+1
383 }
384 if tag == 82 { // 'R' -- control ring
385 // ★★THE GUARD THAT WAS MISSING FOR AS LONG AS THE PART GUARD HAS EXISTED. The 'P' branch above
386 // was hardened after an unguarded write silently overflowed -- and the fix was applied to ONE of
387 // the three branches. R and F kept writing past their arrays. ★A HARDENING APPLIED TO ONE
388 // BRANCH OF THREE IS NOT A HARDENING; it is a note saying somebody once knew about the problem.
389 // ⚠WHY IT NEVER SHOWED: the ring pool is GLOBAL across parts (256 entries for the whole body)
390 // and the canon has used 110 of them, so the overflow was always one anatomy rung away rather
391 // than absent. A 32-tooth arch at 4 rings each is 128 rings ON ITS OWN. mmap rounds to a page,
392 // so the first few hundred over-writes would have "worked" -- a wrong body with a clean exit.
393 if n >= BG_MAXRING {
394 bg_hw("CANON-REFUSED ring count exceeds BG_MAXRING\n" as *u8)
395 sys_exit(5)
396 }
397 pos[0] = j+1
398 ry[n] = bg_rdint(b,pos,e); rx0[n] = bg_rdint(b,pos,e); rz0[n] = bg_rdint(b,pos,e)
399 ra[n] = bg_rdint(b,pos,e); rb[n] = bg_rdint(b,pos,e); n = n+1
400 }
401 if tag == 70 { // 'F' -- relief feature
402 // the F pool was a bare unnamed sys_mmap(128*8) with no constant and no check -- it now has both
403 if nf >= BG_MAXFEAT {
404 bg_hw("CANON-REFUSED feature count exceeds BG_MAXFEAT\n" as *u8)
405 sys_exit(6)
406 }
407 pos[0] = j+1
408 fP[nf] = bg_rdint(b,pos,e); fY[nf] = bg_rdint(b,pos,e); fYw[nf] = bg_rdint(b,pos,e)
409 fT[nf] = bg_rdint(b,pos,e); fTw[nf] = bg_rdint(b,pos,e); fA[nf] = bg_rdint(b,pos,e); nf = nf+1
410 }
411 i = e+1
412 }
413 if np > 0 { pCount[np-1] = n - pStart[np-1] }
414 counts[0] = np; counts[1] = n; counts[2] = nf
415 return 0
416}
417
418// parse the measured-profile file: 'S <part> <ymil> <ra> <rb> <24 ratios>' rows, ';' comments.
419func bg_parse_prof(b: *u8, len: i64, sP: *i64, sY: *i64, sR: *i64, nbo: *i64) -> i64 {
420 var ns: i64 = 0
421 var nb: i64 = 0
422 let pos: *i64 = sys_mmap(8) as *i64
423 var i: i64 = 0
424 while i < len {
425 var j: i64 = i
426 var sk: i64 = 1
427 while sk == 1 {
428 if j >= len { sk = 0 } else {
429 let c: i64 = b[j] as i64
430 if c == 32 { j = j+1 } else { if c == 9 { j = j+1 } else { sk = 0 } }
431 }
432 }
433 var tag: i64 = 0
434 if j < len { tag = b[j] as i64 }
435 var e: i64 = i
436 var fe: i64 = 1
437 while fe == 1 { if e >= len { fe = 0 } else { if (b[e] as i64) == 10 { fe = 0 } else { e = e+1 } } }
438 if tag == 78 { // 'N' -- bins per section
439 pos[0] = j+1
440 nb = bg_rdint(b,pos,e)
441 if nb > BG_NBMAX { nb = 0 }
442 }
443 if tag == 83 { // 'S'
444 // ★COUNT WHAT WAS OFFERED, NOT ONLY WHAT FIT. nbo[1] is the row count PRESENT in the file; the
445 // caller compares it to the accepted count so a cap can never pass itself off as the data.
446 if nb > 0 { nbo[1] = nbo[1] + 1 }
447 if ns < BG_MAXPROF { if nb > 0 {
448 pos[0] = j+1
449 sP[ns] = bg_rdint(b,pos,e); sY[ns] = bg_rdint(b,pos,e)
450 bg_rdint(b,pos,e); bg_rdint(b,pos,e) // measured ra/rb are diagnostics; SIZE stays procedural
451 var k: i64 = 0
452 while k < nb { sR[ns*BG_NBMAX+k] = bg_rdint(b,pos,e); k = k+1 }
453 ns = ns+1
454 }}
455 }
456 i = e+1
457 }
458 nbo[0] = nb
459 return ns
460}
461// look up the shape prior for (part, height, parameter angle): linear in height between the two bracketing
462// measured stations, linear in angle between the two bracketing bins, clamped. Parts with no measured rows
463// (the foot, which runs forward and is measured in its own frame) return 1000 = unchanged.
464// ★HIGH-FREQUENCY RESIDUAL (hf != 0). Four measured attempts agreed that transferring the reference's
465// LOW-frequency shape improves silhouettes and WORSENS surface busyness -- and busyness is the binding
466// judge. The information the detail judge wants is the part of the real section that a smooth shape does
467// NOT explain: the residual against the profile's own smoothed self. Same measured data, opposite band.
468func bg_prof_at(sR: *i64, row: i64, b: i64, nb: i64) -> i64 { return sR[row*BG_NBMAX + ((b%nb)+nb)%nb] }
469func bg_prof_smooth(sR: *i64, row: i64, b: i64, nb: i64) -> i64 {
470 var acc: i64 = 0
471 var k: i64 = 0-3
472 while k <= 3 { acc = acc + bg_prof_at(sR,row,b+k,nb); k = k+1 }
473 return acc/7
474}
475func bg_prof(part: i64, ymil: i64, dq: i64, sP: *i64, sY: *i64, sR: *i64, ns: i64, nb: i64, hf: i64) -> i64 {
476 var lo: i64 = 0-1
477 var hi: i64 = 0-1
478 var k: i64 = 0
479 // ★★★PART-EDGE FEATHER (seq1466). The prior is looked up PER PART, so two parts that MEET can be
480 // perturbed to DIFFERENT radii at their shared boundary and stop meeting -- the skin then shows a hard
481 // step. RENDERED AND CONFIRMED: at PROF=1000 layer 0 alone (not a layer-superposition artifact -- that
482 // hypothesis was tested and refuted) carries a collar at the neck, a waist band, a knee seam and a
483 // capped head, and PROF=0 is clean, so the prior introduces them.
484 // ★The canon's parts are built to meet EXACTLY; the prior only has to stop pulling them apart. Fading
485 // its influence to zero at each part's own data extremes preserves the join by construction while
486 // leaving the part's INTERIOR fully modulated, which is where the detail lives.
487 let edge: i64 = BG_PROFEDGE
488 var pmin: i64 = BG_MAGIC_1000000
489 var pmax: i64 = 0 - BG_MAGIC_1000000
490 while k < ns {
491 if sP[k] == part {
492 if sY[k] < pmin { pmin = sY[k] }
493 if sY[k] > pmax { pmax = sY[k] }
494 if sY[k] <= ymil { if lo < 0 { lo = k } else { if sY[k] > sY[lo] { lo = k } } }
495 if sY[k] >= ymil { if hi < 0 { hi = k } else { if sY[k] < sY[hi] { hi = k } } }
496 }
497 k = k+1
498 }
499 if lo < 0 { if hi < 0 { return 1000 } lo = hi }
500 if hi < 0 { hi = lo }
501 let a: i64 = bg_wrap(dq)
502 let fb: i64 = a*nb*1000/360
503 let b0: i64 = (fb/1000) % nb
504 let b1: i64 = (b0+1) % nb
505 let bm: i64 = (b0+nb-1) % nb
506 let b2: i64 = (b0+2) % nb
507 let ft: i64 = fb % 1000
508 // ★CIRCULAR CATMULL-ROM around the ring, not linear between bins. Linear interpolation of the prior puts
509 // a CREASE at every one of the 24 bin boundaries -- a normal discontinuity every ~2 vertices at radial 44
510 // -- and the detail judge punishes busy-where-the-oracle-is-smooth exactly as it punishes smooth-where-
511 // detailed, so a C0 prior scored WORSE than no prior at all (measured: detail 360 -> 333). A cubic through
512 // the same measured bins is C1 by construction. Same fix that removed the axial banding in the lofting.
513 let v0: i64 = bg_cmr(sR[lo*BG_NBMAX+bm], sR[lo*BG_NBMAX+b0], sR[lo*BG_NBMAX+b1], sR[lo*BG_NBMAX+b2], ft)/1000
514 let v1: i64 = bg_cmr(sR[hi*BG_NBMAX+bm], sR[hi*BG_NBMAX+b0], sR[hi*BG_NBMAX+b1], sR[hi*BG_NBMAX+b2], ft)/1000
515 var w: i64 = 0
516 if sY[hi] != sY[lo] { w = (ymil - sY[lo])*1000/(sY[hi]-sY[lo]) }
517 if w < 0 { w = 0 }
518 if w > 1000 { w = 1000 }
519 // ★★★THE RINGING, FIXED WITH THE FIX ALREADY IN THIS FUNCTION -- ONE AXIS OVER.
520 // The comment above records killing a CREASE AT EVERY BIN BOUNDARY in the THETA direction by replacing
521 // linear interpolation with a circular Catmull-Rom, because a C0 prior scored WORSE than no prior at
522 // all (360 -> 333). ***THE AXIAL BLEND WAS STILL LINEAR***, so the prior was C1 AROUND the body and C0
523 // ALONG it: the tangent jumps at EVERY one of the 156 profile rows, and at strong PROF those creases
524 // read as hard horizontal bands. RENDERED AND CONFIRMED at PROF=1000: a pronounced collar at the neck
525 // plus seams at waist and knees -- which is exactly the 'it rings the neck' the default-off rationale
526 // cited, and it was never a property of the DATA, only of how the data was blended.
527 // ★SMOOTHSTEP makes dw/dy vanish at w=0 and w=1, so both sides of every row meet with equal tangent =
528 // C1 across the row. Exact at the ends by construction: w=0 -> 0, w=1000 -> 1000, w=500 -> 500.
529 w = w*w*(BG_MAGIC_3000 - 2*w)/BG_MAGIC_1000000
530 var v: i64 = (v0*(1000-w) + v1*w)/1000
531 if hf != 0 {
532 // subtract the profile's own smoothed shape -> keep ONLY what the smooth shape cannot explain
533 let s0: i64 = bg_prof_smooth(sR, lo, b0, nb)
534 let s1: i64 = bg_prof_smooth(sR, hi, b0, nb)
535 let sv: i64 = (s0*(1000-w) + s1*w)/1000
536 // hf is a FLAG here, not a scale -- the caller applies the scale. Using it as a per-mille multiplier
537 // made (v-sv)*1/1000 truncate to exactly ZERO, so the whole band was a silent no-op that still built
538 // and still benched identically. Match the units of every factor before believing a null result.
539 v = 1000 + (v - sv)
540 }
541 // ★APPLY THE EDGE FEATHER: within `edge` of this part's own data extremes, ramp the modulation back
542 // toward neutral so the part meets its neighbour exactly as the canon built it. Linear in distance is
543 // enough here because the endpoints are what must agree; the axial blend is already C1 (smoothstep).
544 if pmax > pmin {
545 var d: i64 = ymil - pmin
546 let d2: i64 = pmax - ymil
547 if d2 < d { d = d2 }
548 if d < 0 { d = 0 }
549 // ⚠FEATHER REMOVED FROM THE PATH 2026-07-30 (my first disable was WRONG and the bench caught it:
550 // writing (v-1000)*0/edge forces v=1000 near the extremes, which is the MAXIMUM feather, not none --
551 // it read headline 332, worse than both the feather 340 and the clean 354. Match the units of every
552 // factor before believing a null result -- this file says exactly that 40 lines down about hf.)
553 // ⚠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.
554 }
555 if v > 1000+BG_PROFCLAMP { v = 1000+BG_PROFCLAMP }
556 if v < 1000-BG_PROFCLAMP { v = 1000-BG_PROFCLAMP }
557 return v
558}
559
560func main(argc: i64, argv: *i64) -> i64 {
561 let outp: *u8 = argv[1] as *u8
562 var H: i64 = BG_MAGIC_1750
563 if argc > 2 { H = bg_satoi(argv[2] as *u8) }
564 var RS: i64 = 28
565 if argc > 3 { RS = bg_satoi(argv[3] as *u8) }
566 var SUB: i64 = 4
567 if argc > 4 { SUB = bg_satoi(argv[4] as *u8) }
568 // relief amplitude scale, per-mille -- makes the anatomical surface relief a CONTROLLED VARIABLE
569 // measurable against the oracle, rather than a change assumed to help (1000 = as authored)
570 var RLF: i64 = 1000
571 if argc > 5 { RLF = bg_satoi(argv[5] as *u8) }
572 // ★SUBCUTANEOUS FAT (argv[7], per-mille): the layer between muscle and skin. It attenuates how much of
573 // the muscle definition reaches the SKIN surface -- lean (fat 0) shows the abs, heavy (fat high) smooths
574 // them -- so "abs hidden under fat" is EMERGENT from the layer stack, not a hack. Default lean-ish.
575 var FAT: i64 = 250
576 if argc > 7 { FAT = bg_satoi(argv[7] as *u8) }
577 // ★PROF (argv[8], per-mille): how much of the MEASURED section prior to apply. A CONTROLLED VARIABLE --
578 // 0 reproduces the pre-GX-31 elliptical body exactly, so the A/B is the same code path and any change in
579 // the bench is attributable to the profiles alone (the relief-scale knob earned this pattern in GX-24).
580 // ★DEFAULT OFF, and it stays off until the prior earns it: MEASURED, applying the section prior LOWERS
581 // the honest headline (361 -> 335) because it transfers low-frequency form while flattening the back,
582 // and it rings the neck. Shipping it on by default would have been a silent regression for every other
583 // caller. It stays available as an opt-in so the next rung can build on the measurement, not repeat it.
584 // ★★★DEFAULT 0 -> 250, EARNED 2026-07-30 BY BOTH INSTRUMENTS (seq1441/1448/1451). The rationale above
585 // was correct FOR THE BODY IT WAS WRITTEN AGAINST -- but that body measured 361 baseline and this one
586 // measures 319, so the emitter moved underneath the decision. Re-swept vs the cadaver oracle
587 // knowledge/skin.nxmesh (self_iou=self_shape=self_detail=1000 every run):
588 // PROF 0 -> headline 319 detail_head 155 shape 934
589 // PROF 250 -> headline 360 detail_head 164 shape 934 <-- INTERIOR OPTIMUM, +41, NO shape cost
590 // PROF 500 -> headline 343 detail_head 196 shape 934
591 // PROF 750 -> headline 338 detail_head 242 shape 923
592 // PROF 1000 -> headline 347 detail_head 289 shape 923
593 // ★★AND THE NECK RINGING IS REAL -- I RENDERED IT AND LOOKED, which is the half a bench cannot do.
594 // At PROF=1000 the body shows a pronounced COLLAR AT THE NECK plus hard seams at waist and knees and
595 // a bulbous seamed head: the original author's word 'rings' is exactly right, and their refusal to
596 // ship it on was CORRECT. At PROF=250 those artifacts are ABSENT and the render is INDISTINGUISHABLE
597 // from PROF=0 by eye, while scoring +41. ★So 250 is number-up AND eye-neutral; 1000 was number-up and
598 // eye-DOWN, which is the Goodhart this lane refuses. A rising number is not permission to ship -- a
599 // rising number WITH AN UNCHANGED EYE is.
600 // ⚠The ringing at high prior is an unfixed defect, not a reason to avoid the prior: fixing it is what
601 // would let the strong end of this curve (detail_head 289, +134) be taken. That is the next rung.
602 var PROF: i64 = 250
603 if argc > 8 { PROF = bg_satoi(argv[8] as *u8) }
604 // ★HF (argv[10], per-mille): apply only the measured HIGH-FREQUENCY residual. Independent of PROF so the
605 // two bands are never confounded -- the whole point of the four measurements that led here.
606 var HF: i64 = 0
607 if argc > 10 { HF = bg_satoi(argv[10] as *u8) }
608 // BAS (argv[11], per-mille): analytic shape-basis weight -- a controlled variable like every knob here
609 var BAS: i64 = 0
610 if argc > 11 { BAS = bg_satoi(argv[11] as *u8) }
611 let sinT: *i64 = sys_mmap(400*8) as *i64
612 bg_sin_fill(sinT)
613
614 // ---- CONTROL RINGS, per-mille of stature: y, xoff, zoff, rx, rz ----
615 let ry: *i64 = sys_mmap(BG_MAXRING*8) as *i64
616 let rx0: *i64 = sys_mmap(BG_MAXRING*8) as *i64
617 let rz0: *i64 = sys_mmap(BG_MAXRING*8) as *i64
618 let ra: *i64 = sys_mmap(BG_MAXRING*8) as *i64
619 let rb: *i64 = sys_mmap(BG_MAXRING*8) as *i64
620 // three consecutive station rings (slot 1 = the station being normalled) -> TRUE surface normals
621 // by CENTRAL difference in both surface parameters
622 let sx: *i64 = sys_mmap(3*BG_MAXRS*8) as *i64
623 let sz: *i64 = sys_mmap(3*BG_MAXRS*8) as *i64
624 let sy: *i64 = sys_mmap(4*8) as *i64
625 let scx: *i64 = sys_mmap(4*8) as *i64
626 let scz: *i64 = sys_mmap(4*8) as *i64
627 // vertex rings: c = current station, pp = previous station (quads are emitted between them)
628 let cpx: *i64 = sys_mmap(BG_MAXRS*8) as *i64
629 let cpy: *i64 = sys_mmap(BG_MAXRS*8) as *i64
630 let cpz: *i64 = sys_mmap(BG_MAXRS*8) as *i64
631 let ppy: *i64 = sys_mmap(BG_MAXRS*8) as *i64
632 let cnx: *i64 = sys_mmap(BG_MAXRS*8) as *i64
633 let cny: *i64 = sys_mmap(BG_MAXRS*8) as *i64
634 let cnz: *i64 = sys_mmap(BG_MAXRS*8) as *i64
635 let ppx: *i64 = sys_mmap(BG_MAXRS*8) as *i64
636 let ppz: *i64 = sys_mmap(BG_MAXRS*8) as *i64
637 let pnx: *i64 = sys_mmap(BG_MAXRS*8) as *i64
638 let pny: *i64 = sys_mmap(BG_MAXRS*8) as *i64
639 let pnz: *i64 = sys_mmap(BG_MAXRS*8) as *i64
640 let pStart: *i64 = sys_mmap(BG_MAXPART*8) as *i64
641 let pCount: *i64 = sys_mmap(BG_MAXPART*8) as *i64
642 let pMirror: *i64 = sys_mmap(BG_MAXPART*8) as *i64
643 // ★PART PLACEMENT. Every part is authored in its own canonical frame as a tube stacked along +Y, then
644 // ROTATED about X and offset into place. Without this a part can only run vertically, which is why the
645 // foot had to be faked as two forward-stretched rings on the end of the leg tube (it read as a blunt
646 // cone, and the error map put the feet among the worst regions). A real foot runs FORWARD, so it is a
647 // Y-tube rotated 90 degrees. Same mechanism gives fingers and toes. Existing parts use rot=0, off=0
648 // and are bit-identical to before.
649 let pRot: *i64 = sys_mmap(BG_MAXPART*8) as *i64
650 let pOx: *i64 = sys_mmap(BG_MAXPART*8) as *i64
651 let pOy: *i64 = sys_mmap(BG_MAXPART*8) as *i64
652 let pOz: *i64 = sys_mmap(BG_MAXPART*8) as *i64
653 let pMat: *i64 = sys_mmap(BG_MAXPART*8) as *i64
654 let pRotZ: *i64 = sys_mmap(BG_MAXPART*8) as *i64
655 let pRotY: *i64 = sys_mmap(BG_MAXPART*8) as *i64
656 let tf3: *i64 = sys_mmap(64) as *i64
657 var pz: i64 = 0
658 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 }
659 var n: i64 = 0
660 var np: i64 = 0
661
662 // ---- LOAD THE CANON (DATA-DRIVEN: the emitter carries NO geometry literals; the anthropometric canon --
663 // every ring and every relief feature -- lives in a data file. A different archetype (male, female,
664 // anime, monster) is a different canon fed to the SAME emit algorithm, and the canon itself is produced
665 // by a PROCEDURAL generator, not typed by hand.) ----
666 let fP: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
667 let fY: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
668 let fYw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
669 let fT: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
670 let fTw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
671 let fA: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
672 var canonp: *u8 = "knowledge/canon_male.dat" as *u8
673 if argc > 6 { canonp = argv[6] as *u8 }
674 let clen: *i64 = sys_mmap(16) as *i64
675 let cbuf: *u8 = sys_read_file(canonp, clen)
676 // ★FIXED 2026-07-30: this literal had UNESCAPED QUOTES -- bg_hw("{"error":...") -- so nx_cc read
677 // `error` as a bare identifier and REFUSED the whole module. nx_body_gen, the emitter behind every
678 // body AND the skull mesh, was NOT BUILDABLE FROM ITS OWN SOURCE; the live binary predates the line.
679 // The file already had the right idiom 350 lines further down (\x22), it just was not used here.
680 if (cbuf as i64) == 0 { bg_hw("{\x22error\x22:\x22cannot read canon file\x22}\n" as *u8); return 3 }
681 let counts: *i64 = sys_mmap(32) as *i64
682 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)
683 // ★OPENINGS (seq1387): parsed from the SAME buffer by its own walker, so bg_parse_canon's 24-arg
684 // signature and every caller of it stay untouched. Zero O rows => nopen[0]=0 => provably inert.
685 let oP: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
686 let oY: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
687 let oYw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
688 let oT: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
689 let oTw: *i64 = sys_mmap(BG_MAXFEAT*8) as *i64
690 let nopen: *i64 = sys_mmap(16) as *i64
691 nopen[0] = 0
692 bg_parse_open(cbuf, clen[0], oP, oY, oYw, oT, oTw, nopen)
693 np = counts[0]; n = counts[1]; var nf: i64 = counts[2]
694
695 // ---- MEASURED SECTION PROFILES (optional: absent file -> ns 0 -> every lookup returns 1000) ----
696 let sP: *i64 = sys_mmap(BG_MAXPROF*8) as *i64
697 let sY: *i64 = sys_mmap(BG_MAXPROF*8) as *i64
698 let sR: *i64 = sys_mmap(BG_MAXPROF*BG_NBMAX*8) as *i64
699 let nbp: *i64 = sys_mmap(16) as *i64
700 nbp[0] = 0
701 var ns: i64 = 0
702 var profp: *u8 = "knowledge/profile_human.dat" as *u8
703 if argc > 9 { profp = argv[9] as *u8 }
704 let plen: *i64 = sys_mmap(16) as *i64
705 let pbuf: *u8 = sys_read_file(profp, plen)
706 if (pbuf as i64) != 0 { ns = bg_parse_prof(pbuf, plen[0], sP, sY, sR, nbp) }
707 let nbins: i64 = nbp[0]
708 if nbins < 1 { ns = 0 }
709
710 // ---- emit ----
711 let tri: *i64 = sys_mmap(BG_MAXTRI*21*8) as *i64
712 // ★per-triangle MATERIAL colour (0-1000 each). Lets a feature (eyes) carry its OWN colour instead of the
713 // flat per-layer flesh -- the reason eyes were invisible skin-bumps. Written per tri at emit, read at write.
714 let tcR: *i64 = sys_mmap(BG_MAXTRI*8) as *i64
715 let tcG: *i64 = sys_mmap(BG_MAXTRI*8) as *i64
716 let tcB: *i64 = sys_mmap(BG_MAXTRI*8) as *i64
717 var nt: i64 = 0
718 let layCnt: *i64 = sys_mmap(BG_NLAYER*8) as *i64
719
720 // ★LAYER LOOP -- emit the body once per anatomical layer (bone, muscle, skin), nested inside each other.
721 var Lyr: i64 = 0
722 while Lyr < BG_NLAYER {
723 let lstart: i64 = nt
724 // per-layer radius scale + how much of the muscle relief this layer carries
725 // ★SKIN IS LAYER 0 so the renderer's skin shading (wrap-diffuse SSS + sheen, keyed on layer 0) lands
726 // on it; muscle=1, bone=2 sit inside. Intact z-buffers to the outermost (skin) regardless of order.
727 var rmul: i64 = 1000; var relmul: i64 = 1000
728 var lcR: i64 = 880; var lcG: i64 = 700; var lcB: i64 = 620 // skin flesh
729 if Lyr == 0 { rmul = 1000; relmul = 1000 - FAT } // SKIN: relief attenuated by fat
730 if Lyr == 1 { rmul = BG_MUSCF; relmul = 1000; lcR=760; lcG=286; lcB=262 } // MUSCLE (dark red)
731 if Lyr == 2 { rmul = BG_BONEF; relmul = 0; lcR=928; lcG=918; lcB=872 } // BONE (off-white)
732 if relmul < 0 { relmul = 0 }
733 var p: i64 = 0
734 while p < np {
735 var side: i64 = 0
736 var nsides: i64 = 1
737 if pMirror[p] == 1 { nsides = 2 }
738 while side < nsides {
739 var sgn: i64 = 1
740 if side == 1 { sgn = 0-1 }
741 // ★ONE GLOBAL STATION INDEX. Normals are CENTRAL differences in BOTH surface parameters --
742 // across stations as well as around the ring -- so they are shared by the quads either side
743 // and vary continuously. The previous one-sided within-band difference made the vertical
744 // tilt CONSTANT per band and step at every boundary: true normals, but visible bandaging.
745 let maxst: i64 = (pCount[p]-1)*SUB
746 let prc: i64 = sinT[bg_wrap(pRot[p]+90)]
747 let prs: i64 = sinT[bg_wrap(pRot[p])]
748 // ★Z-rotation (F1084): applied BEFORE the X-rotation at every transform site. The MIRRORED side
749 // gets the NEGATED angle so a left feature is the true mirror of the right one (a brow that
750 // tilts up-and-out on the right must tilt up-and-out on the left too). rotZ=0 gives pzc=Q14,
751 // pzs=0, and x*Q14/Q14 is integer-exact, so unrotated parts are BIT-IDENTICAL by construction.
752 let pzc: i64 = sinT[bg_wrap(sgn*pRotZ[p]+90)]
753 let pzs: i64 = sinT[bg_wrap(sgn*pRotZ[p])]
754 // ★AZIMUTH IS NEGATED ON THE MIRRORED SIDE, exactly as rotZ is -- mirroring flips X, so a limb
755 // swept BACK on the right must sweep BACK on the left, not forward. rotX is deliberately NOT
756 // negated: a forward pitch stays forward on both sides. Getting this wrong yields a body whose
757 // two halves rotate opposite ways, which reads as a twist rather than as a mirror.
758 let pyc: i64 = sinT[bg_wrap(sgn*pRotY[p]+90)]
759 let pys: i64 = sinT[bg_wrap(sgn*pRotY[p])]
760 var st: i64 = 0
761 while st <= maxst {
762 // evaluate the station ring at st-1, st, st+1 (clamped at the caps) into slots 0,1,2
763 var q: i64 = 0
764 while q < 3 {
765 var stq: i64 = st + q - 1
766 if stq < 0 { stq = 0 }
767 if stq > maxst { stq = maxst }
768 var cq: i64 = stq/SUB
769 if cq > pCount[p]-2 { cq = pCount[p]-2 }
770 let j0: i64 = pStart[p]+cq
771 let j1: i64 = j0+1
772 let fq: i64 = (stq - cq*SUB)*1000/SUB
773 // ★SUB-UNIT SAMPLING. Every station coordinate is computed to BG_PSUB sub-units with ONE
774 // division, instead of rounding to raw units first. The old code rounded y to whole units
775 // before the central-difference tangent, so on a SMALL part (the head is ~1/8 the body)
776 // where dy/station is only a few units, the rounding made uy alternate 6,8,6,8 -> the
777 // vertical tilt oscillated -> horizontal BANDING (proven in the normal map, and immune to
778 // relief and to raising PSUB, because y never went through PSUB). yri etc are per-mille*1000.
779 // Catmull-Rom through the ring and its neighbours (clamped at part ends) -- see bg_cmr.
780 var jm: i64 = j0-1
781 if jm < pStart[p] { jm = pStart[p] }
782 var jp: i64 = j1+1
783 if jp > pStart[p]+pCount[p]-1 { jp = pStart[p]+pCount[p]-1 }
784 let yri: i64 = bg_cmr(ry[jm], ry[j0], ry[j1], ry[jp], fq)
785 let xri: i64 = sgn*bg_cmr(rx0[jm], rx0[j0], rx0[j1], rx0[jp], fq)
786 let zri: i64 = bg_cmr(rz0[jm], rz0[j0], rz0[j1], rz0[jp], fq)
787 var ari: i64 = bg_cmr(ra[jm], ra[j0], ra[j1], ra[jp], fq)
788 var bri: i64 = bg_cmr(rb[jm], rb[j0], rb[j1], rb[jp], fq)
789 if ari < 0 { ari = 0 } // cubic overshoot must never go negative
790 if bri < 0 { bri = 0 }
791 let ymq: i64 = yri/1000
792 let xqs: i64 = xri*H*BG_PSUB/BG_MAGIC_1000000
793 let zqs: i64 = zri*H*BG_PSUB/BG_MAGIC_1000000
794 sy[q] = yri*H*BG_PSUB/BG_MAGIC_1000000
795 scx[q] = xqs
796 scz[q] = zqs
797 let aqs: i64 = ari*rmul/1000*H*BG_PSUB/BG_MAGIC_1000000
798 let bqs: i64 = bri*rmul/1000*H*BG_PSUB/BG_MAGIC_1000000
799 var i: i64 = 0
800 while i < RS {
801 let dq: i64 = bg_wrap(i*360/RS)
802 let cw: i64 = sinT[bg_wrap(dq+90)]; let sw: i64 = sinT[dq]
803 // anatomical relief modulates the radius per ANGLE as well as height, so the
804 // surface carries real features instead of being an ellipse of revolution. relmul is
805 // the fraction of that relief THIS layer carries (bone 0, muscle full, skin fat-attenuated).
806 var rq: i64 = 1000 + bg_relief(p, ymq, dq, fP,fY,fYw,fT,fTw,fA, nf)*RLF/1000*relmul/1000
807 if BAS != 0 { rq = rq + bg_shape_basis(p, ymq, dq, sinT)*BAS/1000*relmul/1000 }
808 // ★measured section prior. On a MIRRORED part the ring itself is not mirrored (only its
809 // centre offset is), so theta 0 stays +X and would put the limb's LATERAL profile on the
810 // medial side; flip the angle for side 1 so left and right are true mirrors.
811 if ns > 0 {
812 var pdq: i64 = dq
813 if sgn < 0 { pdq = bg_wrap(180-dq) }
814 if PROF != 0 {
815 let pv: i64 = bg_prof(p, ymq, pdq, sP, sY, sR, ns, nbins, 0)
816 rq = rq*(1000 + (pv-1000)*PROF/1000)/1000
817 }
818 if HF != 0 {
819 let hv: i64 = bg_prof(p, ymq, pdq, sP, sY, sR, ns, nbins, 1)
820 rq = rq*(1000 + (hv-1000)*HF/1000)/1000
821 }
822 }
823 sx[q*BG_MAXRS+i] = xqs + (aqs*rq/1000)*cw/BG_Q14
824 sz[q*BG_MAXRS+i] = zqs + (bqs*rq/1000)*sw/BG_Q14
825 i = i+1
826 }
827 q = q+1
828 }
829 let uy: i64 = sy[2]-sy[0]
830 var i: i64 = 0
831 while i < RS {
832 let ip: i64 = (i+1)%RS
833 let im: i64 = (i+RS-1)%RS
834 // dP/dtheta and dP/dstation, both central
835 let vx: i64 = sx[BG_MAXRS+ip]-sx[BG_MAXRS+im]
836 let vz: i64 = sz[BG_MAXRS+ip]-sz[BG_MAXRS+im]
837 let ux: i64 = sx[2*BG_MAXRS+i]-sx[i]
838 let uz: i64 = sz[2*BG_MAXRS+i]-sz[i]
839 var enx: i64 = uy*vz
840 var eny: i64 = uz*vx - ux*vz
841 var enz: i64 = 0-uy*vx
842 // keep the components squareable without throwing away the fine detail
843 var mx: i64 = enx; if mx < 0 { mx = 0-mx }
844 var m2: i64 = eny; if m2 < 0 { m2 = 0-m2 }
845 if m2 > mx { mx = m2 }
846 m2 = enz; if m2 < 0 { m2 = 0-m2 }
847 if m2 > mx { mx = m2 }
848 while mx > BG_NCAP { enx=enx/2; eny=eny/2; enz=enz/2; mx=mx/2 }
849 // outward = agrees with the ring-centre-to-vertex direction (relief included)
850 let ox: i64 = sx[BG_MAXRS+i]-scx[1]
851 let oz: i64 = sz[BG_MAXRS+i]-scz[1]
852 if (enx*ox + enz*oz) < 0 { enx = 0-enx; eny = 0-eny; enz = 0-enz }
853 var enl: i64 = bg_isqrt(enx*enx + eny*eny + enz*enz)
854 if enl < 1 { enl = 1 }
855 var wnx: i64 = enx*BG_Q14/enl
856 var wny: i64 = eny*BG_Q14/enl
857 var wnz: i64 = enz*BG_Q14/enl
858 var wpx: i64 = sx[BG_MAXRS+i]/BG_PSUB
859 var wpy: i64 = sy[1]/BG_PSUB
860 var wpz: i64 = sz[BG_MAXRS+i]/BG_PSUB
861 // place the part: rotate about Z, then about X, then offset (see PART PLACEMENT above)
862 bg_rot3(wpx,wpy,wpz, pzc,pzs,prc,prs,pyc,pys, tf3)
863 wpx = tf3[0]; wpy = tf3[1]; wpz = tf3[2]
864 wpy = wpy + pOy[p]*H/1000
865 wpz = wpz + pOz[p]*H/1000
866 wpx = wpx + sgn*pOx[p]*H/1000
867 bg_rot3(wnx,wny,wnz, pzc,pzs,prc,prs,pyc,pys, tf3)
868 wnx = tf3[0]; wny = tf3[1]; wnz = tf3[2]
869 cnx[i] = wnx; cny[i] = wny; cnz[i] = wnz
870 cpx[i] = wpx; cpy[i] = wpy; cpz[i] = wpz
871 i = i+1
872 }
873 let cy: i64 = sy[1]/BG_PSUB
874 // ★CAP THE TUBE ENDS. An open tube lets the camera see its own inner wall -- that was the
875 // bright flat trapezoid at the crotch: the torso tube's open bottom, viewed from inside.
876 // The cap faces along the tube AXIS (from the station-centre difference), so it works for
877 // the arm and leg tubes too, whose parameter runs downward rather than up.
878 var capend: i64 = 0
879 if st == 0 { capend = 1 }
880 if st == maxst { capend = 2 }
881 if capend > 0 {
882 var kx: i64 = scx[1]-scx[2]; var ky: i64 = sy[1]-sy[2]; var kz: i64 = scz[1]-scz[2]
883 if capend == 2 { kx = scx[1]-scx[0]; ky = sy[1]-sy[0]; kz = scz[1]-scz[0] }
884 var kl: i64 = bg_isqrt(kx*kx+ky*ky+kz*kz)
885 if kl < 1 { kl = 1 }
886 kx = kx*BG_Q14/kl; ky = ky*BG_Q14/kl; kz = kz*BG_Q14/kl
887 // a station ring is planar in xz, so its face normal is purely +/-y: pick the winding
888 // that agrees with the axis direction. Decided in the CANONICAL frame, before the
889 // placement rotation -- a rotation preserves orientation, so the choice still holds.
890 let kyc: i64 = ky
891 bg_rot3(kx,ky,kz, pzc,pzs,prc,prs,pyc,pys, tf3)
892 kx = tf3[0]; ky = tf3[1]; kz = tf3[2]
893 var ccx: i64 = scx[1]/BG_PSUB
894 var ccy: i64 = cy
895 var ccz: i64 = scz[1]/BG_PSUB
896 bg_rot3(ccx,ccy,ccz, pzc,pzs,prc,prs,pyc,pys, tf3)
897 ccx = tf3[0]; ccy = tf3[1]; ccz = tf3[2]
898 ccy = ccy + pOy[p]*H/1000
899 ccz = ccz + pOz[p]*H/1000
900 ccx = ccx + sgn*pOx[p]*H/1000
901 var e2: i64 = 0
902 while e2 < RS {
903 var v0: i64 = (e2+1)%RS
904 var v1: i64 = e2
905 if kyc < 0 { v0 = e2; v1 = (e2+1)%RS }
906 if nt < BG_MAXTRI-2 {
907 let t3: i64 = nt*21
908 tri[t3]=ccx; tri[t3+1]=ccy; tri[t3+2]=ccz
909 tri[t3+3]=cpx[v0]; tri[t3+4]=cpy[v0]; tri[t3+5]=cpz[v0]
910 tri[t3+6]=cpx[v1]; tri[t3+7]=cpy[v1]; tri[t3+8]=cpz[v1]
911 tri[t3+9]=kx; tri[t3+10]=ky; tri[t3+11]=kz
912 tri[t3+12]=kx; tri[t3+13]=ky; tri[t3+14]=kz
913 tri[t3+15]=kx; tri[t3+16]=ky; tri[t3+17]=kz
914 var cr3: i64 = lcR; var cg3: i64 = lcG; var cb3: i64 = lcB
915 if pMat[p] >= 1 { if pMat[p] <= 4 { cr3 = 958; cg3 = 954; cb3 = 942 } } // eye cap = sclera
916 tcR[nt]=cr3; tcG[nt]=cg3; tcB[nt]=cb3
917 nt = nt+1
918 }
919 e2 = e2+1
920 }
921 }
922 if st > 0 {
923 var e: i64 = 0
924 while e < RS {
925 let ep: i64 = (e+1)%RS
926 // per-quad material colour (flat per-layer, unless a feature material)
927 var qr: i64 = lcR; var qg: i64 = lcG; var qb: i64 = lcB
928 if pMat[p] >= 1 { if pMat[p] <= 4 { // EYE (material = iris colour 1..4)
929 qr = 958; qg = 954; qb = 942 // sclera white
930 var af: i64 = e*360/RS - 90; if af < 0 { af = 0-af } // 0 at the FRONT (+Z)
931 var sf: i64 = st - maxst/2; if sf < 0 { sf = 0-sf }
932 sf = sf*100/maxst // 0 at the eye equator
933 if af < 34 { if sf < 17 {
934 qr = 120; qg = 78; qb = 45 // 1 = brown (default)
935 if pMat[p] == 2 { qr = 86; qg = 122; qb = 170 } // 2 = blue
936 if pMat[p] == 3 { qr = 92; qg = 134; qb = 92 } // 3 = green
937 if pMat[p] == 4 { qr = 150; qg = 116; qb = 66 } // 4 = amber/hazel
938 }}
939 if af < 14 { if sf < 7 { qr = 24; qg = 22; qb = 24 } } // pupil (near-black)
940 }}
941 if pMat[p] == 5 { // FACE (head): colour by region
942 let ymil: i64 = cy*1000/H
943 var af2: i64 = e*360/RS - 90; if af2 < 0 { af2 = 0-af2 } // 0 at the FRONT (+Z)
944 // ★LIP PAINT RETIRED (F1093), same reasoning as the brow: the lips are now two
945 // real Z-rotated parts with a genuine groove between them, and a colour band
946 // calibrated for the HEAD's angular frame lands arbitrarily across them. The
947 // mouth is now a shadow cast by anatomy, not a rectangle drawn on an ovoid.
948 // ★BROW PAINT RETIRED (F1084 prereq 2). The brow is now a real Z-ROTATED PART, and
949 // paint fights geometry: this band is calibrated for the HEAD's angular frame, so
950 // on a small brow part it landed as black bars that read as goggles. A feature is
951 // either geometry or paint; once it is geometry the paint is a lie on top of it.
952 }
953 // ★★THE OPENING TEST. Skipping the quad removes the surface OUTRIGHT -- this is the
954 // thing a radius modulation structurally cannot do. Written as a flag rather than a
955 // wrapping block so the brace structure is UNCHANGED (a stray brace here would be a
956 // silent scoping change in the emitter's hottest loop).
957 var emit: i64 = 0
958 if nt < BG_MAXTRI-2 { emit = 1 }
959 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 } }
960 if emit == 1 {
961 let t1: i64 = nt*21
962 tri[t1]=ppx[e]; tri[t1+1]=ppy[e]; tri[t1+2]=ppz[e]
963 tri[t1+3]=cpx[e]; tri[t1+4]=cpy[e]; tri[t1+5]=cpz[e]
964 tri[t1+6]=cpx[ep];tri[t1+7]=cpy[ep]; tri[t1+8]=cpz[ep]
965 tri[t1+9]=pnx[e]; tri[t1+10]=pny[e]; tri[t1+11]=pnz[e]
966 tri[t1+12]=cnx[e]; tri[t1+13]=cny[e]; tri[t1+14]=cnz[e]
967 tri[t1+15]=cnx[ep];tri[t1+16]=cny[ep]; tri[t1+17]=cnz[ep]
968 tcR[nt]=qr; tcG[nt]=qg; tcB[nt]=qb
969 nt = nt+1
970 let t2: i64 = nt*21
971 tri[t2]=ppx[e]; tri[t2+1]=ppy[e]; tri[t2+2]=ppz[e]
972 tri[t2+3]=cpx[ep]; tri[t2+4]=cpy[ep]; tri[t2+5]=cpz[ep]
973 tri[t2+6]=ppx[ep]; tri[t2+7]=ppy[ep]; tri[t2+8]=ppz[ep]
974 tri[t2+9]=pnx[e]; tri[t2+10]=pny[e]; tri[t2+11]=pnz[e]
975 tri[t2+12]=cnx[ep]; tri[t2+13]=cny[ep]; tri[t2+14]=cnz[ep]
976 tri[t2+15]=pnx[ep]; tri[t2+16]=pny[ep]; tri[t2+17]=pnz[ep]
977 tcR[nt]=qr; tcG[nt]=qg; tcB[nt]=qb
978 nt = nt+1
979 }
980 e = e+1
981 }
982 }
983 var g: i64 = 0
984 while g < RS {
985 ppx[g]=cpx[g]; ppy[g]=cpy[g]; ppz[g]=cpz[g]
986 pnx[g]=cnx[g]; pny[g]=cny[g]; pnz[g]=cnz[g]
987 g = g+1
988 }
989 st = st+1
990 }
991 side = side+1
992 }
993 p = p+1
994 }
995 layCnt[Lyr] = nt - lstart
996 Lyr = Lyr+1
997 }
998
999 // (the head is now PART 4 above -- the ellipsoid-of-revolution cranium is gone, replaced by a ring-tube
1000 // that inherits true normals + the facial relief table + the crown cap, just like every other part.)
1001
1002 // ---- write NXMSH2 (3 LAYERS: bone, muscle, skin -- contiguous tri ranges) ----
1003 let hdr: i64 = 16 + BG_NLAYER*24
1004 let bytes: i64 = hdr + nt*84 + nt*4
1005 let buf: *u8 = sys_mmap(bytes + 64)
1006 buf[0]=78 as u8; buf[1]=88 as u8; buf[2]=77 as u8; buf[3]=83 as u8
1007 buf[4]=72 as u8; buf[5]=50 as u8; buf[6]=0 as u8; buf[7]=0 as u8
1008 bg_wr32(buf, 8, BG_NLAYER); bg_wr32(buf, 12, nt)
1009 // layer table: name[16] off4 cnt4, in emit order 0=bone 1=muscle 2=skin
1010 var lo: i64 = 0; var Lw: i64 = 0
1011 while Lw < BG_NLAYER {
1012 let lb: i64 = 16 + Lw*24
1013 var q: i64 = 0
1014 while q < 16 { buf[lb+q]=0 as u8; q=q+1 }
1015 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"
1016 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"
1017 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"
1018 bg_wr32(buf, lb+16, lo); bg_wr32(buf, lb+20, layCnt[Lw])
1019 lo = lo + layCnt[Lw]
1020 Lw = Lw+1
1021 }
1022 // per-tri geometry + per-TRIANGLE material colour (from the emit; carries feature colours like the eyes).
1023 let l0: i64 = layCnt[0]; let l1: i64 = layCnt[0]+layCnt[1]
1024 var t: i64 = 0
1025 while t < nt {
1026 let o: i64 = hdr + t*84
1027 var j: i64 = 0
1028 while j < 9 { bg_wr32(buf, o+j*4, bg_f32(tri[t*21+j], 1)); j=j+1 }
1029 while j < 18 { bg_wr32(buf, o+j*4, bg_f32(tri[t*21+j], BG_Q14)); j=j+1 }
1030 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))
1031 t = t+1
1032 }
1033 // per-tri layer id (0=skin, 1=muscle, 2=bone)
1034 var z: i64 = 0
1035 while z < nt {
1036 var lid: i64 = 2
1037 if z < l0 { lid = 0 } else { if z < l1 { lid = 1 } }
1038 bg_wr32(buf, hdr + nt*84 + z*4, lid); z=z+1
1039 }
1040 let fd: i64 = sys_openat_wr(outp, 420)
1041 sys_write(fd, buf, bytes)
1042 sys_close(fd)
1043
1044 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)
1045 bg_hw(",\x22height\x22:" as *u8); bg_pn(H)
1046 bg_hw(",\x22parts\x22:" as *u8); bg_pn(np)
1047 bg_hw(",\x22control_rings\x22:" as *u8); bg_pn(n)
1048 bg_hw(",\x22radial\x22:" as *u8); bg_pn(RS)
1049 bg_hw(",\x22sub\x22:" as *u8); bg_pn(SUB)
1050 bg_hw(",\x22tris\x22:" as *u8); bg_pn(nt)
1051 // ★TRUNCATION MUST BE LOUD (F1083). The two raster guards silently STOP emitting at BG_MAXTRI, which is
1052 // the documented root of the \x22red face\x22 bug: the skin layer is emitted LAST, so a full budget drops the
1053 // head/hands and they render as the muscle layer underneath -- a wrong body with a clean exit code. Same
1054 // silent-failure class as the unguarded part array. Report the headroom always, and FAIL LOUD at the cap.
1055 bg_hw(",\x22tri_cap\x22:" as *u8); bg_pn(BG_MAXTRI)
1056 bg_hw(",\x22tri_headroom\x22:" as *u8); bg_pn(BG_MAXTRI - nt)
1057 var trunc: i64 = 0
1058 if nt >= BG_MAXTRI - 4 { trunc = 1 }
1059 bg_hw(",\x22truncated\x22:" as *u8); bg_pn(trunc)
1060 bg_hw(",\x22prof_rows\x22:" as *u8); bg_pn(ns)
1061 // ★OFFERED vs ACCEPTED. Equal = the whole file loaded. Greater = the cap ate rows and every number below
1062 // is measured on PARTIAL data -- publish it rather than let a truncated prior look like a measured one.
1063 bg_hw(",\x22prof_rows_offered\x22:" as *u8); bg_pn(nbp[1])
1064 var proftrunc: i64 = 0
1065 if nbp[1] > ns { proftrunc = 1 }
1066 bg_hw(",\x22prof_truncated\x22:" as *u8); bg_pn(proftrunc)
1067 bg_hw(",\x22prof_scale\x22:" as *u8); bg_pn(PROF)
1068 bg_hw("}\n" as *u8)
1069 return 0
1070}