Mesh faces whose boundary touches itself within tolerance (#1648) (#1850)

A face pinched to a point, where its boundary touches itself or another of its loops within
TOLERANCE (a sphere touching a box's edge leaves one on the box), had a constraint refused by the
CDT, and the fallback cut across the face: it came out with no triangles or without those at the
pinch. When a constraint is refused, the vendored truck-meshalgo now puts every boundary point
within TOLERANCE of a segment into it (at most 8192) and triangulates the face again. Faces whose
segments all go in mesh as before.

The regression tests live in crates/kernel/src/thin_face_tests.rs, and the #1648 property test in
boolean_props.rs is no longer ignored. Robustness seeds 20 and 55 are recorded with their Linux
values.

Co-authored-by: MateuszSychXpl <151755914+MateuszSychXpl@users.noreply.github.com>
This commit is contained in:
Mateusz Sych
2026-10-20 08:28:21 +00:00
committed by GitHub
parent e9b1a077d7
commit 8a562f2719
5 changed files with 138 additions and 8 deletions
+59
View File
@@ -1,11 +1,17 @@
//! A face thinner than the tolerance still meshes: the lens of a sphere's patch that pokes out
//! of a box's side has two edges that, sampled coarsely, run along one line in its parameters.
//! The shell's mesh had a hole there.
//!
//! So does a face pinched to a point, where its boundary touches itself within the tolerance (a
//! sphere touching a box's edge leaves one on the box, #1648).
use std::collections::HashMap;
use solidcraft_geom::Vec3;
use truck_meshalgo::prelude::*;
use truck_modeling as mt;
use crate::body::p3;
use crate::{BoolOp, build::box_solid, build::sphere, ops::boolean};
/// Triangle sides in the mesh with no triangle on the other side of them, with the points
@@ -41,3 +47,56 @@ fn a_lens_thinner_than_the_tolerance_is_meshed() {
assert_eq!(open_sides(&mesh), 0, "open sides at {tol}");
}
}
/// Twice the signed area of a closed polyline in the XY plane.
fn area2(points: &[mt::Point3]) -> f64 {
points.iter().zip(points.iter().cycle().skip(1)).map(|(p, q)| p.x * q.y - q.x * p.y).sum()
}
/// A face in the XY plane between y = 0 and an arch from (5, h) to (0, h) whose middle dips
/// 2.8e-7 below y = 0, as the intersection curve does in the union of #1648. Its loop starts
/// along the bottom, so the bottom goes into the triangulation before the arch.
#[test]
fn a_face_touching_its_own_edge_meshes() {
let (h, dip) = (0.3175, 2.8e-7);
let v = |x: f64, y: f64| mt::builder::vertex(p3(Vec3::new(x, y, 0.0)));
let (lb, rb, rt, lt) = (v(0.0, 0.0), v(5.0, 0.0), v(5.0, h), v(0.0, h));
let control = vec![p3(Vec3::new(5.0, h, 0.0)), p3(Vec3::new(2.5, -h - 2.0 * dip, 0.0)), p3(Vec3::new(0.0, h, 0.0))];
let arch = mt::BSplineCurve::new(mt::KnotVec::bezier_knot(2), control);
let arch = mt::Edge::new(&rt, &lt, mt::Curve::BSplineCurve(arch));
let wire: mt::Wire = vec![mt::builder::line(&lb, &rb), mt::builder::line(&rb, &rt), arch, mt::builder::line(&lt, &lb)].into();
let plane = mt::Plane::new(p3(Vec3::ZERO), p3(Vec3::X), p3(Vec3::Y));
let face = mt::Face::new(vec![wire], mt::Surface::Plane(plane));
let shell: mt::Shell = vec![face].into();
for tol in [0.3, 0.08, 0.02, 0.005] {
let meshed = shell.robust_triangulation_within(Deflection { rulings: true, ..Deflection::chord(tol) });
let face = meshed.face_iter().next().unwrap();
let boundary: Vec<mt::Point3> = face.boundaries()[0].edge_iter().flat_map(|e| e.oriented_curve().0.into_iter().skip(1)).collect();
let mesh = face.surface().unwrap();
let area: f64 = mesh
.faces()
.triangle_iter()
.map(|t| {
let [a, b, c] = [0, 1, 2].map(|i| mesh.positions()[t[i].pos]);
(b - a).cross(c - a).magnitude() / 2.0
})
.sum();
// The triangles cover the face's boundary polygon, the parts on both sides of the pinch.
let polygon = area2(&boundary).abs() / 2.0;
assert!((area - polygon).abs() < 1e-5, "at {tol}: triangles cover {area}, the boundary {polygon}");
}
}
/// The union of #1648: the sphere touches the box's bottom edge at (2.5, 7.5, 17.5).
#[test]
fn sphere_touching_a_box_edge_unites_soundly() {
let a = sphere(Vec3::new(2.5, 15.0, 27.5), 12.5).unwrap();
let b = box_solid(Vec3::new(0.0, 0.0, 17.5), Vec3::new(5.0, 7.5, 22.5)).unwrap();
let union = boolean(&a, &b, BoolOp::Union).unwrap().unwrap();
assert!(union.validity().is_empty(), "{:?}", union.validity());
let size = union.size();
for tol in [size * 1e-2, size * 2.5e-3, size * 6e-4] {
let faces: std::collections::HashSet<u32> = union.tessellate(tol).unwrap().tri_face.into_iter().collect();
assert_eq!(faces.len(), union.face_count(), "at {tol}");
}
}