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