Merge pull request #10309 from dotCooCoo/fix/smooth-bridge-empty-groups

Smooth Bridge: a group the bridged circles leave empty goes
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 13:02:35 -08:00
commit d30f24eb80
3 files changed
+138 -3

No files matched your search

@@ -34,7 +34,7 @@ pub(super) fn specs() -> Vec<CommandSpec> {
"Smooth Bridge",
["Object", "Plug-ins", "Path Precision Tools"],
None,
"{radius?: % 1..1000 (100; the radius of each bridge's two arcs as a percentage of the smaller circle's radius: larger makes a wider, gentler bridge), gap?: pt 0..1000 (instead of radius: the arcs' radius is the smaller circle's radius plus this gap, so each arc stays this gap away from a circle of the smaller circle's size about the arc's center; for equal circles this gap apart that is where a third circle this gap from both sits, as in a triangular grid, so the channels around an empty place in such a grid are one width; giving both is an error), reach?: pt 0..10000 (10; with three or more circles: how far apart two circles' outlines may be for a bridge between them, overlapping ones included; two circles are bridged at any distance), keepOriginals?: bool (false: each outline replaces its circles, painted as the topmost of them; true: it goes directly above that circle and the circles stay), ids?: [id] (default: the selection; a group, other than a clipping group, gives the objects inside it)} join circles (paths whose outline is a circle, not hidden or locked) into metaball (aka meatball) shapes. Two circles become one closed outline: around each circle on its side away from the other, and along two concave arcs, one on each side, that touch both circles so the outline runs on smoothly into them. With three or more, every two circles within reach are bridged the same way, and the bridges that connect are united into one shape each (holes smaller than 1% of the smallest circle's area are filled); a pair within reach but too far apart for the radius gets no bridge, and so does one whose neck would be thinner than the precision its shape is united at, and a circle bridged to no other is left as it is. Overlapping and touching circles are joined too. One circle inside another (among three or more, any such pair within reach) is an error; so are two circles too far apart for the radius (the arcs would not reach both, or would cross), or three or more with no pair the arcs can join, which suggests a larger radius or gap when the largest one would bridge them; so are no two circles within reach, more than 2000 circles and more than 8000 bridges. One undo step; the outlines (and the circles left as they are) are selected → {ids, alone}, with {id} too for two circles",
"{radius?: % 1..1000 (100; the radius of each bridge's two arcs as a percentage of the smaller circle's radius: larger makes a wider, gentler bridge), gap?: pt 0..1000 (instead of radius: the arcs' radius is the smaller circle's radius plus this gap, so each arc stays this gap away from a circle of the smaller circle's size about the arc's center; for equal circles this gap apart that is where a third circle this gap from both sits, as in a triangular grid, so the channels around an empty place in such a grid are one width; giving both is an error), reach?: pt 0..10000 (10; with three or more circles: how far apart two circles' outlines may be for a bridge between them, overlapping ones included; two circles are bridged at any distance), keepOriginals?: bool (false: each outline replaces its circles, painted as the topmost of them, and a group or compound path the other circles leave empty goes; true: it goes directly above that circle and the circles stay), ids?: [id] (default: the selection; a group, other than a clipping group, gives the objects inside it)} join circles (paths whose outline is a circle, not hidden or locked) into metaball (aka meatball) shapes. Two circles become one closed outline: around each circle on its side away from the other, and along two concave arcs, one on each side, that touch both circles so the outline runs on smoothly into them. With three or more, every two circles within reach are bridged the same way, and the bridges that connect are united into one shape each (holes smaller than 1% of the smallest circle's area are filled); a pair within reach but too far apart for the radius gets no bridge, and so does one whose neck would be thinner than the precision its shape is united at, and a circle bridged to no other is left as it is. Overlapping and touching circles are joined too. One circle inside another (among three or more, any such pair within reach) is an error; so are two circles too far apart for the radius (the arcs would not reach both, or would cross), or three or more with no pair the arcs can join, which suggests a larger radius or gap when the largest one would bridge them; so are no two circles within reach, more than 2000 circles and more than 8000 bridges. One undo step; the outlines (and the circles left as they are) are selected → {ids, alone}, with {id} too for two circles",
has_selection,
bridge
)]
@@ -117,7 +117,8 @@ fn bridge(s: &mut Session, p: &Value) -> Result<Value> {
}
beside.finish(d)?;
} else {
// Each shape's topmost circle becomes its outline (a plain path now) and the others go.
// Each shape's topmost circle becomes its outline (a plain path now) and the others go,
// with each group they leave empty.
let mut gone = vec![];
for (members, outline) in shapes {
let Some((&top, rest)) = members.split_last() else { continue };
@@ -128,7 +129,9 @@ fn bridge(s: &mut Session, p: &Value) -> Result<Value> {
made.push(top);
gone.extend_from_slice(rest);
}
let parents: Vec<NodeId> = gone.iter().filter_map(|id| d.parent_of(*id)).collect();
remove_each(d, &gone)?;
remove_emptied(d, parents)?;
}
sel.set(made.iter().chain(&alone).copied());
Ok(made)
@@ -156,6 +159,24 @@ fn members(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
out
}
/// Remove each of `parents` that is left with nothing in it, and then each group above it left with
/// nothing in it in turn. Layers stay.
fn remove_emptied(d: &mut Document, parents: Vec<NodeId>) -> Result<()> {
for parent in parents {
let mut at = Some(parent);
while let Some(id) = at {
match d.node(id) {
Some(n) if !n.is_layer() && n.children().is_some_and(|c| c.is_empty()) => {
at = d.parent_of(id);
d.remove(id)?;
}
_ => break,
}
}
}
Ok(())
}
/// The ids of `circles` at indices `at`, in paint order (the circles are listed in paint order).
fn roots_of(circles: &[(NodeId, pt::Circle)], at: &[usize]) -> Vec<NodeId> {
at.iter().filter_map(|&i| circles.get(i)).map(|(id, _)| *id).collect()
@@ -329,6 +329,118 @@ fn an_id_given_twice_is_one_circle() {
assert_eq!(top_level(&s), [b]);
}
/// The objects directly inside `id`.
fn inside(s: &Session, id: NodeId) -> Vec<NodeId> {
s.doc().unwrap().doc.node(id).unwrap().children().unwrap().iter().map(|n| n.id).collect()
}
fn make(s: &mut Session, command: &str, ids: &[NodeId]) -> NodeId {
select(s, ids);
NodeId(s.execute(command, &json!({})).unwrap()["id"].as_u64().unwrap())
}
#[test]
fn a_group_the_bridged_circles_leave_empty_goes_in_the_same_undo_step() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 160.0, 100.0, 20.0);
let ga = make(&mut s, "object.group", &[a]);
let gb = make(&mut s, "object.group", &[b]);
let steps = s.doc().unwrap().history.undo.len();
let r = s.execute(BRIDGE, &json!({"ids": [a.0, b.0]})).unwrap();
assert_eq!(r["id"], json!(b.0));
assert_eq!(top_level(&s), [gb], "the lower circle's group, left empty, goes");
assert_eq!(inside(&s, gb), [b], "the outline stays in the upper circle's group");
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "one undo step");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(top_level(&s), [ga, gb]);
assert_eq!((inside(&s, ga), inside(&s, gb)), (vec![a], vec![b]));
// Kept circles leave their groups as they are.
s.execute(BRIDGE, &json!({"ids": [a.0, b.0], "keepOriginals": true})).unwrap();
assert_eq!(top_level(&s), [ga, gb]);
assert_eq!(inside(&s, ga), [a]);
}
#[test]
fn groups_above_an_emptied_group_go_in_turn_and_a_group_with_other_objects_stays() {
let mut s = session();
// a in a group that is all of another group; c in a group with a square; b on the layer.
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 150.0, 100.0, 20.0);
let c = circle(&mut s, 200.0, 100.0, 20.0);
let square = NodeId(s.execute("shape.rectangle", &json!({"x": 300, "y": 200, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap());
let inner = make(&mut s, "object.group", &[a]);
let outer = make(&mut s, "object.group", &[inner]);
let gc = make(&mut s, "object.group", &[square, c]);
let layer = s.doc().unwrap().doc.layers[0].id;
let r = s.execute(BRIDGE, &json!({"ids": [a.0, b.0, c.0]})).unwrap();
assert_eq!(ids(&r["ids"]), [c], "one shape, in the topmost circle's place");
assert_eq!(top_level(&s), [gc], "both groups around a go, b goes from the layer");
assert_eq!(inside(&s, gc), [c, square], "the group with the square stays");
let d = &s.doc().unwrap().doc;
assert!(d.node(inner).is_none() && d.node(outer).is_none());
assert!(d.node(layer).is_some(), "the layer stays");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(inside(&s, outer), [inner]);
assert_eq!(inside(&s, inner), [a]);
}
#[test]
fn a_compound_path_the_bridged_circles_leave_empty_goes() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 160.0, 100.0, 20.0);
let compound = make(&mut s, "object.compoundPath.make", &[a]);
assert_eq!(inside(&s, compound), [a]);
s.execute(BRIDGE, &json!({"ids": [a.0, b.0]})).unwrap();
assert_eq!(top_level(&s), [b]);
assert!(s.doc().unwrap().doc.node(compound).is_none());
}
#[test]
fn a_selected_group_of_grouped_circles_keeps_only_the_outlines_group() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 160.0, 100.0, 20.0);
let ga = make(&mut s, "object.group", &[a]);
let gb = make(&mut s, "object.group", &[b]);
let outer = make(&mut s, "object.group", &[ga, gb]);
select(&mut s, &[outer]);
s.execute(BRIDGE, &json!({})).unwrap();
assert_eq!(top_level(&s), [outer]);
assert_eq!(inside(&s, outer), [gb], "the lower circle's group goes, the outline's stays");
assert_eq!(inside(&s, gb), [b]);
}
#[test]
fn layers_and_sublayers_the_bridged_circles_leave_empty_stay() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let upper = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap());
let sub = NodeId(s.execute("layer.newSublayer", &json!({"parent": upper.0})).unwrap()["id"].as_u64().unwrap());
s.execute("layer.setCurrent", &json!({"id": sub.0})).unwrap();
let b = circle(&mut s, 160.0, 100.0, 20.0);
let c = circle(&mut s, 220.0, 100.0, 20.0);
// a below in the first layer, b and c in the sublayer: c's shape takes a's and b's places.
s.execute(BRIDGE, &json!({"ids": [a.0, b.0, c.0], "reach": 30})).unwrap();
let d = &s.doc().unwrap().doc;
assert!(d.layers.iter().any(|l| l.id != upper && l.children().is_some_and(|c| c.is_empty())), "the emptied layer stays");
assert_eq!(inside(&s, sub), [c]);
// The sublayer left with nothing in it stays too.
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let upper = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap());
let sub = NodeId(s.execute("layer.newSublayer", &json!({"parent": upper.0})).unwrap()["id"].as_u64().unwrap());
s.execute("layer.setCurrent", &json!({"id": sub.0})).unwrap();
let b = circle(&mut s, 160.0, 100.0, 20.0);
s.execute("layer.setCurrent", &json!({"id": upper.0})).unwrap();
let c = circle(&mut s, 220.0, 100.0, 20.0);
s.execute(BRIDGE, &json!({"ids": [a.0, b.0, c.0], "reach": 30})).unwrap();
assert_eq!(inside(&s, sub), Vec::<NodeId>::new(), "the emptied sublayer stays");
assert_eq!(inside(&s, upper), [sub, c]);
}
#[test]
fn small_circles_at_the_default_reach_bridge_the_pairs_the_arcs_can_join() {
let mut s = session();
+3 -1
View File
@@ -16870,7 +16870,9 @@ Commands:
larger radius or gap only when the largest (1,000% or 1,000 pt) would bridge the circles. The outline replaces the two
circles, painted as the upper one (a plain path), unless `keepOriginals` is true: then it goes directly above the
upper circle, painted as it, and the circles stay. The outline is selected. Two circles are bridged however far apart
their outlines are; `reach` does not apply to them.
their outlines are; `reach` does not apply to them. When outlines replace circles (with any number of circles), a group
or compound path that the circles leave with nothing in it is removed, and so is each group above it left empty in
turn; layers stay.
With three or more circles, every two whose outlines are at most `reach` apart (0 to 10,000 pt, 10 by default;
overlapping ones included) are bridged the same way, and the bridges that connect are united into one shape each,
which can have holes where bridges enclose space; holes smaller than 1% of the smallest circle's area are filled. A