Face pick by point: linear work budget, so bodies with many faces take a sketch (#6720) (#6721)

* claim: #6720

* Face pick by point: linear work budget, so bodies with many faces take a sketch (#6720)

A face picked by its point alone charged the whole mesh once per face, and
face_contains scanned every triangle for each candidate. A block with a
modelled thread (about 5 000 faces) went past the 2 000 000 limit. The
triangles are grouped by face once, each face's test reads only its own,
and the budget charges triangles + faces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan Szumiec
2026-10-26 11:16:17 +00:00
committed by GitHub
parent 82cd5cc52e
commit 00ff63f75e
3 changed files with 37 additions and 5 deletions
+16 -3
View File
@@ -709,9 +709,22 @@ pub(super) fn frame_reply(out: &mut Value, frame: Option<&str>, placement: &soli
}
}
/// Does triangle set of face `fi` contain point `p` (within `tol` of the face plane)?
pub(super) fn face_contains(m: &solidcraft_geom::Mesh, fi: usize, p: Vec3, tol: f64) -> bool {
m.triangles.iter().zip(&m.tri_face).filter(|(_, f)| **f as usize == fi).any(|(t, _)| {
/// The indices of `m`'s triangles by face, for `faces` faces (triangles of a face past that
/// are left out): one pass, so testing every face costs the triangle count once, not per face.
pub(super) fn face_triangles(m: &solidcraft_geom::Mesh, faces: usize) -> Vec<Vec<usize>> {
let mut by_face = vec![Vec::new(); faces];
for (i, f) in m.tri_face.iter().enumerate().take(m.triangles.len()) {
if let Some(tris) = by_face.get_mut(*f as usize) {
tris.push(i);
}
}
by_face
}
/// Does a face, the triangles `tris` of `m` (from [`face_triangles`]), contain point `p` (within
/// `tol` of the face plane)?
pub(super) fn face_contains(m: &solidcraft_geom::Mesh, tris: &[usize], p: Vec3, tol: f64) -> bool {
tris.iter().filter_map(|i| m.triangles.get(*i)).any(|t| {
let Some([a, b, c]) = m.tri(t) else { return false };
let n = (b - a).cross(c - a);
let Some(nn) = n.normalized() else { return false };
+6 -2
View File
@@ -123,10 +123,14 @@ fn capture_impl(s: &Session, value: &Value, cmd: &str, internal: bool) -> Result
}
let faces = body.body.faces(tolerance)?;
let mesh = body.body.tessellate(tolerance)?;
work = work.saturating_add(mesh.triangles.len().saturating_mul(if requested_index.is_some() { 1 } else { faces.len() }));
// The triangles are grouped by face once, and a face's test reads only its own: the
// work is the mesh plus one plane test per face, not the mesh once per face (a
// modelled thread leaves thousands of faces, #6720).
work = work.saturating_add(mesh.triangles.len()).saturating_add(faces.len());
if work > 2_000_000 {
return Err(bad(cmd, "face capture work limit exceeded; use an exact body and face index"));
}
let by_face = super::face_triangles(&mesh, faces.iter().map(|face| face.index.saturating_add(1)).max().unwrap_or(0));
for face in faces {
if requested_index.is_some_and(|index| index != face.index) {
continue;
@@ -140,7 +144,7 @@ fn capture_impl(s: &Session, value: &Value, cmd: &str, internal: bool) -> Result
};
let normal = exact_plane.normal();
if (local_point - face.centroid).dot(normal).abs() > tolerance * 10.0
|| !super::face_contains(&mesh, face.index, local_point, tolerance * 10.0)
|| !super::face_contains(&mesh, by_face.get(face.index).map_or(&[][..], Vec::as_slice), local_point, tolerance * 10.0)
{
continue;
}
@@ -737,3 +737,18 @@ fn a_sketch_on_a_face_meshes_its_body_once() {
run(&mut s, "sketch.finish", json!({}));
assert!(s.model.state().body("Base").unwrap().cached_mesh().is_some(), "the sketch's projection meshed a copy of the body");
}
/// Issue 6720: a face picked by its point alone was refused on a body with many faces. The
/// search charged the whole mesh once per face, and the 5 000 facets a modelled thread leaves
/// put a plain block past the limit. The search reads each face's own triangles now.
#[test]
fn a_face_picked_by_point_on_a_block_with_a_modelled_thread_is_found() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"corner":[-20,-20,0],"length":40,"width":40,"height":20,"body_name":"Block"}));
run(&mut s, "solid.cylinder", json!({"base":[0,0,-1],"radius":3.4,"height":22,"body_name":"Bore"}));
run(&mut s, "solid.combine", json!({"target":"Block","tools":["Bore"],"operation":"cut"}));
run(&mut s, "solid.thread", json!({"face":[3.4,0,10],"designation":"M8","modeled":true}));
assert!(s.model.state().body("Block").unwrap().body.face_count() > 1000);
run(&mut s, "sketch.create", json!({"plane":{"face":[15,15,20],"fixed":true}}));
let plane = run(&mut s, "sketch.inspect", json!({}))["plane"].clone();
assert!((plane["origin"][2].as_f64().unwrap() - 20.0).abs() < 1e-6, "{plane}");
}