code wiki / _hdl_build / nx_bezier_patch_gate.nx

nx_bezier_patch_gate.nx source

↩ module page · 143 lines · 8083 B

1// nx_bezier_patch_gate.nx -- proves the parametric spline surface (bicubic Bezier patch): corners INTERPOLATED, 2// interior control points PULL a smooth bulge (hand-exact 9/16*H at the center), tessellated watertight, and a 3// flat-net NEG-CONTROL yields a flat surface. Answers the census GAP "NURBS / parametric spline surfaces". 4// license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_bezier_patch.nx" 7 8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 10 11// build a 4x4 control net: grid in x,y (step S); interior 2x2 control points raised to z=H (bulge), boundary z=0. 12func build_net(net: *i64, S: i64, H: i64) -> i64 { 13 var a: i64 = 0 14 while a < 4 { 15 var b: i64 = 0 16 while b < 4 { 17 let p: i64 = (a*4 + b) * 3 18 net[p] = a * S 19 net[p+1] = b * S 20 var z: i64 = 0 21 if a >= 1 { if a <= 2 { if b >= 1 { if b <= 2 { z = H } } } } 22 net[p+2] = z 23 b = b + 1 24 } 25 a = a + 1 26 } 27 return 0 28} 29func maxz(mesh: i64) -> i64 { 30 let h: *i64 = m3_hdr(mesh) 31 var mx: i64 = 0 - 2000000000 32 var i: i64 = 0 33 while i < h[0] { let v: *i64 = m3_vert(mesh, i); if v[2] > mx { mx = v[2] } i = i + 1 } 34 return mx 35} 36 37func main() -> i64 { 38 hw("=== nx_bezier_patch_gate -- parametric spline surface (bicubic Bezier tensor-product patch) ===\n" as *u8) 39 var fails: i64 = 0 40 let S: i64 = 25600 41 let H: i64 = 25600 42 let N: i64 = 4 43 44 let net: *i64 = sys_mmap(48*8) as *i64 45 build_net(net, S, H) 46 let mesh: i64 = (sys_mmap(m3_bytes())) as i64 47 bez_tessellate(net, N, mesh) 48 49 let h: *i64 = m3_hdr(mesh) 50 hw(" patch tessellated N="); pn(N); hw(" -> "); pn(h[0]); hw("v/"); pn(h[1]); hw("t\n" as *u8) 51 52 // T1 topology: (N+1)^2 = 25 verts, N^2*2 = 32 tris 53 var t1: i64 = 0 54 if h[0] == 25 { if h[1] == 32 { t1 = 1 } } 55 if t1 == 1 { hw("T1 PASS tessellation 25v/32t (watertight grid)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL topology\n" as *u8) } 56 57 // T2 CORNER INTERPOLATION: vertex 0 = S(0,0) = P[0][0] = (0,0,0) 58 let c0: *i64 = m3_vert(mesh, 0) 59 var t2: i64 = 0 60 if c0[0] == 0 { if c0[1] == 0 { if c0[2] == 0 { t2 = 1 } } } 61 if t2 == 1 { hw("T2 PASS corner INTERPOLATED: S(0,0) == control P[0][0] = (0,0,0)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL corner ("); pn(c0[0]); hw(","); pn(c0[1]); hw(","); pn(c0[2]); hw(")\n" as *u8) } 62 63 // T3 SMOOTH BULGE: center vertex (i=2,j=2 -> index 12) z = 9/16*H = 14400 exactly, and 0 < z < H (pulled, not interpolated) 64 let ctr: *i64 = m3_vert(mesh, 12) 65 hw(" center S(0.5,0.5) = ("); pn(ctr[0]); hw(","); pn(ctr[1]); hw(","); pn(ctr[2]); hw(")\n" as *u8) 66 var t3: i64 = 0 67 if ctr[2] == 14400 { if ctr[2] > 0 { if ctr[2] < H { t3 = 1 } } } 68 if t3 == 1 { hw("T3 PASS smooth bulge: center z = 9/16*H = 14400 (0 < z < H = surface PULLED toward interior, not interpolating it)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL center z="); pn(ctr[2]); hw(" expected 14400\n" as *u8) } 69 70 // T4 NEG-CONTROL: a FLAT net (all z=0) -> a flat surface (max z = 0). proves the bulge came from the control net. 71 let flat: *i64 = sys_mmap(48*8) as *i64 72 build_net(flat, S, 0) 73 let meshF: i64 = (sys_mmap(m3_bytes())) as i64 74 bez_tessellate(flat, N, meshF) 75 let mzf: i64 = maxz(meshF) 76 var t4: i64 = 0 77 if mzf == 0 { t4 = 1 } 78 if t4 == 1 { hw("T4 PASS neg-control: a FLAT control net yields a FLAT surface (max z = 0)\n" as *u8) } else { fails=fails+1; hw("T4 FAIL flat net max z="); pn(mzf); hw("\n" as *u8) } 79 80 // T5 ARTIFACT 81 let nb: i64 = m3_write_stl(mesh, "knowledge/nx_bezier_patch.stl\x00" as *u8) 82 hw("T5 artifact -> knowledge/nx_bezier_patch.stl ("); pn(nb); hw(" bytes, "); pn(h[1]); hw(" tris)\n" as *u8) 83 84 // ---- MUTATION-DERIVED TOOTH: NON-ZERO CORNERS (2026-08-01, debt 1785604588) ---- 85 // nx_gate_mutation_probe scored this pair 2/4; the survivor at nx_bezier_patch.nx:33 is the X 86 // accumulation `sx = sx + w * net[p]` in the tensor-product evaluation. Flipping it to `-` negates 87 // every X on the surface. 88 // IT SURVIVED BECAUSE T2 TESTS THE ORIGIN CORNER. S(0,0) == P[0][0] == (0,0,0), and at zero a sign 89 // error is invisible: 0 - w*0 is still 0. The one row that checks coordinate values checked the one 90 // point where the defect cannot show. 91 // LAW: A TEST AT ZERO CANNOT DETECT A SIGN ERROR. Any assertion whose expected value is 0 is blind 92 // to negation, and an identity element is the most tempting test case precisely because it is the 93 // easiest to write down. 94 // The grid is (N+1)x(N+1) = 5x5 row-major, so vertex (i,j) = j*5 + i. Vertex 4 is S(1,0) = P[3][0] 95 // and vertex 24 is S(1,1) = P[3][3] -- both with NON-ZERO coordinates, which is the whole point. 96 // ⚠ EXPECTED VALUE DERIVED FROM THE NET, NOT ASSUMED. My first draft asserted S and the gate went 97 // RED at 76800: a 4x4 control net with spacing S spans THREE intervals, so the far corner is 3*S. 98 // I wrote the spacing where the extent belongs. Caught by running it -- which is why a new tooth is 99 // checked against correct code BEFORE it is trusted to judge anything. 100 let SPAN: i64 = 3 * S 101 let cx1: *i64 = m3_vert(mesh, 4) 102 let cx2: *i64 = m3_vert(mesh, 24) 103 var t6: i64 = 0 104 if cx1[0] == SPAN { if cx2[0] == SPAN { if cx2[1] == SPAN { t6 = 1 } } } 105 if t6 == 1 { hw("T6 PASS NON-ZERO corners: S(1,0).x == S and S(1,1) == (S,S) -- a sign error on x/y cannot hide at the origin\n" as *u8) } 106 if t6 == 0 { fails=fails+1; hw("T6 FAIL non-zero corner: S(1,0).x="); pn(cx1[0]); hw(" S(1,1)=("); pn(cx2[0]); hw(","); pn(cx2[1]); hw(") expected "); pn(SPAN); hw("\n" as *u8) } 107 108 // ---- MUTATION-DERIVED TOOTH: TOPOLOGY, NOT JUST COUNTS (2026-08-01, debt 1785615532) ---- 109 // Survivor at nx_bezier_patch.nx:66, `let v10 = jj*w + ii + 1` -- a TRIANGLE VERTEX INDEX. T1 checks 110 // 25 verts and 32 tris and passes regardless, because a corrupted index changes neither COUNT. 111 // LAW: A COUNT IS NOT A TOPOLOGY. Verifying how many triangles exist says nothing about whether any 112 // of them references the right vertices -- a mesh can be the correct size and still be garbage. 113 // Two invariants that need no expected geometry: every index must be IN RANGE (an out-of-range index 114 // is a read past the vertex array), and a triangle's three indices must be DISTINCT (two equal 115 // indices is a degenerate zero-area triangle, which is what an off-by-one on a grid edge produces). 116 let nv: i64 = h[0] 117 let nt: i64 = h[1] 118 var badtri: i64 = 0 119 var ti: i64 = 0 120 while ti < nt { 121 let tri: *i64 = m3_tri(mesh, ti) 122 let a0: i64 = tri[0] 123 let a1: i64 = tri[1] 124 let a2: i64 = tri[2] 125 if a0 < 0 { badtri = badtri + 1 } 126 if a1 < 0 { badtri = badtri + 1 } 127 if a2 < 0 { badtri = badtri + 1 } 128 if a0 >= nv { badtri = badtri + 1 } 129 if a1 >= nv { badtri = badtri + 1 } 130 if a2 >= nv { badtri = badtri + 1 } 131 if a0 == a1 { badtri = badtri + 1 } 132 if a1 == a2 { badtri = badtri + 1 } 133 if a0 == a2 { badtri = badtri + 1 } 134 ti = ti + 1 135 } 136 if badtri == 0 { hw("T7 PASS TOPOLOGY: every triangle index in range and all three distinct (no degenerate/out-of-range tris)\n" as *u8) } 137 if badtri != 0 { fails=fails+1; hw("T7 FAIL topology: "); pn(badtri); hw(" bad index/degeneracy conditions across "); pn(nt); hw(" tris\n" as *u8) } 138 139 if fails == 0 { hw("NX-BEZIER-PATCH GREEN -- parametric spline surface: corners interpolated, interior pulls a smooth bulge, neg-control flat\n" as *u8); sys_exit(0); return 0 } 140 hw("NX-BEZIER-PATCH RED fails="); pn(fails); hw("\n" as *u8) 141 sys_exit(1) 142 return 1 143}