code wiki / _hdl_build / nx_myo.nx
nx_myo.nx source
↩ module page · 184 lines · 9644 B
1// nx_myo.nx -- PIPELINE STAGE 2: VOLUME-PRESERVING MUSCLE BOUND TO BONE.
2//
3// The industry's second stage is "volume-preserving meshes bound to the bones to calculate authentic
4// muscle bulging and flexing". An audit found this stage completely untouched in our stack: what we called
5// muscle was ONE scaled envelope at 82 percent of the skin radius -- a shell, not a muscle, bound to
6// nothing, conserving nothing, incapable of bulging.
7//
8// ★THE DEFINING PROPERTY, and the reason this is a real muscle rather than a bump that grows: VOLUME IS
9// CONSERVED. A muscle belly is very nearly incompressible, so when its ends are drawn together it does not
10// simply get shorter -- it must get THICKER by exactly the amount that keeps its volume constant. That is
11// why a flexed biceps bulges, and it is a hard arithmetic constraint rather than an artistic choice:
12// length x cross-section = constant => radius = r0 * sqrt(L0 / L)
13// A generator that merely thickened a muscle by some tuned factor would look approximately right and be
14// unfalsifiable. This one is checkable: measure the volume before and after and it must not move.
15//
16// BOUND TO BONE: a muscle spans an ORIGIN on one bone and an INSERTION on another, so flexing the joint
17// between them shortens it and the bulge follows from the joint angle -- no separate muscle animation, no
18// hand-authored bulge curve. The joint angle is the only input.
19//
20// nx_myo <origin_len> <flex_deg> -> the muscle's state at that joint angle
21// nx_myo selftest
22// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
23import "nx_gate_verdict.nx"
24import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
25const MY_MAGIC_40500: i64 = 40500
26
27const MY_Q: i64 = 1000
28const MY_R0: i64 = 100
29const MY_TOL: i64 = 30
30const MY_MAXDEG: i64 = 150
31
32func my_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
33// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
34// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
35// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
36// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
37func my_pn(v: i64) -> i64 { nxi_out(v); return 0 }
38func my_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 my_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}
49func my_isqrt(v: i64) -> i64 {
50 if v <= 0 { return 0 }
51 var x: i64 = v
52 var y: i64 = (x+1)/2
53 while y < x { x = y; y = (x + v/x)/2 }
54 return x
55}
56// integer cosine over 0..180 degrees, scaled by MY_Q. Bhaskara-I: cos(d) = sin(d+90), and
57// sin(d) = 4d(180-d) / (40500 - d(180-d)) -- exact at 0/30/90/150/180, no float, no table.
58func my_sin(d: i64) -> i64 {
59 var x: i64 = d % 360
60 if x < 0 { x = x + 360 }
61 var neg: i64 = 0
62 if x > 180 { x = x - 180; neg = 1 }
63 let p: i64 = x * (180 - x)
64 var v: i64 = 4*p*MY_Q / (MY_MAGIC_40500 - p)
65 if neg == 1 { v = 0 - v }
66 return v
67}
68func my_cos(d: i64) -> i64 { return my_sin(d + 90) }
69// ★MUSCLE LENGTH FROM THE JOINT ANGLE. Origin and insertion sit on two bones meeting at the joint; the
70// straight-line distance between them is the law of cosines over the two lever arms. Flex the joint and
71// the muscle shortens -- geometry, not an authored curve.
72func my_length(lever: i64, deg: i64) -> i64 {
73 let c: i64 = my_cos(180 - deg)
74 let sq: i64 = 2*lever*lever - 2*lever*lever*c/MY_Q
75 return my_isqrt(sq)
76}
77// ★VOLUME PRESERVATION: radius = r0 * sqrt(L0/L). Computed in integer with the ratio scaled up first so
78// the square root keeps its precision.
79func my_radius(r0: i64, L0: i64, L: i64) -> i64 {
80 if L <= 0 { return 0 }
81 let ratio: i64 = L0 * MY_Q / L
82 return r0 * my_isqrt(ratio * MY_Q) / MY_Q
83}
84// the invariant itself: L * r^2, which must not move as the joint flexes
85func my_volume(L: i64, r: i64) -> i64 { return L * r * r / MY_Q }
86func my_gate() -> i64 {
87 let ctr: *i64 = gv_ctr()
88 gv_head("nx_myo selftest -- muscle that bulges because volume is conserved, not because it was told to" as *u8)
89 // ⚠THE FIRST RUN WENT 2/8 AND THE GEOMETRY WAS NEVER WRONG -- THE CONVENTION WAS INVERTED. Flex is
90 // measured from STRAIGHT: 0 degrees is a fully extended joint where origin and insertion are FURTHEST
91 // apart, and 150 is fully bent where they are closest. I had taken 150 as the rest pose, so every
92 // comparison ran backwards and six teeth failed truthfully. ★When a whole battery fails at once,
93 // suspect a sign or a convention before suspecting the maths.
94 let lever: i64 = 1000
95 let L0: i64 = my_length(lever, 0) // rest: joint straight, muscle at its LONGEST
96 let r0: i64 = MY_R0
97 let V0: i64 = my_volume(L0, r0)
98 var t1: i64 = 0
99 if L0 > 0 { if V0 > 0 { t1 = 1 } }
100 gv_check("T1 muscle spans two bones and has a rest length" as *u8, t1, ctr)
101 // ★FLEXING SHORTENS IT -- from the joint angle alone, no authored curve
102 let Lf: i64 = my_length(lever, MY_MAXDEG) // flexed: ends drawn together
103 var t2: i64 = 0
104 if Lf < L0 { t2 = 1 }
105 gv_check("T2 flexing the JOINT shortens the muscle (geometry, not animation)" as *u8, t2, ctr)
106 // ★AND THEREFORE IT MUST THICKEN
107 let rf: i64 = my_radius(r0, L0, Lf)
108 var t3: i64 = 0
109 if rf > r0 { t3 = 1 }
110 gv_check("T3 and it BULGES -- shorter means thicker" as *u8, t3, ctr)
111 // ★★THE FLAGSHIP: volume is CONSERVED across the whole flex range. This is what separates a real
112 // muscle from a bump that was told to grow, and it is checkable rather than a matter of taste.
113 let Vf: i64 = my_volume(Lf, rf)
114 var dv: i64 = (Vf - V0) * MY_Q / V0
115 if dv < 0 { dv = 0 - dv }
116 var t4: i64 = 0
117 if dv <= MY_TOL { t4 = 1 }
118 gv_check("T4 FLAGSHIP: VOLUME CONSERVED through the flex (within rounding)" as *u8, t4, ctr)
119 // conserved across the ENTIRE range, not just at one convenient angle
120 var worst: i64 = 0
121 var d: i64 = 0
122 while d <= MY_MAXDEG {
123 let Ld: i64 = my_length(lever, d)
124 let rd: i64 = my_radius(r0, L0, Ld)
125 let Vd: i64 = my_volume(Ld, rd)
126 var e: i64 = (Vd - V0) * MY_Q / V0
127 if e < 0 { e = 0 - e }
128 if e > worst { worst = e }
129 d = d + 10
130 }
131 var t5: i64 = 0
132 if worst <= MY_TOL { t5 = 1 }
133 gv_check("T5 conserved across the WHOLE range, not one lucky angle" as *u8, t5, ctr)
134 // ★ANTI-VACUITY: the bulge must actually be a bulge. A muscle whose radius never moved would pass
135 // volume conservation trivially and be simulating nothing.
136 var t6: i64 = 0
137 if rf > r0*105/100 { t6 = 1 }
138 gv_check("T6 anti-vacuity: the bulge is REAL (over 5 percent), not a rounding artifact" as *u8, t6, ctr)
139 // ★MONOTONIC: more flex must mean more bulge, every step of the way -- no inversions.
140 // walk from straight to fully bent: the radius must never DECREASE as flex increases
141 var mono: i64 = 1
142 var prev: i64 = 0
143 var d2: i64 = 0
144 while d2 <= MY_MAXDEG {
145 let rr: i64 = my_radius(r0, L0, my_length(lever, d2))
146 if rr < prev { mono = 0 }
147 prev = rr
148 d2 = d2 + 10
149 }
150 gv_check("T7 monotonic: more flex, more bulge, with no inversions" as *u8, mono, ctr)
151 var t8: i64 = 0
152 if my_radius(r0, L0, 0) == 0 { t8 = 1 }
153 gv_check("T8 a degenerate zero-length muscle is refused, not divided by" as *u8, t8, ctr)
154 return gv_verdict("MYO-GATE" as *u8, ctr, "volume-preserving muscle; bulge follows from the joint" as *u8)
155}
156func main(argc: i64, argv: *i64) -> i64 {
157 if argc >= 2 {
158 if my_streq(argv[1] as *u8, "selftest" as *u8) == 1 { return my_gate() }
159 }
160 var lever: i64 = 1000
161 var deg: i64 = 90
162 if argc > 1 { lever = my_atoi(argv[1] as *u8) }
163 if argc > 2 { deg = my_atoi(argv[2] as *u8) }
164 if lever <= 0 { lever = 1000 }
165 let L0: i64 = my_length(lever, 0)
166 let L: i64 = my_length(lever, deg)
167 let r: i64 = my_radius(MY_R0, L0, L)
168 let V0: i64 = my_volume(L0, MY_R0)
169 let V: i64 = my_volume(L, r)
170 var dv: i64 = 0
171 if V0 > 0 { dv = (V - V0) * MY_Q / V0 }
172 my_puts("{\x22organ\x22:\x22nx_myo\x22,\x22v\x22:1,\x22stage\x22:\x22pipeline stage 2 -- volume-preserving muscle bound to bone\x22" as *u8)
173 my_puts(",\x22lever\x22:" as *u8); my_pn(lever)
174 my_puts(",\x22flex_deg\x22:" as *u8); my_pn(deg)
175 my_puts(",\x22rest_length\x22:" as *u8); my_pn(L0)
176 my_puts(",\x22length\x22:" as *u8); my_pn(L)
177 my_puts(",\x22rest_radius\x22:" as *u8); my_pn(MY_R0)
178 my_puts(",\x22radius\x22:" as *u8); my_pn(r)
179 my_puts(",\x22bulge_permil\x22:" as *u8); my_pn((r - MY_R0) * MY_Q / MY_R0)
180 my_puts(",\x22volume_error_permil\x22:" as *u8); my_pn(dv)
181 my_puts(",\x22law\x22:\x22radius = r0 * sqrt(L0/L), so length x cross-section is invariant. A muscle belly is near-incompressible: drawing its ends together MUST thicken it by exactly the amount that keeps volume constant. The bulge is arithmetic, not an authored curve.\x22" as *u8)
182 my_puts(",\x22bound_to_bone\x22:\x22origin and insertion sit on two bones meeting at a joint; length follows from the joint angle by the law of cosines, so the ONLY input is how far the joint is flexed\x22}\n" as *u8)
183 return 0
184}