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): point_in_tri_3d tolerance to the edges, not past a sharp corner

pnooyena3a1eaf23d0c
diff --git a/crates/forma-core/src/boolean.rs b/crates/forma-core/src/boolean.rs
index 78ba21c..7932c36 100644
--- a/crates/forma-core/src/boolean.rs
+++ b/crates/forma-core/src/boolean.rs
@@ -707,9 +707,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);
- // Each `c` is the edge's length times `p`'s distance inside it: the tolerance is a distance,
- // or a short edge would take in points far past it.
- c0 >= -1e-7 * e0.len() && c1 >= -1e-7 * e1.len() && c2 >= -1e-7 * e2.len()
+ 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)]
@@ -3087,5 +3094,9 @@ mod tests {
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));
}
}