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