nx_nxa_pect.nx source
↩ module page · 546 lines · 25530 B
1// nx_nxa_pect.nx -- GIVE THE SOFT TISSUE ITS BONES: append the lateral pectoral pair to an NXA
2// and re-bind the bust vertices to it.
3//
4// WHY THIS EXISTS. The reference-figure rig carries lPectoral/rPectoral as first-class bones
5// (measured 2026-08-03 from morph formula targets across three independent packages); our rig has
6// 104 joints and ZERO off-midline at chest height, which is the root cause behind the vertex-band
7// soft-tissue workaround (debt 1785798366). The DYNA section already declares MEASURED lateral
8// anchors (nx_nxa_dyna derives them from the mesh's own bust-band centroids); this organ promotes
9// those declared anchors into actual SKEL bones with skinning weights, so the engine can drive a
10// bone instead of faking one at the vertices.
11//
12// ★THE TRAP THIS DESIGN AVOIDS (found by reading the consumer BEFORE building): the page runtime
13// has NO joint hierarchy -- every joint poses from its OWN absolute ANIM track, and a joint with
14// no track renders the IDENTITY transform. Bones appended naively would leave a FROZEN CHEST PATCH
15// on an animating body. So each new bone receives a byte-copy of its donor chest joint's track(s)
16// with the header jointIdx rewritten: copied (R, dt) applied at the new bone's OWN bind position is
17// exactly rigid attachment under the consumer's dual-quat T = b + dt - D*b.
18//
19// FAIL-CLOSED MEASUREMENTS, NEVER ASSUMPTIONS:
20// - the SKIN weight layout (planar [i,i,i,i,w,w,w,w] vs interleaved [i,w,i,w..]) and the weight
21// basis (the constant per-vertex sum) are DETECTED from the data and REFUSED if ambiguous;
22// - every ANIM track must be chn==2 (the one layout the consumer parses) or we refuse;
23// - a rig that already carries an off-midline bust-band joint is REFUSED (idempotence: run twice
24// and the second run tells you it is done rather than doubling bones);
25// - all checksums are RECOMPUTED, never copied (nx_nxa_dyna's law: a copied checksum would
26// validate its own copy bug).
27//
28// nx_nxa_pect <in.nxa> <out.nxa> (input never modified)
29//
30// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
31import "nx_syscalls.nx"
32import "nx_nxa.nx"
33const PT_MAGIC_4096: i64 = 4096
34
35const PT_HDR: i64 = 32
36const PT_TOCE: i64 = 32
37const PT_MAXSEC: i64 = 64
38const PT_MODE: i64 = 0x1a4
39const PT_EXIT_USAGE: i64 = 2
40const PT_EXIT_BAD: i64 = 3
41const PT_EXIT_IO: i64 = 1
42const PT_EXIT_DONE: i64 = 4
43// a joint this far off the midline in the bust band = the pair already exists (the same 3000
44// threshold the page's chj classifier uses -- shared meaning, one number)
45const PT_MIDLINE: i64 = 3000
46// ...but ONLY in the FRONT hemisphere (bind.y beyond -1200, the page's own chj discriminator).
47// Without this the idempotence check matches ARM-CHAIN joints passing through bust height at the
48// sides -- the first run against the real asset refused as "already done" on an arm joint, which
49// is exactly the count-without-a-discriminator class the floor gate banked on 08-02.
50const PT_FRONT_Y: i64 = 0 - 1200
51// bust band in permil of the VERTEX span -- identical to nx_nxa_dyna's band so the two organs
52// can never disagree about where the bust is
53const PT_NIP_PERMIL: i64 = 744
54const PT_BAND_PERMIL: i64 = 40
55// share of the weight basis granted to the new bone at the anchor centre, tapering linearly to 0
56// at the influence radius. 400 permil at centre keeps the majority of the original binding, so
57// deformation stays anchored to the body while the new bone gains real authority over the apex.
58const PT_FMAX_PERMIL: i64 = 400
59const PT_SKELREC: i64 = 64
60const PT_SKIN_WPV: i64 = 8
61
62func pt_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
63func pt_werr(s: *u8) -> i64 { sys_write(2, s, pt_slen(s)); return 0 }
64func pt_out(s: *u8) -> i64 { sys_write(1, s, pt_slen(s)); return 0 }
65func pt_num(v: i64) -> i64 {
66 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
67 var m: i64 = v
68 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
69 let t: *u8 = sys_mmap(32)
70 var k: i64 = 0
71 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
72 let o: *u8 = sys_mmap(32)
73 var i: i64 = 0
74 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
75 sys_write(1, o, k)
76 return 0
77}
78func pt_rd64(b: *u8, off: i64) -> i64 {
79 var v: i64 = 0
80 var i: i64 = 7
81 while i >= 0 { v = v*256 + ((b[off + i] & 0xff) as i64); i = i - 1 }
82 return v
83}
84func pt_wr64(b: *u8, off: i64, v: i64) -> i64 {
85 var m: i64 = v
86 var i: i64 = 0
87 while i < 8 { b[off + i] = (m & 0xff) as u8; m = m >> 8; i = i + 1 }
88 return 0
89}
90func pt_tageq(b: *u8, off: i64, t: *u8) -> i64 {
91 var i: i64 = 0
92 while i < 4 { if b[off + i] != t[i] { return 0 } i = i + 1 }
93 return 1
94}
95func pt_isqrt(v: i64) -> i64 {
96 if v <= 0 { return 0 }
97 var x: i64 = v
98 var y: i64 = (x + 1) / 2
99 while y < x { x = y; y = (x + v/x) / 2 }
100 return x
101}
102func pt_die(msg: *u8, code: i64) -> i64 {
103 pt_werr(msg)
104 sys_exit(code)
105 return code
106}
107
108func main(argc: i64, argv: *i64) -> i64 {
109 if argc < 3 {
110 pt_werr("usage: nx_nxa_pect <in.nxa> <out.nxa>\n" as *u8)
111 pt_werr(" promotes the DYNA lateral anchors into real SKEL bones with ANIM tracks and skin\n" as *u8)
112 pt_werr(" weights. Input never modified; a rig that already has the pair is refused (exit 4).\n" as *u8)
113 sys_exit(PT_EXIT_USAGE)
114 return PT_EXIT_USAGE
115 }
116 let inp: *u8 = argv[1] as *u8
117 let outp: *u8 = argv[2] as *u8
118 let lp: *i64 = sys_mmap(16) as *i64
119 let b: *u8 = sys_read_file(inp, lp)
120 if (b as i64) == 0 { return pt_die("PECT-RED cannot read input\n" as *u8, PT_EXIT_IO) }
121 let flen: i64 = lp[0]
122 if flen < PT_HDR { return pt_die("PECT-RED too short for NXA\n" as *u8, PT_EXIT_BAD) }
123 if pt_rd64(b, 0) != nxa_magic() { return pt_die("PECT-RED bad magic\n" as *u8, PT_EXIT_BAD) }
124 if pt_rd64(b, 8) > NXA_VER { return pt_die("PECT-RED future version -- refusing\n" as *u8, PT_EXIT_BAD) }
125 let ns: i64 = pt_rd64(b, 16)
126 if ns < 1 { return pt_die("PECT-RED bad section count\n" as *u8, PT_EXIT_BAD) }
127 if ns >= PT_MAXSEC { return pt_die("PECT-RED section table full\n" as *u8, PT_EXIT_BAD) }
128
129 // ---- locate the five required sections (byte offsets + word lengths + toc index) ----
130 var vo: i64 = 0 - 1
131 var so: i64 = 0 - 1
132 var ko: i64 = 0 - 1
133 var ao: i64 = 0 - 1
134 var dyo: i64 = 0 - 1
135 var si_skel: i64 = 0 - 1
136 var si_skin: i64 = 0 - 1
137 var si_anim: i64 = 0 - 1
138 var s: i64 = 0
139 while s < ns {
140 let e: i64 = PT_HDR + s*PT_TOCE
141 if pt_tageq(b, e, "VERT" as *u8) == 1 { vo = pt_rd64(b, e + 8) }
142 if pt_tageq(b, e, "SKEL" as *u8) == 1 { so = pt_rd64(b, e + 8); si_skel = s }
143 if pt_tageq(b, e, "SKIN" as *u8) == 1 { ko = pt_rd64(b, e + 8); si_skin = s }
144 if pt_tageq(b, e, "ANIM" as *u8) == 1 { ao = pt_rd64(b, e + 8); si_anim = s }
145 if pt_tageq(b, e, "DYNA" as *u8) == 1 { dyo = pt_rd64(b, e + 8) }
146 s = s + 1
147 }
148 if vo < 0 { return pt_die("PECT-RED no VERT\n" as *u8, PT_EXIT_BAD) }
149 if so < 0 { return pt_die("PECT-RED no SKEL\n" as *u8, PT_EXIT_BAD) }
150 if ko < 0 { return pt_die("PECT-RED no SKIN\n" as *u8, PT_EXIT_BAD) }
151 if ao < 0 { return pt_die("PECT-RED no ANIM\n" as *u8, PT_EXIT_BAD) }
152 if dyo < 0 { return pt_die("PECT-RED no DYNA -- run nx_nxa_dyna first, the anchors are ITS measurement\n" as *u8, PT_EXIT_BAD) }
153
154 let nv: i64 = pt_rd64(b, vo)
155 let nj: i64 = pt_rd64(b, so)
156 let nk: i64 = pt_rd64(b, ko)
157 if nk != nv { return pt_die("PECT-RED SKIN count != VERT count\n" as *u8, PT_EXIT_BAD) }
158
159 // ---- vertex span (same basis as nx_nxa_dyna and the page: the VERTEX span, never joints) ----
160 var zmin: i64 = pt_rd64(b, vo + 8 + 2*8)
161 var zmax: i64 = zmin
162 var i: i64 = 0
163 while i < nv {
164 let vz: i64 = pt_rd64(b, vo + 8 + (i*3 + 2)*8)
165 if vz < zmin { zmin = vz }
166 if vz > zmax { zmax = vz }
167 i = i + 1
168 }
169 let span: i64 = zmax - zmin
170 if span <= 0 { return pt_die("PECT-RED degenerate span\n" as *u8, PT_EXIT_BAD) }
171 let plo: i64 = PT_NIP_PERMIL - PT_BAND_PERMIL
172 let phi: i64 = PT_NIP_PERMIL + PT_BAND_PERMIL
173
174 // ---- scan mode: `nx_nxa_pect <in> --scan` lists every front off-midline bust-band joint
175 // instead of transforming. Exists because the first real run refused on joint 76 (x=8757,
176 // y=-9817, 763 permil) -- a joint THREE instruments had missed: the floor gate demands a
177 // PAIR, the page's chj classifier is skipped whenever DYNA exists, and my own priors came
178 // from both. Enumerate before concluding. ----
179 var scanmode: i64 = 0
180 if outp[0] == 45 { if outp[1] == 45 { scanmode = 1 } }
181 if scanmode == 1 {
182 pt_out("PECT-SCAN nj=" as *u8); pt_num(nj)
183 pt_out(" span=" as *u8); pt_num(span)
184 pt_out(" band permil " as *u8); pt_num(plo); pt_out(".." as *u8); pt_num(phi); pt_out("\n" as *u8)
185 var js: i64 = 0
186 while js < nj {
187 let sb2: i64 = so + 8 + js*PT_SKELREC
188 let sx: i64 = pt_rd64(b, sb2 + 8)
189 let sy: i64 = pt_rd64(b, sb2 + 16)
190 let sz: i64 = pt_rd64(b, sb2 + 24)
191 var sax: i64 = sx
192 if sax < 0 { sax = 0 - sax }
193 let sp9: i64 = (sz - zmin)*1000/span
194 if sax > PT_MIDLINE { if sy < PT_FRONT_Y { if sp9 >= plo { if sp9 <= phi {
195 pt_out(" joint " as *u8); pt_num(js)
196 pt_out(" parent=" as *u8); pt_num(pt_rd64(b, sb2))
197 pt_out(" x=" as *u8); pt_num(sx)
198 pt_out(" y=" as *u8); pt_num(sy)
199 pt_out(" z=" as *u8); pt_num(sz)
200 pt_out(" permil=" as *u8); pt_num(sp9)
201 // is the bone WIRED or decorative? count skin references (either layout: a
202 // matching index in any of the 4 slots, weight from the paired lane) and ANIM
203 // track presence -- a bone nothing follows and nothing animates is furniture.
204 var refs: i64 = 0
205 var wsum: i64 = 0
206 var v9: i64 = 0
207 while v9 < nv {
208 let kb9: i64 = ko + 8 + v9*PT_SKIN_WPV*8
209 var q9: i64 = 0
210 while q9 < 4 {
211 let iv9: i64 = pt_rd64(b, kb9 + q9*8)
212 let ivI: i64 = pt_rd64(b, kb9 + (q9*2)*8)
213 if iv9 == js { refs = refs + 1; wsum = wsum + pt_rd64(b, kb9 + (4 + q9)*8) }
214 if ivI == js { if q9*2 != 0 { if ivI != iv9 { refs = refs + 1; wsum = wsum + pt_rd64(b, kb9 + (q9*2 + 1)*8) } } }
215 q9 = q9 + 1
216 }
217 v9 = v9 + 1
218 }
219 var trks: i64 = 0
220 let ntrk9: i64 = pt_rd64(b, ao)
221 var tw9: i64 = ao + 8
222 var t9: i64 = 0
223 while t9 < ntrk9 {
224 let tj9: i64 = pt_rd64(b, tw9)
225 let nk9: i64 = pt_rd64(b, tw9 + 16)
226 if tj9 == js { trks = trks + 1 }
227 tw9 = tw9 + 24 + nk9*16
228 t9 = t9 + 1
229 }
230 pt_out(" skin_refs=" as *u8); pt_num(refs)
231 pt_out(" wsum=" as *u8); pt_num(wsum)
232 pt_out(" anim_tracks=" as *u8); pt_num(trks)
233 pt_out("\n" as *u8)
234 } } } }
235 js = js + 1
236 }
237 sys_exit(0)
238 return 0
239 }
240
241 // ---- idempotence: an off-midline joint already in the bust band means the pair exists ----
242 var j: i64 = 0
243 while j < nj {
244 let jb: i64 = so + 8 + j*PT_SKELREC
245 let jx: i64 = pt_rd64(b, jb + 8)
246 let jy: i64 = pt_rd64(b, jb + 16)
247 let jz: i64 = pt_rd64(b, jb + 24)
248 var ax9: i64 = jx
249 if ax9 < 0 { ax9 = 0 - ax9 }
250 let jp: i64 = (jz - zmin)*1000/span
251 if ax9 > PT_MIDLINE { if jy < PT_FRONT_Y { if jp >= plo { if jp <= phi {
252 pt_werr("PECT-DONE the rig already carries a FRONT off-midline bust-band joint -- nothing to add\n" as *u8)
253 pt_out(" triggering joint " as *u8); pt_num(j)
254 pt_out(" bind x=" as *u8); pt_num(jx)
255 pt_out(" y=" as *u8); pt_num(jy)
256 pt_out(" z=" as *u8); pt_num(jz)
257 pt_out(" permil=" as *u8); pt_num(jp)
258 pt_out("\n" as *u8)
259 sys_exit(PT_EXIT_DONE)
260 return PT_EXIT_DONE
261 } } } }
262 j = j + 1
263 }
264
265 // ---- anchors + influence from DYNA (rows: side,x,y,z,k,c,max,infl,falloff,axis,ks,cs) ----
266 let ndb: i64 = pt_rd64(b, dyo)
267 let dstr: i64 = pt_rd64(b, dyo + 8)
268 var lax: i64 = 0
269 var lay: i64 = 0
270 var laz: i64 = 0
271 var rax: i64 = 0
272 var ray: i64 = 0
273 var raz: i64 = 0
274 var infl: i64 = 0
275 var seenL: i64 = 0
276 var seenR: i64 = 0
277 var d: i64 = 0
278 while d < ndb {
279 let rb: i64 = dyo + 16 + d*dstr*8
280 let side: i64 = pt_rd64(b, rb)
281 if side < 0 { lax = pt_rd64(b, rb + 8); lay = pt_rd64(b, rb + 16); laz = pt_rd64(b, rb + 24); infl = pt_rd64(b, rb + 56); seenL = 1 }
282 if side > 0 { rax = pt_rd64(b, rb + 8); ray = pt_rd64(b, rb + 16); raz = pt_rd64(b, rb + 24); seenR = 1 }
283 d = d + 1
284 }
285 if seenL == 0 { return pt_die("PECT-RED DYNA has no left row\n" as *u8, PT_EXIT_BAD) }
286 if seenR == 0 { return pt_die("PECT-RED DYNA has no right row\n" as *u8, PT_EXIT_BAD) }
287 if infl <= 0 { return pt_die("PECT-RED DYNA influence radius invalid\n" as *u8, PT_EXIT_BAD) }
288
289 // ---- donor joint: the MIDLINE joint nearest the anchor midpoint (the chest chain member the
290 // tissue actually hangs from). Its track gives the new bones their motion; its record tail
291 // gives them their unread-but-present fields. ----
292 let mx: i64 = (lax + rax)/2
293 let my: i64 = (lay + ray)/2
294 let mz: i64 = (laz + raz)/2
295 var donor: i64 = 0 - 1
296 var best: i64 = 0
297 j = 0
298 while j < nj {
299 let jb2: i64 = so + 8 + j*PT_SKELREC
300 let jx2: i64 = pt_rd64(b, jb2 + 8)
301 var axm: i64 = jx2
302 if axm < 0 { axm = 0 - axm }
303 if axm <= PT_MIDLINE {
304 let dy2: i64 = pt_rd64(b, jb2 + 16) - my
305 let dz2: i64 = pt_rd64(b, jb2 + 24) - mz
306 let dx2: i64 = jx2 - mx
307 let dd: i64 = dx2*dx2 + dy2*dy2 + dz2*dz2
308 if donor < 0 { donor = j; best = dd }
309 if dd < best { donor = j; best = dd }
310 }
311 j = j + 1
312 }
313 if donor < 0 { return pt_die("PECT-RED no midline joint found to donate motion\n" as *u8, PT_EXIT_BAD) }
314
315 // ---- SKIN layout detection, fail-closed. Two candidate layouts for the 8 words/vertex:
316 // planar [i0 i1 i2 i3 w0 w1 w2 w3] or interleaved [i0 w0 i1 w1 ...]. The true layout is the
317 // one where every index lane is < nj AND the weight lanes sum to the SAME constant on every
318 // sampled vertex. Ambiguity or neither = refuse; guessed weights corrupt silently. ----
319 var okP: i64 = 1
320 var okI: i64 = 1
321 var basP: i64 = 0 - 1
322 var basI: i64 = 0 - 1
323 var t2: i64 = 0
324 while t2 < 64 {
325 let vv: i64 = (t2 * (nv/64 + 1)) % nv
326 let rb2: i64 = ko + 8 + vv*PT_SKIN_WPV*8
327 var sumP: i64 = 0
328 var sumI: i64 = 0
329 var w2: i64 = 0
330 while w2 < 8 {
331 let wv: i64 = pt_rd64(b, rb2 + w2*8)
332 if w2 < 4 { if wv >= nj { okP = 0 } if wv < 0 { okP = 0 } }
333 if w2 >= 4 { sumP = sumP + wv }
334 if w2 % 2 == 0 { if wv >= nj { okI = 0 } if wv < 0 { okI = 0 } }
335 if w2 % 2 == 1 { sumI = sumI + wv }
336 w2 = w2 + 1
337 }
338 if basP < 0 { basP = sumP }
339 if sumP != basP { okP = 0 }
340 if basI < 0 { basI = sumI }
341 if sumI != basI { okI = 0 }
342 t2 = t2 + 1
343 }
344 if okP == 1 { if okI == 1 { return pt_die("PECT-RED SKIN layout ambiguous (both fit) -- refusing to guess\n" as *u8, PT_EXIT_BAD) } }
345 if okP == 0 { if okI == 0 { return pt_die("PECT-RED SKIN layout unrecognized -- refusing to guess\n" as *u8, PT_EXIT_BAD) } }
346 var basis: i64 = basP
347 if okI == 1 { basis = basI }
348 if basis <= 0 { return pt_die("PECT-RED weight basis nonpositive\n" as *u8, PT_EXIT_BAD) }
349
350 // ---- ANIM walk: verify every track is chn==2 and find the donor's tracks ----
351 let ntrk: i64 = pt_rd64(b, ao)
352 var tw: i64 = ao + 8
353 var donorBytes: i64 = 0
354 var donorTrks: i64 = 0
355 var t3: i64 = 0
356 while t3 < ntrk {
357 let tj: i64 = pt_rd64(b, tw)
358 let chn: i64 = pt_rd64(b, tw + 8)
359 let nk2: i64 = pt_rd64(b, tw + 16)
360 if chn != 2 { return pt_die("PECT-RED ANIM track chn != 2 -- layout this organ does not know; refusing\n" as *u8, PT_EXIT_BAD) }
361 let tlen: i64 = 24 + nk2*16
362 if tj == donor { donorBytes = donorBytes + tlen; donorTrks = donorTrks + 1 }
363 tw = tw + tlen
364 }
365 if donorTrks == 0 { return pt_die("PECT-RED donor joint has no ANIM track -- copied motion impossible; pick failed\n" as *u8, PT_EXIT_BAD) }
366
367 // ---- assemble the output ----
368 let e_sk: i64 = PT_HDR + si_skel*PT_TOCE
369 let e_ki: i64 = PT_HDR + si_skin*PT_TOCE
370 let e_an: i64 = PT_HDR + si_anim*PT_TOCE
371 let wl_sk: i64 = pt_rd64(b, e_sk + 16) + 2*(PT_SKELREC/8)
372 let wl_ki: i64 = pt_rd64(b, e_ki + 16)
373 let wl_an: i64 = pt_rd64(b, e_an + 16) + 2*(donorBytes/8)
374 var total: i64 = PT_HDR + ns*PT_TOCE
375 var s4: i64 = 0
376 while s4 < ns {
377 let e4: i64 = PT_HDR + s4*PT_TOCE
378 var wl4: i64 = pt_rd64(b, e4 + 16)
379 if s4 == si_skel { wl4 = wl_sk }
380 if s4 == si_anim { wl4 = wl_an }
381 total = total + wl4*8
382 s4 = s4 + 1
383 }
384 let nb: *u8 = sys_mmap(total + PT_MAGIC_4096)
385 if (nb as i64) == 0 { return pt_die("PECT-RED cannot allocate output\n" as *u8, PT_EXIT_IO) }
386 pt_wr64(nb, 0, nxa_magic())
387 pt_wr64(nb, 8, NXA_VER)
388 pt_wr64(nb, 16, ns)
389
390 var reL: i64 = 0
391 var reR: i64 = 0
392 var wo: i64 = PT_HDR + ns*PT_TOCE
393 var s5: i64 = 0
394 while s5 < ns {
395 let e5: i64 = PT_HDR + s5*PT_TOCE
396 let oldoff: i64 = pt_rd64(b, e5 + 8)
397 let oldwl: i64 = pt_rd64(b, e5 + 16)
398 let te: i64 = PT_HDR + s5*PT_TOCE
399 pt_wr64(nb, te, pt_rd64(b, e5))
400 pt_wr64(nb, te + 8, wo)
401 var newwl: i64 = oldwl
402 // default: byte-copy the section
403 var c9: i64 = 0
404 while c9 < oldwl*8 { nb[wo + c9] = b[oldoff + c9]; c9 = c9 + 1 }
405 if s5 == si_skel {
406 // count -> nj+2, then two appended 64B records: parent=donor, bind=anchor, tail
407 // copied from the donor so unread-but-present fields stay sane for other consumers
408 newwl = wl_sk
409 pt_wr64(nb, wo, nj + 2)
410 let dsrc: i64 = oldoff + 8 + donor*PT_SKELREC
411 var bn: i64 = 0
412 while bn < 2 {
413 let dst: i64 = wo + 8 + (nj + bn)*PT_SKELREC
414 var cc: i64 = 0
415 while cc < PT_SKELREC { nb[dst + cc] = b[dsrc + cc]; cc = cc + 1 }
416 pt_wr64(nb, dst, donor)
417 if bn == 0 { pt_wr64(nb, dst + 8, lax); pt_wr64(nb, dst + 16, lay); pt_wr64(nb, dst + 24, laz) }
418 if bn == 1 { pt_wr64(nb, dst + 8, rax); pt_wr64(nb, dst + 16, ray); pt_wr64(nb, dst + 24, raz) }
419 bn = bn + 1
420 }
421 }
422 if s5 == si_anim {
423 // count -> ntrk + 2*donorTrks, appended copies with jointIdx rewritten
424 newwl = wl_an
425 pt_wr64(nb, wo, ntrk + 2*donorTrks)
426 var ap: i64 = wo + (oldwl*8)
427 var srcw: i64 = oldoff + 8
428 var t5: i64 = 0
429 while t5 < ntrk {
430 let tj5: i64 = pt_rd64(b, srcw)
431 let nk5: i64 = pt_rd64(b, srcw + 16)
432 let tl5: i64 = 24 + nk5*16
433 if tj5 == donor {
434 var bn2: i64 = 0
435 while bn2 < 2 {
436 var cc2: i64 = 0
437 while cc2 < tl5 { nb[ap + cc2] = b[srcw + cc2]; cc2 = cc2 + 1 }
438 pt_wr64(nb, ap, nj + bn2)
439 ap = ap + tl5
440 bn2 = bn2 + 1
441 }
442 }
443 srcw = srcw + tl5
444 t5 = t5 + 1
445 }
446 }
447 if s5 == si_skin {
448 // in-place reweight of the copy: bust-band front vertices inside the influence radius
449 // of their side's anchor lend PT_FMAX weight (tapered) to the new bone. Kept slots are
450 // scaled and the new bone receives EXACTLY basis - sum(kept): the per-vertex sum is
451 // preserved by construction, not by rounding luck.
452 var v6: i64 = 0
453 while v6 < nv {
454 let px6: i64 = pt_rd64(b, vo + 8 + (v6*3)*8)
455 let py6: i64 = pt_rd64(b, vo + 8 + (v6*3 + 1)*8)
456 let pz6: i64 = pt_rd64(b, vo + 8 + (v6*3 + 2)*8)
457 if py6 < 0 {
458 var ax7: i64 = lax
459 var ay7: i64 = lay
460 var az7: i64 = laz
461 var bone: i64 = nj
462 if px6 > 0 { ax7 = rax; ay7 = ray; az7 = raz; bone = nj + 1 }
463 let dx7: i64 = px6 - ax7
464 let dy7: i64 = py6 - ay7
465 let dz7: i64 = pz6 - az7
466 let dist: i64 = pt_isqrt(dx7*dx7 + dy7*dy7 + dz7*dz7)
467 if dist < infl {
468 let take: i64 = basis * PT_FMAX_PERMIL * (infl - dist) / (infl * 1000)
469 if take > 0 {
470 let rb7: i64 = wo + 8 + v6*PT_SKIN_WPV*8
471 // find the smallest-weight slot; scale the other three
472 var slot: i64 = 0
473 var wmin: i64 = 0
474 var q7: i64 = 0
475 while q7 < 4 {
476 var woff: i64 = rb7 + (4 + q7)*8
477 if okI == 1 { woff = rb7 + (q7*2 + 1)*8 }
478 let wv7: i64 = pt_rd64(nb, woff)
479 if q7 == 0 { wmin = wv7 }
480 if wv7 < wmin { wmin = wv7; slot = q7 }
481 if q7 == 0 { slot = 0 }
482 q7 = q7 + 1
483 }
484 var kept: i64 = 0
485 var q8: i64 = 0
486 while q8 < 4 {
487 if q8 != slot {
488 var woff2: i64 = rb7 + (4 + q8)*8
489 if okI == 1 { woff2 = rb7 + (q8*2 + 1)*8 }
490 let oldw: i64 = pt_rd64(nb, woff2)
491 let neww: i64 = oldw * (basis - take) / basis
492 pt_wr64(nb, woff2, neww)
493 kept = kept + neww
494 }
495 q8 = q8 + 1
496 }
497 var ioff: i64 = rb7 + slot*8
498 var woff3: i64 = rb7 + (4 + slot)*8
499 if okI == 1 { ioff = rb7 + (slot*2)*8; woff3 = rb7 + (slot*2 + 1)*8 }
500 pt_wr64(nb, ioff, bone)
501 pt_wr64(nb, woff3, basis - kept)
502 if bone == nj { reL = reL + 1 }
503 if bone == nj + 1 { reR = reR + 1 }
504 }
505 }
506 }
507 v6 = v6 + 1
508 }
509 }
510 pt_wr64(nb, te + 16, newwl)
511 let pw: *i64 = ((nb as i64) + wo) as *i64
512 pt_wr64(nb, te + 24, nxa_check2(1, pw, newwl))
513 wo = wo + newwl*8
514 s5 = s5 + 1
515 }
516 // no-fabrication law: a bone no vertex follows is a lie in SKEL form
517 if reL == 0 { return pt_die("PECT-RED zero left vertices rebound -- refusing to emit a bone nothing follows\n" as *u8, PT_EXIT_BAD) }
518 if reR == 0 { return pt_die("PECT-RED zero right vertices rebound -- refusing to emit a bone nothing follows\n" as *u8, PT_EXIT_BAD) }
519 let tbp: *i64 = ((nb as i64) + PT_HDR) as *i64
520 pt_wr64(nb, 24, nxa_check2(1, tbp, ns*4))
521
522 let fd: i64 = sys_openat_wr(outp, PT_MODE)
523 if fd < 0 { return pt_die("PECT-RED cannot open output\n" as *u8, PT_EXIT_IO) }
524 var w9: i64 = 0
525 while w9 < wo {
526 let kk: i64 = sys_write(fd, ((nb as i64) + w9) as *u8, wo - w9)
527 if kk <= 0 { sys_close(fd); return pt_die("PECT-RED short write\n" as *u8, PT_EXIT_IO) }
528 w9 = w9 + kk
529 }
530 sys_close(fd)
531
532 pt_out("PECT-GREEN joints " as *u8); pt_num(nj); pt_out(" -> " as *u8); pt_num(nj + 2)
533 pt_out(" donor joint " as *u8); pt_num(donor)
534 pt_out(" (tracks copied " as *u8); pt_num(donorTrks); pt_out("x2)" as *u8)
535 pt_out("\n skin layout " as *u8)
536 if okI == 1 { pt_out("interleaved" as *u8) }
537 if okP == 1 { pt_out("planar" as *u8) }
538 pt_out(" basis=" as *u8); pt_num(basis)
539 pt_out("\n rebound verts L=" as *u8); pt_num(reL); pt_out(" R=" as *u8); pt_num(reR)
540 pt_out(" bones lPectoral=" as *u8); pt_num(nj); pt_out(" rPectoral=" as *u8); pt_num(nj + 1)
541 pt_out("\n bytes=" as *u8); pt_num(wo)
542 pt_out("\n <- soft tissue now has bones that are not the spine; the page's chj classifier and\n" as *u8)
543 pt_out(" the floor gate's lateral-pair tooth both find this pair with zero code changes.\n" as *u8)
544 sys_exit(0)
545 return 0
546}