Files
vectorcraft/crates/engine/src/cmd/addons/pathtools/tangents/tests.rs
T
dotCooCoo 5d494db4d9 Tests: Connect tangents' external shape does not cross itself
The test checked only which corners the shape has, so corners joined in
the wrong order passed. It now also checks the shape's area.
2026-12-01 14:16:38 -08:00

467 lines
24 KiB
Rust

//! Common Tangents: the lines for circles, turned ellipses and convex paths, the centerline,
//! the paint, one undo step, the cases with no tangent, and refused selections and params.
use serde_json::{Value, json};
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{AppearanceItem, NodeId, NodeKind};
use vectorcraft_geom::{Point, Vec2};
use crate::Session;
const C: &str = super::COMMON_TANGENTS;
fn session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 600, "height": 400})).unwrap();
s
}
fn id_of(v: &Value) -> NodeId {
NodeId(v["id"].as_u64().unwrap())
}
fn circle(s: &mut Session, cx: f64, cy: f64, r: f64) -> NodeId {
id_of(&s.execute("shape.ellipse", &json!({"x": cx - r, "y": cy - r, "width": 2.0 * r, "height": 2.0 * r})).unwrap())
}
fn select(s: &mut Session, ids: &[NodeId]) {
s.execute("select.set", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
}
fn steps(s: &Session) -> usize {
s.doc().unwrap().history.undo.len()
}
/// The ends of each new line, as `ids` lists them.
fn lines(s: &Session, r: &Value) -> Vec<(Point, Point)> {
let d = &s.doc().unwrap().doc;
r["ids"]
.as_array()
.unwrap()
.iter()
.map(|v| {
let n = d.node(NodeId(v.as_u64().unwrap())).unwrap();
let NodeKind::Path { path, .. } = &n.kind else { panic!("not a path") };
assert_eq!(path.subpaths.len(), 1);
let sp = &path.subpaths[0];
assert!(!sp.closed);
assert_eq!(sp.anchors.len(), 2);
(sp.anchors[0].p, sp.anchors[1].p)
})
.collect()
}
fn near(p: Point, q: Point) -> bool {
p.distance(q) < 1e-6
}
fn has_line(found: &[(Point, Point)], p: Point, q: Point) -> bool {
found.iter().any(|(a, b)| (near(*a, p) && near(*b, q)) || (near(*a, q) && near(*b, p)))
}
#[test]
fn two_circles_get_four_tangents_at_the_exact_points_as_one_undo_step() {
let mut s = session();
let a = circle(&mut s, 100.0, 200.0, 20.0);
let b = circle(&mut s, 300.0, 200.0, 40.0);
select(&mut s, &[a, b]);
let before = steps(&s);
let r = s.execute(C, &json!({})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64(), r["centerline"].is_null()), (Some(2), Some(2), true), "{r}");
assert_eq!(steps(&s), before + 1);
let found = lines(&s, &r);
assert_eq!(found.len(), 4);
let (c1, c2) = (Point::new(100.0, 200.0), Point::new(300.0, 200.0));
// External: (c1 - c2) · n = r2 - r1, n.x = -0.1; internal: (c1 - c2) · n = -(r1 + r2), n.x = 0.3.
for (nx, external) in [(-0.1f64, true), (0.3, false)] {
for sy in [1.0, -1.0] {
let n = Vec2::new(nx, sy * (1.0 - nx * nx).sqrt());
let far = if external { c2 + n * 40.0 } else { c2 - n * 40.0 };
assert!(has_line(&found, c1 + n * 20.0, far), "{n:?} in {found:?}");
}
}
// The lines are the selection, above the two circles.
let st = s.doc().unwrap();
assert_eq!(st.selection.objects.len(), 4);
let ids: Vec<NodeId> = r["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
let order = st.doc.paint_order([a, b].into_iter().chain(ids.iter().copied()));
assert_eq!(&order[..2], &[a, b]);
s.execute("edit.undo", &json!({})).unwrap();
assert!(ids.iter().all(|id| s.doc().unwrap().doc.node(*id).is_none()), "one undo step removes them all");
}
#[test]
fn turned_ellipses_are_measured_exactly() {
let mut s = session();
let a = id_of(&s.execute("shape.ellipse", &json!({"x": 60, "y": 170, "width": 80, "height": 30})).unwrap());
select(&mut s, &[a]);
s.execute("object.rotate", &json!({"angle": 30, "origin": [100, 185]})).unwrap();
let b = id_of(&s.execute("shape.ellipse", &json!({"x": 330, "y": 120, "width": 50, "height": 120})).unwrap());
select(&mut s, &[b]);
s.execute("object.rotate", &json!({"angle": -70, "origin": [355, 180]})).unwrap();
select(&mut s, &[a, b]);
let r = s.execute(C, &json!({"tangents": "all"})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(2)), "{r}");
// Each end lies on its ellipse, where the ellipse runs along the line: turned back about the
// center, (x/rx)² + (y/ry)² = 1 and the gradient is square to the line.
let on = |p: Point, center: Point, rx: f64, ry: f64, deg: f64, dir: Vec2| {
let turn = vectorcraft_geom::kurbo::Affine::rotate(-deg.to_radians());
// `object.rotate` turns counter-clockwise on the page (y down): undo it in page terms.
let page = |v: Vec2| Vec2::new(v.x, -v.y);
let q = page((turn * page(p - center).to_point()).to_vec2());
let d = page((turn * page(dir).to_point()).to_vec2());
let value = (q.x / rx).powi(2) + (q.y / ry).powi(2);
let grad = Vec2::new(q.x / (rx * rx), q.y / (ry * ry));
(value - 1.0).abs() < 1e-9 && (grad.dot(d) / (grad.hypot() * d.hypot())).abs() < 1e-7
};
for (p, q) in lines(&s, &r) {
let dir = q - p;
assert!(on(p, Point::new(100.0, 185.0), 40.0, 15.0, 30.0, dir), "{p:?} on the first ellipse");
assert!(on(q, Point::new(355.0, 180.0), 25.0, 60.0, -70.0, dir), "{q:?} on the second ellipse");
}
}
#[test]
fn a_convex_bezier_path_and_the_centerline() {
let mut s = session();
let anchors = json!([
{"x": 0, "y": 40, "in": [0, 70], "out": [0, 10]},
{"x": 50, "y": 0, "in": [20, 0], "out": [90, 0]},
{"x": 110, "y": 50, "in": [110, 20], "out": [110, 90]},
{"x": 40, "y": 100, "in": [80, 100], "out": [10, 100]}
]);
let blob = id_of(&s.execute("path.create", &json!({"anchors": anchors, "closed": true})).unwrap());
let round = circle(&mut s, 300.0, 200.0, 30.0);
let r = s.execute(C, &json!({"ids": [blob.0, round.0], "centerline": true})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(2)), "{r}");
let found = lines(&s, &r);
assert_eq!(found.len(), 5, "four tangents and the centerline");
// The centerline is the last line and its id is given: from the blob's center of area to the
// circle's center.
assert_eq!(r["centerline"], r["ids"][4]);
let (from, to) = found[4];
assert!(near(to, Point::new(300.0, 200.0)), "{to:?}");
assert!(from.x > 30.0 && from.x < 80.0 && from.y > 30.0 && from.y < 70.0, "{from:?}");
// A tangent touches without crossing: every point of both paths is on one side of it.
let d = &s.doc().unwrap().doc;
let points = |id: NodeId| {
let sp = d.node(id).unwrap().path_data().unwrap().subpaths[0].clone();
let segments: Vec<_> = (0..sp.segment_count()).map(|k| sp.segment(k)).collect();
segments.iter().flat_map(|seg| (0..200).map(|j| vectorcraft_geom::kurbo::ParamCurve::eval(seg, j as f64 / 200.0))).collect::<Vec<_>>()
};
let (pa, pb) = (points(blob), points(round));
for (p, q) in &found[..4] {
let n = Vec2::new(-(q.y - p.y), q.x - p.x).normalize();
for pts in [&pa, &pb] {
let side: Vec<f64> = pts.iter().map(|x| (*x - *p).dot(n)).collect();
let (lo, hi) = side.iter().fold((f64::MAX, f64::MIN), |(lo, hi), v| (lo.min(*v), hi.max(*v)));
// The circle is measured as an exact circle, which its four curves follow to within
// about 0.03% of the radius.
assert!(lo > -1e-2 || hi < 1e-2, "crosses: {lo} .. {hi}");
}
}
}
#[test]
fn tiny_paths_close_together_still_get_the_centerline() {
// Two circles a ten-billionth of a point wide, half a billionth apart: separate, with all four
// tangents, and centers apart by far more than their sizes' rounding.
let mut s = session();
let a = circle(&mut s, 1.0, 1.0, 1e-10);
let b = circle(&mut s, 1.0 + 5e-10, 1.0, 1e-10);
let r = s.execute(C, &json!({"ids": [a.0, b.0], "centerline": true})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(2)), "{r}");
assert_eq!(r["ids"].as_array().map(Vec::len), Some(5), "{r}");
assert_eq!(r["centerline"], r["ids"][4]);
}
#[test]
fn the_lines_take_the_current_stroke_and_no_fill() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 300.0, 100.0, 20.0);
s.execute("select.none", &json!({})).unwrap();
s.execute("paint.setStroke", &json!({"none": true})).unwrap();
let r = s.execute(C, &json!({"ids": [a.0, b.0], "tangents": "external"})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(0)), "{r}");
let d = &s.doc().unwrap().doc;
for v in r["ids"].as_array().unwrap() {
let n = d.node(NodeId(v.as_u64().unwrap())).unwrap();
assert!(n.appearance.fill().is_none_or(|f| f.paint.is_none()), "no fill");
let st = n.appearance.stroke().expect("stroked");
assert!(!st.paint.is_none(), "black when the stroke is None");
}
// A new-art appearance with two fills and a stroke: the lines keep the stroke and neither fill.
let mut look = vectorcraft_doc::Appearance::basic(Paint::solid(Color::BLACK), Paint::solid(Color::BLACK), 2.0);
let fill = look.items.iter().find(|i| matches!(i, AppearanceItem::Fill(_))).cloned().unwrap();
look.items.push(fill);
s.paint.appearance = Some(look);
s.paint.inherited = false;
let r2 = s.execute(C, &json!({"ids": [a.0, b.0], "tangents": "external"})).unwrap();
let d2 = &s.doc().unwrap().doc;
for v in r2["ids"].as_array().unwrap() {
let n = d2.node(NodeId(v.as_u64().unwrap())).unwrap();
assert!(!n.appearance.items.iter().any(|i| matches!(i, AppearanceItem::Fill(_))), "no fill at all");
assert!(n.appearance.stroke().is_some());
}
// Two equal circles: the external tangents run along their tops and bottoms.
assert!(has_line(&lines(&s, &r), Point::new(100.0, 80.0), Point::new(300.0, 80.0)));
assert!(has_line(&lines(&s, &r), Point::new(100.0, 120.0), Point::new(300.0, 120.0)));
}
#[test]
fn nested_and_overlapping_paths_change_nothing_and_say_why() {
let mut s = session();
let big = circle(&mut s, 200.0, 200.0, 100.0);
let small = circle(&mut s, 220.0, 200.0, 20.0);
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let r = s.execute(C, &json!({"ids": [big.0, small.0]})).unwrap();
assert_eq!(r["ids"], json!([]));
assert!(r["warning"].as_str().unwrap().contains("inside the other"), "{r}");
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before, "nothing changes");
// Overlapping circles have external tangents only.
let a = circle(&mut s, 100.0, 100.0, 30.0);
let b = circle(&mut s, 140.0, 100.0, 30.0);
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let r = s.execute(C, &json!({"ids": [a.0, b.0], "tangents": "internal"})).unwrap();
assert!(r["warning"].as_str().unwrap().contains("overlap"), "{r}");
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before);
let r = s.execute(C, &json!({"ids": [a.0, b.0]})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(0)), "{r}");
// Circles that touch share the tangent where they touch.
let c = circle(&mut s, 100.0, 300.0, 10.0);
let e = circle(&mut s, 130.0, 300.0, 20.0);
let r = s.execute(C, &json!({"ids": [c.0, e.0], "tangents": "internal"})).unwrap();
assert_eq!(r["internal"].as_u64(), Some(1), "{r}");
let (p, q) = lines(&s, &r)[0];
assert!((p.x - 110.0).abs() < 1e-5 && (q.x - 110.0).abs() < 1e-5 && (p.distance(q) - 20.0).abs() < 1e-5, "{p:?} {q:?}");
// A circle touching the inside of a larger one: no internal tangent, and the message says the
// one lies inside the other rather than that they overlap.
let outer = circle(&mut s, 400.0, 200.0, 50.0);
let inner = circle(&mut s, 430.0, 200.0, 20.0);
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let r = s.execute(C, &json!({"ids": [outer.0, inner.0], "tangents": "internal"})).unwrap();
assert_eq!(r["ids"], json!([]));
assert!(r["warning"].as_str().unwrap().contains("inside the other, so they have no internal"), "{r}");
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before);
let r = s.execute(C, &json!({"ids": [outer.0, inner.0]})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(1), Some(0)), "{r}");
}
fn point(s: &mut Session, x: f64, y: f64) -> NodeId {
id_of(&s.execute("path.create", &json!({"anchors": [{"x": x, "y": y}]})).unwrap())
}
#[test]
fn a_point_and_an_ellipse_get_the_two_lines_from_the_point_and_the_centerline() {
let mut s = session();
// The point is drawn after the oval, so it is the upper object; the lines still start at it.
let oval = id_of(&s.execute("shape.ellipse", &json!({"x": 260, "y": 170, "width": 80, "height": 60})).unwrap());
let dot = point(&mut s, 100.0, 200.0);
select(&mut s, &[oval, dot]);
let before = steps(&s);
let r = s.execute(C, &json!({"centerline": true})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(0)), "{r}");
assert_eq!(steps(&s), before + 1);
let found = lines(&s, &r);
assert_eq!(found.len(), 3, "two tangents and the centerline");
let (p, c) = (Point::new(100.0, 200.0), Point::new(300.0, 200.0));
// From (-200, 0) to an ellipse with semi-axes 40 and 30 about the origin: the touching points
// are x = -a²/200 = -8, y = ±b √(1 - x²/a²).
let y = 30.0 * (1.0f64 - (8.0f64 / 40.0).powi(2)).sqrt();
for sy in [1.0, -1.0] {
let touch = c + Vec2::new(-8.0, sy * y);
assert!(found[..2].iter().any(|(a, b)| near(*a, p) && near(*b, touch)), "{touch:?} in {found:?}");
}
assert_eq!(r["centerline"], r["ids"][2]);
assert!(near(found[2].0, p) && near(found[2].1, c), "{:?}", found[2]);
// Every `tangents` value gives the same two lines.
for (kind, external, internal) in [("external", 2, 0), ("internal", 0, 2)] {
let r = s.execute(C, &json!({"ids": [oval.0, dot.0], "tangents": kind})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(external), Some(internal)), "{kind}: {r}");
assert_eq!(lines(&s, &r).len(), 2, "{kind}");
}
}
#[test]
fn a_point_below_the_path_still_starts_the_lines() {
let mut s = session();
// The point is drawn first, so it is the lower object and needs no swap.
let dot = point(&mut s, 100.0, 200.0);
let round = circle(&mut s, 300.0, 200.0, 50.0);
let r = s.execute(C, &json!({"ids": [round.0, dot.0], "centerline": true})).unwrap();
assert_eq!((r["external"].as_u64(), r["internal"].as_u64()), (Some(2), Some(0)), "{r}");
let found = lines(&s, &r);
assert_eq!(found.len(), 3, "two tangents and the centerline");
let p = Point::new(100.0, 200.0);
assert!(found.iter().all(|(a, _)| near(*a, p)), "every line starts at the point: {found:?}");
// From 200 away to a circle of radius 50, each tangent is √(200² - 50²) long.
let length = (200.0f64 * 200.0 - 50.0 * 50.0).sqrt();
assert!(found[..2].iter().all(|(a, b)| (a.distance(*b) - length).abs() < 1e-6), "{found:?}");
assert!(near(found[2].1, Point::new(300.0, 200.0)), "{:?}", found[2]);
}
#[test]
fn a_point_on_or_inside_a_path_and_two_points() {
let mut s = session();
let round = circle(&mut s, 200.0, 200.0, 50.0);
// On the circle: one line, centered on the point and as long as the circle is wide.
let on = point(&mut s, 250.0, 200.0);
let r = s.execute(C, &json!({"ids": [round.0, on.0]})).unwrap();
assert_eq!(r["external"].as_u64(), Some(1), "{r}");
let (p, q) = lines(&s, &r)[0];
assert!((p.x - 250.0).abs() < 1e-5 && (q.x - 250.0).abs() < 1e-5 && (p.distance(q) - 100.0).abs() < 1e-5, "{p:?} {q:?}");
// Inside: nothing changes, and the warning says why.
let inside = point(&mut s, 210.0, 190.0);
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let r = s.execute(C, &json!({"ids": [round.0, inside.0], "centerline": true})).unwrap();
assert_eq!(r["ids"], json!([]));
assert!(r["warning"].as_str().unwrap().contains("point lies inside the path"), "{r}");
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before, "nothing changes");
// Two points, or a point and an open path, are refused.
let err = |s: &mut Session, p: Value| s.execute(C, &p).unwrap_err().to_string();
assert!(err(&mut s, json!({"ids": [on.0, inside.0]})).contains("select two closed paths"));
let open = id_of(&s.execute("path.create", &json!({"anchors": [{"x": 0, "y": 0}, {"x": 50, "y": 0}, {"x": 50, "y": 50}]})).unwrap());
assert!(err(&mut s, json!({"ids": [open.0, on.0]})).contains("open"));
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), (before.0 + 1, before.1 + 1), "only the open path was added");
}
#[test]
fn bad_selections_and_params_are_refused() {
let mut s = session();
let a = circle(&mut s, 100.0, 100.0, 20.0);
let b = circle(&mut s, 300.0, 100.0, 20.0);
let star = id_of(&s.execute("shape.star", &json!({"cx": 200, "cy": 300, "radius1": 40, "radius2": 15, "points": 5})).unwrap());
let open = id_of(&s.execute("path.create", &json!({"anchors": [{"x": 0, "y": 0}, {"x": 50, "y": 0}, {"x": 50, "y": 50}]})).unwrap());
let group_of = |s: &mut Session, ids: &[NodeId]| {
select(s, ids);
id_of(&s.execute("object.group", &json!({})).unwrap())
};
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let err = |s: &mut Session, p: Value| s.execute(C, &p).unwrap_err().to_string();
assert!(err(&mut s, json!({"ids": [a.0, b.0], "tangents": "both"})).contains("tangents"));
assert!(err(&mut s, json!({"ids": [a.0, b.0], "tangents": 3})).contains("tangents"));
assert!(err(&mut s, json!({"ids": [a.0, b.0], "centerline": "yes"})).contains("centerline"));
assert!(err(&mut s, json!({"ids": [a.0, b.0], "which": "all"})).contains("which"), "unknown keys are refused");
assert!(err(&mut s, json!({"ids": [a.0]})).contains("select two closed paths"));
assert!(err(&mut s, json!({"ids": [a.0, b.0, star.0]})).contains("select two closed paths"));
assert!(err(&mut s, json!({"ids": [a.0, star.0]})).contains("not convex"));
assert!(err(&mut s, json!({"ids": [a.0, open.0]})).contains("open"));
assert!(s.execute(C, &json!({"ids": [a.0, a.0]})).is_err(), "the same path twice");
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before, "nothing changes");
let copy = id_of(&s.execute("shape.ellipse", &json!({"x": 80, "y": 80, "width": 40, "height": 40})).unwrap());
assert!(err(&mut s, json!({"ids": [a.0, copy.0]})).contains("same outline"));
let g = group_of(&mut s, &[b]);
assert!(err(&mut s, json!({"ids": [a.0, g.0]})).contains("select two closed paths"));
}
/// The one closed outline of path `id`.
fn outline(s: &Session, id: NodeId) -> vectorcraft_geom::SubPath {
let n = s.doc().unwrap().doc.node(id).unwrap();
let NodeKind::Path { path, .. } = &n.kind else { panic!("not a path") };
let [sp] = path.subpaths.as_slice() else { panic!("{} subpaths", path.subpaths.len()) };
assert!(sp.closed);
sp.clone()
}
/// The anchors of path `id`'s one closed outline.
fn corners(s: &Session, id: NodeId) -> Vec<Point> {
outline(s, id).anchors.iter().map(|a| a.p).collect()
}
#[test]
fn connect_joins_the_external_tangents_and_the_chords_into_one_closed_shape_in_one_step() {
let mut s = session();
let (a, b) = (circle(&mut s, 100.0, 100.0, 20.0), circle(&mut s, 200.0, 100.0, 20.0));
let (before, count) = (steps(&s), s.doc().unwrap().doc.node_count());
let r = s.execute(C, &json!({"tangents": "external", "connect": true, "ids": [a.0, b.0]})).unwrap();
assert_eq!(steps(&s), before + 1, "one undo step");
let ids = r["ids"].as_array().unwrap();
assert_eq!(ids.len(), 1, "one shape in place of two lines");
assert_eq!(s.doc().unwrap().doc.node_count(), count + 1, "the circles stay");
let found = corners(&s, NodeId(ids[0].as_u64().unwrap()));
assert_eq!(found.len(), 4);
// The tangents above and below both circles, joined by each circle's vertical diameter.
for p in [Point::new(100.0, 80.0), Point::new(200.0, 80.0), Point::new(200.0, 120.0), Point::new(100.0, 120.0)] {
assert!(found.iter().any(|&q| near(p, q)), "{p:?} in {found:?}");
}
// In order around the band, not crossing itself: the area is the 100 by 40 rectangle's.
let area = found.iter().zip(found.iter().cycle().skip(1)).map(|(p, q)| p.x * q.y - q.x * p.y).sum::<f64>() / 2.0;
assert!((area.abs() - 4000.0).abs() < 1e-6, "area {area} of {found:?}");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().doc.node_count(), count);
}
#[test]
fn connect_with_all_makes_one_shape_of_each_kind_and_a_point_makes_a_triangle() {
let mut s = session();
let (ca, cb) = ((Point::new(100.0, 100.0), 20.0), (Point::new(220.0, 140.0), 40.0));
let (a, b) = (circle(&mut s, ca.0.x, ca.0.y, ca.1), circle(&mut s, cb.0.x, cb.0.y, cb.1));
let r = s.execute(C, &json!({"connect": true, "centerline": true, "ids": [a.0, b.0]})).unwrap();
let ids: Vec<NodeId> = r["ids"].as_array().unwrap().iter().map(|v| NodeId(v.as_u64().unwrap())).collect();
assert_eq!(ids.len(), 3, "an external shape, an internal shape and the centerline");
for &id in &ids[..2] {
let found = corners(&s, id);
assert_eq!(found.len(), 4);
let on = |(c, r): (Point, f64)| found.iter().filter(|p| ((**p - c).hypot() - r).abs() < 1e-6).count();
assert_eq!((on(ca), on(cb)), (2, 2), "two corners where the tangents touch each circle: {found:?}");
}
assert_ne!(corners(&s, ids[0]), corners(&s, ids[1]));
assert_eq!(r["centerline"], json!(ids[2].0));
let mut s = session();
let (p, c) = (point(&mut s, 0.0, 100.0), circle(&mut s, 100.0, 100.0, 20.0));
let r = s.execute(C, &json!({"connect": true, "ids": [p.0, c.0]})).unwrap();
let ids = r["ids"].as_array().unwrap();
assert_eq!(ids.len(), 1);
let found = corners(&s, NodeId(ids[0].as_u64().unwrap()));
assert_eq!(found.len(), 3, "the point and where its two lines touch the circle: {found:?}");
assert!(found.iter().any(|&q| near(q, Point::new(0.0, 100.0))));
}
#[test]
fn curved_makes_the_bridge_the_smooth_bridge_command_makes() {
let build = || {
let mut s = session();
let ids = [circle(&mut s, 100.0, 100.0, 20.0), circle(&mut s, 150.0, 100.0, 20.0), circle(&mut s, 125.0, 140.0, 20.0)];
select(&mut s, &ids);
s
};
for (merged, old) in [
(json!({"curved": true}), json!({})),
(
json!({"tangents": "external", "connect": true, "curved": true, "gap": 5, "reach": 30, "keepOriginals": true}),
json!({"gap": 5, "reach": 30, "keepOriginals": true}),
),
] {
let (mut s, mut t) = (build(), build());
let r = s.execute(C, &merged).unwrap();
let q = t.execute("addon.pathtools.bridge", &old).unwrap();
assert_eq!(r, q, "{merged}");
let kinds = |s: &Session| s.doc().unwrap().doc.layers[0].children().unwrap().iter().map(|n| n.kind.clone()).collect::<Vec<_>>();
assert_eq!(kinds(&s), kinds(&t), "{merged}");
}
}
#[test]
fn options_of_the_other_mode_are_refused() {
let mut s = session();
let (a, b) = (circle(&mut s, 100.0, 100.0, 20.0), circle(&mut s, 200.0, 100.0, 20.0));
select(&mut s, &[a, b]);
let before = (steps(&s), s.doc().unwrap().doc.node_count());
let err = |s: &mut Session, p: Value| s.execute(C, &p).unwrap_err().to_string();
for (p, says) in [
(json!({"connect": "yes"}), "connect"),
(json!({"curved": true, "tangents": "all"}), "external"),
(json!({"curved": true, "centerline": true}), "centerline"),
(json!({"curved": true, "connect": false}), "connected"),
(json!({"radius": 50}), "curved"),
(json!({"connect": true, "keepOriginals": true}), "curved"),
(json!({"curved": true, "radius": 50, "gap": 2}), "not both"),
(json!({"curved": true, "radius": 0}), "radius"),
] {
assert!(err(&mut s, p.clone()).contains(says), "{p}: {}", err(&mut s, p.clone()));
}
assert_eq!((steps(&s), s.doc().unwrap().doc.node_count()), before, "nothing changes");
}