fix: joins, cuts and sketch profiles where outlines touch at a point (#841)
Fixes #588.
This commit is contained in:
1 parent
b6e3a6c366
commit
c3e0ea498a
8 files changed
+458
-37
No files matched your search
@@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
|
||||
use solidcraft_geom::{Loop2, Region2, Seg2, Vec2};
|
||||
|
||||
use crate::model::{CurveKind, Sketch};
|
||||
use crate::shape::{Shape, intersections};
|
||||
use crate::shape::{Shape, intersections_within};
|
||||
use crate::union_find::{find, union};
|
||||
|
||||
const MERGE_TOL: f64 = 1e-6;
|
||||
@@ -742,7 +742,7 @@ fn find_profiles_bounded(sk: &Sketch, max_edges: usize) -> Vec<Profile> {
|
||||
if gi == gj {
|
||||
continue;
|
||||
}
|
||||
let mut pts = intersections(gx, gy);
|
||||
let mut pts = intersections_within(gx, gy, tol);
|
||||
// A tangent touch gives two nearly equal roots: one point.
|
||||
if let [p, q] = pts[..]
|
||||
&& p.dist(q) < tol * 100.0
|
||||
|
||||
@@ -97,6 +97,45 @@ pub fn intersections(x: &Shape, y: &Shape) -> Vec<Vec2> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where two unbounded curves meet, missing by no more than `tol`: a line or circle that touches
|
||||
/// a circle within `tol` (outside or inside) meets it where it comes closest, as a double root.
|
||||
/// [`intersections`] takes the roots of the quadratic as they come, and at a tangency these are
|
||||
/// rounding noise: a line made tangent to a circle by the solver misses it whenever the noise
|
||||
/// falls outside, so the regions it closes off are never found (#588). Here the root's square
|
||||
/// root is clamped instead, and the touch comes back as two equal points (which profile
|
||||
/// detection merges). Circles nearly on one another still cross where they cross. Computed in
|
||||
/// the circle's own frame (no cancellation on long lines or small circles).
|
||||
pub fn intersections_within(x: &Shape, y: &Shape, tol: f64) -> Vec<Vec2> {
|
||||
match (*x, *y) {
|
||||
(Shape::Line { a, b }, Shape::Round { c, r, .. }) | (Shape::Round { c, r, .. }, Shape::Line { a, b }) => {
|
||||
let Some(direction) = (b - a).normalized() else { return Vec::new() };
|
||||
let foot = a + direction * (c - a).dot(direction);
|
||||
let distance = foot.dist(c);
|
||||
if distance > r + tol {
|
||||
return Vec::new();
|
||||
}
|
||||
let half = ((r - distance).max(0.0) * (r + distance)).sqrt();
|
||||
vec![foot - direction * half, foot + direction * half]
|
||||
}
|
||||
(Shape::Round { c: c1, r: r1, .. }, Shape::Round { c: c2, r: r2, .. }) => {
|
||||
let ((small, r), (large, radius)) = if r1 <= r2 { ((c1, r1), (c2, r2)) } else { ((c2, r2), (c1, r1)) };
|
||||
let delta = small - large;
|
||||
let distance = delta.len();
|
||||
if distance < 1e-12 || distance > radius + r + tol || distance < radius - r - tol {
|
||||
return Vec::new();
|
||||
}
|
||||
let u = delta / distance;
|
||||
// Along the line of centres from the large circle; factoring d² - r² avoids the
|
||||
// cancellation of R² - a² for a small circle crossing a huge one.
|
||||
let along = ((distance - r) * (distance + r) + radius * radius) / (2.0 * distance);
|
||||
let height = ((radius - along).max(0.0) * (radius + along).max(0.0)).sqrt();
|
||||
let foot = large + u * along.clamp(-radius, radius);
|
||||
vec![foot - u.perp() * height, foot + u.perp() * height]
|
||||
}
|
||||
_ => intersections(x, y),
|
||||
}
|
||||
}
|
||||
|
||||
impl Shape {
|
||||
/// Unbounded curve parameter of `p` (line: t along a→b; round: angle offset from start in
|
||||
/// [0, 2π)).
|
||||
|
||||
@@ -320,6 +320,73 @@ fn profiles_split_at_crossings_and_touch_points() {
|
||||
assert!(total > PI * 500.0, "{total}");
|
||||
}
|
||||
|
||||
/// Curves made tangent by the solver touch within rounding noise, either side of exact (#588):
|
||||
/// the touch still splits them. Lines running past their tangent points close the band between
|
||||
/// two circles, and a square's side tangent to a circle closes the square off from the disc.
|
||||
#[test]
|
||||
fn solver_tangencies_split_profiles() {
|
||||
use ConstraintKind::*;
|
||||
// Two fixed circles and two free lines, longer than the band, made tangent to both.
|
||||
for (r1, r2, d) in [(15.0, 8.0, 40.0), (20.0, 10.0, 50.0), (7.3, 7.3, 21.1)] {
|
||||
let mut b = Sketch::new();
|
||||
let c1 = b.add_circle(v(0.0, 0.0), r1, None, None).unwrap();
|
||||
let c2 = b.add_circle(v(d, 0.0), r2, None, None).unwrap();
|
||||
let l1 = b.add_line(v(-r1, r1 + 2.0), v(d + r2 + 5.0, r2 - 1.0), None, None, None).unwrap();
|
||||
let l2 = b.add_line(v(-r1, -r1 - 2.0), v(d + r2 + 5.0, 1.0 - r2), None, None, None).unwrap();
|
||||
for c in [c1, c2] {
|
||||
let p = b.resolve_point(&format!("{}.center", b.curves[c].id)).unwrap();
|
||||
b.add_constraint(Fix { p }, None).unwrap();
|
||||
}
|
||||
b.add_constraint(Radius { c: c1, value: r1 }, None).unwrap();
|
||||
b.add_constraint(Radius { c: c2, value: r2 }, None).unwrap();
|
||||
for (l, c) in [(l1, c1), (l1, c2), (l2, c1), (l2, c2)] {
|
||||
b.add_constraint(Tangent { a: l, b: c }, None).unwrap();
|
||||
}
|
||||
assert!(solve(&mut b).ok());
|
||||
let ps = find_profiles(&b);
|
||||
// The band: the trapezoid between the tangent points less the circular segments the
|
||||
// circles take from it (as exact sums: the hull less the two discs).
|
||||
let alpha = ((r1 - r2) / d).asin();
|
||||
let tangent_len = (d * d - (r1 - r2).powi(2)).sqrt();
|
||||
let hull = r1 * r1 * (PI / 2.0 + alpha) + r2 * r2 * (PI / 2.0 - alpha) + (r1 + r2) * tangent_len;
|
||||
let total: f64 = ps.iter().map(|p| p.area).sum();
|
||||
assert_eq!(ps.len(), 3, "r {r1}, {r2}: {:?}", ps.iter().map(|p| p.area).collect::<Vec<_>>());
|
||||
assert!((total - hull).abs() < 1e-6 * hull, "r {r1}, {r2}: {total} vs {hull}");
|
||||
}
|
||||
// A square tangent to a circle inside it and outside it, the circle placed by the solver.
|
||||
for (inside, centre) in [(true, v(5.0, 6.3)), (false, v(5.0, 16.7))] {
|
||||
let mut s = Sketch::new();
|
||||
let sides = [v(0.0, 0.0), v(10.0, 0.0), v(10.0, 10.0), v(0.0, 10.0)];
|
||||
let mut lines = Vec::new();
|
||||
for k in 0..4 {
|
||||
lines.push(s.add_line(sides[k], sides[(k + 1) % 4], None, None, None).unwrap());
|
||||
}
|
||||
for k in 0..4 {
|
||||
let (a, b) = (
|
||||
s.resolve_point(&format!("{}.end", s.curves[lines[k]].id)).unwrap(),
|
||||
s.resolve_point(&format!("{}.start", s.curves[lines[(k + 1) % 4]].id)).unwrap(),
|
||||
);
|
||||
s.add_constraint(Coincident { p: a, q: b }, None).unwrap();
|
||||
s.add_constraint(Fix { p: a }, None).unwrap();
|
||||
}
|
||||
let c = s.add_circle(centre, 3.7, None, None).unwrap();
|
||||
s.add_constraint(Radius { c, value: 3.7 }, None).unwrap();
|
||||
s.add_constraint(Tangent { a: lines[2], b: c }, None).unwrap();
|
||||
assert!(solve(&mut s).ok());
|
||||
let ps = find_profiles(&s);
|
||||
let disc = PI * 3.7 * 3.7;
|
||||
let mut areas: Vec<f64> = ps.iter().map(|p| p.area).collect();
|
||||
areas.sort_by(f64::total_cmp);
|
||||
let want = if inside { [disc, 100.0 - disc] } else { [disc, 100.0] };
|
||||
let mut want = want.to_vec();
|
||||
want.sort_by(f64::total_cmp);
|
||||
assert_eq!(areas.len(), 2, "inside {inside}: {areas:?}");
|
||||
for (a, w) in areas.iter().zip(&want) {
|
||||
assert!((a - w).abs() < 1e-6, "inside {inside}: {areas:?} vs {want:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sorted curve ids.
|
||||
fn id_set(sk: &Sketch, curves: &[usize]) -> Vec<String> {
|
||||
let mut ids: Vec<String> = curves.iter().map(|c| sk.curves[*c].id.clone()).collect();
|
||||
|
||||
Reference in new issue
Block a user