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