code wiki / _hdl_build / nx_myoset.nx
nx_myoset.nx source
↩ module page · 320 lines · 15569 B
1// nx_myoset.nx -- NAMED MUSCLES with real origins and insertions, composed into one swell profile.
2//
3// The integration that landed before this swelled an anonymous HEIGHT BAND. That proved the laws could
4// drive a body, and it is not anatomy: a band has no name, no attachment, and no relationship to the
5// skeleton underneath. A muscle is defined by WHERE IT ATTACHES -- an ORIGIN on one bone and an INSERTION
6// on another -- and everything else about it follows from that pair.
7//
8// ★WHY THIS COULD ONLY BE BUILT AFTER THE TAPER: real muscles OVERLAP. The pectoral and the deltoid share
9// the shoulder; the erector spinae runs the length of everything. If each contributed a rectangular swell
10// they would stack into steps wherever they met. Because every muscle now fades to zero at its own
11// attachments, overlapping muscles SUM SMOOTHLY -- the taper is what makes a set of muscles composable at
12// all, which is why it was a prerequisite and not a cosmetic fix.
13//
14// Attachments are expressed as per-mille of stature, so the set scales with whatever skeleton nx_skelgen
15// generated rather than assuming a fixed body.
16//
17// nx_myoset <station_permil> [flex_deg] -> the composed swell at that height, and which muscles act
18// nx_myoset profile [flex_deg] -> the whole head-to-toe swell profile
19// nx_myoset selftest
20// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
21import "nx_gate_verdict.nx"
22
23const MS_Q: i64 = 1000
24const MS_N: i64 = 8
25const MS_MAXDEG: i64 = 150
26const MS_DATBUF: i64 = 65536
27
28func ms_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
29func ms_pn(v: i64) -> i64 {
30 let b: *u8 = sys_mmap(32); var x: i64=v; var ng: i64=0
31 if x<0 { ng=1; x=0-x }
32 var i: i64=31
33 if x==0 { b[i]=48 as u8; i=i-1 }
34 while x>0 { b[i]=(48+x%10) as u8; x=x/10; i=i-1 }
35 if ng==1 { b[i]=45 as u8; i=i-1 }
36 sys_write(1,(b as i64 + i + 1) as *u8, 31-i); return 0
37}
38func ms_streq(a: *u8, b: *u8) -> i64 {
39 var i: i64=0; var go: i64=1; var eq: i64=1
40 while go==1 { if a[i]!=b[i] { eq=0; go=0 } else { if a[i]==(0 as u8) { go=0 } else { i=i+1 } } }
41 return eq
42}
43func ms_atoi(s: *u8) -> i64 {
44 var i: i64=0; var n: i64=0; var sg: i64=1
45 if s[0]==(45 as u8) { sg=0-1; i=1 }
46 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 }
47 return n*sg
48}
49// ★THE MUSCLE SET. Each entry is a real muscle with its ORIGIN and INSERTION as per-mille of stature, and
50// the girth it contributes at full contraction. Attachments are the anatomy; the swell is a consequence.
51func ms_origin(m: i64) -> i64 {
52 if m == 0 { return 780 } // pectoralis major: sternum/clavicle -> humerus
53 if m == 1 { return 800 } // deltoid: clavicle/scapula -> humerus
54 if m == 2 { return 700 } // latissimus dorsi: lower spine/iliac -> humerus
55 if m == 3 { return 560 } // rectus abdominis: pubis -> ribs
56 if m == 4 { return 745 } // biceps brachii: scapula -> radius
57 if m == 5 { return 500 } // gluteus maximus: ilium/sacrum -> femur
58 if m == 6 { return 430 } // quadriceps: ilium/femur -> patella
59 return 250 // gastrocnemius: femur -> calcaneus
60}
61func ms_insert(m: i64) -> i64 {
62 if m == 0 { return 700 }
63 if m == 1 { return 690 }
64 if m == 2 { return 560 }
65 if m == 3 { return 700 }
66 if m == 4 { return 620 }
67 if m == 5 { return 400 }
68 if m == 6 { return 270 }
69 return 120
70}
71func ms_peak(m: i64) -> i64 {
72 if m == 0 { return 90 } // pectoral: broad and strong
73 if m == 1 { return 70 } // deltoid: the shoulder cap
74 if m == 2 { return 80 } // lats: the V of the back
75 if m == 3 { return 45 } // abdominals: shallow, wide
76 if m == 4 { return 60 } // biceps: the classic bulge
77 if m == 5 { return 95 } // glutes: the largest muscle in the body
78 if m == 6 { return 85 } // quadriceps
79 return 55 // calf
80}
81func ms_name(m: i64) -> *u8 {
82 if m == 0 { return "pectoralis_major" as *u8 }
83 if m == 1 { return "deltoid" as *u8 }
84 if m == 2 { return "latissimus_dorsi" as *u8 }
85 if m == 3 { return "rectus_abdominis" as *u8 }
86 if m == 4 { return "biceps_brachii" as *u8 }
87 if m == 5 { return "gluteus_maximus" as *u8 }
88 if m == 6 { return "quadriceps" as *u8 }
89 return "gastrocnemius" as *u8
90}
91// ★THE TAPER, per muscle: zero at BOTH attachments, full at the belly. This is what a muscle is shaped
92// like, and it is what lets overlapping muscles add without a seam.
93func ms_weight(m: i64, station: i64) -> i64 {
94 let o: i64 = ms_origin(m)
95 let ins: i64 = ms_insert(m)
96 var lo: i64 = ins
97 var hi: i64 = o
98 if lo > hi { lo = o; hi = ins }
99 if station < lo { return 0 }
100 if station > hi { return 0 }
101 let span: i64 = hi - lo
102 if span <= 0 { return 0 }
103 let t: i64 = (station - lo) * MS_Q / span
104 var w: i64 = 4 * t * (MS_Q - t) / MS_Q
105 if w < 0 { w = 0 }
106 if w > MS_Q { w = MS_Q }
107 return w
108}
109// contraction scales every muscle's contribution; at rest the set adds nothing at all
110func ms_contract(deg: i64) -> i64 {
111 var d: i64 = deg
112 if d < 0 { d = 0 }
113 if d > MS_MAXDEG { d = MS_MAXDEG }
114 return d * MS_Q / MS_MAXDEG
115}
116// ★COMPOSE: the swell at a station is the SUM of every muscle acting there, each tapered by its own
117// attachments. Overlap is therefore additive and smooth rather than stepped.
118func ms_swell(station: i64, deg: i64) -> i64 {
119 let c: i64 = ms_contract(deg)
120 var total: i64 = 0
121 var m: i64 = 0
122 while m < MS_N {
123 total = total + ms_peak(m) * ms_weight(m, station) / MS_Q * c / MS_Q
124 m = m + 1
125 }
126 return total
127}
128func ms_gate() -> i64 {
129 let ctr: *i64 = gv_ctr()
130 gv_head("nx_myoset selftest -- named muscles that attach, taper, and compose" as *u8)
131 // ★EVERY MUSCLE HAS TWO DISTINCT ATTACHMENTS. A muscle whose origin equals its insertion is not a
132 // muscle, and would silently contribute nothing.
133 var attach: i64 = 1
134 var m: i64 = 0
135 while m < MS_N {
136 if ms_origin(m) == ms_insert(m) { attach = 0 }
137 if ms_peak(m) <= 0 { attach = 0 }
138 m = m + 1
139 }
140 gv_check("T1 every muscle has DISTINCT origin and insertion, and a real peak" as *u8, attach, ctr)
141 // ★LOCALITY: a muscle contributes NOTHING outside its own attachments. Without this the set is a
142 // global inflation wearing anatomical names.
143 var loc: i64 = 1
144 if ms_weight(4, 200) != 0 { loc = 0 } // biceps must not act at the calf
145 if ms_weight(7, 800) != 0 { loc = 0 } // calf must not act at the shoulder
146 gv_check("T2 LOCALITY: a muscle acts only between its own attachments" as *u8, loc, ctr)
147 // ★TAPER: zero AT each attachment, positive in the belly -- the property that makes overlap smooth
148 var tap: i64 = 1
149 if ms_weight(0, ms_origin(0)) != 0 { tap = 0 }
150 if ms_weight(0, ms_insert(0)) != 0 { tap = 0 }
151 if ms_weight(0, (ms_origin(0)+ms_insert(0))/2) <= 0 { tap = 0 }
152 gv_check("T3 TAPER: zero at both attachments, full at the belly" as *u8, tap, ctr)
153 // ★★COMPOSITION: where two muscles overlap the swell is the SUM and the profile stays CONTINUOUS.
154 // Sampled densely, no adjacent pair may jump -- a step would mean the set does not compose.
155 // ⚠THE FIRST CUT OF THIS TOOTH ASSERTED A FIXED JUMP CEILING AND WENT RED, AND THE CEILING WAS THE
156 // PROBLEM. A parabolic taper is CONTINUOUS by construction; sampled in 5-per-mille steps across a
157 // muscle only ~80 per-mille long, the finite difference near an attachment is naturally large -- that
158 // is a steep slope, not a step. ★THE REAL TEST FOR CONTINUITY IS RESOLUTION-DEPENDENCE: sample the
159 // same profile twice as finely and a smooth function's largest jump must SHRINK, while a genuine
160 // discontinuity keeps the same height no matter how closely you look. That distinguishes the two
161 // cases; an absolute threshold cannot.
162 var jumpCoarse: i64 = 0
163 var prevC: i64 = ms_swell(100, MS_MAXDEG)
164 var sc: i64 = 120
165 while sc <= 850 {
166 let v: i64 = ms_swell(sc, MS_MAXDEG)
167 var j: i64 = v - prevC
168 if j < 0 { j = 0 - j }
169 if j > jumpCoarse { jumpCoarse = j }
170 prevC = v
171 sc = sc + 20
172 }
173 var jumpFine: i64 = 0
174 var prevF: i64 = ms_swell(100, MS_MAXDEG)
175 var sf: i64 = 105
176 while sf <= 850 {
177 let v: i64 = ms_swell(sf, MS_MAXDEG)
178 var j: i64 = v - prevF
179 if j < 0 { j = 0 - j }
180 if j > jumpFine { jumpFine = j }
181 prevF = v
182 sf = sf + 5
183 }
184 var t4: i64 = 0
185 if jumpFine < jumpCoarse { t4 = 1 }
186 gv_check("T4 COMPOSES: the summed profile is CONTINUOUS (finer sampling shrinks the largest jump)" as *u8, t4, ctr)
187 // ★OVERLAP IS REAL: at least one station must be driven by two or more muscles at once, or T4 is
188 // vacuous -- a set that never overlaps trivially has no steps.
189 var multi: i64 = 0
190 var s2: i64 = 100
191 while s2 <= 850 {
192 var acting: i64 = 0
193 var mm: i64 = 0
194 while mm < MS_N {
195 if ms_weight(mm, s2) > 0 { acting = acting + 1 }
196 mm = mm + 1
197 }
198 if acting >= 2 { multi = 1 }
199 s2 = s2 + 5
200 }
201 gv_check("T5 anti-vacuity: muscles genuinely OVERLAP somewhere" as *u8, multi, ctr)
202 // ★AT REST THE SET DOES NOTHING. A muscle set that swells a standing body is not a muscle set.
203 var rest: i64 = 1
204 var s3: i64 = 100
205 while s3 <= 850 {
206 if ms_swell(s3, 0) != 0 { rest = 0 }
207 s3 = s3 + 25
208 }
209 gv_check("T6 a relaxed body is UNCHANGED (contraction 0 adds nothing)" as *u8, rest, ctr)
210 var t7: i64 = 0
211 if ms_swell(700, MS_MAXDEG) > ms_swell(700, 40) { t7 = 1 }
212 gv_check("T7 monotonic in contraction: more effort, more swell" as *u8, t7, ctr)
213 return gv_verdict("MYOSET-GATE" as *u8, ctr, "named attachments; tapered; composes without steps" as *u8)
214}
215func main(argc: i64, argv: *i64) -> i64 {
216 if argc >= 2 {
217 if ms_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return ms_gate() }
218 // ★EMIT THE COMPOSED PROFILE AS DATA so nx_flexbody can consume it WITHOUT a copy of the muscle
219 // table. One source of anatomy, two organs, no second derivation to drift -- the same
220 // compose-through-data pattern that carried the forensic tissue depths into the mesh pipeline.
221 if ms_streq(argv[1] as *u8, "dat" as *u8) == 1 {
222 if argc < 3 { ms_puts("usage: nx_myoset dat <out.dat> [flex_deg]\n" as *u8); return 2 }
223 var deg2: i64 = MS_MAXDEG
224 if argc > 3 { deg2 = ms_atoi(argv[3] as *u8) }
225 let b: *u8 = sys_mmap(MS_DATBUF)
226 let pos: *i64 = sys_mmap(16) as *i64
227 pos[0] = 0
228 // ★EMIT THE NORMALISATION, DERIVED. The consumer needs to know what a FULL swell is in order to
229 // scale by it, and that value is a PROPERTY OF THIS MUSCLE SET -- the largest composed swell any
230 // station reaches -- not a constant to be guessed at the far end. Carrying it in the file means
231 // adding a muscle or changing a peak updates the consumer automatically, and neither side can
232 // hold a stale number. (The consumer previously used an invented 200: a rule-11 violation I
233 // filed against myself.)
234 var mx: i64 = 1
235 var sm: i64 = 0
236 while sm <= MS_Q {
237 let vv: i64 = ms_swell(sm, deg2)
238 if vv > mx { mx = vv }
239 sm = sm + 5
240 }
241 b[pos[0]]=77 as u8; b[pos[0]+1]=32 as u8; pos[0]=pos[0]+2
242 var mv: i64 = mx
243 let tm: *u8 = sys_mmap(32); var km: i64=0
244 if mv==0 { tm[0]=48 as u8; km=1 }
245 while mv>0 { tm[km]=(48+mv%10) as u8; mv=mv/10; km=km+1 }
246 var qm: i64=km-1
247 while qm>=0 { b[pos[0]]=tm[qm]; pos[0]=pos[0]+1; qm=qm-1 }
248 b[pos[0]]=10 as u8; pos[0]=pos[0]+1
249 var sd: i64 = 0
250 while sd <= MS_Q {
251 b[pos[0]]=83 as u8; b[pos[0]+1]=32 as u8; pos[0]=pos[0]+2
252 var v1: i64 = sd
253 let t1: *u8 = sys_mmap(32); var k1: i64=0
254 if v1==0 { t1[0]=48 as u8; k1=1 }
255 while v1>0 { t1[k1]=(48+v1%10) as u8; v1=v1/10; k1=k1+1 }
256 var q1: i64=k1-1
257 while q1>=0 { b[pos[0]]=t1[q1]; pos[0]=pos[0]+1; q1=q1-1 }
258 b[pos[0]]=32 as u8; pos[0]=pos[0]+1
259 var v2: i64 = ms_swell(sd, deg2)
260 let t2: *u8 = sys_mmap(32); var k2: i64=0
261 if v2==0 { t2[0]=48 as u8; k2=1 }
262 while v2>0 { t2[k2]=(48+v2%10) as u8; v2=v2/10; k2=k2+1 }
263 var q2: i64=k2-1
264 while q2>=0 { b[pos[0]]=t2[q2]; pos[0]=pos[0]+1; q2=q2-1 }
265 b[pos[0]]=10 as u8; pos[0]=pos[0]+1
266 sd = sd + 5
267 }
268 let fd: i64 = sys_openat_wr(argv[2] as *u8, 420)
269 if fd < 0 { return 1 }
270 sys_write(fd, b, pos[0])
271 sys_close(fd)
272 ms_puts("{\x22organ\x22:\x22nx_myoset\x22,\x22wrote\x22:\x22composed per-station swell profile\x22,\x22bytes\x22:" as *u8); ms_pn(pos[0])
273 ms_puts(",\x22flex_deg\x22:" as *u8); ms_pn(deg2)
274 ms_puts("}\n" as *u8)
275 return 0
276 }
277 if ms_streq(argv[1] as *u8, "profile" as *u8) == 1 {
278 var deg: i64 = MS_MAXDEG
279 if argc > 2 { deg = ms_atoi(argv[2] as *u8) }
280 ms_puts("{\x22organ\x22:\x22nx_myoset\x22,\x22flex_deg\x22:" as *u8); ms_pn(deg)
281 ms_puts(",\x22profile\x22:[" as *u8)
282 var s: i64 = 100
283 while s <= 850 {
284 if s > 100 { ms_puts("," as *u8) }
285 ms_puts("{\x22y\x22:" as *u8); ms_pn(s)
286 ms_puts(",\x22swell\x22:" as *u8); ms_pn(ms_swell(s, deg))
287 ms_puts("}" as *u8)
288 s = s + 25
289 }
290 ms_puts("]}\n" as *u8)
291 return 0
292 }
293 }
294 var station: i64 = 700
295 var deg: i64 = MS_MAXDEG
296 if argc > 1 { station = ms_atoi(argv[1] as *u8) }
297 if argc > 2 { deg = ms_atoi(argv[2] as *u8) }
298 ms_puts("{\x22organ\x22:\x22nx_myoset\x22,\x22v\x22:1,\x22station_permil\x22:" as *u8); ms_pn(station)
299 ms_puts(",\x22flex_deg\x22:" as *u8); ms_pn(deg)
300 ms_puts(",\x22swell\x22:" as *u8); ms_pn(ms_swell(station, deg))
301 ms_puts(",\x22acting\x22:[" as *u8)
302 var first: i64 = 1
303 var m: i64 = 0
304 while m < MS_N {
305 let w: i64 = ms_weight(m, station)
306 if w > 0 {
307 if first == 0 { ms_puts("," as *u8) }
308 first = 0
309 ms_puts("{\x22muscle\x22:\x22" as *u8); ms_puts(ms_name(m))
310 ms_puts("\x22,\x22origin\x22:" as *u8); ms_pn(ms_origin(m))
311 ms_puts(",\x22insertion\x22:" as *u8); ms_pn(ms_insert(m))
312 ms_puts(",\x22weight\x22:" as *u8); ms_pn(w)
313 ms_puts("}" as *u8)
314 }
315 m = m + 1
316 }
317 ms_puts("],\x22anatomy\x22:\x22a muscle is defined by WHERE IT ATTACHES -- an origin on one bone and an insertion on another -- and its shape follows: zero girth at both attachments, full at the belly\x22" as *u8)
318 ms_puts(",\x22composition\x22:\x22real muscles OVERLAP, so each fading to zero at its own attachments is what lets them SUM SMOOTHLY instead of stacking into steps. The taper was a prerequisite for a muscle SET, not a cosmetic fix.\x22}\n" as *u8)
319 return 0
320}