diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 9d095aa9e..69e86209a 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -6403,7 +6403,7 @@ fn with_named_face_points(doc: &Document, state: &ModelState, f: &Feature) -> Op p.x, p.y, p.z )), }, - _ => *p = hit.point, + _ => *p = hit.pick_point(&fst, *p), } } None => notes.push(format!("face `{n}` no longer exists; using the face at the picked point")), diff --git a/crates/doc/src/name_lookup.rs b/crates/doc/src/name_lookup.rs index 05635bd7b..8311c5a0e 100644 --- a/crates/doc/src/name_lookup.rs +++ b/crates/doc/src/name_lookup.rs @@ -241,6 +241,76 @@ impl FaceHit { } best.map(|t| p + d * t) } + + /// A point that picks the face found again by itself: [`point`](Self::point) when the + /// reference's point still lies on the face clear of its edges; else the point of the face + /// nearest `p` that keeps clear of every edge of it. The nearest point of a face the + /// reference's point no longer lies on is on the face's boundary (a parameter moved the face + /// away from the pick), where a pick by point finds the face beside it as well (#2531). + pub fn pick_point(&self, st: &ModelState, p: Vec3) -> Vec3 { + let Some(b) = st.bodies.get(self.body) else { return self.point }; + let m = b.mesh(); + // Clear of the edges by a few pick tolerances, or by half the face's roomiest triangle + // on a face narrower than that. + let Some(margin) = roomiest_inradius(&m, self.face).map(|r| (4.0 * (b.body.size() * 1e-3).max(1e-3)).min(r * 0.5)) else { + return self.point; + }; + if self.on_point && edge_clearance(&m, self.face, self.point) >= margin { + return self.point; + } + inside_nearest(&m, self.face, p, margin).unwrap_or(self.point) + } +} + +/// The incentre and inradius of triangle abc (`None`: degenerate). +fn incircle(a: Vec3, b: Vec3, c: Vec3) -> Option<(Vec3, f64)> { + // The incentre weighs each corner by the side opposite it; the inradius is area / half the perimeter. + let (la, lb, lc) = (b.dist(c), c.dist(a), a.dist(b)); + let perimeter = la + lb + lc; + let radius = (b - a).cross(c - a).len() / perimeter; + let centre = (a * la + b * lb + c * lc) / perimeter; + (radius.is_finite() && radius > 0.0 && centre.is_finite()).then_some((centre, radius)) +} + +/// The triangles of face `face` of a mesh. +fn face_triangles(m: &Mesh, face: usize) -> impl Iterator + '_ { + m.triangles.iter().zip(&m.tri_face).filter(move |(_, f)| **f as usize == face).filter_map(|(t, _)| m.tri(t)) +} + +/// The greatest inradius of the triangles of face `face` of a mesh (`None`: it has none). +fn roomiest_inradius(m: &Mesh, face: usize) -> Option { + face_triangles(m, face).filter_map(|[a, b, c]| incircle(a, b, c)).map(|(_, r)| r).reduce(f64::max) +} + +/// How far `q` is from the nearest edge bounding face `face` of a mesh (infinite: it has none). +fn edge_clearance(m: &Mesh, face: usize, q: Vec3) -> f64 { + m.edges + .iter() + .zip(&m.edge_faces) + .filter(|(_, fs)| fs.iter().any(|f| *f as usize == face)) + .flat_map(|(e, _)| e.windows(2).map(|w| q.dist_to_segment(w[0], w[1]))) + .filter(|d| d.is_finite()) + .fold(f64::INFINITY, f64::min) +} + +/// The point of face `face` of a mesh nearest `p` among the points at least `margin` inside one +/// of its triangles (each triangle shrunk about its incentre by `margin`): such a point is at +/// least `margin` from every edge of the face, which runs along the sides of its triangles. +/// `None`: no triangle of the face is roomier than `margin`. +fn inside_nearest(m: &Mesh, face: usize, p: Vec3, margin: f64) -> Option { + let mut best: Option<(f64, Vec3)> = None; + for [a, b, c] in face_triangles(m, face) { + let Some((centre, radius)) = incircle(a, b, c).filter(|(_, r)| *r > margin) else { continue }; + // Shrunk about the incentre so each side moves in by `margin`. + let s = (radius - margin) / radius; + let shrink = |v: Vec3| centre + (v - centre) * s; + let q = closest(p, shrink(a), shrink(b), shrink(c)); + let d = q.dist(p); + if d.is_finite() && best.is_none_or(|(old, _)| d < old) { + best = Some((d, q)); + } + } + best.map(|(_, q)| q) } impl EdgeHit { diff --git a/crates/engine/src/naming_history_tests.rs b/crates/engine/src/naming_history_tests.rs index 980c14fbc..37611a7c9 100644 --- a/crates/engine/src/naming_history_tests.rs +++ b/crates/engine/src/naming_history_tests.rs @@ -623,3 +623,34 @@ fn one_fillet_over_stacked_boxes_rounds_the_upper_box_s_edge_even_when_saved_nam s.refresh(); check(&s, "saved by an older build"); } + +/// A shell opened by a point pick on a bar's end face keeps opening that face when a parameter +/// edit moves the bar so the stored point no longer lies on it (it now lies on the bottom face). +/// The point is moved onto the named face clear of its edges: the face's nearest point to the old +/// pick is on the edge it shares with the bottom face, where a pick finds both faces (#2531). +#[test] +fn a_shell_point_pick_moved_off_its_face_reopens_only_that_face() { + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "L", "expression": "100 mm", "unit": "mm"})); + run(&mut s, "parameters.add", json!({"name": "Y0", "expression": "5 mm", "unit": "mm"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 5], "p1": [100, 25]})); + run(&mut s, "sketch.constraint.horizontal_vertical", json!({"points": ["origin", "l1.start"], "mode": "vertical"})); + run(&mut s, "sketch.dimension", json!({"entities": ["origin", "l1.start"], "type": "vertical", "value": "Y0"})); + run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "type": "length", "value": "L"})); + run(&mut s, "sketch.dimension", json!({"entities": ["l4"], "type": "length", "value": "20 mm"})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "S", "distance": "20 mm", "operation": "new", "body_name": "Bar"})); + run(&mut s, "solid.shell", json!({"faces": [[100, 15, 10]], "thickness": "2 mm", "direction": "inside", "body": "Bar", "name": "Shell"})); + change(&mut s, "L", "150 mm"); + change(&mut s, "Y0", "15 mm"); + assert_eq!(error(&s, "Shell"), None); + let state = s.model.state(); + let bar = state.body("Bar").expect("the shelled bar"); + let volume = solidcraft_kernel::measure(&bar.body).expect("measure").volume; + // 150 x 20 x 20 open at x = 150 only: 60000 - 148 * 16 * 16. + assert!((volume - 22112.0).abs() < 1e-3, "{volume}"); + assert_eq!(bar.body.face_count(), 11); + assert!(solid_at(&s, [100.0, 16.0, 10.0]), "the bottom face stays closed"); + assert!(!solid_at(&s, [149.0, 25.0, 10.0]), "the end at x = 150 is open"); +} diff --git a/crates/kernel/src/boolean_history_tests.rs b/crates/kernel/src/boolean_history_tests.rs index ab4bdd621..f090cec83 100644 --- a/crates/kernel/src/boolean_history_tests.rs +++ b/crates/kernel/src/boolean_history_tests.rs @@ -614,6 +614,28 @@ mod smlib { assert!((v - 2000.0).abs() < 1e-6 * 2000.0, "{v}"); } + /// A block touching itself along a line: a through hole tangent to its side from inside, and + /// a blind hole tangent to its side (the side then on both sides of the line). SMLib's + /// result uses the tangent line four times; it comes back as one solid whose edges each bound + /// two faces, of the block's volume less the hole's, every face and edge's history verified + /// against the geometry. + #[test] + fn holes_tangent_to_the_side_come_back_as_one_solid() { + let a = boxed(0.0, 0.0, 0.0, 30.0, 20.0, 10.0); + for (name, z0, depth) in [("through", -1.0, 12.0), ("blind", 2.0, 9.0)] { + let b = crate::cylinder(Vec3::new(10.0, 15.0, z0), Vec3::Z, 5.0, depth).unwrap(); + let r = through_smlib(&a, &b, BoolOp::Cut); + assert_eq!(r.solid.boundaries().len(), 1, "{name}"); + let record = crate::brep_record(&r).unwrap(); + assert!(record.edges.iter().all(|e| e.uses.len() == 2), "{name}"); + let hole = std::f64::consts::PI * 25.0 * (10.0 - z0.max(0.0)); + let v = crate::measure(&r).unwrap().volume; + assert!((v - (6000.0 - hole)).abs() < 1e-4 * 6000.0, "{name}: {v}"); + assert!(r.validity().is_empty(), "{name}: {:?}", r.validity()); + verified(&r, &[&a, &b], 0.02); + } + } + #[test] fn disjoint_intersection_is_empty() { let a = boxed(0.0, 0.0, 0.0, 1.0, 1.0, 1.0); diff --git a/crates/kernel/src/modify_history_tests.rs b/crates/kernel/src/modify_history_tests.rs index 1c6165ff2..ccbe6bff5 100644 --- a/crates/kernel/src/modify_history_tests.rs +++ b/crates/kernel/src/modify_history_tests.rs @@ -571,6 +571,28 @@ mod smlib { assert!(e.to_string().contains("no room"), "{e}"); } + #[test] + fn draft_tilts_a_side_and_keeps_its_name() { + let b = cube(); + let side = Vec3::new(40.0, 15.0, 10.0); + let deg = std::f64::consts::PI / 180.0; + let r = offset_backend::draft(&b, &[side], &Plane::XY, Vec3::Z, 5.0 * deg).unwrap(); + // The side leans in 20 tan 5 deg at the top: a wedge of 20 x 20 tan 5 deg / 2 x 30 comes off. + let wedge = 0.5 * 20.0 * 20.0 * (5.0 * deg).tan() * 30.0; + assert!((volume(&r) - (24000.0 - wedge)).abs() < 1e-3, "{}", volume(&r)); + let h = verified(&r, &b, Reach { made: 0.0, moved: 2.0 }); + assert_eq!(h.faces.len(), 6); + let tilted = with(h, &[face(face_at(&b, side))]); + assert_eq!(tilted.len(), 1, "{h:?}"); + assert_eq!(h.faces[tilted[0]].change, Change::Modified); + // A round wall isn't flat; the top isn't parallel to the pull. + let rod = cylinder(Vec3::ZERO, Vec3::Z, 5.0, 10.0).unwrap(); + let e = offset_backend::draft(&rod, &[Vec3::new(5.0, 0.0, 5.0)], &Plane::XY, Vec3::Z, 5.0 * deg).unwrap_err(); + assert!(e.to_string().contains("not supported yet"), "{e}"); + let e = offset_backend::draft(&b, &[Vec3::new(20.0, 15.0, 20.0)], &Plane::XY, Vec3::Z, 5.0 * deg).unwrap_err(); + assert!(e.to_string().contains("not supported yet"), "{e}"); + } + #[test] fn thicken_offsets_the_surface() { let s = crate::patch_region(&Plane::XY, &Region2 { outer: square(0.0, 0.0, 10.0, 10.0), holes: vec![] }).unwrap(); diff --git a/crates/kernel/src/offset_backend.rs b/crates/kernel/src/offset_backend.rs index 10ecbe7a8..cc3fec73e 100644 --- a/crates/kernel/src/offset_backend.rs +++ b/crates/kernel/src/offset_backend.rs @@ -36,6 +36,7 @@ fn refused(op: &str, e: SmError) -> KernelError { SmError::NoRoom => KernelError::Invalid(format!("{op}: the wall is too thick: it leaves no room inside the body")), SmError::NoMatch { point } => KernelError::Invalid(format!("{op}: no face at selection point {}", point + 1)), SmError::Ambiguous { point } => KernelError::Invalid(format!("{op}: selection point {} is as near to two faces", point + 1)), + SmError::Unsupported => KernelError::Failed(format!("not supported yet: SMLib {op} for this geometry")), e => failed(op, "operation", e), } } @@ -123,6 +124,35 @@ pub(crate) fn thicken(b: &Body, thickness: f64, symmetric: bool) -> Result Ok(r.with_checked_history(h, &record::counts(&[b]))) } +/// [`crate::draft`] through SMLib: the faces under the points `at` (flat, parallel to `pull`) +/// tilted by `angle` about their lines on `neutral`, leaning in going along `pull`; the faces +/// around them extended or trimmed to meet them. A tilted face is the face itself (Modified). +/// A face SMLib can't draft (not flat, or not parallel to the pull) is "not supported yet". +pub(crate) fn draft(b: &Body, at: &[Vec3], neutral: &solidcraft_geom::Plane, pull: Vec3, angle: f64) -> Result { + if !angle.is_finite() || angle.abs() >= std::f64::consts::FRAC_PI_2 - 1e-3 { + return Err(KernelError::Invalid("draft angle must be between −90° and 90°".into())); + } + let pull = pull.normalized().ok_or_else(|| KernelError::Invalid("pull direction".into()))?; + let normal = neutral.normal().normalized().ok_or_else(|| KernelError::Invalid("neutral plane".into()))?; + if normal.dot(pull).abs() < 1e-9 { + return Err(KernelError::Failed("the neutral plane is parallel to the pull direction".into())); + } + let chosen = faces_under(b, at)?; + let tol = pick_tolerance(b); + let pts = points(&face_points(b, &chosen)?); + let o = neutral.origin; + let (r, h) = run("draft", b, |s| s.draft(&pts, tol, [o.x, o.y, o.z], [normal.x, normal.y, normal.z], [pull.x, pull.y, pull.z], angle))?; + let h = h.map(|mut h| { + for f in &mut h.faces { + if f.change == Change::Kept && f.origins.iter().any(|o| matches!(o, FaceOrigin::Face { face, .. } if chosen.contains(face))) { + f.change = Change::Modified; + } + } + h + }); + Ok(r.with_checked_history(h, &record::counts(&[b]))) +} + /// The face of `b` under each point: the face of the nearest triangle, within the picking reach. fn faces_under(b: &Body, at: &[Vec3]) -> Result> { let reach = pick_tolerance(b); diff --git a/crates/kernel/src/polyhedron.rs b/crates/kernel/src/polyhedron.rs index f87648396..76745556f 100644 --- a/crates/kernel/src/polyhedron.rs +++ b/crates/kernel/src/polyhedron.rs @@ -251,6 +251,10 @@ fn offsets(offset: &Body) -> Option { /// lean in (positive angle) going along `pull`. pub fn draft(b: &Body, faces: &[Vec3], neutral: &Plane, pull: Vec3, angle: f64) -> Result { b.require_brep("draft")?; + #[cfg(feature = "smlib")] + if crate::backend()? == crate::Backend::Smlib { + return crate::offset_backend::draft(b, faces, neutral, pull, angle); + } if !angle.is_finite() || angle.abs() >= std::f64::consts::FRAC_PI_2 - 1e-3 { return Err(KernelError::Invalid("draft angle must be between −90° and 90°".into())); } diff --git a/crates/smlib/src/export.rs b/crates/smlib/src/export.rs index 90c0c027a..3bff85f43 100644 --- a/crates/smlib/src/export.rs +++ b/crates/smlib/src/export.rs @@ -19,6 +19,10 @@ //! oriented faces as the side in the region that is not material uses them (out of the //! material). A sheet's one shell holds both uses of each face: the `same` one is kept. The //! infinite region is never a void (SMLib marks it one). +//! - **Touching:** an edge or vertex solids touching each other share is copied once per solid; +//! an edge one solid touches itself along (four or more uses in a shell) is copied once per +//! pair of faces SMLib's radial order puts across one sector round it, so every edge bounds +//! two faces. //! - **Order:** faces, edges and vertices renumbered in the canonical walk; each edge's uses //! from that walk (`upper = !forward`), never SMLib's radial entry flags. //! - **Pcurves:** computed from each edge record on its face's surface record @@ -160,6 +164,8 @@ const I64_IDS: &[i32] = &[ LOOP_VERTEX, EDGEUSE_EDGE, EDGEUSE_ORIENTATION, + EDGEUSE_NEXT_RADIAL, + EDGEUSE_RADIAL_ENTRY, EDGE_CURVE_TYPE, EDGE_VERTICES, EDGE_NURBS_VERTEX_COUNT, @@ -572,6 +578,8 @@ struct Face { /// SMLib's natural normal is the record's. agrees: bool, loops: Vec, + /// The array edgeuse record of each use, loop by loop as `loops` (none for a vertex loop). + records: Vec>, } #[derive(Clone)] @@ -696,14 +704,17 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> { let natural = surface.natural(); let domain = [onto([r[0], r[2]], natural[0]), onto([r[1], r[3]], natural[1])]; let mut out = Vec::with_capacity(loops.len()); + let mut records = Vec::with_capacity(loops.len()); for l in loops.clone() { let uses = loop_edgeuses.get(l).ok_or_else(|| invalid("loop"))?; if uses.is_empty() { let v = idx(at(flat.i(LOOP_VERTEX), l, "loop vertex")?, n_vertices, "loop vertex")?; out.push(Loop::Vertex(index(v)?)); + records.push(Vec::new()); continue; } let mut l = Vec::with_capacity(uses.len()); + records.push(uses.clone().collect()); for u in uses.clone() { let e = idx(at(flat.i(EDGEUSE_EDGE), u, "edgeuse")?, n_edges, "edgeuse edge")?; let same = at(flat.i(EDGEUSE_ORIENTATION), u, "edgeuse")? == 1; @@ -712,7 +723,7 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> { } out.push(Loop::Edges(l)); } - faces.push(Face { surface, domain, agrees: true, loops: out }); + faces.push(Face { surface, domain, agrees: true, loops: out, records }); } for face in &mut faces { if outer_area(face, &edges, largest)? < 0.0 { @@ -723,6 +734,7 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> { uses.iter_mut().for_each(|u| u.1 = !u.1); } } + face.records.iter_mut().for_each(|r| r.reverse()); report.reversed_faces += 1; } } @@ -816,7 +828,13 @@ fn native_ordered(arrays: &Arrays) -> Res<(b::Brep, Report, Order)> { } let shells: Vec> = order.iter().filter_map(|s| shells.get(*s as usize).cloned()).collect(); - let parts = Parts { faces, edges, shells, regions }; + // Round each edge: SMLib's radial order of its uses, and the side of each face the material + // is on, which say which faces bound the same material where a solid touches itself. + let material_same = + face_uses.iter().map(|uses| uses.iter().find(|(s, _)| material(region_of.get(*s).copied().unwrap_or(0))).map(|(_, same)| *same)).collect(); + let radial = radial(flat, n_edgeuses, n_edges, material_same)?; + + let parts = Parts { faces, edges, shells, regions, radial }; let made = match canonical(parts.clone(), n_vertices, flat, &tolerances) { // SMLib's own tolerance for an edge or vertex can understate what its approximated // geometry needs here (a blend's end curve 2.3e-5 mm off its plane at 2e-5): then the @@ -837,6 +855,56 @@ struct Parts { edges: Vec, shells: Vec>, regions: Vec, + radial: Option, +} + +/// SMLib's order of the faces round each edge (`EDGEUSE_NEXT_RADIAL`, `EDGEUSE_RADIAL_ENTRY`), +/// read where the arrays have it. Each edgeuse record is a face's two sides along the edge; a +/// right-hand traversal round the edge meets one side of each record, then its other side, then +/// the next record. Between two records lies one sector of space round the edge, empty or +/// material. +#[derive(Clone)] +struct Radial { + /// The next record round the edge. + next: Vec, + /// The traversal enters the record from its face's `same` side (SMLib's top edgeuse, the + /// one on the faceuse the surface normal points out of) and leaves from the other. + top_entry: Vec, + /// Per array face: whether the material is on its `same` side; none for a face with no + /// material on either side (a sheet's). + material_same: Vec>, +} + +/// The radial arrays, checked: in range, entries 0 or 1, each edge's records one cycle or more. +/// None where the arrays have neither (the export of a library without them). +fn radial(flat: View, n_edgeuses: usize, n_edges: usize, material_same: Vec>) -> Res> { + let (next, entry) = (flat.i(EDGEUSE_NEXT_RADIAL), flat.i(EDGEUSE_RADIAL_ENTRY)); + if next.is_empty() && entry.is_empty() { + return Ok(None); + } + if next.len() != n_edgeuses || entry.len() != n_edgeuses { + return Err(invalid("radial arrays")); + } + let edge = flat.i(EDGEUSE_EDGE).iter().map(|e| idx(*e, n_edges, "edgeuse edge")).collect::>>()?; + let next = next.iter().map(|n| idx(*n, n_edgeuses, "next radial edgeuse")).collect::>>()?; + // Each record is some record's next (the lists are cycles), on its own edge. + let mut reached = vec![false; n_edgeuses]; + for (u, n) in next.iter().enumerate() { + let slot = reached.get_mut(*n).ok_or_else(|| invalid("next radial edgeuse"))?; + if *slot || edge.get(u) != edge.get(*n) { + return Err(invalid("a radial list that is not a cycle round its edge")); + } + *slot = true; + } + let top_entry = entry + .iter() + .map(|x| match x { + 0 => Ok(false), + 1 => Ok(true), + _ => Err(invalid("radial entry")), + }) + .collect::>>()?; + Ok(Some(Radial { next, top_entry, material_same })) } /// The record in canonical order: faces in first appearance through the shells, edges through @@ -862,10 +930,11 @@ impl Tolerances { fn canonical(parts: Parts, n_vertices: usize, flat: View, tolerances: &Tolerances) -> Res<(b::Brep, Order)> { let tolerance = tolerances.body; - let Parts { mut faces, mut edges, shells, regions } = parts; + let Parts { mut faces, mut edges, shells, regions, radial } = parts; let n_edges = edges.len(); - // A copy made by the split keeps the tolerance of the array edge or vertex it copies. - let (edge_source, vertex_source) = split_shared(&mut faces, &mut edges, &shells, n_vertices)?; + // A copy made by the splits keeps the tolerance of the array edge or vertex it copies. + let (mut edge_source, vertex_source) = split_shared(&mut faces, &mut edges, &shells, n_vertices)?; + split_touching(&mut faces, &mut edges, &mut edge_source, radial.as_ref())?; let n_vertices = vertex_source.len(); let mut face_new: Vec> = vec![None; faces.len()]; let (mut face_order, mut direction) = (Vec::new(), Vec::new()); @@ -1112,6 +1181,119 @@ fn split_shared(faces: &mut [Face], edges: &mut Vec, shells: &[Vec<(u32, b Ok((edge_source, vertex_source)) } +/// Edges one shell uses more than twice, where a solid touches itself along a line (a hole +/// tangent to its outline, holes touching each other), split so that every edge bounds two faces: +/// SMLib's radial order round the edge pairs each record with the next one across a sector, and +/// each pair gets its own copy of the edge. The pairs are those across a material sector, so the +/// surface is cut apart along the line round each wedge of material, unless that would give one +/// face two copies (a face on both sides of the line, its loop running along one copy and back +/// along the other), which SMLib refuses (its check's tests 33 and 34, coincident edges in a +/// face): then the pairs across the empty sectors, so that face meets itself across the line as +/// SMLib's crack edges do, one edge used twice. The vertices at the ends stay shared, the points +/// where the boundary meets itself: copies would give a face whose loop passes the point twice two +/// coincident vertices, which SMLib also refuses (test 32). The geometry is unchanged; each copy +/// is added to `edge_source` with its original's source. +fn split_touching(faces: &mut [Face], edges: &mut Vec, edge_source: &mut Vec, radial: Option<&Radial>) -> Res<()> { + // The records using each edge, and where each record is: (face, loop, position). + let mut records_of: Vec> = vec![Vec::new(); edges.len()]; + let mut place: HashMap = HashMap::new(); + for (f, face) in faces.iter().enumerate() { + for (li, (l, records)) in face.loops.iter().zip(&face.records).enumerate() { + let Loop::Edges(uses) = l else { continue }; + if records.len() != uses.len() { + return Err(invalid("loop records")); + } + for (p, (&(e, _), &r)) in uses.iter().zip(records).enumerate() { + records_of.get_mut(e as usize).ok_or_else(|| invalid("edge"))?.push(r); + if place.insert(r, (f, li, p)).is_some() { + return Err(invalid("an edgeuse record in two loops")); + } + } + } + } + let crowded: Vec = (0..edges.len()).filter(|e| records_of.get(*e).is_some_and(|r| r.len() > 2)).collect(); + if crowded.is_empty() { + return Ok(()); + } + let radial = radial.ok_or_else(|| unsupported("a solid touching itself along an edge, without SMLib's radial order"))?; + let face_of = |r: usize| place.get(&r).map(|p| p.0).ok_or_else(|| invalid("edgeuse record")); + let material_same = |r: usize| -> Res { + radial.material_same.get(face_of(r)?).copied().flatten().ok_or_else(|| unsupported("a sheet touching itself along an edge")) + }; + let top_entry = |r: usize| radial.top_entry.get(r).copied().ok_or_else(|| invalid("radial entry")); + // Whether the sector the traversal leaves a record into, and the one it enters a record + // from, is material: it leaves from the side it did not enter. + let after = |r: usize| -> Res { Ok(material_same(r)? != top_entry(r)?) }; + let before = |r: usize| -> Res { Ok(material_same(r)? == top_entry(r)?) }; + + for e in crowded { + let mine = records_of.get(e).cloned().unwrap_or_default(); + let start = *mine.first().ok_or_else(|| invalid("edge"))?; + // The edge's radial list from one of its records, keeping this edge's (another solid's + // faces may lie between them where solids touch each other too). + let mut cycle = Vec::with_capacity(mine.len()); + let mut r = start; + for _ in 0..radial.next.len() { + if mine.contains(&r) { + cycle.push(r); + } + r = radial.next.get(r).copied().ok_or_else(|| invalid("next radial edgeuse"))?; + if r == start { + break; + } + } + if r != start || cycle.len() != mine.len() { + return Err(invalid("a radial list that does not go once round its edge's uses")); + } + let k = cycle.len(); + let at = |i: usize| cycle.get(i % k).copied().ok_or_else(|| invalid("radial list")); + // Sectors alternate material and space, each seen alike from the records on its sides. + let mut alternate = k % 2 == 0; + for i in 0..k { + let (a, b) = (at(i)?, at(i + 1)?); + alternate &= after(a)? == before(b)? && after(a)? != after(b)?; + } + if !alternate { + return Err(unsupported("a solid touching itself along an edge whose faces do not alternate between material and space round it")); + } + // The pairs across material sectors, unless one face would get two copies; then those + // across empty sectors, unless that does too. + let material = if after(at(0)?)? { 0 } else { 1 }; + let one_copy_per_face = |first: usize| -> Res { + let mut pair_of: HashMap = HashMap::new(); + for i in 0..k { + let f = face_of(at(first + i)?)?; + if *pair_of.entry(f).or_insert(i / 2) != i / 2 { + return Ok(false); + } + } + Ok(true) + }; + let first = if one_copy_per_face(material)? || !one_copy_per_face(1 - material)? { material } else { 1 - material }; + for pair in 0..k / 2 { + let (a, b) = (at(first + 2 * pair)?, at(first + 2 * pair + 1)?); + let copy = if pair == 0 { + e + } else { + let c = edges.len(); + edges.push(edges.get(e).ok_or_else(|| invalid("edge"))?.clone()); + edge_source.push(edge_source.get(e).copied().ok_or_else(|| invalid("edge"))?); + c + }; + for r in [a, b] { + let (f, li, p) = place.get(&r).copied().ok_or_else(|| invalid("edgeuse record"))?; + let slot = faces + .get_mut(f) + .and_then(|face| face.loops.get_mut(li)) + .and_then(|l| if let Loop::Edges(u) = l { u.get_mut(p) } else { None }) + .ok_or_else(|| invalid("edgeuse record"))?; + slot.0 = index(copy)?; + } + } + } + Ok(()) +} + /// A face's pcurves, edge loop by edge loop, from the plain-record routine on the face's /// records, each loop moved by whole periods into the face's domain. fn pcurves(surface: &b::Surface, domain: [[f64; 2]; 2], loops: &[Loop], edges: &[b::Edge], tolerance: f64) -> Res>> { diff --git a/crates/smlib/src/export/tests.rs b/crates/smlib/src/export/tests.rs index e48c29c32..a8a4c5a9e 100644 --- a/crates/smlib/src/export/tests.rs +++ b/crates/smlib/src/export/tests.rs @@ -106,6 +106,95 @@ mod native_tests { assert!((half.unwrap() + 0.5f64.atan()).abs() < 1e-12); } + /// A hole tangent to the block's outline from inside: the material touches itself along the + /// tangent line, where SMLib's one shell uses the edge four times. The record has two copies + /// of that edge, each bounding one wedge of material (a face of the side and the half of the + /// hole on the same side of the line), so every edge has two uses; the line's two vertices + /// stay shared. The record goes back into SMLib as a valid solid of the same volume. + #[test] + fn a_hole_tangent_to_the_outline_comes_back_as_a_manifold_boundary() { + let block = Brep::make_box([0.0; 3], [31.0, 29.0, 10.0]).unwrap(); + let hole = Brep::make_cylinder([5.0, 14.5, -1.0], 5.0, 12.0).unwrap(); + let r = block.boolean(&hole, crate::BooleanOp::Subtract).unwrap().unwrap(); + assert!(!r.is_manifold_solid().unwrap(), "SMLib keeps the touching boundary as one non-manifold shell"); + let (record, order) = r.to_native_ordered().unwrap(); + let [f, e, v] = r.counts().unwrap(); + assert_eq!((record.faces.len(), record.edges.len(), record.vertices.len()), (f, e + 1, v)); + assert_eq!((record.shells.len(), record.regions.len()), (1, 2)); + assert!(record.edges.iter().all(|e| e.uses.len() == 2)); + record.check_geometry().unwrap(); + let on_line = |e: &b::Edge| { + e.vertices.iter().all(|v| { + let p = record.vertices[*v as usize].point; + p[0].abs() < 1e-9 && (p[1] - 14.5).abs() < 1e-9 + }) + }; + let middle = |e: &b::Edge| b::eval::CurveEval::new(&e.curve).unwrap().point(0.5 * (e.interval[0] + e.interval[1])).unwrap(); + let copies: Vec<&b::Edge> = record.edges.iter().filter(|e| on_line(e)).collect(); + assert_eq!(copies.len(), 2); + for copy in copies { + // Each face's other edges lie on one side of y = 14.5: the same side for both faces. + let sides: Vec = copy + .uses + .iter() + .map(|u| { + let b::Loop::Edges { edges } = &record.faces[u.face as usize].loops[u.r#loop as usize] else { panic!("vertex loop") }; + let others: Vec = + edges.iter().map(|o| &record.edges[o.edge as usize]).filter(|o| !on_line(o)).map(|o| middle(o).y - 14.5).collect(); + others.iter().sum::() / others.len() as f64 + }) + .collect(); + assert!(sides[0] * sides[1] > 0.0, "the copy joins faces on opposite sides of the line: {sides:?}"); + } + // SMLib's history of the tangent line goes to both copies. + assert_eq!(order.pieces.len(), 2); + let back = Brep::from_record(&record, true).unwrap(); + let check = back.check(CheckLevel::All).unwrap(); + assert!(check.is_valid(), "{check}"); + let want = 31.0 * 29.0 * 10.0 - PI * 25.0 * 10.0; + let got = back.mass_properties(MEASURE_ACCURACY).unwrap().volume; + assert!((got - want).abs() < 1e-6 * want, "volume {got} vs {want}"); + } + + /// A blind hole from z = 2 tangent to the block's side y = 20 from inside: the side is one + /// face on both sides of the tangent line (joined below the hole), so pairing the faces round + /// the line across the material would give it two coincident copies of the line, which + /// SMLib refuses. The line is then copied across the empty sectors: the side uses one copy + /// twice (a crack, as SMLib keeps a face meeting itself), the hole's two halves the other. + #[test] + fn a_blind_hole_tangent_to_a_side_leaves_the_side_a_crack() { + let block = Brep::make_box([0.0; 3], [30.0, 20.0, 10.0]).unwrap(); + let hole = Brep::make_cylinder([10.0, 15.0, 2.0], 5.0, 9.0).unwrap(); + let r = block.boolean(&hole, crate::BooleanOp::Subtract).unwrap().unwrap(); + let (record, _) = r.to_native_ordered().unwrap(); + assert!(record.edges.iter().all(|e| e.uses.len() == 2)); + record.check_geometry().unwrap(); + let on_line = |e: &b::Edge| { + e.vertices.iter().all(|v| { + let p = record.vertices[*v as usize].point; + (p[0] - 10.0).abs() < 1e-9 && (p[1] - 20.0).abs() < 1e-9 + }) + }; + let copies: Vec<&b::Edge> = record.edges.iter().filter(|e| on_line(e)).collect(); + assert_eq!(copies.len(), 2); + let kinds: Vec<(bool, bool)> = copies + .iter() + .map(|e| { + let faces: Vec<&b::Face> = e.uses.iter().map(|u| &record.faces[u.face as usize]).collect(); + let side = |f: &b::Face| matches!(f.surface, b::Surface::Plane { .. }); + (faces.iter().all(|f| side(f)), e.uses[0].face == e.uses[1].face) + }) + .collect(); + assert!(kinds.contains(&(true, true)), "one copy is the side's crack: {kinds:?}"); + assert!(kinds.contains(&(false, false)), "the other joins the hole's halves: {kinds:?}"); + let back = Brep::from_record(&record, true).unwrap(); + let check = back.check(CheckLevel::All).unwrap(); + assert!(check.is_valid(), "{check}"); + let want = 6000.0 - PI * 25.0 * 8.0; + let got = back.mass_properties(MEASURE_ACCURACY).unwrap().volume; + assert!((got - want).abs() < 1e-6 * want, "volume {got} vs {want}"); + } + #[test] fn hostile_arrays_are_errors_never_panics() { let good = Brep::make_box([0.0; 3], [1.0, 2.0, 3.0]).unwrap().arrays().unwrap(); diff --git a/crates/smlib/src/ffi/sweeps.rs b/crates/smlib/src/ffi/sweeps.rs index d47a32abf..d92ad9af0 100644 --- a/crates/smlib/src/ffi/sweeps.rs +++ b/crates/smlib/src/ffi/sweeps.rs @@ -22,7 +22,8 @@ pub const SWEEP_FOLLOW: i32 = 0; pub const SWEEP_TRANSLATE: i32 = 1; /// A curve list as the C API reads it: kinds (with a NURBS curve's degree and point count) and -/// each curve's numbers. +/// each curve's numbers. Only the calls read them, which a build without SMLib does not have. +#[cfg_attr(not(feature = "native"), allow(dead_code))] pub struct List<'a> { pub kinds: &'a [i64], pub reals: &'a [f64],