fix(sketch): a side put onto a projected line keeps its closed profile (#635) (#780)

A side constrained onto a projected line keeps the sketch's closed profile. Fixes #635.
This commit is contained in:
semmelrinde authored and GitHub committed 2026-10-13 12:35:33 -04:00
1 parent dfa9e094cd
commit 159f9913c1
4 files changed
+441 -6

No files matched your search

+240
View File
@@ -320,6 +320,246 @@ fn profiles_split_at_crossings_and_touch_points() {
assert!(total > PI * 500.0, "{total}");
}
/// 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();
ids.sort();
ids
}
/// A projected line of `a`–`b` in `sk`.
fn reference_line(sk: &mut Sketch, a: Vec2, b: Vec2) -> usize {
let link = sk.add_link(LinkKind::Project, LinkSource::SketchCurve { sketch: 1, curve: "edge".into() }, &[LinkGeom::Line(a, b)]).unwrap();
sk.link_curves(&link)[0]
}
/// #635: a side constrained onto a longer projected line lies on it only up to the solver's
/// round-off. Where they overlap they are one edge of the drawing, so the rectangle is still a
/// closed profile, and the curve that came first names that edge, as when the side lies on the
/// line exactly.
#[test]
fn a_side_on_a_projected_line_still_closes_its_rectangle() {
// Round-off left on the side's two ends (the first pair as solved in #635), and none.
for (y0, y1) in
[(1.6e-12, 2.2e-12), (2.2e-12, 1.6e-12), (-1e-12, 1e-12), (1e-12, -1e-12), (-3e-11, -1e-11), (-3e-10, 3e-10), (2e-10, 4e-10), (0.0, 0.0)]
{
for reference_first in [true, false] {
for (ra, rb) in [(v(40.0, 0.0), v(-10.0, 0.0)), (v(-10.0, 0.0), v(40.0, 0.0))] {
let mut sk = Sketch::new();
let early = reference_first.then(|| reference_line(&mut sk, ra, rb));
let sides = rect(&mut sk, [v(5.0, y0), v(25.0, y1), v(25.0, 15.0), v(5.0, 15.0)]);
let reference = early.unwrap_or_else(|| reference_line(&mut sk, ra, rb));
let case = format!("ends at y {y0}, {y1}; reference {} {ra:?} to {rb:?}", if reference_first { "first" } else { "last" });
let ps = find_profiles(&sk);
assert_eq!(ps.len(), 1, "{case}: {:?}", ps.iter().map(|p| (p.area, &p.outer_curves)).collect::<Vec<_>>());
assert!((ps[0].area - 300.0).abs() < 1e-6, "{case}: {}", ps[0].area);
let mut got = ps[0].outer_curves.clone();
got.sort();
let want = id_set(&sk, &[if reference_first { reference } else { sides[0] }, sides[1], sides[2], sides[3]]);
assert_eq!(got, want, "{case}");
// Whole curves make a lasting identity; a piece of the reference cut at the corners does not.
let whole = (!reference_first).then(|| (want, Vec::new()));
assert_eq!(ps[0].whole_curve_boundaries(), whole, "{case}");
}
}
}
}
/// A third curve meeting a side and the projected line under it would meet each a little apart:
/// a circle touching the line from inside the rectangle (it misses or crosses the side by the
/// round-off), and a line crossing both at a shallow angle (the crossings 3e-5 mm apart). It
/// meets both where it meets the first of them, so they still make one edge, whichever way the
/// side runs.
#[test]
fn a_curve_meeting_a_side_on_a_projected_line_meets_both_at_once() {
for y0 in [3e-10, -3e-10, 0.0] {
for reference_first in [true, false] {
for reversed in [false, true] {
let case = format!("side at y {y0}, reference {}, side reversed {reversed}", if reference_first { "first" } else { "last" });
let corners = |x1: f64| {
let (a, b) = if reversed { (x1, 5.0) } else { (5.0, x1) };
[v(a, y0), v(b, y0), v(b, 15.0), v(a, 15.0)]
};
let mut sk = Sketch::new();
let early = reference_first.then(|| reference_line(&mut sk, v(-10.0, 0.0), v(40.0, 0.0)));
rect(&mut sk, corners(25.0));
sk.add_circle(v(15.0, 5.0), 5.0, None, None).unwrap();
early.unwrap_or_else(|| reference_line(&mut sk, v(-10.0, 0.0), v(40.0, 0.0)));
let mut areas: Vec<f64> = find_profiles(&sk).iter().map(|p| p.area).collect();
areas.sort_by(f64::total_cmp);
assert_eq!(areas.len(), 2, "touching circle, {case}: {areas:?}");
assert!((areas[0] - 25.0 * PI).abs() < 1e-6 && (areas[1] - (300.0 - 25.0 * PI)).abs() < 1e-6, "touching circle, {case}: {areas:?}");
let mut sk = Sketch::new();
let early = reference_first.then(|| reference_line(&mut sk, v(-10.0, 0.0), v(440.0, 0.0)));
rect(&mut sk, corners(405.0));
// Through (200, 0), rising 1e-5 per mm: a sliver of 205 x 0.00205 / 2 under it.
sk.add_line(v(0.0, -0.002), v(430.0, 0.0023), None, None, None).unwrap();
early.unwrap_or_else(|| reference_line(&mut sk, v(-10.0, 0.0), v(440.0, 0.0)));
let mut areas: Vec<f64> = find_profiles(&sk).iter().map(|p| p.area).collect();
areas.sort_by(f64::total_cmp);
assert_eq!(areas.len(), 2, "shallow line, {case}: {areas:?}");
assert!((areas[0] - 0.210125).abs() < 1e-6 && (areas[1] - (6000.0 - 0.210125)).abs() < 1e-6, "shallow line, {case}: {areas:?}");
}
}
}
}
/// The same for drawn curves on drawn curves: two rectangles drawn side by side, the second's
/// side solved onto the first's; and a quarter disc whose arc is solved onto a projected circle.
#[test]
fn curves_solved_onto_each_other_keep_the_regions_on_both_sides() {
for (x0, x1) in [(1e-12, -1e-12), (-2e-12, -1e-12), (2.2e-12, 1.6e-12), (0.0, 0.0)] {
let mut sk = Sketch::new();
let a = rect(&mut sk, [v(0.0, 0.0), v(20.0, 0.0), v(20.0, 15.0), v(0.0, 15.0)]);
let b = rect(&mut sk, [v(20.0 + x0, 0.0), v(40.0, 0.0), v(40.0, 15.0), v(20.0 + x1, 15.0)]);
let ps = find_profiles(&sk);
let areas: Vec<f64> = ps.iter().map(|p| p.area).collect();
assert_eq!(ps.len(), 2, "{x0}, {x1}: {areas:?}");
assert!(areas.iter().all(|a| (a - 300.0).abs() < 1e-6), "{x0}, {x1}: {areas:?}");
// The shared side is the first rectangle's (it was there first).
let mut first = ps[0].outer_curves.clone();
first.sort();
assert_eq!(first, id_set(&sk, &a), "{x0}, {x1}");
let mut second = ps[1].outer_curves.clone();
second.sort();
assert_eq!(second, id_set(&sk, &[b[0], b[1], b[2], a[1]]), "{x0}, {x1}");
}
let hole = |sk: &mut Sketch| {
let link = sk
.add_link(LinkKind::Project, LinkSource::SketchCurve { sketch: 1, curve: "hole".into() }, &[LinkGeom::Circle(v(0.0, 0.0), 10.0)])
.unwrap();
sk.link_curves(&link)[0]
};
for (dx, dr) in [(1e-12, 2e-12), (-1e-12, -1e-12), (0.0, 0.0)] {
for circle_first in [true, false] {
let mut sk = Sketch::new();
let early = circle_first.then(|| hole(&mut sk));
let c = sk.add_point(v(dx, -dx), None).unwrap();
let (p0, p1) = (sk.add_point(v(10.0 + dr, 0.0), None).unwrap(), sk.add_point(v(0.0, 10.0 + dr), None).unwrap());
let arc = sk.add_arc(v(dx, -dx), v(10.0 + dr, 0.0), v(0.0, 10.0 + dr), [Some(c), Some(p0), Some(p1)], None).unwrap();
let (l0, l1) = (sk.add_line_pts(c, p0, None).unwrap(), sk.add_line_pts(p1, c, None).unwrap());
let circle = early.unwrap_or_else(|| hole(&mut sk));
let case = format!("{dx}, {dr}, circle {}", if circle_first { "first" } else { "last" });
let ps = find_profiles(&sk);
let areas: Vec<f64> = ps.iter().map(|p| p.area).collect();
assert_eq!(ps.len(), 2, "{case}: {areas:?}");
let quarter = ps.iter().find(|p| p.area < 100.0).unwrap();
assert!((quarter.area - 25.0 * PI).abs() < 1e-6, "{case}: {areas:?}");
// One piece each: round-off alone does not cut the arc where it crosses the circle.
let mut got = quarter.outer_curves.clone();
got.sort();
assert_eq!(got, id_set(&sk, &[if circle_first { circle } else { arc }, l0, l1]), "{case}");
let rest = ps.iter().find(|p| p.area > 100.0).unwrap();
assert!((rest.area - 75.0 * PI).abs() < 1e-6, "{case}: {areas:?}");
}
}
}
/// What does not run along one path stays apart: a chord and an arc bulging past it by about
/// the merge tolerance still bound their thin region, and two whole circles further apart than
/// it are still cut where they cross, into a lens and two crescents. Closer than it, their
/// halves are one path: one disc.
#[test]
fn near_overlaps_that_are_not_one_path_keep_their_regions() {
let s = 1.5e-5;
let r = (25.0 + s * s) / (2.0 * s);
let mut sk = Sketch::new();
let (a, b) = (sk.add_point(v(-5.0, 0.0), None).unwrap(), sk.add_point(v(5.0, 0.0), None).unwrap());
let chord = sk.add_line_pts(a, b, None).unwrap();
let arc = sk.add_arc(v(0.0, s - r), v(5.0, 0.0), v(-5.0, 0.0), [None, Some(b), Some(a)], None).unwrap();
let ps = find_profiles(&sk);
assert_eq!(ps.len(), 1, "{:?}", ps.iter().map(|p| p.area).collect::<Vec<_>>());
// Two thirds of chord times height, to well within the rounding of the area.
assert!((ps[0].area - 2.0 / 3.0 * 10.0 * s).abs() < 1e-8, "{}", ps[0].area);
let mut got = ps[0].outer_curves.clone();
got.sort();
assert_eq!(got, id_set(&sk, &[chord, arc]));
// The same with a free-form curve: a flat cubic (height s) over its chord. It takes part as
// sampled pieces, one of them on the chord, but its exact curve bounds the region.
let s = 8e-6;
let mut sk = Sketch::new();
let ids: Vec<usize> = [v(-5.0, 0.0), v(-5.0 / 3.0, 4.0 * s / 3.0), v(5.0 / 3.0, 4.0 * s / 3.0), v(5.0, 0.0)]
.iter()
.map(|p| sk.add_point(*p, None).unwrap())
.collect();
sk.add_curve(CurveKind::Spline { pts: ids.clone(), control: true, degree: 3, periodic: false }, None).unwrap();
sk.add_line_pts(ids[3], ids[0], None).unwrap();
let ps = find_profiles(&sk);
assert_eq!(ps.len(), 1, "{:?}", ps.iter().map(|p| p.area).collect::<Vec<_>>());
assert!((ps[0].area - 20.0 * s / 3.0).abs() < 1e-9, "{}", ps[0].area);
// An arc a little off a circle, more than the merge tolerance apart at its ends (so they
// don't cut the circle): the two still cut each other where they cross, and no profile is
// the uncut circle.
let mut sk = Sketch::new();
let circle = sk.add_circle(v(0.0, 0.0), 10.0, None, None).unwrap();
let (c, r) = (v(8e-6, 0.0), 10.0 + 6e-6);
let (p0, p1) = (c + Vec2::from_angle(PI / 4.0) * r, c + Vec2::from_angle(-PI / 4.0) * r);
let (a0, a1, tip) = (sk.add_point(p0, None).unwrap(), sk.add_point(p1, None).unwrap(), sk.add_point(v(30.0, 0.0), None).unwrap());
sk.add_arc(c, p0, p1, [None, Some(a0), Some(a1)], None).unwrap();
sk.add_line_pts(a1, tip, None).unwrap();
sk.add_line_pts(tip, a0, None).unwrap();
let ps = find_profiles(&sk);
let circle_id = sk.curves[circle].id.clone();
assert!(ps.iter().all(|p| p.outer_curves != [circle_id.clone()] && p.whole_curve_boundaries().is_none()), "{ps:?}");
// Profiles don't overlap: together they cover the disc and the arc's region once, that is the
// disc and the triangle to the tip less the disc's segment inside it (to within the slivers
// between arc and circle).
let beyond = 0.5 * (p0 - v(30.0, 0.0)).cross(p1 - v(30.0, 0.0)).abs() - 50.0 * (PI / 2.0 - 1.0);
let covered = ps.iter().map(|p| p.area).sum::<f64>();
assert!((covered - PI * 100.0 - beyond).abs() < 1e-2, "{:?}", ps.iter().map(|p| p.area).collect::<Vec<_>>());
let r = 10.0_f64;
for (d, regions) in [(1e-3, 3), (1e-4, 3), (8e-6, 1)] {
let mut sk = Sketch::new();
let first = sk.add_circle(v(0.0, 0.0), r, None, None).unwrap();
sk.add_circle(v(d, 0.0), r, None, None).unwrap();
let ps = find_profiles(&sk);
let areas: Vec<f64> = ps.iter().map(|p| p.area).collect();
assert_eq!(ps.len(), regions, "{d}: {areas:?}");
let lens = 2.0 * r * r * (d / (2.0 * r)).acos() - d / 2.0 * (4.0 * r * r - d * d).sqrt();
let union = if regions == 1 { PI * r * r } else { 2.0 * PI * r * r - lens };
assert!((areas.iter().sum::<f64>() - union).abs() < 1e-6, "{d}: {areas:?}");
// Cut where they cross: no whole-circle identity.
assert!(ps.iter().all(|p| p.whole_curve_boundaries().is_none()), "{d}");
if regions == 1 {
assert_eq!(ps[0].outer_curves, vec![sk.curves[first].id.clone(); 2], "{d}: the first circle's halves");
}
}
}
/// Curves count as lying on one another only within the merge tolerance of the one carrying
/// them, not along a chain: an arc between two whole circles 1.6e-5 apart (each within 8e-6 of
/// it) does not stop the circles cutting each other where they cross.
#[test]
fn curves_lying_on_one_another_do_not_chain_past_the_tolerance() {
let (d, r) = (16e-6, 10.0_f64);
let mut sk = Sketch::new();
sk.add_circle(v(0.0, 0.0), r, None, None).unwrap();
sk.add_circle(v(d, 0.0), r, None, None).unwrap();
let c = v(d / 2.0, 0.0);
let (p0, p1) = (c + Vec2::from_angle(PI / 4.0) * r, c + Vec2::from_angle(3.0 * PI / 4.0) * r);
sk.add_arc(c, p0, p1, [None, None, None], None).unwrap();
let areas: Vec<f64> = find_profiles(&sk).iter().map(|p| p.area).collect();
let lens = 2.0 * r * r * (d / (2.0 * r)).acos() - d / 2.0 * (4.0 * r * r - d * d).sqrt();
// The discs' union once, not each disc whole.
assert!((areas.iter().sum::<f64>() - (2.0 * PI * r * r - lens)).abs() < 1e-6, "{areas:?}");
// Two dividers of a square, each carried by a short line 8e-6 off it that does not reach
// their crossing: they still cross at one node, and the square has four quarters.
let mut sk = Sketch::new();
sk.add_line(v(-20.0, 0.0), v(-10.0, 0.0), None, None, None).unwrap();
sk.add_line(v(0.0, -20.0), v(0.0, -10.0), None, None, None).unwrap();
rect(&mut sk, [v(-15.0, -15.0), v(15.0, -15.0), v(15.0, 15.0), v(-15.0, 15.0)]);
sk.add_line(v(-15.0, 8e-6), v(15.0, 8e-6), None, None, None).unwrap();
sk.add_line(v(8e-6, -15.0), v(8e-6, 15.0), None, None, None).unwrap();
let areas: Vec<f64> = find_profiles(&sk).iter().map(|p| p.area).collect();
assert_eq!(areas.len(), 4, "{areas:?}");
assert!((areas.iter().sum::<f64>() - 900.0).abs() < 1e-6, "{areas:?}");
}
#[test]
fn merging_adjacent_regions() {
let a =