SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Project: round edges project as arcs, not short lines (#10312)
Frank7239c9585855
diff --git a/crates/doc/src/project.rs b/crates/doc/src/project.rsindex 677e68aa60..780099e062 100644--- a/crates/doc/src/project.rs+++ b/crates/doc/src/project.rs@@ -148,7 +148,7 @@ fn resolve_uncached(let mesh = b.mesh();match kind {LinkKind::Intersect => section(plane, &mesh, None),- _ => silhouette(plane, &mesh),+ _ => silhouette(plane, &mesh, Some(&b.body)),}}LinkSource::Vertex { body, at } => {@@ -302,9 +302,78 @@ fn edge_geom(plane: &Plane, kind: LinkKind, exact: Option<Vec<LinkGeom>>, pts: V/// Edges `edges` of `body` (indices into its mesh's `edges`) on `plane` as their exact curves:/// lines, circles and arcs, and the Bézier pieces of a free-form curve. `None` for an edge that/// doesn't lie in a plane parallel to `plane` or isn't read exactly (a mesh body's): it is-/// fitted to its polyline instead.+/// fitted to its polyline instead. An edge read as spline pieces, or not read, that runs along+/// a circle in a plane parallel to `plane` comes in as that circle ([`circular`]): a round's+/// edge is a spline that only follows its circle.fn exact_edges(body: &crate::ModelBody, plane: &Plane, edges: &[usize]) -> Vec<Option<Vec<LinkGeom>>> {- body.body.planar_edges(plane, edges).into_iter().map(|segs| segs?.iter().map(link_geom).collect()).collect()+ let mut mesh = None;+ body.body+ .planar_edges(plane, edges)+ .into_iter()+ .zip(edges)+ .map(|(segs, e)| match segs {+ Some(segs) if !segs.iter().any(|s| matches!(s, Seg2::Cubic { .. } | Seg2::Bezier { .. })) => segs.iter().map(link_geom).collect(),+ Some(segs) => {+ let samples: Vec<Vec2> =+ segs.iter().flat_map(|s| (0..=CIRCLE_SAMPLES).map(move |i| s.point_at(f64::from(i) / f64::from(CIRCLE_SAMPLES)))).collect();+ match circular(&samples) {+ Some((g, _)) => Some(vec![g]),+ None => segs.iter().map(link_geom).collect(),+ }+ }+ None if body.body.is_mesh() => None,+ None => {+ // Not read: its polyline lies on the curve, but only many points of it show a circle.+ let pts = mesh.get_or_insert_with(|| body.mesh()).edges.get(*e).filter(|pts| pts.len() > 8)?;+ let level = plane.height(*pts.first()?);+ let (g, radius) = circular(&pts.iter().map(|p| plane.to_local(*p)).collect::<Vec<_>>())?;+ pts.iter().all(|p| (plane.height(*p) - level).abs() <= radius * CIRCLE_TOL).then(|| vec![g])+ }+ })+ .collect()+}++/// Points sampled along each Bézier piece of an edge to see whether it runs along a circle.+const CIRCLE_SAMPLES: u16 = 16;+/// How far round (radians) a curve turns at least to be taken for a circle: a letter's nearly+/// straight stroke lies near a large circle too.+const CIRCLE_MIN_TURN: f64 = 0.1;+/// How far (a fraction of its radius) a curve may stray from a circle and still be that circle:+/// a round's edge keeps to it within 3e-7, a curve that merely looks round (a letter's bowl of+/// four cubic spans) only within 3e-4.+const CIRCLE_TOL: f64 = 1e-5;++/// The circle (closing points) or arc through `samples`, taken in order along a curve, with+/// its radius: when they all lie on one within [`CIRCLE_TOL`]. The arc ends at the first and+/// last sample, so it meets what the curve met.+fn circular(samples: &[Vec2]) -> Option<(LinkGeom, f64)> {+ let (first, last) = (*samples.first()?, *samples.last()?);+ let (c, radius) = ls_circle(samples)?;+ let tol = radius * CIRCLE_TOL;+ if !(radius.is_finite() && tol > 0.0) || samples.iter().any(|p| (p.dist(c) - radius).abs() > tol) {+ return None;+ }+ let mut sweep = 0.0;+ for w in samples.windows(2) {+ let (Some(a), Some(b)) = (w.first(), w.get(1)) else { continue };+ let mut d = (*b - c).angle() - (*a - c).angle();+ if d > std::f64::consts::PI {+ d -= std::f64::consts::TAU;+ } else if d < -std::f64::consts::PI {+ d += std::f64::consts::TAU;+ }+ sweep += d;+ }+ let turn = sweep.abs();+ if turn > std::f64::consts::TAU + 1e-6 || turn < CIRCLE_MIN_TURN {+ return None;+ }+ if first.dist(last) <= tol {+ return (turn > std::f64::consts::TAU - 1e-6).then_some((LinkGeom::Circle(c, radius), radius));+ }+ // Counter-clockwise from `a` to `b`.+ let (a, b) = if sweep > 0.0 { (first, last) } else { (last, first) };+ Some((LinkGeom::Arc { c, a, b }, radius))}/// `edges` of `body` projected on `plane`: each exactly ([`exact_edges`]) or fitted to its@@ -1058,36 +1127,171 @@ fn section_of_polyline(plane: &Plane, pts: &[Vec3]) -> Vec<LinkGeom> {}/// The outline of a body seen along the sketch normal: mesh edges between triangles facing the-/// sketch and triangles facing away (or edge-on), projected and fitted.-fn silhouette(plane: &Plane, m: &Mesh) -> Vec<LinkGeom> {+/// sketch and triangles facing away (or edge-on), projected and fitted. A face of `body` that+/// faces one way all over ([`face_facing`]) does so in every triangle, though they lean a hair+/// either way: a round wall seen edge-on joins its two rims at different angles, and thin+/// slivers of a narrow chamfer can point the other way.+fn silhouette(plane: &Plane, m: &Mesh, body: Option<&solidcraft_kernel::Body>) -> Vec<LinkGeom> {let n = plane.normal();let size = m.bounds().diagonal().max(1e-9);- let q = size * 1e-7;- let key = |p: Vec3| ((p.x / q).round() as i64, (p.y / q).round() as i64, (p.z / q).round() as i64);+ // Vertices welded within this: the copies of a vertex on the seam between two faces can lie+ // a few 1e-7 apart, and an edge used by one triangle only is outline.+ let weld = size * 1e-6;+ let cell = |p: Vec3| ((p.x / weld).floor() as i64, (p.y / weld).floor() as i64, (p.z / weld).floor() as i64);+ let mut grid: BTreeMap<(i64, i64, i64), Vec<usize>> = BTreeMap::new();+ let mut verts: Vec<Vec3> = Vec::new();+ let mut vertex = |p: Vec3| -> usize {+ let (x, y, z) = cell(p);+ for dx in -1..=1 {+ for dy in -1..=1 {+ for dz in -1..=1 {+ let found = grid+ .get(&(x + dx, y + dy, z + dz))+ .and_then(|ids| ids.iter().copied().find(|i| verts.get(*i).is_some_and(|v| v.dist(p) <= weld)));+ if let Some(i) = found {+ return i;+ }+ }+ }+ }+ verts.push(p);+ grid.entry((x, y, z)).or_default().push(verts.len() - 1);+ verts.len() - 1+ };+ let tris: Vec<([Vec3; 3], Vec3, Option<u32>)> = m+ .triangles+ .iter()+ .enumerate()+ .filter_map(|(ti, t)| {+ let [a, b, c] = m.tri(t)?;+ Some(([a, b, c], (b - a).cross(c - a), m.tri_face.get(ti).copied()))+ })+ .collect();+ // Each face's area-weighted facing, and whether any triangle of it is in doubt: near+ // edge-on, or facing against the rest of its face. Only those faces ask what they are.+ let mut faces: BTreeMap<u32, (f64, bool)> = BTreeMap::new();+ for (_, w, f) in &tris {+ if let Some(f) = f {+ faces.entry(*f).or_default().0 += w.dot(n);+ }+ }+ for (_, w, f) in &tris {+ let (Some(f), Some(un)) = (f, w.normalized()) else { continue };+ if let Some(face) = faces.get_mut(f) {+ face.1 |= un.dot(n).abs() < 0.5 || (un.dot(n) > 0.0) != (face.0 > 0.0);+ }+ }+ let facing: BTreeMap<u32, Facing> = match body {+ Some(body) => faces.iter().filter(|(_, (_, doubt))| *doubt).filter_map(|(f, (sum, _))| Some((*f, face_facing(body, *f, n, *sum)?))).collect(),+ None => BTreeMap::new(),+ };// Undirected mesh edge → facing flags of the triangles using it.- let mut edges: BTreeMap<((i64, i64, i64), (i64, i64, i64)), (Vec3, Vec3, Vec<bool>)> = BTreeMap::new();- for t in &m.triangles {- let Some([a, b, c]) = m.tri(t) else { continue };- let fnrm = (b - a).cross(c - a);- let Some(un) = fnrm.normalized() else { continue };- let front = un.dot(n) > 1e-6;- for (p, r) in [(a, b), (b, c), (c, a)] {- let (kp, kr) = (key(p), key(r));- let k = if kp <= kr { (kp, kr) } else { (kr, kp) };- edges.entry(k).or_insert_with(|| (p, r, Vec::new())).2.push(front);+ let ids: Vec<[usize; 3]> = tris.iter().map(|([a, b, c], _, _)| [vertex(*a), vertex(*b), vertex(*c)]).collect();+ let mut edges: BTreeMap<(usize, usize), (Vec3, Vec3, Vec<(bool, Option<u32>)>)> = BTreeMap::new();+ for ((_, w, f), [ia, ib, ic]) in tris.iter().zip(ids) {+ let Some(un) = w.normalized() else { continue };+ let front = match f.and_then(|f| facing.get(&f)) {+ Some(Facing::Front) => true,+ Some(Facing::Away) => false,+ None => un.dot(n) > 1e-6,+ };+ for (p, r) in [(ia, ib), (ib, ic), (ic, ia)] {+ if p == r {+ continue;+ }+ let (Some(vp), Some(vr)) = (verts.get(p).copied(), verts.get(r).copied()) else { continue };+ edges.entry((p.min(r), p.max(r))).or_insert((vp, vr, Vec::new())).2.push((front, *f));}}- let segs: Vec<(Vec2, Vec2)> = edges+ let mut segs: Vec<(Vec2, Vec2)> = edges.values().filter(|(_, _, f)| {- let fronts = f.iter().filter(|x| **x).count();- fronts > 0 && fronts < f.len() || (f.len() == 1 && fronts == 1)+ let fronts = f.iter().filter(|x| x.0).count();+ fronts > 0 && fronts < f.len()}).map(|(a, b, _)| (plane.to_local(*a), plane.to_local(*b))).collect();+ let singles: Vec<(Vec3, Vec3, bool, Option<u32>)> = edges+ .values()+ .filter_map(|(a, b, f)| match f.as_slice() {+ [(front, face)] => Some((*a, *b, *front, *face)),+ _ => None,+ })+ .collect();+ segs.extend(open_edges(&singles, size).into_iter().map(|(a, b)| (plane.to_local(a), plane.to_local(b))));fit_segments(&segs, size, true)}+/// Of the mesh edges used by one triangle each (`a`, `b`, whether the triangle faces the sketch,+/// its face), those facing the sketch that are outline: an open sheet's border. Where two faces+/// meet, one face's triangles can split the seam at a vertex the other's run straight past, and+/// both sides are then used once; a seam whose other side faces the sketch too is no outline.+fn open_edges(singles: &[(Vec3, Vec3, bool, Option<u32>)], size: f64) -> Vec<(Vec3, Vec3)> {+ // The two sides of a seam stray from each other by at most a chord's sag.+ let seam = size * 2e-3;+ let cell = seam * 8.0;+ let at = |p: Vec3| ((p.x / cell).floor() as i64, (p.y / cell).floor() as i64, (p.z / cell).floor() as i64);+ let mut grid: BTreeMap<(i64, i64, i64), Vec<usize>> = BTreeMap::new();+ let mut long: Vec<usize> = Vec::new();+ for (i, (a, b, front, _)) in singles.iter().enumerate() {+ if !front {+ continue;+ }+ let (lo, hi) = (at(a.min(*b) - Vec3::new(seam, seam, seam)), at(a.max(*b) + Vec3::new(seam, seam, seam)));+ let cells = (hi.0 - lo.0 + 1).saturating_mul(hi.1 - lo.1 + 1).saturating_mul(hi.2 - lo.2 + 1);+ if cells > 512 {+ long.push(i);+ continue;+ }+ for x in lo.0..=hi.0 {+ for y in lo.1..=hi.1 {+ for z in lo.2..=hi.2 {+ grid.entry((x, y, z)).or_default().push(i);+ }+ }+ }+ }+ singles+ .iter()+ .enumerate()+ .filter(|(i, (a, b, front, face))| {+ let m = (*a + *b) * 0.5;+ *front+ && !grid+ .get(&at(m))+ .into_iter()+ .flatten()+ .chain(&long)+ .any(|j| singles.get(*j).is_some_and(|(p, q, _, other)| j != i && other != face && m.dist_to_segment(*p, *q) <= seam))+ })+ .map(|(_, (a, b, _, _))| (*a, *b))+ .collect()+}++/// Which way a whole face of a body faces, seen along a sketch normal.+#[derive(Clone, Copy)]+enum Facing {+ Front,+ /// Away from the sketch or edge-on.+ Away,+}++/// Which way face `face` of `body` faces seen along `n`, when it faces one way all over: a+/// plane (edge-on when it contains `n`), a cylinder about `n` (edge-on), a cone about `n`.+/// `sum` is the face's triangles' normals weighted by their areas, dotted with `n`: the side a+/// plane or such a cone faces. `None` for any other face: each triangle says.+fn face_facing(body: &solidcraft_kernel::Body, face: u32, n: Vec3, sum: f64) -> Option<Facing> {+ use solidcraft_kernel::FaceSurface;+ let side = if sum > 0.0 { Facing::Front } else { Facing::Away };+ match solidcraft_kernel::face_surface(body, face as usize)? {+ FaceSurface::Plane { normal, .. } if normal.vec().dot(n).abs() < 1e-9 => Some(Facing::Away),+ FaceSurface::Plane { .. } => Some(side),+ FaceSurface::Cylinder { axis, .. } if axis.vec().dot(n).abs() > 1.0 - 1e-12 => Some(Facing::Away),+ FaceSurface::Cone { axis, .. } if axis.vec().dot(n).abs() > 1.0 - 1e-12 => Some(side),+ _ => None,+ }+}+/// Section of a mesh (or one of its faces) with the sketch plane.pub(crate) fn section(plane: &Plane, m: &Mesh, face: Option<u32>) -> Vec<LinkGeom> {let size = m.bounds().diagonal().max(1e-9);diff --git a/crates/engine/src/cmd/sketch_project_tests.rs b/crates/engine/src/cmd/sketch_project_tests.rsindex a17c1fa40d..5565554574 100644--- a/crates/engine/src/cmd/sketch_project_tests.rs+++ b/crates/engine/src/cmd/sketch_project_tests.rs@@ -1086,6 +1086,106 @@ fn whole_body_project_of_a_filleted_plate_keeps_exact_lines_and_arcs() {}}+/// A U-slot cut into a plate's edge with its top rim rounded: the round meets the top face on a+/// circle of radius 6.5 + 1 about the slot's axis, but the kernel makes that edge a spline that+/// only follows the circle (within 2e-6 mm), and it projected as twenty spline pieces. The+/// edge is a circle (the selection readout says so): it projects as an arc of that circle.+#[test]+fn a_rounded_slot_rim_projects_as_an_arc_of_its_circle() {+ use solidcraft_sketch::CurveKind;+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 10, "body_name": "Plate"}));+ run(&mut s, "solid.cylinder", json!({"radius": 6.5, "height": 20, "base": [20, 2, -5], "operation": "cut", "targets": ["Plate"]}));+ run(&mut s, "solid.fillet", json!({"edges": [[20, 8.5, 10]], "radius": 1}));+ let edge = run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 30, "project_edges": false}))["sketch"].as_u64().unwrap();+ run(&mut s, "sketch.project", json!({"refs": [{"body": "Plate", "edge": [20, 9.5, 10]}]}));+ run(&mut s, "sketch.finish", json!({}));+ let face = run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 30, "project_edges": false}))["sketch"].as_u64().unwrap();+ run(&mut s, "sketch.project", json!({"refs": [{"body": "Plate", "face": [5, 25, 10]}]}));+ run(&mut s, "sketch.finish", json!({}));++ let state = s.model.state();+ let center = solidcraft_geom::Vec2::new(20., 2.);+ for id in [edge, face] {+ let sk = &state.sketch(id).unwrap().sketch;+ let pos = |i: usize| sk.points[i].pos;+ let mut arcs = 0;+ for curve in &sk.curves {+ match curve.kind {+ CurveKind::Line { .. } => {}+ CurveKind::Arc { c, a, b } => {+ arcs += 1;+ assert!(pos(c).dist(center) < 1e-5, "arc centre {:?}", pos(c));+ for end in [a, b] {+ assert!((pos(end).dist(pos(c)) - 7.5).abs() < 1e-5, "arc end {:?} off the circle", pos(end));+ }+ }+ ref other => panic!("sketch {id}: the rim projects as {other:?}, not as lines and arcs"),+ }+ }+ assert!(arcs >= 1, "sketch {id}: no arc in {:?}", sk.curves);+ }+ // The face's outline still closes: the plate's top less the slot's disc of radius 7.5 (its+ // part above the front face, whose chord lies 2 below the centre).+ let (r, d) = (7.5f64, 2f64);+ let below = r * r * (d / r).acos() - d * (r * r - d * d).sqrt();+ let area = 40. * 30. - (std::f64::consts::PI * r * r - below);+ let profiles = &state.sketch(face).unwrap().profiles;+ assert!(profiles.iter().any(|p| (p.area - area).abs() < 1e-3), "{:?} vs {area}", profiles.iter().map(|p| p.area).collect::<Vec<_>>());+}++/// A hook: a J of lines and two arcs about (-15, 0), of radii 7.5 and 15, extruded 20 with its+/// top edges chamfered. Seen along the extrusion its outline is the J again. The walls are seen+/// edge-on, but the triangles of a round wall join its two rims at slightly different angles+/// and each leans a hair towards the sketch or away: the outline ran zig-zag along the walls+/// and came in as 74 short lines. Faces that lie edge-on are edge-on in every triangle.+#[test]+fn whole_body_project_of_a_chamfered_hook_keeps_its_arcs() {+ use solidcraft_sketch::CurveKind;+ let mut s = Session::default();+ run(&mut s, "sketch.create", json!({"plane": "XY"}));+ run(&mut s, "sketch.line", json!({"points": [[0, 60], [0, 0]]}));+ run(&mut s, "sketch.arc.center_point", json!({"center": [-15, 0], "start": [-30, 0], "end": [0, 0]}));+ run(&mut s, "sketch.line", json!({"points": [[-30, 0], [-30, 10], [-22.5, 10], [-22.5, 0]]}));+ run(&mut s, "sketch.arc.center_point", json!({"center": [-15, 0], "start": [-22.5, 0], "end": [-7.5, 0]}));+ run(&mut s, "sketch.line", json!({"points": [[-7.5, 0], [-7.5, 60], [0, 60]]}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.extrude", json!({"distance": 20, "body_name": "Hook"}));+ run(&mut s, "solid.chamfer", json!({"edges": [[-15, -15, 20], [-15, -7.5, 20], [0, 30, 20], [-7.5, 30, 20]], "distance": 0.25}));+ let sketch = run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 40, "project_edges": false}))["sketch"].as_u64().unwrap();+ run(&mut s, "sketch.project", json!({"refs": [{"body": "Hook"}]}));+ run(&mut s, "sketch.finish", json!({}));++ let state = s.model.state();+ let solved = state.sketch(sketch).unwrap();+ let sk = &solved.sketch;+ let pos = |i: usize| sk.points[i].pos;+ let mut radii = Vec::new();+ for curve in &sk.curves {+ match curve.kind {+ CurveKind::Line { .. } => {}+ CurveKind::Arc { c, a, b } => {+ assert!(pos(c).dist(solidcraft_geom::Vec2::new(-15., 0.)) < 1e-3, "arc centre {:?}", pos(c));+ assert!((pos(a).dist(pos(c)) - pos(b).dist(pos(c))).abs() < 1e-6);+ radii.push(pos(a).dist(pos(c)));+ }+ ref other => panic!("the outline projects as {other:?}, not as lines and arcs"),+ }+ }+ radii.sort_by(f64::total_cmp);+ assert_eq!(radii.len(), 2, "{} curves, arcs of radii {radii:?}", sk.curves.len());+ assert!((radii[0] - 7.5).abs() < 1e-3 && (radii[1] - 15.).abs() < 1e-3, "{radii:?}");+ assert_eq!(sk.curves.len(), 8, "{:?}", sk.curves);+ // One closed profile: the J's area, a rectangle 7.5 x 60 and a half ring 7.5 to 15 with its+ // 7.5 x 10 tip.+ let area = 7.5 * 60. + std::f64::consts::PI * (15. * 15. - 7.5 * 7.5) / 2. + 7.5 * 10.;+ assert!(+ solved.profiles.iter().any(|p| (p.area - area).abs() < 0.05),+ "{:?} vs {area}",+ solved.profiles.iter().map(|p| p.area).collect::<Vec<_>>()+ );+}+#[test]fn whole_body_intersect_of_a_filleted_plate_keeps_exact_lines_and_arcs() {use solidcraft_sketch::CurveKind;