nx_devpaint_gate.nx source
↩ module page · 260 lines · 15542 B
1// nx_devpaint_gate.nx -- GATE for the derived-scale deviation heatmap and the two root defects it
2// had to fix first (2026-08-26).
3//
4// THE TOOTH THAT MATTERED MOST IS THE ONE THE EXISTING GATES COULD NOT HAVE HAD. nx_mmdev_gate and
5// nx_xport_gate both write their NXMSH2 fixtures through the SAME nx_nxmesh_lib constant they then read
6// back, so the position-unit constant CANCELS and every round-trip tooth passes at any value of it. It
7// passed at a value that was wrong by 1000x for every file a real producer writes, and the consequence
8// was that nx_mmdev -- this estate's exact surface-deviation ruler -- refused every real mesh on its
9// extent bound. So this gate decodes a LITERAL float32 BIT PATTERN that no writer here produced, and
10// carries a neg-control showing the old constant would have refused a 1.594 m human body.
11//
12// The second root defect is the colour codec: nx_mesh2glb decoded a legal colour to zero, tripping its
13// own bone fallback, so every painted heatmap emitted uniformly bone while the organ printed "per-tri
14// colour carried". The neg-control below reproduces that exact expression and requires it to be wrong.
15// license_tier: ORIGINAL
16
17import "nx_syscalls.nx"
18import "nx_gate_verdict.nx"
19import "nx_vecmath.nx"
20import "nx_nxmesh_lib.nx"
21import "nx_mmdev_lib.nx"
22import "nx_devpaint_lib.nx"
23
24// 200 mm reference square and a 5.0 mm lift, both in ruler units (tenths of a millimetre).
25const DG_SIDE: i64 = 2000
26const DG_LIFT: i64 = 50
27// Slack of ONE per-mille unit, DERIVED not chosen: nm_col_bits stores permille/1000 as float32 and
28// nm_col_permil_of truncates on the way back, so the worst case is one unit. Printed beside the values.
29const DG_SLACK: i64 = 1
30// float32 bit pattern for 1594.0 -- the measured height in millimetres of the fitted body this heatmap
31// is built for. Hand-written, NOT produced by nm_put_tri: that is the whole point of this fixture.
32const DG_F32_1594MM: i64 = 1153908736
33const DG_EXPECT_1594: i64 = 15940 // 1594 mm in ruler units
34const DG_OLD_UNIT_PER_M: i64 = 10000 // the pre-fix multiplier, kept ONLY as a neg-control
35const DG_TEST_PERMIL: i64 = 900
36const DG_TEST_U8: i64 = 229 // 900 permille of 255, truncated
37const DG_CAND_TRIS: i64 = 3
38// The stop colours again, as LITERALS, written independently of dp_stop_r/g/b.
39// MEASURED, and this is why they exist: the first draft of this gate asserted dp_ramp's output against
40// dp_stop_r(n)/dp_stop_g(n)/dp_stop_b(n) -- the very functions dp_ramp calls. nx_gate_bite left SIX
41// comparison mutants alive in that stop table, because mutating a stop moves BOTH sides of the
42// comparison identically and the tooth cannot fail. A TEST is the one place a second, independent copy
43// of a table is correct rather than duplication: it is the oracle, and without it the table is
44// unfalsifiable.
45const DG_FLOOR_R: i64 = 100
46const DG_FLOOR_G: i64 = 850
47const DG_FLOOR_B: i64 = 150
48const DG_MID_R: i64 = 950
49const DG_MID_G: i64 = 550
50const DG_MID_B: i64 = 80
51const DG_TOP_R: i64 = 850
52const DG_TOP_G: i64 = 100
53const DG_TOP_B: i64 = 850
54
55func dg_near(a: i64, b: i64, slack: i64) -> i64 {
56 var d: i64 = a - b
57 if d < 0 { d = 0 - d }
58 if d <= slack { return 1 }
59 return 0
60}
61
62// is triangle t painted the per-mille colour (r,g,bl), within the codec's declared slack?
63func dg_col_is(buf: *u8, tb: i64, t: i64, r: i64, g: i64, bl: i64) -> i64 {
64 if dg_near(nm_col_get(buf, tb, t, 0), r, DG_SLACK) == 0 { return 0 }
65 if dg_near(nm_col_get(buf, tb, t, 1), g, DG_SLACK) == 0 { return 0 }
66 if dg_near(nm_col_get(buf, tb, t, 2), bl, DG_SLACK) == 0 { return 0 }
67 return 1
68}
69
70// a flat 200 mm square at z=0, two triangles -- the reference surface
71func dg_ref(buf: *u8) -> i64 {
72 nm_put_u32(buf, NM_OFF_NLAYERS, 0)
73 nm_put_u32(buf, NM_OFF_NTRIS, 2)
74 let tb: i64 = nm_tri_base(buf)
75 let p: *i64 = sys_mmap(9 * 8) as *i64
76 p[0]=0; p[1]=0; p[2]=0; p[3]=DG_SIDE; p[4]=0; p[5]=0; p[6]=DG_SIDE; p[7]=DG_SIDE; p[8]=0
77 nm_put_tri(buf, tb, 0, p)
78 p[0]=0; p[1]=0; p[2]=0; p[3]=DG_SIDE; p[4]=DG_SIDE; p[5]=0; p[6]=0; p[7]=DG_SIDE; p[8]=0
79 nm_put_tri(buf, tb, 1, p)
80 return 0
81}
82
83// three small triangles inside the reference footprint. Triangle 1 is lifted WHOLE by `lift`;
84// triangles 0 and 2 lie exactly on the reference plane. `corner_only` lifts ONLY the third corner of
85// triangle 1 instead, which is the case that separates max-of-corners from a centroid.
86func dg_cand(buf: *u8, lift: i64, corner_only: i64) -> i64 {
87 nm_put_u32(buf, NM_OFF_NLAYERS, 0)
88 nm_put_u32(buf, NM_OFF_NTRIS, DG_CAND_TRIS)
89 let tb: i64 = nm_tri_base(buf)
90 let p: *i64 = sys_mmap(9 * 8) as *i64
91 p[0]=100; p[1]=100; p[2]=0; p[3]=300; p[4]=100; p[5]=0; p[6]=300; p[7]=300; p[8]=0
92 nm_put_tri(buf, tb, 0, p)
93 var z0: i64 = lift
94 var z1: i64 = lift
95 if corner_only == 1 { z0 = 0; z1 = 0 }
96 p[0]=500; p[1]=500; p[2]=z0; p[3]=700; p[4]=500; p[5]=z1; p[6]=700; p[7]=700; p[8]=lift
97 nm_put_tri(buf, tb, 1, p)
98 p[0]=900; p[1]=900; p[2]=0; p[3]=1100; p[4]=900; p[5]=0; p[6]=1100; p[7]=1100; p[8]=0
99 nm_put_tri(buf, tb, 2, p)
100 return 0
101}
102
103func main() -> i64 {
104 let c: *i64 = gv_ctr()
105 gv_head("NX-DEVPAINT-GATE -- derived-scale deviation heatmap, and the unit and colour roots under it" as *u8)
106
107 // ---- A. the position unit, proven WITHOUT a round trip ---------------------------------------
108 gv_check("unit-ladder-identity-holds-so-the-derived-constant-cannot-drift" as *u8,
109 NM_UNIT_PER_M == NM_UNIT_PER_FILE_UNIT * NM_FILE_UNIT_PER_M, c)
110
111 let got1594: i64 = vm_f32_to_int(DG_F32_1594MM, NM_UNIT_PER_FILE_UNIT)
112 let old1594: i64 = vm_f32_to_int(DG_F32_1594MM, DG_OLD_UNIT_PER_M)
113 gv_puts(" literal f32 1594.0 mm decodes to " as *u8); gv_num(got1594)
114 gv_puts(" ruler units (expect " as *u8); gv_num(DG_EXPECT_1594)
115 gv_puts("); with the pre-fix multiplier it decodes to " as *u8); gv_num(old1594)
116 gv_puts(" against an extent bound of " as *u8); gv_num(MD_EXTENT_MAX); gv_puts("\n" as *u8)
117 gv_check("hand-written-f32-millimetres-decode-to-the-right-ruler-units" as *u8, got1594 == DG_EXPECT_1594, c)
118 gv_check("a-1594mm-body-is-now-inside-the-extent-bound" as *u8, got1594 < MD_EXTENT_MAX, c)
119 gv_check("neg-control-the-pre-fix-multiplier-would-refuse-a-real-human-body" as *u8, old1594 > MD_EXTENT_MAX, c)
120
121 // ---- B. the colour codec ---------------------------------------------------------------------
122 let bits9: i64 = nm_col_bits(DG_TEST_PERMIL)
123 let u8got: i64 = nm_col_u8_of(bits9)
124 let pmgot: i64 = nm_col_permil_of(bits9)
125 let bug: i64 = vm_f32_to_int(bits9, NM_COL_U8_MAX) / NM_COL_PERMIL
126 gv_puts(" permille " as *u8); gv_num(DG_TEST_PERMIL); gv_puts(" -> u8 " as *u8); gv_num(u8got)
127 gv_puts(" (expect " as *u8); gv_num(DG_TEST_U8); gv_puts(") -> permille " as *u8); gv_num(pmgot)
128 gv_puts(" slack " as *u8); gv_num(DG_SLACK)
129 gv_puts("; the nx_mesh2glb expression yields " as *u8); gv_num(bug); gv_puts("\n" as *u8)
130 gv_check("colour-decodes-to-the-right-glTF-byte" as *u8, dg_near(u8got, DG_TEST_U8, DG_SLACK) == 1, c)
131 gv_check("colour-round-trips-within-the-declared-one-permille-slack" as *u8, dg_near(pmgot, DG_TEST_PERMIL, DG_SLACK) == 1, c)
132 gv_check("neg-control-the-shipped-mesh2glb-expression-destroyed-every-legal-colour" as *u8, bug == 0, c)
133 gv_check("neg-control-codec-is-not-a-constant-two-colours-decode-differently" as *u8,
134 nm_col_u8_of(nm_col_bits(100)) != nm_col_u8_of(nm_col_bits(900)), c)
135
136 // ---- C. the ramp -----------------------------------------------------------------------------
137 let rgb: *i64 = sys_mmap(NM_COL_CHANNELS * 8) as *i64
138 dp_ramp(0, 0, 400, rgb)
139 gv_puts(" ramp floor " as *u8); gv_num(rgb[0]); gv_puts(" " as *u8); gv_num(rgb[1]); gv_puts(" " as *u8); gv_num(rgb[2])
140 gv_puts(" (expect " as *u8); gv_num(DG_FLOOR_R); gv_puts(" " as *u8); gv_num(DG_FLOOR_G); gv_puts(" " as *u8); gv_num(DG_FLOOR_B); gv_puts(")\n" as *u8)
141 gv_check("ramp-floor-is-exactly-the-zero-colour-against-an-independent-literal" as *u8,
142 rgb[0] == DG_FLOOR_R && rgb[1] == DG_FLOOR_G && rgb[2] == DG_FLOOR_B, c)
143 dp_ramp(400, 0, 400, rgb)
144 gv_puts(" ramp ceiling " as *u8); gv_num(rgb[0]); gv_puts(" " as *u8); gv_num(rgb[1]); gv_puts(" " as *u8); gv_num(rgb[2])
145 gv_puts(" (expect " as *u8); gv_num(DG_TOP_R); gv_puts(" " as *u8); gv_num(DG_TOP_G); gv_puts(" " as *u8); gv_num(DG_TOP_B); gv_puts(")\n" as *u8)
146 gv_check("ramp-ceiling-is-exactly-the-top-stop-against-an-independent-literal" as *u8,
147 rgb[0] == DG_TOP_R && rgb[1] == DG_TOP_G && rgb[2] == DG_TOP_B, c)
148 dp_ramp(200, 0, 400, rgb)
149 gv_check("ramp-midpoint-lands-on-the-middle-stop-against-an-independent-literal" as *u8,
150 rgb[0] == DG_MID_R && rgb[1] == DG_MID_G && rgb[2] == DG_MID_B, c)
151 dp_ramp(100, 0, 400, rgb)
152 let qr: i64 = rgb[0]
153 gv_check("ramp-quarter-point-lies-strictly-between-the-floor-and-the-middle-stop" as *u8,
154 qr > DG_FLOOR_R && qr < DG_MID_R, c)
155 dp_ramp(0 - 5, 0, 400, rgb)
156 gv_check("ramp-clamps-below-the-floor-instead-of-wrapping" as *u8,
157 rgb[0] == DG_FLOOR_R && rgb[1] == DG_FLOOR_G, c)
158 dp_ramp(4000, 0, 400, rgb)
159 gv_check("ramp-clamps-above-the-ceiling-instead-of-wrapping" as *u8,
160 rgb[0] == DG_TOP_R && rgb[1] == DG_TOP_G, c)
161 dp_ramp(0, 0, 400, rgb)
162 let sameA: i64 = rgb[1]
163 dp_ramp(400, 0, 400, rgb)
164 gv_check("neg-control-ramp-actually-varies-a-single-flat-colour-cannot-pass" as *u8, sameA != rgb[1], c)
165
166 // ---- D. the derivation -----------------------------------------------------------------------
167 let r1: *i64 = dp_res()
168 r1[DP_R_P95] = 2307
169 let h1: i64 = dp_scale(r1)
170 gv_puts(" p95 2307 -> hi " as *u8); gv_num(h1); gv_puts(" (expect 2310, the next whole millimetre)\n" as *u8)
171 gv_check("ceiling-is-p95-rounded-up-to-a-whole-millimetre" as *u8, h1 == 2310, c)
172 gv_check("floor-is-zero-so-zero-deviation-is-the-zero-colour" as *u8, r1[DP_R_LO] == 0, c)
173 let r2: *i64 = dp_res()
174 r2[DP_R_P95] = 0
175 let h2: i64 = dp_scale(r2)
176 gv_puts(" p95 0 -> hi " as *u8); gv_num(h2); gv_puts(" (one millimetre floor, never a zero span)\n" as *u8)
177 gv_check("identical-meshes-give-a-one-millimetre-span-not-a-division-by-zero" as *u8, h2 == NM_UNIT_PER_FILE_UNIT, c)
178 let r3: *i64 = dp_res()
179 r3[DP_R_P95] = 50
180 r3[DP_R_MAX] = 30000
181 gv_check("neg-control-ceiling-tracks-p95-not-max-so-one-outlier-cannot-flatten-the-picture" as *u8,
182 dp_scale(r3) == 50, c)
183
184 // ---- E. end to end, on geometry whose answer is known before the ruler runs -------------------
185 let refb: *u8 = sys_mmap(nm_file_bytes(0, 2) + 16)
186 dg_ref(refb)
187 let candb: *u8 = sys_mmap(nm_file_bytes(0, DG_CAND_TRIS) + 16)
188 dg_cand(candb, DG_LIFT, 0)
189 let res: *i64 = dp_res()
190 let dev: *i64 = sys_mmap(DG_CAND_TRIS * 3 * 8 + 64) as *i64
191 let rc: i64 = dp_measure(candb, refb, dev, res)
192 gv_subjects("corners-measured" as *u8, res[DP_R_CORNERS], c)
193 gv_puts(" planted lift " as *u8); gv_num(DG_LIFT); gv_puts(": tri devs " as *u8)
194 gv_num(dp_tri_dev(dev, 0)); gv_puts(" " as *u8); gv_num(dp_tri_dev(dev, 1)); gv_puts(" " as *u8)
195 gv_num(dp_tri_dev(dev, 2)); gv_puts(" p95 " as *u8); gv_num(res[DP_R_P95]); gv_puts("\n" as *u8)
196 gv_check("measurement-visited-every-corner-of-every-triangle-not-a-sample" as *u8,
197 rc == 0 && res[DP_R_CORNERS] == DG_CAND_TRIS * 3 && res[DP_R_TRIS] == DG_CAND_TRIS, c)
198 gv_check("fixture-reached-the-condition-the-planted-triangle-really-is-off-the-surface" as *u8,
199 dg_near(dp_tri_dev(dev, 1), DG_LIFT, DG_SLACK) == 1, c)
200 gv_check("fixture-reached-the-condition-the-untouched-triangles-really-are-on-it" as *u8,
201 dp_tri_dev(dev, 0) == 0 && dp_tri_dev(dev, 2) == 0, c)
202
203 let hi: i64 = dp_scale(res)
204 let tbc: i64 = nm_tri_base(candb)
205 dp_paint(candb, tbc, DG_CAND_TRIS, dev, res[DP_R_LO], hi)
206 gv_check("planted-deviation-is-painted-at-the-top-of-the-ramp-ON-THE-RIGHT-TRIANGLE" as *u8,
207 dg_col_is(candb, tbc, 1, DG_TOP_R, DG_TOP_G, DG_TOP_B) == 1, c)
208 gv_check("neg-control-colour-everything-hot-fails-the-undeviated-triangles-stay-the-zero-colour" as *u8,
209 dg_col_is(candb, tbc, 0, DG_FLOOR_R, DG_FLOOR_G, DG_FLOOR_B) == 1 &&
210 dg_col_is(candb, tbc, 2, DG_FLOOR_R, DG_FLOOR_G, DG_FLOOR_B) == 1, c)
211 gv_puts(" saturated corners " as *u8); gv_num(dp_saturated(dev, res[DP_R_CORNERS], hi))
212 gv_puts(" of " as *u8); gv_num(res[DP_R_CORNERS]); gv_puts(" (the three lifted corners clamp at the ceiling)\n" as *u8)
213 gv_check("saturation-count-names-exactly-the-corners-that-clamp-at-the-ceiling" as *u8,
214 dp_saturated(dev, res[DP_R_CORNERS], hi) == 3, c)
215 gv_check("neg-control-saturation-is-not-everything-and-not-nothing" as *u8,
216 dp_saturated(dev, res[DP_R_CORNERS], hi) > 0 &&
217 dp_saturated(dev, res[DP_R_CORNERS], hi) < res[DP_R_CORNERS], c)
218
219 // self against self: a mesh is zero from itself, so the WHOLE mesh must be the zero colour.
220 // This is the tooth a "colour everything red" implementation cannot pass.
221 let selfb: *u8 = sys_mmap(nm_file_bytes(0, DG_CAND_TRIS) + 16)
222 dg_cand(selfb, DG_LIFT, 0)
223 let sres: *i64 = dp_res()
224 let sdev: *i64 = sys_mmap(DG_CAND_TRIS * 3 * 8 + 64) as *i64
225 let src: i64 = dp_measure(selfb, selfb, sdev, sres)
226 let shi: i64 = dp_scale(sres)
227 let tbs: i64 = nm_tri_base(selfb)
228 dp_paint(selfb, tbs, DG_CAND_TRIS, sdev, sres[DP_R_LO], shi)
229 var uniform: i64 = 0
230 var checked: i64 = 0
231 var t: i64 = 0
232 while t < DG_CAND_TRIS {
233 if dg_col_is(selfb, tbs, t, DG_FLOOR_R, DG_FLOOR_G, DG_FLOOR_B) == 1 { uniform = uniform + 1 }
234 checked = checked + 1
235 t = t + 1
236 }
237 gv_puts(" self-compare: max dev " as *u8); gv_num(sres[DP_R_MAX])
238 gv_puts(", triangles at the zero colour " as *u8); gv_num(uniform); gv_puts(" of " as *u8); gv_num(checked); gv_puts("\n" as *u8)
239 gv_check("a-mesh-against-itself-measures-exactly-zero-everywhere" as *u8,
240 src == 0 && sres[DP_R_MAX] == 0, c)
241 gv_check("a-mesh-against-itself-paints-uniformly-in-the-zero-colour-bound-to-its-own-count" as *u8,
242 checked == DG_CAND_TRIS && uniform == checked, c)
243
244 // max-of-corners, the declared reduction: ONE deviant corner must not be averaged away.
245 let cornb: *u8 = sys_mmap(nm_file_bytes(0, DG_CAND_TRIS) + 16)
246 dg_cand(cornb, DG_LIFT, 1)
247 let cres: *i64 = dp_res()
248 let cdev: *i64 = sys_mmap(DG_CAND_TRIS * 3 * 8 + 64) as *i64
249 dp_measure(cornb, refb, cdev, cres)
250 gv_puts(" single lifted corner: corner devs " as *u8); gv_num(cdev[3]); gv_puts(" " as *u8)
251 gv_num(cdev[4]); gv_puts(" " as *u8); gv_num(cdev[5])
252 gv_puts(" -> triangle takes " as *u8); gv_num(dp_tri_dev(cdev, 1)); gv_puts("\n" as *u8)
253 gv_check("fixture-reached-the-condition-exactly-one-corner-is-off-the-surface" as *u8,
254 cdev[3] == 0 && cdev[4] == 0 && dg_near(cdev[5], DG_LIFT, DG_SLACK) == 1, c)
255 gv_check("one-deviant-corner-is-reported-in-full-not-averaged-toward-the-centroid" as *u8,
256 dg_near(dp_tri_dev(cdev, 1), DG_LIFT, DG_SLACK) == 1, c)
257
258 return gv_verdict("nx_devpaint_gate" as *u8, c,
259 "Every deviation is measured against geometry whose answer is known before the ruler runs, the colour scale is derived from that measurement rather than passed in, and both root defects carry a neg-control that reproduces the shipped wrong arithmetic and requires it to fail." as *u8)
260}