Merge pull request #10298 from dotCooCoo/fix/smooth-bridge-circles

Smooth Bridge: a selected group of circles is bridged
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 11:00:51 -08:00
commit d794ccda0d
4 files changed
+132 -9

No files matched your search

@@ -3,7 +3,7 @@
//! the bridges connect, replacing the circles or added above them.
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use vectorcraft_doc::{Document, Node, NodeId, NodeKind};
use vectorcraft_geom::PathData;
use vectorcraft_pathops::addons::pathtools as pt;
@@ -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)} 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; 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
)]
@@ -54,8 +54,8 @@ fn bridge(s: &mut Session, p: &Value) -> Result<Value> {
None => pt::ArcRadius::Share(percent.unwrap_or(RADIUS) / 100.0),
};
let keep = switch_or(p, "keepOriginals", false, C)?;
let mut roots = checked_ids_or_selection(s, p, C)?;
// An id given twice is one circle.
let mut roots = members(&s.doc()?.doc, &checked_ids_or_selection(s, p, C)?);
// An id given twice, or a circle given with its group, is one circle.
roots.sort_unstable();
roots.dedup();
if roots.len() > pt::MAX_BRIDGED_CIRCLES {
@@ -140,6 +140,22 @@ fn bridge(s: &mut Session, p: &Value) -> Result<Value> {
Ok(out)
}
/// The objects `ids` names, with each group among them (a clipping group excepted) as the objects
/// inside it, at any depth.
fn members(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
fn visit(n: &Node, out: &mut Vec<NodeId>) {
match &n.kind {
NodeKind::Group { children, clip: false } => children.iter().for_each(|c| visit(c, out)),
_ => out.push(n.id),
}
}
let mut out = vec![];
for n in ids.iter().filter_map(|&id| d.node(id)) {
visit(n, &mut out);
}
out
}
/// 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()
@@ -354,3 +354,60 @@ fn locked_and_hidden_circles_are_not_bridged() {
s.execute("object.hide", &json!({})).unwrap();
assert!(err(&mut s, json!({"ids": [a.0, b.0]})).contains("select two or more circles to bridge"));
}
fn group(s: &mut Session, ids: &[NodeId]) -> NodeId {
NodeId(s.execute("object.group", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap()["id"].as_u64().unwrap())
}
fn children(s: &Session, id: NodeId) -> Vec<NodeId> {
s.doc().unwrap().doc.node(id).unwrap().children().unwrap().iter().map(|n| n.id).collect()
}
#[test]
fn a_selected_group_of_circles_bridges_the_circles_inside_it() {
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 g = group(&mut s, &[a, b]);
select(&mut s, &[g]);
let steps = s.doc().unwrap().history.undo.len();
let r = s.execute(BRIDGE, &json!({})).unwrap();
assert_eq!(r["id"], json!(b.0), "two circles, joined in the upper one's place");
assert_eq!(top_level(&s), [g]);
assert_eq!(children(&s, g), [b], "the outline stays in the group");
assert!(area(&s, b) > 2.0 * std::f64::consts::PI * 400.0);
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "one undo step");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(children(&s, g), [a, b]);
// A group inside a group, given with one of its own circles, and a loose circle: three circles, so the reach
// applies.
let c = circle(&mut s, 300.0, 100.0, 20.0);
let outer = group(&mut s, &[g]);
let r = s.execute(BRIDGE, &json!({"ids": [outer.0, a.0, c.0], "reach": 30})).unwrap();
assert_eq!(r["ids"], json!([b.0]), "the grouped circles are joined");
assert_eq!(r["alone"], json!([c.0]), "the far circle is out of reach");
}
#[test]
fn a_selected_group_with_other_objects_or_a_clipping_group_is_refused() {
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 square = NodeId(s.execute("shape.rectangle", &json!({"x": 200, "y": 200, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap());
let g = group(&mut s, &[a, b, square]);
let steps = s.doc().unwrap().history.undo.len();
assert!(err(&mut s, json!({"ids": [g.0]})).contains("select two or more circles to bridge"));
assert_eq!(children(&s, g), [a, b, square]);
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "nothing changed, no 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 mask = circle(&mut s, 130.0, 100.0, 80.0);
select(&mut s, &[a, b, mask]);
let clip = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap());
let before = top_level(&s);
assert!(err(&mut s, json!({"ids": [clip.0]})).contains("select two or more circles to bridge"));
assert_eq!(top_level(&s), before);
}
+50 -2
View File
@@ -40,8 +40,8 @@ fn params(fields: &DialogFields) -> Value {
/// Opens Smooth Bridge's dialog on the command's defaults.
pub(crate) fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
// The reach applies to three or more circles; two are bridged at any distance. Counted once, before a preview
// replaces the selection with its result.
let many = app.session.doc().map(|doc| doc.selection.objects.len() > 2).unwrap_or(true);
// replaces the selection with its result, with a selected group counting the objects inside it.
let many = app.session.doc().map(|doc| members(&doc.doc, &doc.selection.objects) > 2).unwrap_or(true);
let fields = DialogFields::from_pairs([
("radius", FieldValue::Number(RADIUS)),
("constantGap", FieldValue::Bool(false)),
@@ -72,6 +72,24 @@ pub(crate) fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
})
}
/// How many objects the command bridges for objects `ids`: each one, or for a group (a clipping group excepted) the
/// objects inside it, at any depth; an object given twice, or with its group, counts once.
fn members(doc: &vectorcraft_doc::Document, ids: &[vectorcraft_doc::NodeId]) -> usize {
fn visit(n: &vectorcraft_doc::Node, out: &mut std::collections::BTreeSet<vectorcraft_doc::NodeId>) {
match &n.kind {
vectorcraft_doc::NodeKind::Group { children, clip: false } => children.iter().for_each(|c| visit(c, out)),
_ => {
out.insert(n.id);
}
}
}
let mut out = std::collections::BTreeSet::new();
for n in ids.iter().filter_map(|&id| doc.node(id)) {
visit(n, &mut out);
}
out.len()
}
#[cfg(test)]
mod tests {
use serde_json::{Value, json};
@@ -158,6 +176,36 @@ mod tests {
assert_eq!(geometry(&panel.app()), original);
}
#[test]
fn reach_counts_the_circles_inside_a_selected_group() {
use crate::tests::panel_harness::Panel;
for (xs, many) in [(&[100.0, 124.0][..], false), (&[100.0, 124.0, 148.0][..], true)] {
let mut app = bridge_app(xs);
let ids: Vec<u64> = app.session.doc().unwrap().selection.objects.iter().map(|id| id.0).collect();
let group = app.run("object.group", json!({ "ids": ids })).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [group]})).unwrap();
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
menus::invoke(&mut panel.app(), super::COMMAND, Value::Null);
panel.frame();
assert_eq!(panel.enabled("Reach"), many, "{} circles in the group", xs.len());
panel.click("OK");
panel.frame();
let inside = panel.app().session.doc().unwrap().doc.node(vectorcraft_doc::NodeId(group)).unwrap().children().unwrap().len();
assert_eq!(inside, 1, "the circles in the group became one shape");
}
// A group of two circles selected together with one of them: still two circles.
let mut app = bridge_app(&[100.0, 124.0]);
let ids: Vec<u64> = app.session.doc().unwrap().selection.objects.iter().map(|id| id.0).collect();
let group = app.run("object.group", json!({ "ids": ids })).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [group, ids[0]]})).unwrap();
assert_eq!(app.session.doc().unwrap().selection.objects.len(), 2);
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
menus::invoke(&mut panel.app(), super::COMMAND, Value::Null);
panel.frame();
assert!(!panel.enabled("Reach"), "a circle selected with its group counts once");
}
#[test]
fn constant_gap_shows_the_gap_and_bridges_many_circles_within_reach() {
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
+5 -3
View File
@@ -16854,8 +16854,9 @@ Commands:
drawn, and the path's fills stay on it, below its dashes (a path without fills goes). A compound path's dashes are
plain paths with its appearance. Paths without a dashed stroke stay as they are, and it is an error if none has one.
The dashes are selected; more than 100,000 (or a pattern too fine to draw) is refused. → `{ids, dashes}`.
- `addon.pathtools.bridge {radius?, gap?, reach?, keepOriginals?, ids?}` joins circles (`ids`, default: the selection;
paths whose outline is a circle, not hidden or locked) into metaball (aka meatball) shapes, as one undo step (Object ›
- `addon.pathtools.bridge {radius?, gap?, reach?, keepOriginals?, ids?}` joins circles (`ids`, default: the selection,
with a group other than a clipping group giving the objects inside it at any depth, which must all be circles; paths
whose outline is a circle, not hidden or locked) into metaball (aka meatball) shapes, as one undo step (Object ›
Plug-ins › Path Precision Tools › Smooth Bridge…). Two circles become one closed outline that goes around each circle
on its side away from the other and along two concave arcs, one on each side of the line through the centers, each
touching both circles from outside, so every anchor is smooth. `radius` is the arcs' radius as a percentage of the
@@ -16881,7 +16882,8 @@ Commands:
only when the largest would bridge a pair of them under these rules), when no two circles are within reach, and above 2,000 circles or 8,000 bridges. → `{ids, alone}`:
the shapes and the circles left as they are, with `id` as well for two circles. Smooth Bridge… opens a dialog
(`ui.menuDialog {command}`, kind `smoothBridge`) with Constant Gap (a Gap field in place of the Radius field in
percent), Reach (unavailable with two circles selected), Keep Originals and Preview.
percent), Reach (unavailable when the selection, a selected group's objects counted, holds two objects), Keep Originals
and Preview.
- `addon.pathtools.tangentCircle {ids?, radius?, circle?}` draws the circles that touch two paths or compound paths
and have `radius` (0.01 to 1,000,000 pt, needed with two), or that touch three (which set the radius themselves, so
`radius` is then an error), as one undo step (Object › Plug-ins › Path Precision Tools › Tangent Circle…). `ids`