code wiki / (root) / nx_slice_contour_test.nx

nx_slice_contour_test.nx source

↩ module page · 73 lines · 2271 B

1// nx_slice_contour_test.nx -- end-to-end: tetrahedron mesh -> 2// BVH -> slice at midplane -> contour assembly. Verifies the 3// 3 segments produced by nx_slice_plane_smoke get linked into 4// exactly 1 closed polygon (the right triangle at z=0.5mm). 5// 6// Closed-form invariants: 7// (a) Contours verdict == OK (every contour closed). 8// (b) Exactly 1 polygon emitted. 9// (c) n_open == 0. 10// (d) Polygon has exactly 3 vertices (= 3 segments forming a 11// closed triangle). 12// (e) The vertex set equals {(0,0), (8192,0), (0,8192)} (any 13// cyclic order accepted). 14// (f) Polygon is well-formed: nx_polygon_signed_2area_q28 != 0 15// (non-degenerate triangle). 16// 17// expect_exit: 0 18// license_tier: ORIGINAL 19 20import "nx_syscalls.nx" 21import "nx_mesh.nx" 22import "nx_mesh_print_check.nx" 23import "nx_bvh.nx" 24import "nx_slice_plane.nx" 25import "nx_polygon.nx" 26import "nx_slice_contour.nx" 27 28const Q14_ONE: i64 = 16384 29const Q14_HALF: i64 = 8192 30 31// Returns 1 if (x,y) appears as a vertex in p. 32func smoke_has_vert(p: *NxPolygon, x: i64, y: i64) -> i64 { 33 var i: i64 = 0 34 while i < p.n_verts { 35 if nx_polygon_get_x(p, i) == x { 36 if nx_polygon_get_y(p, i) == y { return 1 } 37 } 38 i = i + 1 39 } 40 return 0 41} 42 43func main() -> i64 { 44 let m: *NxMesh = nx_mesh_make_tetrahedron(Q14_ONE, 0) 45 let b: *NxBvh = nx_bvh_build(m) 46 let s: *NxSliceSoup = nx_slice_plane(m, b, Q14_HALF) 47 if s.n_segments != 3 { return 5 } // precondition 48 49 let c: *NxSliceContours = nx_slice_contour_build(s) 50 51 // --- (a)(c) Verdict + open count --- 52 if c.verdict != NX_SLICE_CONTOUR_OK { return 10 } 53 if c.n_open != 0 { return 11 } 54 55 // --- (b) Polygon count --- 56 if c.n_polys != 1 { return 20 } 57 58 // --- (d) Vertex count --- 59 let p: *NxPolygon = nx_slice_contours_get(c, 0) 60 if (p as i64) == 0 { return 30 } 61 if p.n_verts != 3 { return 31 } 62 63 // --- (e) Vertex set --- 64 if smoke_has_vert(p, 0, 0) != 1 { return 40 } 65 if smoke_has_vert(p, Q14_HALF, 0) != 1 { return 41 } 66 if smoke_has_vert(p, 0, Q14_HALF) != 1 { return 42 } 67 68 // --- (f) Non-degenerate --- 69 let area2: i64 = nx_polygon_signed_2area_q28(p) 70 if area2 == 0 { return 50 } 71 72 return 0 73}