From e37a8fa6bb69bfdc5939443b0cd03b6da3778358 Mon Sep 17 00:00:00 2001 From: Lucas Montenegro Carvalhaes Date: Tue, 20 Oct 2026 12:38:48 +0000 Subject: [PATCH] Fillet: skip smooth seams in tangent chain and restore picked edges when unticked (#3370) (#3437) * claim: #3370 * fillet: stop tangent chains at smooth seams and sync chain toggle (#3370) --- crates/geom/src/mesh.rs | 33 ++++++- crates/geom/src/tests.rs | 12 +++ crates/ui-egui/src/dialogs.rs | 119 ++++++++++++++++++++------ crates/ui-egui/src/preselect_tests.rs | 48 +++++++++++ 4 files changed, 185 insertions(+), 27 deletions(-) diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs index f6234320d..eea0cd305 100644 --- a/crates/geom/src/mesh.rs +++ b/crates/geom/src/mesh.rs @@ -448,6 +448,34 @@ impl Mesh { rim.sort_unstable(); rim } + /// Whether the two faces at edge `i` meet tangentially there (a smooth seam, with no corner). + fn smooth_seam(&self, i: usize, cos: f64) -> bool { + if self.sharp_edges.get(i).copied().unwrap_or(false) { + return false; + } + if self.smooth_edges.get(i).copied().unwrap_or(false) { + return true; + } + let Some([a, b]) = self.edge_faces.get(i).map(Vec::as_slice) else { return false }; + if a == b { + return false; + } + let Some(p) = self.edges.get(i) else { return false }; + let (Some(&first), Some(&mid)) = (p.first(), p.get(p.len() / 2)) else { return false }; + let normal_near = |f: u32, q: Vec3| -> Option { + self.triangles + .iter() + .zip(&self.tri_face) + .filter(|(_, tf)| **tf == f) + .flat_map(|(t, _)| t.iter().copied()) + .filter_map(|v| Some((self.positions.get(v as usize)?.dist(q), *self.normals.get(v as usize)?))) + .min_by(|x, y| x.0.total_cmp(&y.0)) + .and_then(|(_, n)| n.normalized()) + }; + let both = [first, mid]; + let sample = if p.len() == 2 { &both[..] } else { std::slice::from_ref(&mid) }; + sample.iter().all(|&q| matches!((normal_near(*a, q), normal_near(*b, q)), (Some(na), Some(nb)) if na.dot(nb) > cos)) + } /// The edges reached from `e` through vertices where the next edge continues smoothly /// (angle between the end tangents below `max_angle`), including `e` itself. pub fn tangent_chain(&self, e: usize, max_angle: f64) -> Vec { @@ -473,11 +501,14 @@ impl Mesh { let tol = size * 1e-7; let cos = max_angle.cos(); let mut chain = vec![e]; + if self.smooth_seam(e, cos) { + return chain; + } let mut todo = vec![e]; while let Some(cur) = todo.pop() { let Some(ce) = ends(cur) else { continue }; for (i, _) in self.edges.iter().enumerate() { - if chain.contains(&i) || !self.edge_pickable(i) { + if chain.contains(&i) || !self.edge_pickable(i) || self.smooth_seam(i, cos) { continue; } let Some(ne) = ends(i) else { continue }; diff --git a/crates/geom/src/tests.rs b/crates/geom/src/tests.rs index a2b494349..a50384a86 100644 --- a/crates/geom/src/tests.rs +++ b/crates/geom/src/tests.rs @@ -150,6 +150,18 @@ fn tangent_chains_follow_smooth_edges() { assert_eq!(c, vec![0, 1]); assert_eq!(m.tangent_chain(2, 5f64.to_radians()), vec![2]); assert_eq!(m.face_edges(1), vec![1, 2]); + // A smooth seam where two faces share the same normal along the edge is not a corner to round, + // so tangent chaining stops at it. + let seam = Mesh { + positions: vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 0.0, 0.0), Vec3::new(20.0, 0.0, 0.0), Vec3::new(15.0, 5.0, 0.0)], + normals: vec![Vec3::Z; 4], + triangles: vec![[0, 1, 3], [1, 2, 3], [1, 2, 3]], + tri_face: vec![0, 1, 2], + edges: vec![vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 0.0, 0.0)], vec![Vec3::new(10.0, 0.0, 0.0), Vec3::new(20.0, 0.0, 0.0)]], + edge_faces: vec![vec![0], vec![1, 2]], + ..Default::default() + }; + assert_eq!(seam.tangent_chain(0, 5f64.to_radians()), vec![0]); } #[test] diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 89a5b5e1f..aeaa2115d 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -408,6 +408,10 @@ pub struct Dialog { pub refused: Option, /// Create Sketch opened by a sketch tool's shortcut: that tool starts once the sketch is open. pub then_start: Option<&'static str>, + /// Edges picked by hand in a Fillet or Chamfer dialog, before tangent-chain expansion. + pub(crate) picked_edges: Vec, + /// Whether `inputs[0]` currently includes the tangent chains of `picked_edges`. + pub(crate) chain_applied: bool, } /// OK computing off the window thread: the dialog's commands evaluated on a scratch copy of the @@ -507,6 +511,7 @@ impl Dialog { } pub(crate) fn new(kind: Kind, inputs: Vec) -> Dialog { + let chain_applied = matches!(kind, Kind::Fillet { chain: true, .. }); Dialog { kind, inputs, @@ -519,6 +524,8 @@ impl Dialog { extra: serde_json::Map::new(), refused: None, then_start: None, + picked_edges: Vec::new(), + chain_applied, } } @@ -766,20 +773,16 @@ impl Dialog { fits(i.accept, sel) && (i.multi || i.items.is_empty()) && (planar_ok || i.accept & FACES != 0 || !matches!(sel, Sel::Face { .. })) }; if let Some(inp) = d.inputs.iter_mut().find(|i| takes(i)) { + if matches!(d.kind, Kind::Fillet { .. }) { + for x in crate::viewport::grouped_selection(s, sel) { + if !d.picked_edges.iter().any(|y| selection::same_item(y, &x)) { + d.picked_edges.push(x); + } + } + } // Fillets and chamfers take an edge with its tangent chain, as a pick does. let picked = match (&d.kind, sel) { - (Kind::Fillet { chain: true, .. }, Sel::Edge { body, index, .. }) => s - .model - .state() - .body(body) - .map(|b| { - let m = b.mesh(); - m.tangent_chain(*index, 2f64.to_radians()) - .into_iter() - .filter_map(|i| m.edges.get(i).map(|e| Sel::Edge { body: body.clone(), index: i, point: polyline_mid(e) })) - .collect::>() - }) - .unwrap_or_else(|| vec![sel.clone()]), + (Kind::Fillet { chain, .. }, _) => fillet_edges(s, sel, *chain), (Kind::Measure { .. }, _) => vec![crate::viewport::measurement_selection(s, sel)], _ if inp.multi => crate::viewport::grouped_selection(s, sel), _ => vec![sel.clone()], @@ -1045,20 +1048,10 @@ impl Dialog { let measure = matches!(self.kind, Kind::Measure { .. }); let sel = if measure { crate::viewport::measurement_selection(s, &sel) } else { sel }; let chain = matches!(self.kind, Kind::Fillet { chain: true, .. }); - let mut picked = + let single = if !measure && self.active_input().is_some_and(|i| i.multi) { crate::viewport::picked_selection(s, &sel) } else { vec![sel.clone()] }; // Tangent chain: an edge brings the edges that continue it smoothly. - if chain - && let Sel::Edge { body, index, .. } = &sel - && let Some(b) = s.world_state().body(body) - { - let m = b.mesh(); - picked = m - .tangent_chain(*index, 2f64.to_radians()) - .into_iter() - .filter_map(|i| m.edges.get(i).map(|e| Sel::Edge { body: body.clone(), index: i, point: polyline_mid(e) })) - .collect(); - } + let picked = if chain { fillet_edges(s, &sel, true) } else { single.clone() }; // Extrude and revolve take profiles of one sketch. if let Sel::Profile { sketch, .. } = &sel && !matches!(self.kind, Kind::Loft { .. }) @@ -1066,7 +1059,20 @@ impl Dialog { { inp.items.retain(|x| !matches!(x, Sel::Profile { sketch: s2, .. } if s2 != sketch)); } + let is_fillet = matches!(self.kind, Kind::Fillet { .. }) && self.active == 0; let Some(inp) = self.inputs.get_mut(self.active) else { return }; + if is_fillet { + self.chain_applied = chain; + if picked.iter().all(|p| inp.items.iter().any(|x| selection::same_item(x, p))) { + self.picked_edges.retain(|x| !picked.iter().any(|p| selection::same_item(x, p))); + } else { + for p in single { + if !self.picked_edges.iter().any(|x| selection::same_item(x, &p)) { + self.picked_edges.push(p); + } + } + } + } inp.toggle(picked); self.focus = true; // A measurement is between two items: a third pick starts over from the last one. @@ -1091,6 +1097,11 @@ impl Dialog { self.profile_selection_changed = true; } let measure = matches!(self.kind, Kind::Measure { .. }); + let is_fillet = matches!(self.kind, Kind::Fillet { .. }) && self.active == 0; + let chain = matches!(self.kind, Kind::Fillet { chain: true, .. }); + if is_fillet && !add { + self.picked_edges.clear(); + } let Some(inp) = self.inputs.get_mut(self.active) else { return }; if !add { inp.items.clear(); @@ -1105,12 +1116,62 @@ impl Dialog { inp.items.push(x); break; } - if !inp.items.iter().any(|y| selection::same_item(y, &x)) { + if is_fillet { + if !self.picked_edges.iter().any(|y| selection::same_item(y, &x)) { + self.picked_edges.push(x.clone()); + } + for y in fillet_edges(s, &x, chain) { + if !inp.items.iter().any(|z| selection::same_item(z, &y)) { + inp.items.push(y); + } + } + } else if !inp.items.iter().any(|y| selection::same_item(y, &x)) { inp.items.push(x); } } self.sync_thread(s); } + + /// Rebuild a Fillet or Chamfer dialog's edge input from the edges that were picked when + /// Tangent Chain is ticked or unticked. + pub fn sync_fillet_chain(&mut self, s: &Session) { + let Kind::Fillet { chain, .. } = &self.kind else { return }; + if *chain == self.chain_applied { + return; + } + self.chain_applied = *chain; + let Some(inp) = self.inputs.first_mut() else { return }; + let mut items = Vec::new(); + for pick in &self.picked_edges { + for x in fillet_edges(s, pick, *chain) { + if !items.iter().any(|y| selection::same_item(y, &x)) { + items.push(x); + } + } + } + inp.items = items; + self.error = None; + } +} + +/// The edges a Fillet or Chamfer pick stands for: its tangent chain when `chain` is on, else +/// the picked edge (both halves of a split circular rim). +fn fillet_edges(s: &Session, sel: &Sel, chain: bool) -> Vec { + let mut picked = crate::viewport::picked_selection(s, sel); + if chain + && let Sel::Edge { body, index, .. } = sel + && let Some(b) = s.world_state().body(body) + { + let m = b.mesh(); + for i in m.tangent_chain(*index, 2f64.to_radians()) { + if !picked.iter().any(|x| matches!(x, Sel::Edge { index: j, .. } if *j == i)) + && let Some(e) = m.edges.get(i) + { + picked.push(Sel::Edge { body: body.clone(), index: i, point: polyline_mid(e) }); + } + } + } + picked } /// Profiles of the sketch a feature would use, when there is just one: the active sketch's, else @@ -1864,6 +1925,7 @@ fn input_rows_range(d: &mut Dialog, ui: &mut egui::Ui, s: &Session, range: std:: inp.items.clear(); if i == 0 { d.profile_selection_changed = true; + d.picked_edges.clear(); } d.active = i; } @@ -2432,6 +2494,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { ui.label(tr("dialog.selection_guidance")); ui.end_row(); } + d.sync_fillet_chain(&app.session); input_rows(&mut d, ui, &app.session); match &mut d.kind { Kind::Sketch => { @@ -2787,7 +2850,8 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { d.extra.insert("radii".into(), json!(radii)); } row_label(ui, "Tangent Chain"); - ui.checkbox(chain, ""); + let r = ui.checkbox(chain, ""); + publish_row(&r); ui.end_row(); } Kind::Shell { thickness } => { @@ -3171,6 +3235,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { } } } + d.sync_fillet_chain(&app.session); error_rows(app, ctx, &mut d, ui, false); }); }); @@ -5103,7 +5168,9 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option) -> Option { diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index 5f6ef6172..9afc5cf45 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -660,6 +660,54 @@ fn edges_then_fillet_takes_them_with_chains() { assert_eq!(d.inputs[0].items.len(), 2, "box edges are not tangent to others"); let cmds = apply_commands(&app, &d).unwrap(); assert_eq!(cmds[0].1["edges"].as_array().unwrap().len(), 2); + + // #3370: on a bar with rounded ends and a slot whose wall lies on the seam between the flat + // top and the rounded end, picking the slot's sharp top edge must not chain the two smooth + // seams that continue it in a straight line. + let mut h = crate::scenario::Harness::new(); + for (cmd, p) in [ + ("sketch.create", json!({"plane": "YZ", "name": "Stadium"})), + ("sketch.line", json!({"points": [[0, -9.5], [25, -9.5]]})), + ("sketch.arc.center_point", json!({"center": [25, 0], "start": [25, -9.5], "end": [25, 9.5]})), + ("sketch.line", json!({"points": [[25, 9.5], [0, 9.5]]})), + ("sketch.arc.center_point", json!({"center": [0, 0], "start": [0, 9.5], "end": [0, -9.5]})), + ("sketch.finish", json!({})), + ("solid.extrude", json!({"sketch": "Stadium", "distance": 14})), + ("solid.box", json!({"corner": [3.5, -12, -10], "length": 6, "width": 12, "height": 20, "operation": "cut"})), + ] { + h.app.run(cmd, p).unwrap(); + } + let slot_top = crate::viewport::grouped_selection(&h.app.session, &edge(&h.app, Vec3::new(6.5, 0.0, 9.5))); + h.app.session.selection = slot_top.clone(); + h.app.start("solid.fillet"); + assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items, slot_top, "smooth stadium seams are not chained onto a sharp slot edge"); + + // On a rounded slot extrusion, picking one straight top edge chains the 4 tangent top edges; + // unticking Tangent Chain leaves only the picked edge, and ticking it again re-chains all 4. + let mut h = crate::scenario::Harness::new(); + for (cmd, p) in [ + ("sketch.create", json!({"plane":"XY"})), + ("sketch.slot.center_to_center", json!({"p0":[-20,0],"p1":[20,0],"width":20})), + ("sketch.finish", json!({})), + ("solid.extrude", json!({"distance":10})), + ] { + h.app.run(cmd, p).unwrap(); + } + let top_line = crate::viewport::grouped_selection(&h.app.session, &edge(&h.app, Vec3::new(0.0, 10.0, 10.0))); + h.app.session.selection = top_line.clone(); + h.app.start("solid.fillet"); + h.frames(2); + assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items.len(), 4); + h.step(&json!({"click":{"handle":"dialog:Tangent Chain"}})).unwrap(); + assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items, top_line); + h.step(&json!({"click":{"handle":"dialog:Tangent Chain"}})).unwrap(); + assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items.len(), 4); + // Picking while Tangent Chain is off adds only that edge; unpicking with Tangent Chain on removes its chain. + h.step(&json!({"click":{"handle":"dialog:Tangent Chain"}})).unwrap(); + assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items, top_line); + h.step(&json!({"click":{"handle":"dialog:Tangent Chain"}})).unwrap(); + h.app.dialog.as_mut().unwrap().pick(&h.app.session, top_line[0].clone()); + assert!(h.app.dialog.as_ref().unwrap().inputs[0].items.is_empty()); } #[test]