mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Merge pull request #10270 from dotCooCoo/fix/curvature-stretch-narrows
Fix #6192: Curvature, a point dragged far out narrows toward it
This commit is contained in:
2 files changed
+138
-2
No files matched your search
@@ -136,7 +136,9 @@ fn neighbours(sp: &SubPath, i: usize) -> [Option<usize>; 2] {
|
||||
/// Move anchor `i` of `sp` to `q` as the Curvature tool drags a point: only the two segments at it
|
||||
/// change. A smooth anchor takes the Curvature tool's own handles ([`smooth_handles`]: along the
|
||||
/// circle through it and its neighbours; at an end of an open path the one [`end_handle`] into the
|
||||
/// curve). The neighbours' handles on those segments, and a corner's own
|
||||
/// curve), each no longer than [`fit_to_tangent`] allows against the neighbor's handle on its
|
||||
/// segment, so a point dragged far out narrows toward it instead of swinging wide around that
|
||||
/// circle (#6192). The neighbors' handles on those segments, and a corner's own
|
||||
/// handles, keep their directions, their lengths scaling with their segment's chord, so the
|
||||
/// segments beyond don't move and a corner without handles keeps none.
|
||||
///
|
||||
@@ -163,7 +165,7 @@ pub fn curvature_move(sp: &mut SubPath, i: usize, q: Point) -> bool {
|
||||
match ends {
|
||||
[Some(prev), Some(next)] => {
|
||||
if let Some((h_in, h_out)) = smooth_handles(prev.p, q, next.p) {
|
||||
(m.h_in, m.h_out) = (h_in, h_out);
|
||||
(m.h_in, m.h_out) = (fit_to_tangent(q, h_in, prev.p, prev.h_out), fit_to_tangent(q, h_out, next.p, next.h_in));
|
||||
}
|
||||
}
|
||||
// An end of an open path has one segment: its handle only, and a corner like the
|
||||
@@ -179,6 +181,48 @@ pub fn curvature_move(sp: &mut SubPath, i: usize, q: Point) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// cos 75°: [`fit_to_tangent`] shortens a handle in full up to a turn of 150°, and less toward a
|
||||
/// half turn.
|
||||
const FADE_HALF_TURN: f64 = 0.258_819_045_102_520_8;
|
||||
|
||||
/// Handle `h` of the point `q` on its segment to `b`, whose handle there is `hb`, shortened toward
|
||||
/// the handle of the circular arc tangent to both handle lines: `2/3 · (1 − tan²(θ/4))` of `q`'s
|
||||
/// distance `r` to where the lines meet, `θ` the turn between them. A longer handle swings the
|
||||
/// segment out past `b`'s tangent line. The shortening applies in full while the lines meet at
|
||||
/// least half the chord ahead of `b` and the turn is at most 150°, and fades to none as the meeting
|
||||
/// point comes back to `b` or the turn nears a half turn, so the handle changes continuously as `q`
|
||||
/// moves. Where `b` is on the circle [`smooth_handles`]
|
||||
/// follows, the arc is the circle's and `h` is unchanged; `h` is also unchanged when `b` has no
|
||||
/// handle there or the lines are parallel.
|
||||
fn fit_to_tangent(q: Point, h: Point, b: Point, hb: Point) -> Point {
|
||||
let (d, t) = (h - q, hb - b);
|
||||
let (ld, lt) = (d.hypot(), t.hypot());
|
||||
let w = b - q;
|
||||
let chord = w.hypot();
|
||||
if !(ld > 1e-9 && lt > 1e-9 && chord > 1e-9) {
|
||||
return h;
|
||||
}
|
||||
let (d, t) = (d / ld, t / lt);
|
||||
let den = d.cross(t);
|
||||
if den.abs() <= 1e-12 {
|
||||
return h;
|
||||
}
|
||||
// q + r·d = b + s·t.
|
||||
let (r, s) = (w.cross(t) / den, w.cross(d) / den);
|
||||
// The curve leaves `b` along `t` and comes into `q` along −d.
|
||||
let cos_half = ((1.0 - t.dot(d)) / 2.0).clamp(0.0, 1.0).sqrt();
|
||||
// A circular arc under a half turn meets its tangents at least half its chord out. Toward a
|
||||
// half turn the lines meet ever further away, on one side or the other, and the shortening
|
||||
// fades out.
|
||||
let weight = (2.0 * s / chord).clamp(0.0, 1.0) * (cos_half / FADE_HALF_TURN).min(1.0);
|
||||
let tan2 = (1.0 - cos_half) / (1.0 + cos_half);
|
||||
let cap = 2.0 / 3.0 * (1.0 - tan2) * r.abs();
|
||||
if !(weight > 0.0 && cap.is_finite() && ld > cap) {
|
||||
return h;
|
||||
}
|
||||
q + d * (ld + (cap - ld) * weight)
|
||||
}
|
||||
|
||||
/// Add a smooth anchor at `t` on segment `seg` of `sp` without changing its shape: the new anchor's
|
||||
/// index. On a straight segment its handles lie along it, each a third of its own part of the
|
||||
/// segment ([`curvature_smooth`]: a line is a circle's limit), so a drag bends the line evenly; a drag then moves it as a
|
||||
|
||||
@@ -139,6 +139,98 @@ fn moving_a_corner_without_handles_keeps_its_sides_straight() {
|
||||
assert_eq!(sp.anchors[0], Anchor::corner(pt(80.0, 70.0)));
|
||||
}
|
||||
|
||||
/// A circle's top point dragged far up stretches it into a shape that narrows from its sides
|
||||
/// toward the point (#6192), instead of swinging out wider than the circle first. The moved point
|
||||
/// stays smooth, and its segments meet the neighbors' unchanged tangents.
|
||||
#[test]
|
||||
fn stretching_a_circle_narrows_toward_the_dragged_point() {
|
||||
let k = 100.0 * 0.552_284_749_830_793_4;
|
||||
let circle = SubPath::new(
|
||||
vec![
|
||||
Anchor::with_handles(pt(0.0, -100.0), pt(-k, -100.0), pt(k, -100.0)),
|
||||
Anchor::with_handles(pt(100.0, 0.0), pt(100.0, -k), pt(100.0, k)),
|
||||
Anchor::with_handles(pt(0.0, 100.0), pt(k, 100.0), pt(-k, 100.0)),
|
||||
Anchor::with_handles(pt(-100.0, 0.0), pt(-100.0, k), pt(-100.0, -k)),
|
||||
],
|
||||
true,
|
||||
);
|
||||
// On the circle, the neighbors' tangents match it, and the point keeps the circle's handles.
|
||||
let mut near = circle.clone();
|
||||
assert!(curvature_move(&mut near, 0, pt(0.0, -100.0)));
|
||||
for (a, b) in near.anchors.iter().zip(&circle.anchors) {
|
||||
assert!(a.h_in.distance(b.h_in) < 1e-6 && a.h_out.distance(b.h_out) < 1e-6, "{a:?} != {b:?}");
|
||||
}
|
||||
for top in [-150.0, -250.0, -400.0, -800.0] {
|
||||
let mut sp = circle.clone();
|
||||
assert!(curvature_move(&mut sp, 0, pt(0.0, top)));
|
||||
let a = sp.anchors[0];
|
||||
assert_eq!(a.kind, AnchorKind::Smooth);
|
||||
assert!(collinear(a.h_in - a.p, a.h_out - a.p) && (a.h_in - a.p).dot(a.h_out - a.p) < 0.0);
|
||||
// Segment 0 runs from the top to the right point, segment 3 from the left point to the top.
|
||||
for (seg, side) in [(0, 1.0), (3, -1.0)] {
|
||||
let c = sp.segment(seg);
|
||||
let mut last: Option<f64> = None;
|
||||
for j in 0..=200 {
|
||||
let t = j as f64 / 200.0;
|
||||
let p = c.eval(if side > 0.0 { t } else { 1.0 - t });
|
||||
assert!(p.x.abs() <= 100.0 + 1e-9, "top {top}: segment {seg} swings out to x = {}", p.x);
|
||||
// From the top down to the side the width only grows.
|
||||
if let Some(l) = last {
|
||||
assert!(p.x.abs() >= l - 1e-9, "top {top}: segment {seg} narrows again at t = {t}");
|
||||
}
|
||||
last = Some(p.x.abs());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The moved point's handles change continuously while it crosses a neighbor's tangent line, while
|
||||
/// the line its own handle runs along passes through the neighbor, and while the two lines turn
|
||||
/// parallel.
|
||||
#[test]
|
||||
fn a_moved_point_s_handles_change_continuously() {
|
||||
let handles = |sp: &SubPath, q: Point| -> (Vec2, Vec2) {
|
||||
let mut sp = sp.clone();
|
||||
assert!(curvature_move(&mut sp, 1, q));
|
||||
(sp.anchors[1].h_in - q, sp.anchors[1].h_out - q)
|
||||
};
|
||||
let near = |a: (Vec2, Vec2), b: (Vec2, Vec2)| (a.0 - b.0).hypot() < 0.05 && (a.1 - b.1).hypot() < 0.05;
|
||||
// The neighbor's handle points away from the moved point: its line passes through it at y = 0.
|
||||
let away = SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(pt(-100.0, 0.0)),
|
||||
Anchor::with_handles(pt(0.0, -20.0), pt(-30.0, -20.0), pt(30.0, -20.0)),
|
||||
Anchor::with_handles(pt(100.0, 0.0), pt(200.0, 10.0), pt(0.0, -10.0)),
|
||||
],
|
||||
false,
|
||||
);
|
||||
assert!(near(handles(&away, pt(0.0, -0.001)), handles(&away, pt(0.0, 0.001))));
|
||||
// The neighbor's handle runs the same way as the moved point's: their lines are parallel at x = 0.
|
||||
let parallel = SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(pt(-100.0, 0.0)),
|
||||
Anchor::with_handles(pt(0.0, -50.0), pt(-30.0, -50.0), pt(30.0, -50.0)),
|
||||
Anchor::with_handles(pt(100.0, 0.0), pt(150.0, 0.0), pt(50.0, 0.0)),
|
||||
],
|
||||
false,
|
||||
);
|
||||
assert!(near(handles(¶llel, pt(-0.001, -50.0)), handles(¶llel, pt(0.001, -50.0))));
|
||||
// A circle's top dragged across the right point's tangent line, x = 100.
|
||||
let k = 100.0 * 0.552_284_749_830_793_4;
|
||||
let circle = SubPath::new(
|
||||
vec![
|
||||
Anchor::with_handles(pt(-100.0, 0.0), pt(-100.0, k), pt(-100.0, -k)),
|
||||
Anchor::with_handles(pt(0.0, -100.0), pt(-k, -100.0), pt(k, -100.0)),
|
||||
Anchor::with_handles(pt(100.0, 0.0), pt(100.0, -k), pt(100.0, k)),
|
||||
Anchor::with_handles(pt(0.0, 100.0), pt(k, 100.0), pt(-k, 100.0)),
|
||||
],
|
||||
true,
|
||||
);
|
||||
for y in [-150.0, -300.0] {
|
||||
assert!(near(handles(&circle, pt(99.999, y)), handles(&circle, pt(100.001, y))), "y {y}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn going_on_from_an_end_keeps_the_old_segments() {
|
||||
let before = wave();
|
||||
|
||||
Reference in new issue
Block a user