mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 16:10:14 +00:00
Path Precision Tools: shallow crossings are placed where the paths meet
The crossing search splits curves into pieces until each lies within 0.0001 pt of its chord, and takes the place where two chords cross. Where a line meets an arc at under a degree, that place sat up to 0.025 pt along the arc from the real crossing, so Trim at Crossings, its hover and Add Points at Crossings cut there. Each place found is now moved onto both curves with Newton's method on a(s) = b(t). The move is kept only when it stays within both segments, ends closer than 1e-9 pt, and moves no farther than a piece bent by 0.0001 pt can shift a crossing at that angle; otherwise the place stays as it was.
This commit is contained in:
@@ -48,6 +48,42 @@ fn chords_cross(a: &CubicBez, b: &CubicBez) -> Option<(f64, f64)> {
|
||||
(within(s) && within(u)).then(|| (s.clamp(0.0, 1.0), u.clamp(0.0, 1.0)))
|
||||
}
|
||||
|
||||
/// `(s, t)` moved to where `a` and `b` cross exactly, by Newton's method on a(s) = b(t), or as it
|
||||
/// was when that doesn't settle within both segments, closer than it started, and within the
|
||||
/// distance a piece bent by [`FLAT`] can shift a crossing at their angle there.
|
||||
fn refined(a: &CubicBez, b: &CubicBez, (s0, t0): (f64, f64)) -> (f64, f64) {
|
||||
let gap = |s: f64, t: f64| a.eval(s).distance(b.eval(t));
|
||||
let start = gap(s0, t0);
|
||||
if start <= 1e-12 || !start.is_finite() {
|
||||
return (s0, t0);
|
||||
}
|
||||
let (da, db) = (a.deriv(), b.deriv());
|
||||
let (mut s, mut t) = (s0, t0);
|
||||
for _ in 0..8 {
|
||||
let f = a.eval(s) - b.eval(t);
|
||||
let (u, v) = (da.eval(s).to_vec2(), db.eval(t).to_vec2());
|
||||
// a(s + ds) - b(t + dt) ≈ f + u ds - v dt, which is zero for these steps.
|
||||
let den = u.cross(v);
|
||||
let (ds, dt) = (-f.cross(v) / den, u.cross(f) / den);
|
||||
if !(ds.is_finite() && dt.is_finite()) {
|
||||
return (s0, t0);
|
||||
}
|
||||
(s, t) = (s + ds, t + dt);
|
||||
if !(-1e-12..=1.0 + 1e-12).contains(&s) || !(-1e-12..=1.0 + 1e-12).contains(&t) {
|
||||
return (s0, t0);
|
||||
}
|
||||
if gap(s, t) <= 1e-12 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (s, t) = (s.clamp(0.0, 1.0), t.clamp(0.0, 1.0));
|
||||
let (u, v) = (da.eval(s).to_vec2(), db.eval(t).to_vec2());
|
||||
let sin = u.cross(v).abs() / (u.hypot() * v.hypot());
|
||||
let reach = 4.0 * FLAT / sin + SAME;
|
||||
let moved = a.eval(s).distance(a.eval(s0)).max(b.eval(t).distance(b.eval(t0)));
|
||||
if gap(s, t) < start.min(1e-9) && moved <= reach { (s, t) } else { (s0, t0) }
|
||||
}
|
||||
|
||||
/// Where segments `a` and `b` cross: (t on `a`, t on `b`) pairs, halving both while their control
|
||||
/// boxes meet until each piece is straight. `None` once `budget` steps are spent.
|
||||
fn segment_crossings(a: &CubicBez, b: &CubicBez, budget: &mut usize) -> Option<Vec<(f64, f64)>> {
|
||||
@@ -80,6 +116,11 @@ fn segment_crossings(a: &CubicBez, b: &CubicBez, budget: &mut usize) -> Option<V
|
||||
stack.extend([(a0, a1, b0, bm, d), (a0, a1, bm, b1, d)]);
|
||||
}
|
||||
}
|
||||
// A piece counts as straight while it bends by up to FLAT, so where the curves meet at a shallow
|
||||
// angle the chords cross some way along from where the curves do.
|
||||
for f in &mut found {
|
||||
*f = refined(a, b, *f);
|
||||
}
|
||||
found.sort_by(|x, y| x.0.total_cmp(&y.0));
|
||||
// The same crossing found by neighbouring pieces counts once.
|
||||
found.dedup_by(|x, y| a.eval(x.0).distance(a.eval(y.0)) < SAME && b.eval(x.1).distance(b.eval(y.1)) < SAME);
|
||||
|
||||
@@ -244,6 +244,42 @@ fn crossings_are_found_on_both_paths() {
|
||||
assert!(near(hits[1][0].2, 0.0, 1e-6) && near(b.subpaths[0].segment(0).eval(hits[0][0].2).x, 50.0, 1e-6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shallow_crossing_is_placed_where_the_curves_meet() {
|
||||
// Lines square to the middle of a quarter circle of radius 100, just inside it: each crosses the
|
||||
// arc twice, at under a degree for the nearest one.
|
||||
let arc = PathData::single(quarter());
|
||||
let k = std::f64::consts::FRAC_1_SQRT_2;
|
||||
for gap in [0.5, 0.05, 0.005, 0.0005] {
|
||||
let mid = pt((100.0 - gap) * k, (100.0 - gap) * k);
|
||||
let (from, to) = (mid + Vec2::new(-k, k) * 60.0, mid + Vec2::new(k, -k) * 60.0);
|
||||
let across = PathData::single(line(&[(from.x, from.y), (to.x, to.y)], false));
|
||||
let exact: Vec<Point> = kurbo::PathSeg::Cubic(arc.subpaths[0].segment(0))
|
||||
.intersect_line(kurbo::Line::new(from, to))
|
||||
.iter()
|
||||
.map(|x| from.lerp(to, x.line_t))
|
||||
.collect();
|
||||
assert_eq!(exact.len(), 2, "{gap}");
|
||||
let on = |p: &PathData, other: &PathData| -> Vec<Point> {
|
||||
crossings_on(p, &[other]).unwrap().iter().map(|q| p.subpaths[0].segment(q.1).eval(q.2)).collect()
|
||||
};
|
||||
let both = crossings(&[&arc, &across], false).unwrap();
|
||||
let found = [
|
||||
on(&arc, &across),
|
||||
on(&across, &arc),
|
||||
both[0].iter().map(|q| arc.subpaths[0].segment(q.1).eval(q.2)).collect(),
|
||||
both[1].iter().map(|q| across.subpaths[0].segment(q.1).eval(q.2)).collect(),
|
||||
];
|
||||
for places in found {
|
||||
assert_eq!(places.len(), 2, "{gap}: {places:?}");
|
||||
for x in &exact {
|
||||
let off = places.iter().map(|p| p.distance(*x)).fold(f64::INFINITY, f64::min);
|
||||
assert!(off < 1e-6, "{gap}: {off} pt from {x:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_at_keeps_the_shape_and_names_the_new_anchors() {
|
||||
let sp = line(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0)], false);
|
||||
|
||||
Reference in New Issue
Block a user