Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions crates/forma-core/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,16 @@ fn point_in_tri_3d(p: Vec3, t: &Tri) -> bool {
let c0 = e0.cross(p - t.v[0]).dot(t.normal);
let c1 = e1.cross(p - t.v[1]).dot(t.normal);
let c2 = e2.cross(p - t.v[2]).dot(t.normal);
c0 >= -1e-7 && c1 >= -1e-7 && c2 >= -1e-7
if c0 >= 0.0 && c1 >= 0.0 && c2 >= 0.0 {
return true;
}
// Outside, `p` is in when within 1e-7 of an edge, the edge itself and not its line: past a
// needle's sharp corner both long edges' lines stay that close for far longer than the needle.
let q = p - t.normal * (t.normal.dot(p) - t.w);
[(t.v[0], e0), (t.v[1], e1), (t.v[2], e2)].into_iter().any(|(a, e)| {
let s = (q - a).dot(e) / e.len_sq().max(1e-300);
(q - (a + e * s.clamp(0.0, 1.0))).len_sq() <= 1e-14
})
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand All @@ -806,11 +815,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
Expand Down Expand Up @@ -3613,3 +3624,21 @@ fn self_intersection_pass(mesh: &PolyMesh, frame: Option<[Vec3; 3]>, budget: u64
}
OverlapCheck::Clear
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn point_in_tri_tolerance_is_a_distance_even_past_a_short_edge() {
// A long sliver whose short edge, 1e-4 long, closes it at x = 1.
let t = Tri::from_verts(Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 1e-4, 0.0), 0, 0, 0).unwrap();
assert!(point_in_tri_3d(Vec3::new(0.999, 0.5e-4, 0.0), &t));
// 1e-4 past the short edge, within the wedge of the long ones: outside.
assert!(!point_in_tri_3d(Vec3::new(1.0001, 0.5e-4, 0.0), &t));
// 1e-3 past its sharp corner, within 1e-7 of both long edges' lines: outside.
assert!(!point_in_tri_3d(Vec3::new(-1e-3, -0.5e-7, 0.0), &t));
// Within 1e-7 of an edge: in.
assert!(point_in_tri_3d(Vec3::new(0.5, -0.5e-7, 0.0), &t) && point_in_tri_3d(Vec3::new(-0.5e-7, 0.0, 0.0), &t));
}
}
131 changes: 131 additions & 0 deletions crates/forma-core/tests/boolean_seams.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Loading