From f8b1f09c97bbc69761e8f1c9ff0be551fb1ddfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Ortiz?= Date: Mon, 16 Nov 2026 23:12:18 +0100 Subject: [PATCH] Remove faces left behind by full-thickness push/pull When a push/pull on a closed solid reaches its far side, the walls and cap of the extrusion land exactly on pieces of the body's existing faces. Those regions were merged into a single face and kept, so the board stopped being a closed solid and solid tools rejected it. Push/pull now erases these coincident faces, together with the edges and vertices that only they used, whenever the pushed face belongs to a closed shell. Loose surfaces such as a single floor face keep their previous behaviour. Fixes #39 --- crates/benchup-core/src/modeling.rs | 100 ++++++++++++++++++++-- crates/benchup-core/src/modeling_tests.rs | 69 +++++++++++++++ 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/crates/benchup-core/src/modeling.rs b/crates/benchup-core/src/modeling.rs index e39f56d..2de54d3 100644 --- a/crates/benchup-core/src/modeling.rs +++ b/crates/benchup-core/src/modeling.rs @@ -437,6 +437,65 @@ impl Model { id } + /// Whether the connected body holding `face_id` is a closed two-manifold + /// shell: every edge bounds exactly two of its faces and none is stray. + /// This is a topological check only, cheap enough for every Push/Pull. + fn closed_shell(&self, face_id: u64) -> bool { + let Ok(ids) = self.solid_entities(face_id) else { + return false; + }; + let mut uses: BTreeMap<(u64, u64), usize> = BTreeMap::new(); + for face in ids.iter().filter_map(|id| self.faces.get(id)) { + for ring in std::iter::once(&face.vertices).chain(&face.holes) { + for i in 0..ring.len() { + let (a, b) = (ring[i], ring[(i + 1) % ring.len()]); + *uses.entry((a.min(b), a.max(b))).or_default() += 1; + } + } + } + let edges: BTreeSet<_> = ids + .iter() + .filter_map(|id| self.edges.get(id)) + .map(|edge| (edge.a.min(edge.b), edge.a.max(edge.b))) + .collect(); + !uses.is_empty() + && uses.values().all(|&count| count == 2) + && edges.len() == uses.len() + && edges.iter().all(|key| uses.contains_key(key)) + } + + /// Erase faces together with their boundary edges and vertices that no + /// remaining face uses, so the removal does not leave stray geometry. + fn erase_enclosed_surfaces(&mut self, faces: &BTreeSet) { + if faces.is_empty() { + return; + } + let key = |a: u64, b: u64| (a.min(b), a.max(b)); + let rings = |face: &Face| { + std::iter::once(&face.vertices) + .chain(&face.holes) + .flat_map(|ring| (0..ring.len()).map(move |i| key(ring[i], ring[(i + 1) % ring.len()]))) + .collect::>() + }; + let boundary: BTreeSet<_> = faces + .iter() + .filter_map(|id| self.faces.get(id)) + .flat_map(rings) + .collect(); + let used: BTreeSet<_> = self + .faces + .values() + .filter(|face| !faces.contains(&face.id)) + .flat_map(rings) + .collect(); + let mut ids = faces.clone(); + ids.extend(self.edges.values().filter_map(|edge| { + let key = key(edge.a, edge.b); + (boundary.contains(&key) && !used.contains(&key)).then_some(edge.id) + })); + self.erase(&ids); + } + /// Connect new extrusion boundaries to the editable drawing before adding /// their explicit faces. Raw walls alone do not split an existing floor. fn connect_extrusion( @@ -856,19 +915,50 @@ impl Model { if !keep_starting_face { next.connect_extrusion(&mut rings, drawing_edges, &drawing_vertices, &surfaces); } + let existing: BTreeSet<_> = next.faces.keys().copied().collect(); + let mut coincident = BTreeSet::new(); for ring in &rings[..walls] { - next.raw_face(ring.clone(), vec![], color); + let id = next.raw_face(ring.clone(), vec![], color); + if existing.contains(&id) { + coincident.insert(id); + } } let cap = next.raw_face(rings[walls].clone(), rings[walls + 1..].to_vec(), color); + if existing.contains(&cap) { + coincident.insert(cap); + } + if !keep_starting_face && !coincident.is_empty() && self.closed_shell(face_id) { + // On a closed body, a wall or cap retracing one of its surfaces + // separates removed material from the outside (a cut through the + // far side) or joins two volumes (a pull meeting the body). Neither + // side of that region is a boundary any more, so it goes, together + // with the edges and vertices only it used. Connecting the + // extrusion can also fill brand-new walls before they are added; + // those do not lie inside an earlier surface and are kept. + let body = self.solid_entities(face_id).unwrap_or_default(); + coincident.retain(|id| { + let Some(triangle) = next.face_triangles(&next.faces[id]).first().copied() else { + return false; + }; + let point = triangle.into_iter().sum::() / 3.; + body.iter() + .filter(|&&other| other != face_id) + .filter_map(|other| self.faces.get(other)) + .any(|other| self.face_contains(other, point, false)) + }); + next.erase_enclosed_surfaces(&coincident); + } if !keep_starting_face && self.wood_glue.contains_key(&face_id) { // A Push/Pull without a retained starting surface moves its // coating to the cap. The extrusion walls are new stock. next.wood_glue.remove(&face_id); - let mut polygons = self.glued_polygons(face_id); - for point in polygons.iter_mut().flatten() { - *point += delta; + if next.faces.contains_key(&cap) { + let mut polygons = self.glued_polygons(face_id); + for point in polygons.iter_mut().flatten() { + *point += delta; + } + next.transfer_glue(cap, &polygons); } - next.transfer_glue(cap, &polygons); } next.validate()?; *self = next; diff --git a/crates/benchup-core/src/modeling_tests.rs b/crates/benchup-core/src/modeling_tests.rs index 3662087..df18b37 100644 --- a/crates/benchup-core/src/modeling_tests.rs +++ b/crates/benchup-core/src/modeling_tests.rs @@ -1259,3 +1259,72 @@ fn gpu_scale_requires_complete_isolated_geometry_and_safe_numeric_limits() { model.add_text(DVec3::X, DVec3::Z, "Attached label").unwrap(); assert!(ScalePreview::new(&model, &selected).unwrap().gpu_factors(DVec3::ZERO, 2., None).is_none()); } + +/// A 450 × 350 × 18 mm board inside a group, edited in that group's context. +fn board_in_group() -> (Model, u64) { + let mut model = Model::default(); + let base = rectangle(&mut model, DVec3::ZERO, DVec3::new(0.45, 0.35, 0.)); + let normal = crate::faces::plane(&model, &model.faces[&base].vertices).unwrap().normal; + model.push_pull(base, 0.018 / normal.z).unwrap(); + let board = model.make_object(&model.root_entities(), false, "Board").unwrap(); + assert!(model.enter_object(board)); + (model, board) +} + +fn board_face_at(model: &Model, board: u64, point: DVec3) -> u64 { + model.faces.values().find(|face| { + model.parent(face.id) == Some(board) && model.face_contains(face, point, false) + }).unwrap().id +} + +fn push_through(model: &mut Model, face: u64, depth: f64) -> u64 { + let normal = crate::faces::plane(model, &model.faces[&face].vertices).unwrap().normal; + model.push_pull(face, -depth / normal.z).unwrap() +} + +#[test] +fn full_thickness_push_pull_removes_faces_coincident_with_the_far_side() { + // Cutting off a corner through the whole board (issue #39). + let (mut model, board) = board_in_group(); + model.add_edge(DVec3::new(0., 0.01, 0.018), DVec3::new(0.01, 0., 0.018)).unwrap(); + let triangle = board_face_at(&model, board, DVec3::new(0.002, 0.002, 0.018)); + push_through(&mut model, triangle, 0.018); + for point in [ + DVec3::new(0.002, 0.002, 0.), + DVec3::new(0., 0.004, 0.009), + DVec3::new(0.004, 0., 0.009), + ] { + assert!(model.faces.values().all(|face| !model.face_contains(face, point, true)), + "No face may remain where the corner was removed at {point:?}"); + } + assert!(model.vertices.values().all(|v| v.position.truncate().length() > 1e-6), + "The removed corner must not leave stray vertices or edges"); + model.edit_context = None; + let volume = model.solid_volume(board).unwrap(); + assert!((volume - (0.45 * 0.35 - 0.00005) * 0.018).abs() < 1e-12, "{volume}"); + model.validate().unwrap(); + + // An inset pushed through the board opens a hole through the bottom. + let (mut model, board) = board_in_group(); + let top = board_face_at(&model, board, DVec3::new(0.2, 0.2, 0.018)); + model.offset_face(top, 0.1).unwrap(); + let inner = board_face_at(&model, board, DVec3::new(0.2, 0.2, 0.018)); + push_through(&mut model, inner, 0.018); + assert!(model.nearest_face(crate::Ray { + origin: DVec3::new(0.2, 0.2, 1.), direction: -DVec3::Z, + }).is_none(), "The through hole must stay open at the bottom"); + model.edit_context = None; + let volume = model.solid_volume(board).unwrap(); + assert!((volume - (0.45 * 0.35 - 0.25 * 0.15) * 0.018).abs() < 1e-12, "{volume}"); + model.validate().unwrap(); + + // A partial cut keeps its floor and does not touch the far side. + let (mut model, board) = board_in_group(); + model.add_edge(DVec3::new(0., 0.01, 0.018), DVec3::new(0.01, 0., 0.018)).unwrap(); + let triangle = board_face_at(&model, board, DVec3::new(0.002, 0.002, 0.018)); + let floor = push_through(&mut model, triangle, 0.009); + assert!(model.face_contains(&model.faces[&floor], DVec3::new(0.002, 0.002, 0.009), false)); + model.edit_context = None; + let volume = model.solid_volume(board).unwrap(); + assert!((volume - (0.45 * 0.35 * 0.018 - 0.00005 * 0.009)).abs() < 1e-12, "{volume}"); +}