diff --git a/crates/engine/src/cmd/shape.rs b/crates/engine/src/cmd/shape.rs index 569f28550..0db43cb0c 100644 --- a/crates/engine/src/cmd/shape.rs +++ b/crates/engine/src/cmd/shape.rs @@ -34,7 +34,7 @@ pub fn specs() -> Vec { "Connect Shapes", [], None, - "{from: shape id, to: shape id, fromSite?, toSite?: site index (default: the closest pair), id?: existing line to glue (else a new connector), preset?: straightConnector1|bentConnector3|curvedConnector3} → {id}", + "{from: shape id, to: shape id, fromSite?, toSite?: site index (default: with neither given, the sites facing each other along the dominant direction between the shapes, a tie to the lower index; with one given, the closest site of the other shape; a box has 0 top, 1 left, 2 bottom, 3 right; shape.sites lists them), id?: existing line to glue (else a new connector), preset?: straightConnector1|bentConnector3|curvedConnector3} → {id}", has_slide, connect ), diff --git a/crates/engine/src/cmd/shape/freeform.rs b/crates/engine/src/cmd/shape/freeform.rs index f6886c75f..75daff898 100644 --- a/crates/engine/src/cmd/shape/freeform.rs +++ b/crates/engine/src/cmd/shape/freeform.rs @@ -1,4 +1,4 @@ -use super::{ColorRef, Fill, Geom, Line, NO_ID_SPACE, Result, Session, Value, bad, bool_or, id_param, json, str_param, usize_param}; +use super::{ColorRef, Fill, Geom, Line, NO_ID_SPACE, Result, Session, Value, bad, bool_or, id_param, json, str_param, usize_param, xfrm_of}; pub(super) fn sites(s: &mut Session, p: &Value) -> Result { let id = id_param(p, "id", "shape.sites")?.ok_or_else(|| bad("shape.sites", "missing `id`"))?; @@ -24,20 +24,50 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result { return Err(bad("shape.connect", "both shapes need connection sites (lines and groups have none)")); } let pick = |v: &[deckcraft_geom::Point], key: &str| usize_param(p, key).filter(|i| *i < v.len()); + // A site left unsaid is the one facing the other shape along the dominant direction between + // the two (a box mostly below is left from the bottom), which is where a person glues an + // arrow; "the closest pair" chose a side site for a diagonal neighbour and crossed boxes. + // The direction runs between the shapes' box centres: the mean of the sites is off-centre + // for a shape whose sites are not symmetric (a triangle's lie mostly along its base). + // In slide space, as the sites are: a shape inside a group is placed where the group draws it. + let centre = |id| { + crate::connect::locate(&st.doc, &st.selection, st.shapes(), id) + .map(|(sh, to_abs)| to_abs * xfrm_of(&st.doc, &st.selection, sh).center()) + .unwrap_or_default() + }; + let (ca, cb) = (centre(from), centre(to)); + // A tie goes to the lowest site index: an exact diagonal leaves a box from its bottom (site 2, + // before the right side, 3) and enters the other at its top (0, before the left side, 1), and + // shapes with one centre, where every direction is as good, both take site 0. + let facing = |v: &[deckcraft_geom::Point], c: deckcraft_geom::Point, dx: f64, dy: f64| { + let len = dx.hypot(dy).max(1e-9); + let cos: Vec = v + .iter() + .map(|q| { + let (sx, sy) = (q.x - c.x, q.y - c.y); + (sx * dx + sy * dy) / (sx.hypot(sy).max(1e-9) * len) + }) + .collect(); + let best = cos.iter().copied().fold(f64::NEG_INFINITY, f64::max); + cos.iter().position(|x| *x >= best - 1e-9).unwrap_or(0) + }; + // With one site given, the other end is the site of the other shape closest to it, as before; + // the facing rule is for the case where neither is given. + let closest = |v: &[deckcraft_geom::Point], to: deckcraft_geom::Point| { + let mut best = (f64::MAX, 0); + for (i, q) in v.iter().enumerate() { + let d = (*q - to).hypot(); + if d < best.0 { + best = (d, i); + } + } + best.1 + }; let (ia, ib) = match (pick(&a, "fromSite"), pick(&b, "toSite")) { (Some(i), Some(j)) => (i, j), - (fi, fj) => { - let mut best = (f64::MAX, 0, 0); - for (i, pa) in a.iter().enumerate().filter(|(i, _)| fi.is_none_or(|f| f == *i)) { - for (j, pb) in b.iter().enumerate().filter(|(j, _)| fj.is_none_or(|f| f == *j)) { - let d = (*pa - *pb).hypot(); - if d < best.0 { - best = (d, i, j); - } - } - } - (best.1, best.2) - } + (Some(i), None) => (i, closest(&b, a[i])), + (None, Some(j)) => (closest(&a, b[j]), j), + (None, None) => (facing(&a, ca, cb.x - ca.x, cb.y - ca.y), facing(&b, cb, ca.x - cb.x, ca.y - cb.y)), }; let (p0, p1) = (a[ia], b[ib]); let id = match id_param(p, "id", "shape.connect")? { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a74284a46..7d0f92259 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7189,6 +7189,50 @@ fn invalid_slide_index_never_falls_back_to_the_selection_in_export_commands() { let _ = std::fs::remove_dir_all(&dir); } +/// G6: with no sites given, shape.connect joins the sites that face each other along the +/// dominant direction between the two shapes (a box mostly below leaves from the bottom), not +/// "the closest pair", which for a diagonal neighbour picked a side site and crossed boxes. +#[test] +fn connector_default_sites_follow_the_dominant_direction() { + let mut s = session(); + let a = s.execute("shape.insert", &json!({"preset": "rect", "rect": [0, 0, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let diag = s.execute("shape.insert", &json!({"preset": "rect", "rect": [150, 200, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(0)), "mostly below: bottom → top, not right → left: {r}"); + let r = s.execute("shape.connect", &json!({"from": diag, "to": a})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(0), Some(2)), "and back: top → bottom: {r}"); + // The direction runs between the shapes' centres, not between the means of their sites: a + // triangle's sites lie mostly along its base, which pulled the direction sideways and chose + // the box's left site for a box that is mostly below. + let tri = s.execute("shape.insert", &json!({"preset": "triangle", "rect": [400, 0, 100, 100]})).unwrap()["id"].as_u64().unwrap(); + let below = s.execute("shape.insert", &json!({"preset": "rect", "rect": [520, 155, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let r = s.execute("shape.connect", &json!({"from": tri, "to": below})).unwrap(); + assert_eq!(r["toSite"].as_u64(), Some(0), "centre to centre is mostly down: the box's top, not its left: {r}"); + // An exact diagonal ties the bottom with the right side (and the top with the left): the lower + // site index wins, so bottom → top, and top → bottom back. + let p = s.execute("shape.insert", &json!({"preset": "rect", "rect": [0, 300, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let q = s.execute("shape.insert", &json!({"preset": "rect", "rect": [200, 500, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let sites = |s: &mut Session, from: u64, to: u64| { + let r = s.execute("shape.connect", &json!({"from": from, "to": to})).unwrap(); + (r["fromSite"].as_u64(), r["toSite"].as_u64()) + }; + assert_eq!(sites(&mut s, p, q), (Some(2), Some(0))); + assert_eq!(sites(&mut s, q, p), (Some(0), Some(2))); + // Shapes with one centre tie every site: site 0 on both. + let big = s.execute("shape.insert", &json!({"preset": "rect", "rect": [600, 300, 200, 100]})).unwrap()["id"].as_u64().unwrap(); + let small = s.execute("shape.insert", &json!({"preset": "rect", "rect": [650, 325, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + assert_eq!(sites(&mut s, big, small), (Some(0), Some(0))); + // One site given: the other end is the other shape's site closest to it, as before the facing + // rule (which is for when neither is given). From a's right side (100, 25) the diagonal box's + // top (200, 200) is nearest; to its left side (150, 225), a's bottom (50, 50). + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "fromSite": 3})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(3), Some(0)), "fromSite only: {r}"); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "toSite": 1})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(1)), "toSite only: {r}"); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "fromSite": 2, "toSite": 3})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(3)), "both given: {r}"); +} + #[test] fn highlight_with_an_invalid_colour_is_rejected_and_keeps_the_highlight() { let mut s = session();