Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Tapered extrude: arcs meeting at corners meet where their moved circles cross (#2810) (#2813)

Mariusz Pacek1999e763ab85
diff --git a/crates/kernel/src/build.rs b/crates/kernel/src/build.rs
index dc3ed31c0f..d4ff0dcd66 100644
--- a/crates/kernel/src/build.rs
+++ b/crates/kernel/src/build.rs
@@ -633,25 +633,99 @@ fn offset_loop(lp: &Loop2, d: f64) -> Result<Vec<Option<Seg2>>> {
}
let all_arcs = lp.segs.iter().all(|s| matches!(s, Seg2::Arc { .. }));
if all_arcs {
- return lp
+ let moved = lp
.segs
.iter()
.map(|s| match *s {
Seg2::Arc { center, radius, start, sweep } => {
let r = radius + sweep.signum() * d;
if r > 1e-6 {
- Ok(Some(Seg2::Arc { center, radius: r, start, sweep }))
+ Ok(Seg2::Arc { center, radius: r, start, sweep })
} else {
Err(KernelError::Invalid("the taper closes the profile".into()))
}
}
_ => Err(KernelError::Failed("taper: profile loop".into())),
})
- .collect();
+ .collect::<Result<Vec<_>>>()?;
+ return arc_corners(lp, moved).map(|arcs| arcs.into_iter().map(Some).collect());
}
offset_segments(lp, d)
}
+/// The moved arcs of an all-arc loop made to meet again: where two arcs of different circles meet
+/// at a corner (a circle and the lobes on its sides), their moved circles no longer cross at the
+/// arcs' old end angles, so the corner goes to where the moved circles cross, nearest the old
+/// corner, and both arcs are cut there. Arcs meeting tangentially (or on one circle) keep their
+/// angles: moved along their shared normal, they still meet.
+fn arc_corners(lp: &Loop2, moved: Vec<Seg2>) -> Result<Vec<Seg2>> {
+ let bad = || KernelError::Failed("taper: profile loop".into());
+ let closes = || KernelError::Invalid("the taper closes the profile".into());
+ let n = moved.len();
+ // Each arc's start and end angle once moved.
+ let mut ang: Vec<(f64, f64)> = moved
+ .iter()
+ .map(|s| match *s {
+ Seg2::Arc { start, sweep, .. } => Ok((start, start + sweep)),
+ _ => Err(bad()),
+ })
+ .collect::<Result<_>>()?;
+ for i in 0..n {
+ let k = (i + n - 1) % n;
+ let (Some(prev), Some(this), Some(Seg2::Arc { center: c1, radius: r1, .. }), Some(Seg2::Arc { center: c2, radius: r2, .. })) =
+ (lp.segs.get(k), lp.segs.get(i), moved.get(k).copied(), moved.get(i).copied())
+ else {
+ return Err(bad());
+ };
+ let (tin, tout) = (prev.end_tangent(), this.start_tangent());
+ if tin.cross(tout).abs() < 1e-6 && tin.dot(tout) > 0.0 {
+ continue;
+ }
+ let x = circles_cross(c1, r1, c2, r2, this.start()).ok_or_else(|| {
+ KernelError::Failed("not supported yet: a taper this steep for arcs meeting at a corner (their moved circles part)".into())
+ })?;
+ if let Some(a) = ang.get_mut(k) {
+ a.1 = (x - c1).angle();
+ }
+ if let Some(a) = ang.get_mut(i) {
+ a.0 = (x - c2).angle();
+ }
+ }
+ moved
+ .iter()
+ .zip(&ang)
+ .map(|(s, &(a0, a1))| match *s {
+ Seg2::Arc { center, radius, sweep, .. } => {
+ // The same turning sense, the whole-turn equivalent nearest the old sweep.
+ let tau = std::f64::consts::TAU;
+ let mut sw = a1 - a0;
+ sw += ((sweep - sw) / tau).round() * tau;
+ if sw * sweep <= 0.0 || sw.abs() < 1e-9 {
+ return Err(closes());
+ }
+ Ok(Seg2::Arc { center, radius, start: a0, sweep: sw })
+ }
+ _ => Err(bad()),
+ })
+ .collect()
+}
+
+/// Where the circles (`c1`, `r1`) and (`c2`, `r2`) cross, the crossing nearest `near`; `None`
+/// when they don't (apart, one inside the other, or concentric).
+fn circles_cross(c1: Vec2, r1: f64, c2: Vec2, r2: f64, near: Vec2) -> Option<Vec2> {
+ let d = c1.dist(c2);
+ if d < 1e-12 || d > r1 + r2 + 1e-9 || d < (r1 - r2).abs() - 1e-9 {
+ return None;
+ }
+ let u = (c2 - c1) * (1.0 / d);
+ let a = (r1 * r1 - r2 * r2 + d * d) / (2.0 * d);
+ let h = (r1 * r1 - a * a).max(0.0).sqrt();
+ let base = c1 + u * a;
+ let (p, q) = (base + u.perp() * h, base - u.perp() * h);
+ let x = if p.dist(near) <= q.dist(near) { p } else { q };
+ x.is_finite().then_some(x)
+}
+
/// Where the line through `p` along `tin` and the one through `q` along `tout` meet once each
/// has moved `d` to its right (`q` moved, where they run on together).
fn mitre(p: Vec2, tin: Vec2, q: Vec2, tout: Vec2, d: f64) -> Vec2 {
diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs
index 69840f9da0..d29152568e 100644
--- a/crates/kernel/src/tests.rs
+++ b/crates/kernel/src/tests.rs
@@ -490,6 +490,43 @@ fn tapered_extrude_of_lines_and_arcs() {
assert!(e.to_string().contains("arc meets another edge at a corner"), "{e}");
}
+/// A circle with a lobe on each side (four arcs of three circles, meeting at corners: the outline of
+/// a scale pocket with finger notches) tapered inward: every circle shrinks by d = z · tan 45°,
+/// and the corners go to where the shrunk circles cross, so each section is the union of the
+/// shrunk circles. Before, the arcs kept their end angles and the sides missed the corners (#2810).
+#[test]
+fn tapered_extrude_of_arcs_meeting_at_corners() {
+ let (r0, w0, c, h, a) = (49.2, 16.2, 41.0, 2.5, 45f64.to_radians());
+ let corner = |r: f64, w: f64| {
+ let x = (r * r - w * w + c * c) / (2.0 * c);
+ (x, (r * r - x * x).sqrt())
+ };
+ let (x, y) = corner(r0, w0);
+ let at = |cx: f64, px: f64, py: f64| (py).atan2(px - cx);
+ let lobed = Loop2 {
+ segs: vec![
+ Seg2::Arc { center: Vec2::ZERO, radius: r0, start: at(0.0, x, y), sweep: at(0.0, -x, y) - at(0.0, x, y) },
+ Seg2::Arc { center: Vec2::new(-c, 0.0), radius: w0, start: at(-c, -x, y), sweep: 2.0 * PI - 2.0 * at(-c, -x, y) },
+ Seg2::Arc { center: Vec2::ZERO, radius: r0, start: at(0.0, -x, -y), sweep: at(0.0, x, -y) - at(0.0, -x, -y) },
+ Seg2::Arc { center: Vec2::new(c, 0.0), radius: w0, start: at(c, x, -y), sweep: 2.0 * at(c, x, y) },
+ ],
+ };
+ let b = extrude_tapered(&Plane::XY, &Region2 { outer: lobed, holes: vec![] }, h, -1.0, -a).unwrap();
+ // Union of the circle and two lobes: the circle plus each lobe less its lens with the circle.
+ let area = |d: f64| {
+ let (r, w) = (r0 - d, w0 - d);
+ let lens = r * r * ((c * c + r * r - w * w) / (2.0 * c * r)).acos() + w * w * ((c * c + w * w - r * r) / (2.0 * c * w)).acos()
+ - 0.5 * ((-c + r + w) * (c + r - w) * (c - r + w) * (c + r + w)).sqrt();
+ PI * r * r + 2.0 * (PI * w * w - lens)
+ };
+ let k = 200;
+ let v: f64 = (0..k).map(|j| area((j as f64 + 0.5) * h / k as f64 * a.tan())).sum::<f64>() * h / k as f64;
+ let m = measure(&b).unwrap();
+ assert!(rel(m.volume, v) < 2e-4, "{} vs {v}", m.volume);
+ assert_eq!(m.merged.faces, 6);
+ assert!(m.bbox.max.z < 1e-6 && m.bbox.min.z > -h - 1e-6, "{:?}", m.bbox);
+}
+
/// A circle drawn as four conics (the way free curves come, as in text) tapered inward: each
/// conic's control polygon moves in by d, which for a circle is the exact smaller circle, so
/// the solid is a cone frustum.