diff --git a/crates/doc/src/project.rs b/crates/doc/src/project.rs index fa590979d..e77c24970 100644 --- a/crates/doc/src/project.rs +++ b/crates/doc/src/project.rs @@ -309,6 +309,41 @@ fn exact_edges(body: &crate::ModelBody, plane: &Plane, edges: &[usize]) -> Vec Vec { + let mut out = projected_edges_apart(body, mesh, plane, edges); + meet_at_shared_ends(&mut out, order::join_tolerance(mesh)); + out +} + +/// Where curve ends lie within `gap` of each other, moves them onto the first of them. A fitted +/// round's edges end a few 1e-5 mm short of their neighbours (#8284); the sketch only shares a +/// point between ends 1e-7 apart, and without it the outline has no closed profile. +fn meet_at_shared_ends(geom: &mut [LinkGeom], gap: f64) { + let mut seen: Vec = Vec::new(); + let mut snap = |p: &mut Vec2| match seen.iter().find(|q| q.dist(*p) <= gap) { + Some(q) => *p = *q, + None => seen.push(*p), + }; + for g in geom { + match g { + LinkGeom::Line(a, b) | LinkGeom::Arc { a, b, .. } | LinkGeom::Conic { a, b, .. } => { + snap(a); + snap(b); + } + LinkGeom::Spline { pts, periodic: false, .. } => { + if let Some(first) = pts.first_mut() { + snap(first); + } + if let Some(last) = pts.last_mut() { + snap(last); + } + } + _ => {} + } + } +} + +/// Each of `edges` projected on its own ([`projected_edges`] without the ends brought together). +fn projected_edges_apart(body: &crate::ModelBody, mesh: &Mesh, plane: &Plane, edges: &[usize]) -> Vec { let mut out = Vec::new(); for (e, exact) in edges.iter().zip(exact_edges(body, plane, edges)) { match exact { diff --git a/crates/doc/src/project_order.rs b/crates/doc/src/project_order.rs index 2964468c1..d6fdace2a 100644 --- a/crates/doc/src/project_order.rs +++ b/crates/doc/src/project_order.rs @@ -9,6 +9,13 @@ use crate::{DocError, Result}; type Key = (i64, i64, i64); +/// How far apart two edge ends of `mesh` may be and still meet. A round's fitted edges end a few +/// 1e-5 mm short of their neighbours, far more than the tolerance that ranks equal areas (#8284). +pub(super) fn join_tolerance(mesh: &Mesh) -> f64 { + let diagonal = mesh.bounds().diagonal(); + (diagonal * 1e-6).max(1e-9) +} + /// Boundary of the picked face as drawn: a closed surface cut into pieces (a cylinder or a /// round's torus in halves) projects as the whole patch, since its seams are hidden. pub(super) fn face_edges(mesh: &Mesh, face: u32, plane: &Plane) -> Result> { @@ -53,12 +60,13 @@ fn boundary_edges(mesh: &Mesh, mut edges: Vec, faces: &BTreeSet, p let origin = bounds.min; let local_origin = plane.to_local(origin); let tol = (bounds.diagonal() * 1e-8).max(1e-9); - if !tol.is_finite() { + let join = join_tolerance(mesh); + if !tol.is_finite() || !join.is_finite() { return Err(DocError::Invalid("face projection has invalid bounds".into())); } let key = |p: Vec3| -> Key { let p = p - origin; - ((p.x / tol).round() as i64, (p.y / tol).round() as i64, (p.z / tol).round() as i64) + ((p.x / join).round() as i64, (p.y / join).round() as i64, (p.z / join).round() as i64) }; if edges.iter().any(|e| !mesh.edges.get(*e).is_some_and(|p| p.len() >= 2 && p.iter().all(|p| p.is_finite()))) { return Err(DocError::Invalid("face projection has an invalid boundary edge".into())); @@ -71,6 +79,9 @@ fn boundary_edges(mesh: &Mesh, mut edges: Vec, faces: &BTreeSet, p let b: Aabb3 = pts.iter().copied().collect(); (key(b.min), key(b.max)) }; + // A round that dies away at a corner leaves an edge from a point to itself: it bounds nothing, + // and left in, it reads as a branch where the real edges meet. + edges.retain(|e| mesh.edges.get(*e).is_some_and(|p| p.len() > 2 || p.first().zip(p.last()).is_some_and(|(a, b)| a.dist(*b) > join))); edges.sort_by_key(|e| geometry_key(*e)); let mut at: BTreeMap> = BTreeMap::new(); for e in &edges { @@ -107,7 +118,7 @@ fn boundary_edges(mesh: &Mesh, mut edges: Vec, faces: &BTreeSet, p *slot = true; } boundary.push(edge); - if current.dist(start) <= tol { + if current.dist(start) <= join { break; } // Neighbouring quantization cells cover points on either side of a rounding @@ -121,7 +132,7 @@ fn boundary_edges(mesh: &Mesh, mut edges: Vec, faces: &BTreeSet, p if let Some(list) = at.get(&cell) { incident.extend(list.iter().copied().filter(|e| { mesh.edges.get(*e).is_some_and(|p| { - p.first().is_some_and(|a| a.dist(current) <= tol) || p.last().is_some_and(|b| b.dist(current) <= tol) + p.first().is_some_and(|a| a.dist(current) <= join) || p.last().is_some_and(|b| b.dist(current) <= join) }) })); } @@ -148,7 +159,7 @@ fn boundary_edges(mesh: &Mesh, mut edges: Vec, faces: &BTreeSet, p edge = *next; } boundary.sort_by_key(|e| geometry_key(*e)); - let closed = current.dist(start) <= tol; + let closed = current.dist(start) <= join; if !closed || !twice_area.is_finite() { return Err(DocError::Invalid("face projection has an open or invalid boundary".into())); } @@ -379,6 +390,63 @@ mod tests { } } + /// A 100 mm square whose edges each end `gap` short of the next one's start. + fn square_with_gaps(gap: f64) -> Mesh { + let corners = [Vec3::new(0., 0., 0.), Vec3::new(100., 0., 0.), Vec3::new(100., 100., 0.), Vec3::new(0., 100., 0.)]; + let mut mesh = Mesh::default(); + mesh.positions.extend(corners); + for i in 0..4 { + let (a, b) = (corners[i], corners[(i + 1) % 4]); + let back = (a - b).normalized().unwrap() * gap; + mesh.edges.push(vec![a, b + back]); + mesh.edge_faces.push(vec![0]); + } + mesh + } + + #[test] + fn edges_of_a_round_that_end_a_hair_short_of_their_neighbours_still_close() { + // A fitted round's edges end a few 1e-5 mm from the vertex they share (#8284). + let mesh = square_with_gaps(3e-5); + assert_eq!(face_edges(&mesh, 0, &Plane::XY).unwrap().len(), 4); + // A real gap is still an open boundary. + let open = square_with_gaps(1e-2); + assert!(face_edges(&open, 0, &Plane::XY).unwrap_err().to_string().contains("open")); + } + + #[test] + fn an_edge_from_a_point_to_itself_is_not_a_branch() { + // Where a round dies away at a corner, the mesh keeps a zero-length edge there (#8284). + let mut mesh = square_with_gaps(0.); + for corner in mesh.positions.clone() { + mesh.edges.push(vec![corner, corner]); + mesh.edge_faces.push(vec![0, 1]); + } + assert_eq!(face_edges(&mesh, 0, &Plane::XY).unwrap().len(), 4); + } + + #[test] + fn curve_ends_a_hair_apart_become_one_point_and_distant_ones_stay() { + use solidcraft_geom::Vec2; + use solidcraft_sketch::LinkGeom; + let mut geom = vec![ + LinkGeom::Line(Vec2::new(0., 0.), Vec2::new(10., 0.)), + LinkGeom::Spline { + pts: vec![Vec2::new(10. + 3e-5, 2e-5), Vec2::new(11., 1.), Vec2::new(12., 5.)], + control: true, + degree: 3, + periodic: false, + }, + LinkGeom::Line(Vec2::new(12., 5. - 1e-5), Vec2::new(0., 0.1)), + ]; + super::super::meet_at_shared_ends(&mut geom, 1e-4); + assert_eq!( + geom[1], + LinkGeom::Spline { pts: vec![Vec2::new(10., 0.), Vec2::new(11., 1.), Vec2::new(12., 5.)], control: true, degree: 3, periodic: false } + ); + assert_eq!(geom[2], LinkGeom::Line(Vec2::new(12., 5.), Vec2::new(0., 0.1))); + } + #[test] fn incomplete_and_branched_boundaries_are_reported_without_partial_geometry() { let mut open = rectangles(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 94e94ef83..16b55cfdc 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -345,6 +345,7 @@ mod primitive_points; mod rim_cut; mod robustness; mod robustness_goldens; +mod rounded_loft_face; mod sphere_lens; mod spiral_join; mod step_import_options; diff --git a/crates/engine/src/tests/rounded_loft_face.rs b/crates/engine/src/tests/rounded_loft_face.rs new file mode 100644 index 000000000..0029ae2f1 --- /dev/null +++ b/crates/engine/src/tests/rounded_loft_face.rs @@ -0,0 +1,44 @@ +//! A sketch on an end face of a loft whose side edges are rounded (#8284). The rounds' fitted edges +//! end a few 1e-5 mm short of the face's other edges, and where a round dies away at a corner the +//! mesh keeps an edge from a point to itself; the face's outline still closes into one profile. + +use solidcraft_geom::Vec3; + +use super::*; + +/// A 40 mm square at z = 0 lofted to a circle of radius 25 at z = 40, its four side edges rounded. +fn rounded_loft(s: &mut Session) { + run(s, "sketch.create", json!({"plane": "XY", "name": "Square"})); + run(s, "sketch.rectangle.two_point", json!({"p0": [-20, -20], "p1": [20, 20]})); + run(s, "sketch.finish", json!({})); + run(s, "sketch.create", json!({"plane": "XY", "offset": 40, "name": "Circle"})); + run(s, "sketch.circle.center", json!({"center": [0, 0], "radius": 25})); + run(s, "sketch.finish", json!({})); + let section = |name: &str| json!({"sketch": name, "profiles": [{"point": [0, 0]}]}); + run(s, "solid.loft", json!({"sections": [section("Square"), section("Circle")], "operation": "new", "body_name": "Loft"})); + // Each side edge runs from a corner of the square to the circle; pick it halfway up. + let mid = (20.0 + 25.0 * std::f64::consts::FRAC_1_SQRT_2) / 2.0; + let edges: Vec = [(1.0, 1.0), (1.0, -1.0), (-1.0, 1.0), (-1.0, -1.0)].iter().map(|(x, y)| json!([x * mid, y * mid, 20])).collect(); + run(s, "solid.fillet", json!({"edges": edges, "radius": 3})); +} + +#[test] +fn a_sketch_on_either_end_of_a_rounded_loft_takes_the_whole_face() { + let mut s = Session::default(); + rounded_loft(&mut s); + for up in [-1.0, 1.0] { + let world = s.world_state(); + let loft = world.body("Loft").unwrap(); + let face = loft.body.faces(0.01).unwrap().into_iter().find(|f| f.plane_normal.is_some_and(|n| n.dot(Vec3::Z * up) > 0.999)).unwrap(); + let pick = json!({"body": "Loft", "index": face.index, "face": [face.centroid.x, face.centroid.y, face.centroid.z]}); + let id = run(&mut s, "sketch.create", json!({"plane": pick}))["sketch"].as_u64().unwrap(); + run(&mut s, "sketch.finish", json!({})); + let mut s2 = Session::new((*s.doc).clone()); + run(&mut s2, "solid.extrude", json!({"sketch": id, "distance": 5, "operation": "new", "body_name": "Pad"})); + let state = s2.model.state(); + let pad = state.bodies.iter().find(|b| b.name == "Pad").unwrap(); + let v = pad.body.tessellate(1e-2).unwrap().measure().volume; + // The projected outline fits the round with splines, so it is close to the face, not exact. + assert!(rel(v, face.area * 5.0) < 1e-2, "end {up}: {v} vs {}", face.area * 5.0); + } +}