Merge pull request #10304 from dotCooCoo/fix/smooth-bridge-says-why

Smooth Bridge: a refusal says why an object is not a circle
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 14:39:43 -08:00
commit 639ce5004e
4 files changed
+115 -10

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::{Document, Node, NodeId, NodeKind};
use vectorcraft_doc::{Document, LiveShape, 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, 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",
"{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. An object that is not a circle is an error that says why (an ellipse whose width and height differ, a compound path, a hidden or locked object, or another object). 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
)]
@@ -61,17 +61,30 @@ fn bridge(s: &mut Session, p: &Value) -> Result<Value> {
if roots.len() > pt::MAX_BRIDGED_CIRCLES {
return Err(bad(C, "select at most 2000 circles to bridge"));
}
let circles: Vec<(NodeId, pt::Circle)> = {
// The circles, and why the first object that is not one is refused.
let (circles, why): (Vec<(NodeId, pt::Circle)>, Option<&str>) = {
let d = &s.doc()?.doc;
let editable = d.editable();
d.paint_order(roots.iter().copied())
let mut why = None;
let circles = d
.paint_order(roots.iter().copied())
.into_iter()
.filter_map(|id| match &d.node(id)?.kind {
NodeKind::Path { path, guide: false, clipping: false, .. } if editable(id) => Some((id, pt::circle_of(path)?)),
_ => None,
.filter_map(|id| {
let found = match &d.node(id)?.kind {
NodeKind::Path { path, guide: false, clipping: false, .. } if editable(id) => pt::circle_of(path).map(|c| (id, c)),
_ => None,
};
if found.is_none() && why.is_none() {
why = Some(not_a_circle(d, id, editable(id)));
}
found
})
.collect()
.collect();
(circles, why)
};
if let Some(why) = why {
return Err(bad(C, format!("select two or more circles to bridge: {why}")));
}
if circles.len() != roots.len() || circles.len() < 2 {
return Err(bad(C, "select two or more circles to bridge"));
}
@@ -159,6 +172,26 @@ fn members(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
out
}
/// Why object `id`, which is not a circle the command bridges, is refused; `editable` says whether it can be
/// edited (not hidden or locked).
fn not_a_circle(d: &Document, id: NodeId, editable: bool) -> &'static str {
// A pie spanning the whole turn, or none of it, is the whole ellipse.
let whole = |(start, end): (f64, f64)| {
let span = (end - start).rem_euclid(360.0);
span < 1e-9 || 360.0 - span < 1e-9
};
match d.node(id).map(|n| &n.kind) {
_ if !editable => "a selected object is hidden or locked",
// A transform that stretches an ellipse leaves its stored width and height as they were, so one stored with
// equal width and height gets the general message.
Some(NodeKind::Path { live: Some(live), guide: false, clipping: false, .. }) if matches!(**live, LiveShape::Ellipse { w, h, pie, .. } if whole(pie) && w != h) => {
"an ellipse whose width and height differ is not a circle: draw it with Shift held, or give it equal width and height"
}
Some(NodeKind::Compound { .. }) => "a compound path is not a circle: release it first",
_ => "a selected object is not a circle",
}
}
/// 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<()> {
@@ -2,7 +2,7 @@
//! gap, many circles joined to their neighbors within reach, and what it refuses.
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use vectorcraft_doc::{LiveShape, NodeId, NodeKind};
use vectorcraft_geom::kurbo::Shape;
use crate::Session;
@@ -173,6 +173,50 @@ fn far_apart_nested_and_other_objects_are_refused() {
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "nothing changed, no undo step");
}
#[test]
fn a_refusal_says_why_an_object_is_not_a_circle() {
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);
// A hand-drawn ellipse 1 pt taller than wide.
let oval = NodeId(s.execute("shape.ellipse", &json!({"x": 200, "y": 80, "width": 40, "height": 41})).unwrap()["id"].as_u64().unwrap());
let square = NodeId(s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap());
let stretched = circle(&mut s, 300.0, 100.0, 20.0);
s.execute("object.transform", &json!({"ids": [stretched.0], "matrix": [2.0, 0.0, 0.0, 1.0, 0.0, 0.0]})).unwrap();
let steps = s.doc().unwrap().history.undo.len();
let e = err(&mut s, json!({"ids": [a.0, oval.0]}));
assert!(
e.ends_with(
"select two or more circles to bridge: an ellipse whose width and height differ is not a circle: draw it with Shift held, or give it equal width and height"
),
"{e}"
);
let e = err(&mut s, json!({"ids": [a.0, square.0]}));
assert!(e.ends_with("select two or more circles to bridge: a selected object is not a circle"), "{e}");
// A circle stretched by a transform keeps its equal stored width and height, so it is not called unequal.
let equal = match s.doc().unwrap().doc.node(stretched).map(|n| &n.kind) {
Some(NodeKind::Path { live: Some(live), .. }) => matches!(**live, LiveShape::Ellipse { w, h, .. } if w == h),
_ => false,
};
assert!(equal, "the stretched circle keeps a live ellipse of equal width and height");
let e = err(&mut s, json!({"ids": [a.0, stretched.0]}));
assert!(e.ends_with("select two or more circles to bridge: a selected object is not a circle"), "{e}");
let e = err(&mut s, json!({"ids": [a.0]}));
assert!(e.ends_with("select two or more circles to bridge"), "one circle: {e}");
select(&mut s, &[b]);
s.execute("object.hide", &json!({})).unwrap();
let e = err(&mut s, json!({"ids": [a.0, b.0]}));
assert!(e.ends_with("select two or more circles to bridge: a selected object is hidden or locked"), "{e}");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "no undo step for a refusal");
select(&mut s, &[a, b]);
let compound = NodeId(s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap());
let c = circle(&mut s, 300.0, 200.0, 20.0);
let e = err(&mut s, json!({"ids": [compound.0, c.0]}));
assert!(e.ends_with("select two or more circles to bridge: a compound path is not a circle: release it first"), "{e}");
}
/// A 3 × 3 grid of circles of radius 10, 4 apart (centers 24 apart), in rows from (100, 100).
fn grid(s: &mut Session) -> Vec<NodeId> {
(0..9).map(|k| circle(s, 100.0 + 24.0 * (k % 3) as f64, 100.0 + 24.0 * (k / 3) as f64, 10.0)).collect()
@@ -228,4 +228,27 @@ mod tests {
let top: Vec<u64> = app.session.doc().unwrap().doc.layers[0].children().unwrap().iter().map(|n| n.id.0).collect();
assert_eq!(top, [ids[2], ids[3]], "one shape in the topmost joined circle's place, and the far circle");
}
#[test]
fn the_status_line_says_why_an_ellipse_is_refused() {
use crate::tests::panel_harness::Panel;
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
let a = circle(&mut app, 100.0, 10.0);
let oval = app.run("shape.ellipse", json!({"x": 130, "y": 90, "width": 20, "height": 21})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [a, oval]})).unwrap();
let original = geometry(&app);
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
let before = panel.app().session.doc().unwrap().history.undo.len();
menus::invoke(&mut panel.app(), super::COMMAND, Value::Null);
panel.frame();
let why = "an ellipse whose width and height differ is not a circle";
assert!(panel.app().ui.status.contains(why), "the preview's refusal: {}", panel.app().ui.status);
panel.click("OK");
panel.frame();
assert!(panel.app().ui.status.contains(why), "OK's refusal: {}", panel.app().ui.status);
assert_eq!(geometry(&panel.app()), original);
assert_eq!(panel.app().session.doc().unwrap().history.undo.len(), before, "no undo step");
}
}
+6 -1
View File
@@ -16959,7 +16959,12 @@ Commands:
circle of the smaller circle's size about the arc's center. For equal circles that gap apart, that center is where a
third circle the gap from both would sit, as in a triangular grid, so the channels around an empty place in such a
grid are one width; in a square grid they are not. Giving both `radius` and `gap` is an error. Overlapping and
touching circles are joined at every radius. One circle inside the other is an error, and so are circles too far apart
touching circles are joined at every radius. An object that is not a circle is an error that says why: an ellipse whose
width and height differ, a compound path, an object that is hidden or locked, or another object. More than 2,000
objects (with each group other than a clipping group counted as the objects inside it, and a clipping group as one
object, which is not a circle) are refused as more than 2,000 circles before any of them is
checked, so that error does not say which object is not a circle. One circle inside the
other is an error, and so are circles too far apart
for the radius or gap: the arcs would not reach both circles, or would cross before they do. The error suggests a
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