From e016c40f4323aa78217243d206bfd93998ff0330 Mon Sep 17 00:00:00 2001 From: jeromehzl <51324719+jeromehzl@users.noreply.github.com> Date: Mon, 2 Nov 2026 00:16:06 +0000 Subject: [PATCH] Sketch Intersect: picked faces meet as their whole surface, not the span under the pick (#9009) (#9107) A surface extruded from a fit-point spline is one B-rep face per span, joined by hidden seams; the viewport picks and highlights it as one face (Mesh::surface_patch). sketch.intersection_curve turned a face pick into the single nearest face, so the curve stopped at that span's ends. project3d::mesh_of now returns the picked face's surface patch and mesh_intersection keeps the triangles of every face in it. Body sources and Project to Surface are unchanged. --- crates/doc/src/project.rs | 2 +- crates/doc/src/project3d.rs | 19 ++--- crates/engine/src/cmd/sketch_project_tests.rs | 71 +++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/crates/doc/src/project.rs b/crates/doc/src/project.rs index 3887765b7..f1497a660 100644 --- a/crates/doc/src/project.rs +++ b/crates/doc/src/project.rs @@ -208,7 +208,7 @@ fn resolve_uncached( LinkSource::Intersection { a, b } => { let (ma, fa) = crate::project3d::mesh_of(st, a).ok_or_else(|| lost(&a.describe()))?; let (mb, fb) = crate::project3d::mesh_of(st, b).ok_or_else(|| lost(&b.describe()))?; - wires = crate::project3d::mesh_intersection(&ma, fa, &mb, fb); + wires = crate::project3d::mesh_intersection(&ma, fa.as_deref(), &mb, fb.as_deref()); Vec::new() } LinkSource::Iso { body, at, dir } => { diff --git a/crates/doc/src/project3d.rs b/crates/doc/src/project3d.rs index 42f23a37c..68f0a0f2e 100644 --- a/crates/doc/src/project3d.rs +++ b/crates/doc/src/project3d.rs @@ -12,15 +12,17 @@ use crate::eval::ModelState; /// Most triangle pairs examined by one intersection. const MAX_PAIRS: usize = 20_000_000; -/// The mesh (and face, for a face source) of a body or face reference. -pub(crate) fn mesh_of(st: &ModelState, src: &LinkSource) -> Option<(Arc, Option)> { +/// The mesh of a body or face reference and, for a face, the faces of its surface patch: the B-rep +/// pieces joined across hidden seams, as the viewport picks and highlights it (#9009). +pub(crate) fn mesh_of(st: &ModelState, src: &LinkSource) -> Option<(Arc, Option>)> { match src { LinkSource::Body { body } => Some((st.body(body)?.mesh(), None)), LinkSource::Face { body, at } => { let b = st.body(body).or_else(|| st.bodies.iter().min_by(|x, y| dist_to(&x.mesh(), *at).total_cmp(&dist_to(&y.mesh(), *at))))?; let m = b.mesh(); let (f, _, _) = crate::project::nearest_face(&m, *at)?; - Some((m, Some(f))) + let patch: Vec = m.surface_patch(f as usize).into_iter().filter_map(|i| u32::try_from(i).ok()).collect(); + Some((m, Some(patch))) } _ => None, } @@ -30,8 +32,9 @@ fn dist_to(m: &Mesh, p: Vec3) -> f64 { crate::project::nearest_face(m, p).map(|x| x.2).unwrap_or(f64::INFINITY) } -fn tris(m: &Mesh, face: Option) -> Vec<[Vec3; 3]> { - m.triangles.iter().zip(&m.tri_face).filter(|(_, f)| face.is_none_or(|x| x == **f)).filter_map(|(t, _)| m.tri(t)).collect() +/// The triangles of the faces in `faces` (sorted ascending), or of the whole mesh for `None`. +fn tris(m: &Mesh, faces: Option<&[u32]>) -> Vec<[Vec3; 3]> { + m.triangles.iter().zip(&m.tri_face).filter(|(_, f)| faces.is_none_or(|fs| fs.binary_search(f).is_ok())).filter_map(|(t, _)| m.tri(t)).collect() } /// Ray–triangle hit parameter (Möller–Trumbore), any sign. @@ -60,7 +63,7 @@ fn ray_hit(o: Vec3, d: Vec3, t: &[Vec3; 3]) -> Option { /// miss the face split the result. pub(crate) fn onto_face(m: &Mesh, f: u32, pts: &[Vec3], dir: Vec3) -> Vec> { let Some(d) = dir.normalized() else { return Vec::new() }; - let ts = tris(m, Some(f)); + let ts = tris(m, Some(std::slice::from_ref(&f))); let size = m.bounds().diagonal().max(1e-9); // Resample so the projected curve follows the surface. let step = size / 200.0; @@ -138,8 +141,8 @@ fn tri_tri(a: &[Vec3; 3], b: &[Vec3; 3]) -> Option<(Vec3, Vec3)> { (pr(hi) - pr(lo) > 1e-12).then_some((lo, hi)) } -/// Where two meshes (or faces of them) meet, as polylines. -pub(crate) fn mesh_intersection(ma: &Mesh, fa: Option, mb: &Mesh, fb: Option) -> Vec> { +/// Where two meshes (or face patches of them) meet, as polylines. +pub(crate) fn mesh_intersection(ma: &Mesh, fa: Option<&[u32]>, mb: &Mesh, fb: Option<&[u32]>) -> Vec> { let (ta, tb) = (tris(ma, fa), tris(mb, fb)); let bbox = |t: &[Vec3; 3]| (t[0].min(t[1]).min(t[2]), t[0].max(t[1]).max(t[2])); // Uniform grid over B. diff --git a/crates/engine/src/cmd/sketch_project_tests.rs b/crates/engine/src/cmd/sketch_project_tests.rs index c76c364e7..b4d5823af 100644 --- a/crates/engine/src/cmd/sketch_project_tests.rs +++ b/crates/engine/src/cmd/sketch_project_tests.rs @@ -500,6 +500,77 @@ fn three_d_sketch_curves() { assert!((c["start_at"][0].as_f64().unwrap().abs() - 6.0).abs() < 1e-3, "{c}"); } +/// #9009: picked faces intersect as the viewport picks them, the whole surface patch, not just the +/// B-rep piece under the pick; a fit-point spline's wall is one face per span joined by hidden seams. +#[test] +fn intersection_curve_of_picked_faces_spans_the_whole_surface() { + use solidcraft_doc::project::resolve; + use solidcraft_geom::Plane; + use solidcraft_sketch::{LinkKind, LinkSource}; + use std::collections::BTreeMap; + + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "wall_h", "expression": "20 mm"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Wave"})); + let wave = ids(&run(&mut s, "sketch.spline.fit_point", json!({"points": [[0, 0], [9.5, 12], [30.5, 7], [52, 15]]}))["curves"])[0].clone(); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "surface.extrude", json!({"sketch": "Wave", "curves": [wave], "distance": "wall_h", "body_name": "Wall"})); + run(&mut s, "sketch.create", json!({"plane": "XZ", "name": "Roof"})); + // XZ sketch y is world -Z, so the arc peaks at world z = 15. + let arc = run(&mut s, "sketch.arc.three_point", json!({"start": [0, 0], "end": [50, 0], "through": [25, -15]}))["created"]["curves"][0] + .as_str() + .unwrap() + .to_string(); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "surface.extrude", json!({"sketch": "Roof", "curves": [arc], "distance": 30, "body_name": "Roof"})); + + let st = s.model.state(); + let wall = st.body("Wall").unwrap(); + let m = wall.mesh(); + assert!(wall.body.face_count() > 1, "premise of #9009: one face per spline span"); + assert_eq!(m.surface_patch(m.tri_face[0] as usize).len(), wall.body.face_count(), "premise: the spans are one surface patch (hidden seams)"); + // A pick on the middle span, away from the joins at x = 9.5 and x = 30.5. + let pick = + *m.positions.iter().min_by(|a, b| ((a.x - 20.).abs() + (a.z - 10.).abs()).total_cmp(&((b.x - 20.).abs() + (b.z - 10.).abs()))).unwrap(); + + let check = |s: &Session, id: u64| { + let st = s.model.state(); + let sk = &st.sketch(id).unwrap().sketch; + assert!(sk.links.iter().all(|l| !l.lost), "no link lost"); + let pts: Vec<_> = sk.wires.iter().flat_map(|w| w.pts.iter().copied()).collect(); + assert!(!pts.is_empty()); + let (lo, hi) = pts.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(l, h), p| (l.min(p.x), h.max(p.x))); + assert!(lo < 1.0 && hi > 49.0, "the whole crossing, not one span: x {lo}..{hi}"); + for p in &pts { + assert!((((p.x - 25.).powi(2) + (p.z + 40. / 3.).powi(2)).sqrt() - 85. / 3.).abs() < 0.1, "{p:?}"); + assert!(p.y > -1e-6 && p.y < 30. + 1e-6, "{p:?}"); + } + }; + + run(&mut s, "sketch.create", json!({"plane": "XY"})); + let id = s.active_sketch.unwrap(); + let r = run( + &mut s, + "sketch.intersection_curve", + json!({"a": {"face": [pick.x, pick.y, pick.z], "body": "Wall"}, "b": {"face": [25, 15, 15], "body": "Roof"}}), + ); + assert!(!r["wires"].as_array().unwrap().is_empty(), "{r}"); + check(&s, id); + + // The same as giving the two bodies. + let st = s.model.state(); + let body = |n: &str| Box::new(LinkSource::Body { body: n.to_string() }); + let whole = resolve(&s.doc, &BTreeMap::new(), &st, &Plane::XY, LinkKind::Include, &LinkSource::Intersection { a: body("Wall"), b: body("Roof") }) + .unwrap(); + let sk = &st.sketch(id).unwrap().sketch; + assert_eq!(sk.wires.len(), whole.wires.len()); + + // Editing the wall re-resolves the stored pick to the same patch. + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "parameters.change", json!({"name": "wall_h", "expression": "25 mm"})); + check(&s, id); +} + #[test] fn isoparametric_curves_of_a_cylinder() { let mut s = Session::default();