Skip to content

armgit / forma

Public Imported from GitHub
0

A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server

fix(core): close boolean seams at slivers and near-parallel faces

pnooyen336de90a479b
diff --git a/crates/forma-core/src/boolean.rs b/crates/forma-core/src/boolean.rs
index 229d2d8..8ff6653 100644
--- a/crates/forma-core/src/boolean.rs
+++ b/crates/forma-core/src/boolean.rs
@@ -725,11 +725,13 @@ fn classify_point_vs_mesh(
other_bvh: &TriBvh,
local_cands: &[u32],
) -> Loc {
- // Check if `p` lies on a coplanar triangle of `other`
+ // Check if `p` lies on a coplanar triangle of `other`, by the normal test `subdivide_tri_local`
+ // splits with: a nearly parallel pair it cut along their crossing line is classified by winding
+ // on both sides of it, never as coplanar on the fragments that happen to lie within 1e-5 of it.
for &ci in local_cands {
let tb = &other_tris[ci as usize];
let dn = n_p.dot(tb.normal);
- if dn.abs() > 1.0 - 1e-5 && (tb.normal.dot(p) - tb.w).abs() < 1e-5 {
+ if dn.abs() > 1.0 - 1e-6 && (tb.normal.dot(p) - tb.w).abs() < 1e-5 {
if point_in_tri_3d(p, tb) {
return if dn > 0.0 {
Loc::CoplanarSame
@@ -1385,6 +1387,19 @@ fn canonical_tri(a: usize, b: usize, c: usize) -> ([usize; 3], i32) {
/// Fast sort-based boundary vertex extraction (no HashMap SipHash overhead).
fn collect_bad_verts(mesh: &PolyMesh) -> Vec<usize> {
+ verts_of_edges(&collect_bad_edges(mesh))
+}
+
+/// Sorted, deduplicated endpoints of `edges`.
+fn verts_of_edges(edges: &[(usize, usize)]) -> Vec<usize> {
+ let mut verts: Vec<usize> = edges.iter().flat_map(|&(a, b)| [a, b]).collect();
+ verts.sort_unstable();
+ verts.dedup();
+ verts
+}
+
+/// Sorted keys of the boundary + non-manifold edges (zero HashMaps).
+fn collect_bad_edges(mesh: &PolyMesh) -> Vec<(usize, usize)> {
let mut edges: Vec<(usize, usize)> = Vec::with_capacity(mesh.faces.len() * 3);
for f in &mesh.faces {
let n = f.verts.len();
@@ -1397,7 +1412,7 @@ fn collect_bad_verts(mesh: &PolyMesh) -> Vec<usize> {
}
}
edges.sort_unstable();
- let mut bad: Vec<usize> = Vec::new();
+ let mut bad: Vec<(usize, usize)> = Vec::new();
let mut i = 0;
while i < edges.len() {
let e = edges[i];
@@ -1406,13 +1421,10 @@ fn collect_bad_verts(mesh: &PolyMesh) -> Vec<usize> {
j += 1;
}
if j - i != 2 {
- bad.push(e.0);
- bad.push(e.1);
+ bad.push(e);
}
i = j;
}
- bad.sort_unstable();
- bad.dedup();
bad
}
@@ -1532,7 +1544,8 @@ fn eliminate_t_junctions(mesh: &mut PolyMesh, seg_tol: f64) {
if mesh.faces.len() >= max_faces {
return;
}
- let bverts = collect_bad_verts(mesh);
+ let bedges = collect_bad_edges(mesh);
+ let bverts = verts_of_edges(&bedges);
if bverts.is_empty() {
return;
}
@@ -1606,6 +1619,12 @@ fn eliminate_t_junctions(mesh: &mut PolyMesh, seg_tol: f64) {
if ab_len_sq < end_tol_sq * 4.0 {
continue;
}
+ // Only an open edge has a T-junction. A closed one stays whole even when a vertex
+ // lies within `seg_tol` of it (past a neighbouring sliver thinner than `seg_tol`):
+ // splitting it on one side only would open it.
+ if bedges.binary_search(&edge_key(tri[ei], tri[(ei + 1) % 3])).is_err() {
+ continue;
+ }
let e_min = a.min(b) - Vec3::splat(pad);
let e_max = a.max(b) + Vec3::splat(pad);
// Endpoint exclusion as a fraction of the edge, capped to an absolute length so a
diff --git a/crates/forma-core/tests/boolean_seams.rs b/crates/forma-core/tests/boolean_seams.rs
new file mode 100644
index 0000000..72a4032
--- /dev/null
+++ b/crates/forma-core/tests/boolean_seams.rs
@@ -0,0 +1,131 @@
+//! Boolean seams that must close:
+//! - Holes drilled one after another into one face of a board. The first cut leaves the face as a
+//! fan with a long sliver from a corner to the hole's rim, thinner than the T-junction tolerance;
+//! the second cut's seam vertices then lie within that tolerance of both its long edges.
+//! - Two round prisms of the same radius with different facets (near-parallel faces crossing at
+//! small angles), intersected.
+
+use forma_core::boolean::{boolean_op, BoolOp};
+use forma_core::invariants::MeshInvariants;
+use forma_core::math::Vec3;
+use forma_core::mesh::PolyMesh;
+use forma_core::primitives::{make_box, make_cone};
+
+/// 564 x 560 x 18, from (18, 0, 100) to (582, 560, 118).
+fn board() -> PolyMesh {
+ let mut b = make_box(564.0, 560.0, 18.0, 0);
+ b.translate(Vec3::new(300.0, 280.0, 109.0));
+ b
+}
+
+/// A 25-sided rod of radius 4, 9.5 long along `axis` (a column-major rotation) from `at`.
+fn rod(axis: [f64; 9], at: Vec3) -> PolyMesh {
+ let mut c = make_cone(4.0, 4.0, 9.5, 25, 0);
+ c.translate(Vec3::new(0.0, 0.0, 4.75));
+ let m = axis;
+ for v in &mut c.verts {
+ let (x, y, z) = (v.x, v.y, v.z);
+ *v = Vec3::new(m[0] * x + m[3] * y + m[6] * z, m[1] * x + m[4] * y + m[7] * z, m[2] * x + m[5] * y + m[8] * z) + at;
+ }
+ c
+}
+
+/// Up from the bottom face, turned a quarter about z; down from the top face; into the front
+/// face (y = 0); into the left face (x = 18).
+const UP: [f64; 9] = [0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
+const DOWN: [f64; 9] = [1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0];
+const ALONG_Y: [f64; 9] = [1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0];
+const ALONG_X: [f64; 9] = [0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0];
+
+#[test]
+fn two_holes_in_a_line_leave_a_closed_board() {
+ let once = boolean_op(&board(), &rod(UP, Vec3::new(550.0, 50.0, 99.5)), BoolOp::Difference, None, 0).unwrap();
+ assert!(MeshInvariants::compute(&once).is_valid_solid(), "first hole");
+ let twice = boolean_op(&once, &rod(UP, Vec3::new(550.0, 510.0, 99.5)), BoolOp::Difference, None, 0).unwrap();
+ let inv = MeshInvariants::compute(&twice);
+ assert!(inv.is_valid_solid(), "second hole: {inv:?}");
+}
+
+/// Drills `holes` 9 mm deep holes, one by one, at seeded positions into the board's `faces` (0
+/// bottom, 1 top, 2 front, 3 left), half of them on a 32 mm grid so rims line up; after each the
+/// board must be a closed solid with one more hole's volume gone.
+fn drill_seeded(seed: u64, holes: usize, faces: &[usize]) {
+ let mut s = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
+ let mut next = move || {
+ s ^= s << 13;
+ s ^= s >> 7;
+ s ^= s << 17;
+ (s >> 11) as f64 / (1u64 << 53) as f64
+ };
+ let hole = 0.5 * 25.0 * 16.0 * (2.0 * std::f64::consts::PI / 25.0).sin() * 9.0;
+ let mut part = board();
+ let expected = MeshInvariants::compute(&part).volume;
+ let mut taken: Vec<(usize, f64, f64)> = Vec::new();
+ let mut k = 0;
+ while k < holes {
+ let face = faces[(next() * faces.len() as f64) as usize];
+ let grid = next() < 0.5;
+ // (u, v) on the face: x then y, x then z, or y then z; clear of the board's edges.
+ let (u_lo, u_hi, v_lo, v_hi) = match face {
+ 0 | 1 => (50.0, 550.0, 30.0, 530.0),
+ 2 => (30.0, 570.0, 109.0, 109.0),
+ _ => (30.0, 530.0, 109.0, 109.0),
+ };
+ let pick = |t: f64, lo: f64, hi: f64| if grid { lo + (t * (hi - lo) / 32.0).floor() * 32.0 } else { lo + t * (hi - lo) };
+ let (u, v) = (pick(next(), u_lo, u_hi), pick(next(), v_lo, v_hi));
+ // Top and bottom holes meet at mid-thickness, so they keep clear of each other too.
+ let near = |f: usize| f == face || f.max(face) == 1;
+ if taken.iter().any(|&(f, tu, tv)| near(f) && (tu - u).hypot(tv - v) < 12.0) {
+ continue;
+ }
+ taken.push((face, u, v));
+ let tool = match face {
+ 0 => rod(UP, Vec3::new(u, v, 99.5)),
+ 1 => rod(DOWN, Vec3::new(u, v, 118.5)),
+ 2 => rod(ALONG_Y, Vec3::new(u, -0.5, v)),
+ _ => rod(ALONG_X, Vec3::new(17.5, u, v)),
+ };
+ part = boolean_op(&part, &tool, BoolOp::Difference, None, 0).unwrap();
+ k += 1;
+ let inv = MeshInvariants::compute(&part);
+ let want = expected - k as f64 * hole;
+ assert!(inv.is_valid_solid(), "seed {seed}, hole {k} {:?}: {inv:?}", taken.last().unwrap());
+ assert!((inv.volume - want).abs() < 1e-6 * want, "seed {seed}, hole {k}: volume {} not {want}", inv.volume);
+ }
+}
+
+#[test]
+fn many_holes_in_one_face_leave_a_closed_board() {
+ for seed in 0..20 {
+ drill_seeded(seed, 24, &[0]);
+ }
+}
+
+#[test]
+fn many_holes_in_several_faces_leave_a_closed_board() {
+ for seed in 110..120 {
+ drill_seeded(seed, 24, &[0, 1, 2, 3]);
+ }
+}
+
+#[test]
+fn round_prisms_with_different_facets_intersect_closed() {
+ for na in [120usize, 122, 124] {
+ for nb in [120usize, 122, 124] {
+ for turn in [0.0f64, 0.1] {
+ let a = make_cone(450.0, 450.0, 100.0, na, 0);
+ let mut b = make_cone(450.0, 450.0, 300.0, nb, 0);
+ let (sin, cos) = turn.to_radians().sin_cos();
+ for v in &mut b.verts {
+ *v = Vec3::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos, v.z);
+ }
+ let both = boolean_op(&a, &b, BoolOp::Intersect, None, 0).unwrap();
+ let inv = MeshInvariants::compute(&both);
+ let va = MeshInvariants::compute(&a).volume;
+ assert!(inv.is_valid_solid(), "{na} and {nb} sides, turned {turn} deg: {inv:?}");
+ // Both are within their sagitta (~0.1 mm) of the same round.
+ assert!(inv.volume <= va * (1.0 + 1e-9) && inv.volume > va * (1.0 - 1e-3), "{na} and {nb} sides, turned {turn} deg: volume {} of {va}", inv.volume);
+ }
+ }
+ }
+}