Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 95 additions & 5 deletions crates/benchup-core/src/modeling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) {
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::<Vec<_>>()
};
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(
Expand Down Expand Up @@ -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::<DVec3>() / 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;
Expand Down
69 changes: 69 additions & 0 deletions crates/benchup-core/src/modeling_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Loading