mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-16 07:15:38 +00:00
Merge pull request #2241 from TrevorBrown-dev/fix/mirror-sharp-point-2110
M4: Vector edit: a mirror mode turns a sharp point smooth (#2110)
This commit is contained in:
@@ -124,9 +124,62 @@ pub fn move_handles(net: &mut VectorNetwork, hs: &[(usize, bool)], d: Vec2, inde
|
||||
}
|
||||
}
|
||||
|
||||
/// Each handle of a sharp point turned smooth by a mirror mode is this fraction of the length of its
|
||||
/// own segment. Measured in the incumbent (0.3905 and 0.390 at two angles): known to about 3 digits only.
|
||||
const MIRROR_SMOOTH_FACTOR: f64 = 0.3905;
|
||||
|
||||
/// Give a sharp point (two segments, no handles) the handles a mirror mode makes in the incumbent.
|
||||
/// Both lie on one line through the point, along `(B - A) / |BA|^2 + (C - B) / |BC|^2` (A, C: the far
|
||||
/// ends of the two segments). "Angle": each is [`MIRROR_SMOOTH_FACTOR`] x its own segment.
|
||||
/// "Angle and length": both are that factor x the shorter segment (medium confidence: the
|
||||
/// measurement could not separate this from other rules). Open-path ends, points with a handle
|
||||
/// already and coincident neighbours are left alone. Returns whether handles were made.
|
||||
fn smooth_sharp_point(net: &mut VectorNetwork, vi: usize, mode: HandleMirroring) -> bool {
|
||||
if mode == HandleMirroring::None {
|
||||
return false;
|
||||
}
|
||||
let inc = net.incident(vi);
|
||||
let [(s1, a1), (s2, a2)] = inc.as_slice() else { return false };
|
||||
let (Some(h1), Some(h2)) = (net.handle(*s1, *a1), net.handle(*s2, *a2)) else { return false };
|
||||
if tvec(h1).hypot() > 1e-9 || tvec(h2).hypot() > 1e-9 {
|
||||
return false;
|
||||
}
|
||||
let far = |s: usize, st: bool| net.segments.get(s).and_then(|x| net.vertices.get(if st { x.b } else { x.a })).map(|v| v.point());
|
||||
let (Some(b), Some(a), Some(c)) = (net.vertices.get(vi).map(|v| v.point()), far(*s1, *a1), far(*s2, *a2)) else { return false };
|
||||
let (ab, bc) = (b - a, c - b);
|
||||
let (la, lc) = (ab.hypot(), bc.hypot());
|
||||
if !(la > 1e-9 && lc > 1e-9 && la.is_finite() && lc.is_finite()) {
|
||||
return false;
|
||||
}
|
||||
let d = ab / (la * la) + bc / (lc * lc);
|
||||
if !finite(d) || d.hypot() < 1e-12 {
|
||||
return false;
|
||||
}
|
||||
let d = d.normalize();
|
||||
let (l_in, l_out) = if mode == HandleMirroring::AngleAndLength {
|
||||
let l = MIRROR_SMOOTH_FACTOR * la.min(lc);
|
||||
(l, l)
|
||||
} else {
|
||||
(MIRROR_SMOOTH_FACTOR * la, MIRROR_SMOOTH_FACTOR * lc)
|
||||
};
|
||||
let (in_h, out_h) = (-d * l_in, d * l_out);
|
||||
if !(finite(in_h) && finite(out_h)) {
|
||||
return false;
|
||||
}
|
||||
net.set_handle(*s1, *a1, (in_h.x, in_h.y));
|
||||
net.set_handle(*s2, *a2, (out_h.x, out_h.y));
|
||||
true
|
||||
}
|
||||
|
||||
/// Set the mirroring of vertices and make their handles agree with it.
|
||||
pub fn set_mirroring(net: &mut VectorNetwork, verts: &[usize], mode: HandleMirroring) {
|
||||
for &vi in verts {
|
||||
if smooth_sharp_point(net, vi, mode) {
|
||||
if let Some(v) = net.vertices.get_mut(vi) {
|
||||
v.mirror = Some(mode);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let inc = net.incident(vi);
|
||||
if let [(s1, a1), (s2, a2)] = inc.as_slice()
|
||||
&& let (Some(h1), Some(h2)) = (net.handle(*s1, *a1), net.handle(*s2, *a2))
|
||||
@@ -949,6 +1002,72 @@ mod tests {
|
||||
assert!((a + b).hypot() < 1e-9, "made symmetric: {a:?} {b:?}");
|
||||
}
|
||||
|
||||
/// An open path A, B, C (the measured triangle of #2110) with B sharp.
|
||||
fn triangle_path() -> VectorNetwork {
|
||||
VectorNetwork::from_points(&[Point::new(-250.0, -174.0), Point::new(117.0, -174.0), Point::new(-67.0, 194.0)], false)
|
||||
}
|
||||
|
||||
fn handles_at_b(n: &VectorNetwork) -> (Vec2, Vec2) {
|
||||
(tvec(n.segments[0].tb), tvec(n.segments[1].ta))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_modes_smooth_a_sharp_point_as_measured() {
|
||||
// Mirror angle and length: both handles 0.3905 x the shorter segment (367), opposite.
|
||||
let mut n = triangle_path();
|
||||
set_mirroring(&mut n, &[1], HandleMirroring::AngleAndLength);
|
||||
let (i, o) = handles_at_b(&n);
|
||||
assert!((o - Vec2::new(86.24, 114.47)).hypot() < 0.1, "{o:?}");
|
||||
assert!((i + o).hypot() < 1e-9, "{i:?} {o:?}");
|
||||
assert!((o.hypot() - 143.3).abs() < 0.1, "{}", o.hypot());
|
||||
// Mirror angle: each handle 0.3905 x its own segment; still collinear.
|
||||
let mut n = triangle_path();
|
||||
set_mirroring(&mut n, &[1], HandleMirroring::Angle);
|
||||
let (i, o) = handles_at_b(&n);
|
||||
assert!((i - Vec2::new(-86.24, -114.47)).hypot() < 0.1, "{i:?}");
|
||||
assert!((o - Vec2::new(96.69, 128.33)).hypot() < 0.1, "{o:?}");
|
||||
assert!((i.x * o.y - i.y * o.x).abs() < 1e-6, "collinear");
|
||||
assert!((i.hypot() - 143.3).abs() < 0.1 && (o.hypot() - 160.7).abs() < 0.1);
|
||||
assert_eq!(n.mirroring(1), HandleMirroring::Angle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_modes_smooth_other_angles_and_several_points() {
|
||||
// A right angle at B with legs 100 and 50: d = (1,0)/100 + (0,1)/50, so the line leans to +y.
|
||||
let mut n = VectorNetwork::from_points(&[Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 50.0)], false);
|
||||
set_mirroring(&mut n, &[1], HandleMirroring::Angle);
|
||||
let (i, o) = handles_at_b(&n);
|
||||
let want = Vec2::new(0.01, 0.02).normalize();
|
||||
assert!((o.normalize() - want).hypot() < 1e-9, "{o:?}");
|
||||
assert!((o.hypot() - 19.525).abs() < 1e-9 && (i.hypot() - 39.05).abs() < 1e-9, "{i:?} {o:?}");
|
||||
// Two sharp points of a closed square, both selected: both become smooth.
|
||||
let mut sq = square();
|
||||
set_mirroring(&mut sq, &[0, 2], HandleMirroring::AngleAndLength);
|
||||
for v in [0, 2] {
|
||||
assert!(sq.incident(v).iter().all(|(s, st)| sq.handle(*s, *st).is_some_and(|h| tvec(h).hypot() > 1.0)), "vertex {v}");
|
||||
assert_eq!(sq.mirroring(v), HandleMirroring::AngleAndLength);
|
||||
}
|
||||
assert!(sq.segments.iter().all(|s| tvec(s.ta).x.is_finite() && tvec(s.tb).y.is_finite()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_modes_leave_unmeasured_points_alone() {
|
||||
// Open-path ends have only one segment.
|
||||
let mut n = open_line();
|
||||
set_mirroring(&mut n, &[0, 2], HandleMirroring::Angle);
|
||||
assert!(n.segments.iter().all(|s| s.is_straight()));
|
||||
// A zero-length neighbour: stays sharp, finite.
|
||||
let mut z = VectorNetwork::from_points(&[Point::new(5.0, 5.0), Point::new(5.0, 5.0), Point::new(60.0, 0.0)], false);
|
||||
set_mirroring(&mut z, &[1], HandleMirroring::AngleAndLength);
|
||||
assert!(z.segments.iter().all(|s| s.is_straight()));
|
||||
// One handle already: the old behaviour (the lone handle stays, the other mirrors it).
|
||||
let mut h = open_line();
|
||||
h.segments[1].ta = (10.0, 5.0);
|
||||
set_mirroring(&mut h, &[1], HandleMirroring::Angle);
|
||||
assert_eq!(h.segments[1].ta, (10.0, 5.0));
|
||||
assert_eq!(h.segments[0].tb, (-10.0, -5.0));
|
||||
}
|
||||
|
||||
fn loop_is_continuous(n: &VectorNetwork, lp: &[usize]) -> bool {
|
||||
let ends: Vec<(usize, usize)> = n
|
||||
.loop_walk(lp)
|
||||
|
||||
@@ -407,6 +407,24 @@ fn mirroring_position_and_join_commands() {
|
||||
assert!(s.execute("vector.join", &json!({"ids": [id]})).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_mode_on_sharp_points_is_one_undo_step() {
|
||||
// #2110: a mirror mode turns sharp points smooth; the whole command undoes in one step.
|
||||
let mut s = Session::new();
|
||||
let r = ex(&mut s, "vector.create", json!({"points": [[-250, -174], [117, -174], [-67, 194]], "closed": false}));
|
||||
let id = crate::params::id_of(&r["id"]).unwrap();
|
||||
ex(&mut s, "vector.edit.begin", json!({"id": id}));
|
||||
ex(&mut s, "vector.edit.select", json!({"vertices": [1]}));
|
||||
ex(&mut s, "vector.edit.mirroring", json!({"mode": "angleAndLength"}));
|
||||
let n = net(&s, id);
|
||||
let (i, o) = (n.segments[0].tb, n.segments[1].ta);
|
||||
assert!((o.0 - 86.24).abs() < 0.1 && (o.1 - 114.47).abs() < 0.1, "{o:?}");
|
||||
assert!((i.0 + o.0).abs() < 1e-9 && (i.1 + o.1).abs() < 1e-9, "{i:?}");
|
||||
s.undo().unwrap();
|
||||
let n = net(&s, id);
|
||||
assert!(n.segments.iter().all(|x| x.is_straight()), "one undo restores the sharp point");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_point_dropped_on_another_merges_into_it() {
|
||||
// #401: an open path's end dragged onto its start closes the path at that point.
|
||||
|
||||
@@ -215,9 +215,12 @@ fn corner_radius_mirroring_and_delete(s: &mut Session, a: NodeId, b: NodeId, atl
|
||||
|
||||
fn bend_select_all_pen_and_clicks(s: &mut Session, a: NodeId, b: NodeId) {
|
||||
// The Bend tool (held with Cmd/Ctrl) bends the segment under the pointer, in its own layer.
|
||||
// (The earlier mirroring step already turned the selected corners smooth, #2110.)
|
||||
let a_before = net(s, a).segments;
|
||||
let b_before = net(s, b).segments;
|
||||
drag(s, (250.0, 100.0), (250.0, 130.0), CMD);
|
||||
assert!(net(s, b).segments.iter().any(|sg| !sg.is_straight()), "the bend lands on the layer under the pointer");
|
||||
assert!(net(s, a).segments.iter().all(|sg| sg.is_straight()), "other layers keep their segments");
|
||||
assert!(net(s, b).segments != b_before, "the bend lands on the layer under the pointer");
|
||||
assert!(net(s, a).segments == a_before, "other layers keep their segments");
|
||||
// Select all takes every edited layer's points.
|
||||
let r = ex(s, "vector.edit.selectAll", json!({}));
|
||||
assert_eq!(r["count"], json!(8), "silent: select all takes the points of every edited layer (pinned)");
|
||||
|
||||
+3
-2
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user