mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 17:15:56 +00:00
Path Precision Tools: the Tangent Circle Tool draws a circle on two clicked segments
The Tangent Circle Tool (tangentCircle) joins the add-on's tools, in Object > Plug-ins > Path Precision Tools and the All Tools drawer. A click on a path segment picks it; a click on a second segment draws the circle that is tangent to the first segment at the point clicked and touches the second. The segments beside the second one count with it, and of the circles found the one touching them nearest the pointer is drawn. - Shift on the second click uses the tool's radius (36 pt to start, set in its options), anywhere along the first segment. - A drag from the second segment sets the radius to the drag's length; Shift steps it by 1, 2 or 5 times a power of 10 pt, at least 4 screen pixels, and Cmd/Ctrl pressed during the drag moves it by a tenth. - Before the click the circle shows with its center, its radii to the touch points and its radius, or a translated note says there is none. The preview follows a pan, a zoom or an edit under a still pointer. - The circle is drawn with shape.circleThroughPoints, so Align New Objects to Pixel Grid leaves it tangent; one undo step. A circle that its three points cannot give back in floating point is not drawn, and the first segment stays picked. Escape, an edit outside the tool or another document forgets the first segment. The geometry is circles_through and circles_of_radius in the pathops crate: the distance to the second segments is sampled along the first segment's normal (or along the segment, for a radius) and the places where it equals the radius are taken, including touches between samples. Part of #7178. Docs: docs/mcp.md, docs/ui-parity.md, docs/target-app-parity.md, ROADMAP.
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
//! where paths cross ([`crossings`]), the stretches between them ([`spans`]), anchors slid along
|
||||
//! their path ([`slide`]), touching edges merged ([`shared_edge`]), paths divided into dashes
|
||||
//! ([`divide_dashes`]), the common tangents of two convex outlines ([`common_tangents`]), the
|
||||
//! Parametric Shape Tool's shapes ([`shapes`]) and the circles that touch two or three shapes
|
||||
//! ([`tangent_circles`]).
|
||||
//! Parametric Shape Tool's shapes ([`shapes`]), the circles that touch two or three shapes
|
||||
//! ([`tangent_circles`]) and the Tangent Circle tool's circles on two segments
|
||||
//! ([`circles_through`], [`circles_of_radius`]).
|
||||
|
||||
mod crossings;
|
||||
mod dashes;
|
||||
@@ -32,7 +33,10 @@ pub use shared_edge::{
|
||||
COINCIDENCE, EndSide, Follow, MAX_COINCIDENCE, MergePlan, MergePreview, Stretch, apply_merge, ends_at, merge_preview, stretches_at,
|
||||
};
|
||||
pub use slide::{MAX_SLIDE, slide_anchor, slide_stops, slide_t_at};
|
||||
pub use tangent_circle::{Circle, MAX_TANGENT_CIRCLE_SEGMENTS, MAX_TANGENT_CIRCLES, TangentCircleError, tangent_circle_segments, tangent_circles};
|
||||
pub use tangent_circle::{
|
||||
Circle, MAX_TANGENT_CIRCLE_SEGMENTS, MAX_TANGENT_CIRCLES, SegmentCircle, TangentCircleError, circles_of_radius, circles_through,
|
||||
tangent_circle_segments, tangent_circles,
|
||||
};
|
||||
pub use tangents::{Coincide, MAX_TANGENT_SEGMENTS, Outline, OutlineError, Tangent, TangentKind, common_tangents};
|
||||
pub use trim::{Span, slice, span_at, spans, without_spans};
|
||||
|
||||
|
||||
@@ -14,9 +14,12 @@
|
||||
use kurbo::{CubicBez, ParamCurve, Point, Vec2};
|
||||
use vectorcraft_geom::PathData;
|
||||
|
||||
mod on_segments;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use on_segments::{SegmentCircle, circles_of_radius, circles_through};
|
||||
|
||||
/// Most segments the shapes may hold together, counted by [`tangent_circle_segments`].
|
||||
pub const MAX_TANGENT_CIRCLE_SEGMENTS: usize = 2_000;
|
||||
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
//! The circles the Tangent Circle tool draws on two picked segments: the circle tangent to the
|
||||
//! first segment at a point that also touches the second ([`circles_through`]), or the circles of a
|
||||
//! radius tangent to the first segment that also touch the second ([`circles_of_radius`]). "The
|
||||
//! second" is a list of segments (the picked one and those next to it). A circle touches them when
|
||||
//! the nearest of their points to its center is on the circle, so they lie outside it, and it never
|
||||
//! crosses the first segment.
|
||||
//!
|
||||
//! The roots are found by sampling a distance function along one parameter: samples where it is 0,
|
||||
//! bisection where it changes sign, and a golden-section search where it comes down to 0 between
|
||||
//! samples without changing sign.
|
||||
|
||||
use kurbo::{CubicBez, ParamCurve, ParamCurveDeriv, ParamCurveNearest, Point, Vec2};
|
||||
|
||||
use super::Circle;
|
||||
|
||||
/// A circle on two picked segments: the circle, where it touches the first segment and where it
|
||||
/// touches the second ones.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct SegmentCircle {
|
||||
pub circle: Circle,
|
||||
pub first: Point,
|
||||
pub second: Point,
|
||||
}
|
||||
|
||||
/// Samples along the parameter the roots are looked for on.
|
||||
const SAMPLES: usize = 512;
|
||||
/// Bisection steps per root.
|
||||
const BISECTIONS: usize = 64;
|
||||
/// The largest radius [`circles_through`] looks at, as a multiple of the diagonal of the box around
|
||||
/// the segments' control points.
|
||||
const MAX_RADIUS: f64 = 1e4;
|
||||
/// Below this share of the radius two points of a circle are one.
|
||||
const SAME: f64 = 1e-7;
|
||||
/// Below this share of the radius (or, while the radius is looked for, of the gap from the point to
|
||||
/// the second segments) a circle's distance from the second segments counts as touching.
|
||||
const CONTACT: f64 = 1e-9;
|
||||
/// Below this (in units of the first segment's size) a length is rounding.
|
||||
const ROUNDING: f64 = 1e-12;
|
||||
|
||||
/// The diagonal of the box around the control points of `first` and `second`; none when it is 0 or
|
||||
/// not finite.
|
||||
fn scale(first: &CubicBez, second: &[CubicBez]) -> Option<f64> {
|
||||
let pts = std::iter::once(first).chain(second).flat_map(|c| [c.p0, c.p1, c.p2, c.p3]);
|
||||
let (mut lo, mut hi) = (Point::new(f64::INFINITY, f64::INFINITY), Point::new(f64::NEG_INFINITY, f64::NEG_INFINITY));
|
||||
for p in pts {
|
||||
if !(p.x.is_finite() && p.y.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
lo = Point::new(lo.x.min(p.x), lo.y.min(p.y));
|
||||
hi = Point::new(hi.x.max(p.x), hi.y.max(p.y));
|
||||
}
|
||||
let d = (hi - lo).hypot();
|
||||
(d.is_finite() && d > 0.0).then_some(d)
|
||||
}
|
||||
|
||||
/// The nearest point to `q` on `segs` and its distance.
|
||||
fn nearest(q: Point, segs: &[CubicBez]) -> Option<(f64, Point)> {
|
||||
segs.iter()
|
||||
.map(|c| {
|
||||
let n = c.nearest(q, 1e-9);
|
||||
(n.distance_sq.sqrt(), c.eval(n.t))
|
||||
})
|
||||
.min_by(|a, b| a.0.total_cmp(&b.0))
|
||||
}
|
||||
|
||||
/// The unit normal of `c` at `t` (its tangent turned a quarter turn). Where the derivative is 0 (a
|
||||
/// handle lying on its anchor), the direction the curve leaves in: toward the far handle, or the
|
||||
/// other end. None where the segment does not move by more than rounding in units of `scale`, its
|
||||
/// size.
|
||||
fn normal(c: &CubicBez, t: f64, scale: f64) -> Option<Vec2> {
|
||||
let tiny = ROUNDING * scale;
|
||||
let d = c.deriv().eval(t).to_vec2();
|
||||
let d = if d.hypot() > tiny {
|
||||
d
|
||||
} else if t < 0.5 {
|
||||
c.p2 - c.p0
|
||||
} else {
|
||||
c.p3 - c.p1
|
||||
};
|
||||
let d = if d.hypot() > tiny { d } else { c.p3 - c.p0 };
|
||||
let len = d.hypot();
|
||||
(len.is_finite() && len > tiny).then(|| Vec2::new(-d.y / len, d.x / len))
|
||||
}
|
||||
|
||||
/// The parameters where `f` is 0, within `zero`, in order: every sample where it is (a stretch where
|
||||
/// it stays 0 gives each of its samples), each place between two samples where it changes sign
|
||||
/// (found by bisection), and, where `f` comes toward 0 between samples and goes back without a
|
||||
/// sample on the other side, the place it comes nearest (a golden-section search): a touch when it
|
||||
/// reaches 0 there, and the two places on either side, by bisection, when it passes 0.
|
||||
fn roots(samples: &[f64], f: impl Fn(f64) -> Option<f64>, zero: f64) -> Vec<f64> {
|
||||
let mut out = vec![];
|
||||
let values: Vec<Option<f64>> = samples.iter().map(|s| f(*s)).collect();
|
||||
let is_zero = |v: f64| v.abs() <= zero;
|
||||
for (s, v) in samples.iter().zip(&values) {
|
||||
if v.is_some_and(is_zero) {
|
||||
out.push(*s);
|
||||
}
|
||||
}
|
||||
for (k, w) in values.windows(3).enumerate() {
|
||||
let (Some(a), Some(b), Some(c)) = (w[0], w[1], w[2]) else { continue };
|
||||
let (Some(&lo), Some(&hi)) = (samples.get(k), samples.get(k + 2)) else { continue };
|
||||
let same_side = (a > 0.0) == (b > 0.0) && (b > 0.0) == (c > 0.0);
|
||||
// A low point, or one of two equal low samples about a touch half way between them.
|
||||
let low = b.abs() <= a.abs() && b.abs() <= c.abs() && (b.abs() < a.abs() || b.abs() < c.abs());
|
||||
if !same_side || is_zero(a) || is_zero(b) || is_zero(c) || !low {
|
||||
continue;
|
||||
}
|
||||
out.extend(near_zero(lo, hi, b > 0.0, &f, zero));
|
||||
}
|
||||
// At either end the lowest sample has no sample beyond it: the stretch to the next one.
|
||||
for (end, next) in [(0, 1), (values.len().saturating_sub(1), values.len().saturating_sub(2))] {
|
||||
let (Some(Some(a)), Some(Some(b))) = (values.get(end), values.get(next)) else { continue };
|
||||
let (Some(&s), Some(&t)) = (samples.get(end), samples.get(next)) else { continue };
|
||||
if (a > &0.0) == (b > &0.0) && !is_zero(*a) && !is_zero(*b) && a.abs() < b.abs() {
|
||||
out.extend(near_zero(s.min(t), s.max(t), *a > 0.0, &f, zero));
|
||||
}
|
||||
}
|
||||
for (k, w) in values.windows(2).enumerate() {
|
||||
let (Some(a), Some(b)) = (w[0], w[1]) else { continue };
|
||||
let (Some(&lo), Some(&hi)) = (samples.get(k), samples.get(k + 1)) else { continue };
|
||||
if (a > 0.0) == (b > 0.0) || is_zero(a) || is_zero(b) {
|
||||
continue;
|
||||
}
|
||||
out.extend(bisect(lo, hi, &f));
|
||||
}
|
||||
out.sort_by(f64::total_cmp);
|
||||
out
|
||||
}
|
||||
|
||||
/// Where `f`, above 0 (`positive`) or below at the samples `lo` and `hi`, comes nearest 0 between
|
||||
/// them: that place when it reaches 0 there, the two places on either side where it passes 0, or
|
||||
/// none.
|
||||
fn near_zero(lo: f64, hi: f64, positive: bool, f: &impl Fn(f64) -> Option<f64>, zero: f64) -> Vec<f64> {
|
||||
// Toward 0 is down for a positive `f`, up for a negative one.
|
||||
let toward = if positive { 1.0 } else { -1.0 };
|
||||
let Some(m) = smallest(lo, hi, |s| f(s).map(|v| v * toward)) else { return vec![] };
|
||||
let Some(fm) = f(m) else { return vec![] };
|
||||
if fm.abs() <= zero {
|
||||
vec![m]
|
||||
} else if fm * toward < 0.0 {
|
||||
bisect(lo, m, f).into_iter().chain(bisect(m, hi, f)).collect()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
/// Where `f` changes sign between `lo` and `hi`, by bisection; none when it does not.
|
||||
fn bisect(mut lo: f64, mut hi: f64, f: impl Fn(f64) -> Option<f64>) -> Option<f64> {
|
||||
let (a, b) = (f(lo)?, f(hi)?);
|
||||
if (a > 0.0) == (b > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let positive_low = a > 0.0;
|
||||
for _ in 0..BISECTIONS {
|
||||
let mid = 0.5 * (lo + hi);
|
||||
match f(mid) {
|
||||
Some(v) if (v > 0.0) == positive_low => lo = mid,
|
||||
Some(_) => hi = mid,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
Some(0.5 * (lo + hi))
|
||||
}
|
||||
|
||||
/// Where `g` is smallest between `lo` and `hi`, by a golden-section search; none where `g` has no
|
||||
/// value.
|
||||
fn smallest(mut lo: f64, mut hi: f64, g: impl Fn(f64) -> Option<f64>) -> Option<f64> {
|
||||
const INV_PHI: f64 = 0.618_033_988_749_894_9;
|
||||
let (mut a, mut b) = (hi - INV_PHI * (hi - lo), lo + INV_PHI * (hi - lo));
|
||||
let (mut ga, mut gb) = (g(a)?, g(b)?);
|
||||
for _ in 0..BISECTIONS {
|
||||
if ga < gb {
|
||||
hi = b;
|
||||
(b, gb) = (a, ga);
|
||||
a = hi - INV_PHI * (hi - lo);
|
||||
ga = g(a)?;
|
||||
} else {
|
||||
lo = a;
|
||||
(a, ga) = (b, gb);
|
||||
b = lo + INV_PHI * (hi - lo);
|
||||
gb = g(b)?;
|
||||
}
|
||||
}
|
||||
Some(0.5 * (lo + hi))
|
||||
}
|
||||
|
||||
/// Where the circle about `center` of radius `r` touches `second`: the nearest of their points, when
|
||||
/// it is on the circle (within a millionth of the radius, plus rounding in units of `scale`). None
|
||||
/// for a root where the distance only jumps past the radius, as it does where the first segment
|
||||
/// turns back on itself and its normal flips.
|
||||
fn touching(center: Point, r: f64, second: &[CubicBez], scale: f64) -> Option<Point> {
|
||||
nearest(center, second).filter(|(d, _)| (d - r).abs() <= 1e-6 * r + ROUNDING * scale).map(|(_, p)| p)
|
||||
}
|
||||
|
||||
/// `found` without circles that cross `first`, that touch the two at one point, or that repeat an
|
||||
/// earlier one.
|
||||
fn keep(found: Vec<SegmentCircle>, first: &CubicBez) -> Vec<SegmentCircle> {
|
||||
let mut out: Vec<SegmentCircle> = vec![];
|
||||
for c in found {
|
||||
let r = c.circle.radius;
|
||||
let crosses = nearest(c.circle.center, std::slice::from_ref(first)).is_none_or(|(d, _)| d < r * (1.0 - 1e-6));
|
||||
let one_point = (c.first - c.second).hypot() <= SAME * r;
|
||||
// The roots come in order along their parameter, so a repeat follows its first.
|
||||
let repeat = out.last().is_some_and(|o| (o.circle.center - c.circle.center).hypot() <= SAME * r * 10.0);
|
||||
if !(crosses || one_point || repeat) && r.is_finite() && r > 0.0 {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The circles tangent to `first` at parameter `t` (passing through that point, their centers on
|
||||
/// its normal there) that touch `second`. An empty list when `first` has no direction at `t`, when
|
||||
/// the point is on `second`, or when no circle touches it; at most two in practice, one on each side.
|
||||
pub fn circles_through(first: CubicBez, t: f64, second: &[CubicBez]) -> Vec<SegmentCircle> {
|
||||
if second.is_empty() || !t.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
let t = t.clamp(0.0, 1.0);
|
||||
let (Some(scale), Some(own)) = (scale(&first, second), scale(&first, &[])) else { return vec![] };
|
||||
let Some(n) = normal(&first, t, own) else { return vec![] };
|
||||
let p = first.eval(t);
|
||||
// On the second segments (a corner the two share), up to rounding in units of the first
|
||||
// segment's size: no circle.
|
||||
let on = ROUNDING * own;
|
||||
let Some((gap, _)) = nearest(p, second).filter(|(d, _)| *d > on) else { return vec![] };
|
||||
// Radii spaced evenly in their logarithm, from half the gap from the point to the second
|
||||
// segments (no smaller circle through the point reaches them) to far past the segments.
|
||||
let (lo, hi) = (0.5 * gap * (1.0 - 1e-9), MAX_RADIUS * scale.max(gap));
|
||||
let samples: Vec<f64> = (0..=SAMPLES).map(|k| lo * (hi / lo).powf(k as f64 / SAMPLES as f64)).collect();
|
||||
let mut found = vec![];
|
||||
for side in [1.0, -1.0] {
|
||||
let center = |r: f64| p + n * (side * r);
|
||||
for r in roots(&samples, |r| nearest(center(r), second).map(|(d, _)| d - r), CONTACT * gap) {
|
||||
if let Some(touch) = touching(center(r), r, second, own) {
|
||||
found.push(SegmentCircle { circle: Circle { center: center(r), radius: r }, first: p, second: touch });
|
||||
}
|
||||
}
|
||||
}
|
||||
keep(found, &first)
|
||||
}
|
||||
|
||||
/// The circles of `radius` tangent to `first` (their centers `radius` from it along its normal)
|
||||
/// that touch `second`. An empty list when the radius is not above 0 or none is found. Where the
|
||||
/// segments run twice the radius apart along a stretch, every circle along it touches both, and one
|
||||
/// is listed for each of the 513 places along `first` that are tried there.
|
||||
pub fn circles_of_radius(first: CubicBez, second: &[CubicBez], radius: f64) -> Vec<SegmentCircle> {
|
||||
if second.is_empty() || !radius.is_finite() || radius <= 0.0 {
|
||||
return vec![];
|
||||
}
|
||||
let (Some(_), Some(own)) = (scale(&first, second), scale(&first, &[])) else { return vec![] };
|
||||
let samples: Vec<f64> = (0..=SAMPLES).map(|k| k as f64 / SAMPLES as f64).collect();
|
||||
let mut found = vec![];
|
||||
for side in [1.0, -1.0] {
|
||||
let center = |t: f64| normal(&first, t, own).map(|n| first.eval(t) + n * (side * radius));
|
||||
for t in roots(&samples, |t| center(t).and_then(|c| nearest(c, second)).map(|(d, _)| d - radius), CONTACT * radius) {
|
||||
if let Some(c) = center(t)
|
||||
&& let Some(touch) = touching(c, radius, second, own)
|
||||
{
|
||||
found.push(SegmentCircle { circle: Circle { center: c, radius }, first: first.eval(t), second: touch });
|
||||
}
|
||||
}
|
||||
}
|
||||
keep(found, &first)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use kurbo::{Affine, Point, Rect};
|
||||
use kurbo::{Affine, CubicBez, Point, Rect};
|
||||
use vectorcraft_geom::{PathData, SubPath, shapes};
|
||||
|
||||
use super::*;
|
||||
@@ -485,3 +485,205 @@ fn answers_about_a_tiny_shape_far_away_stay_apart() {
|
||||
assert!(a.y < b.y - 1e-3 || ((a.y - b.y).abs() <= 1e-3 && a.x < b.x), "{found:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A straight segment from `a` to `b`, its handles at its thirds so that `t` runs evenly along it.
|
||||
fn line(a: (f64, f64), b: (f64, f64)) -> CubicBez {
|
||||
let (a, b) = (Point::new(a.0, a.1), Point::new(b.0, b.1));
|
||||
CubicBez::new(a, a.lerp(b, 1.0 / 3.0), a.lerp(b, 2.0 / 3.0), b)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_with_its_handles_on_its_anchors_has_a_direction_at_its_ends() {
|
||||
let (a, b) = (Point::new(0.0, 0.0), Point::new(100.0, 0.0));
|
||||
for t in [0.0, 1.0] {
|
||||
let p = CubicBez::new(a, a, b, b).eval(t);
|
||||
let found = circles_through(CubicBez::new(a, a, b, b), t, &[line((-50.0, 40.0), (150.0, 40.0))]);
|
||||
assert_eq!(found.len(), 1, "{t}: {found:?}");
|
||||
assert!(near(found[0].circle.center, p.x, 20.0, 1e-6), "{t}: {found:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn near(p: Point, x: f64, y: f64, tol: f64) -> bool {
|
||||
(p - Point::new(x, y)).hypot() <= tol
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_circle_through_a_point_of_one_line_touches_a_parallel_line() {
|
||||
let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.5, &[line((0.0, 40.0), (100.0, 40.0))]);
|
||||
// Only toward the other line: away from it, no circle reaches it.
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
let c = found[0];
|
||||
assert!(near(c.circle.center, 50.0, 20.0, 1e-6) && (c.circle.radius - 20.0).abs() < 1e-6, "{c:?}");
|
||||
assert!(near(c.first, 50.0, 0.0, 1e-9) && near(c.second, 50.0, 40.0, 1e-6), "{c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_crossing_line_gives_a_circle_on_each_side() {
|
||||
let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.3, &[line((80.0, -100.0), (80.0, 100.0))]);
|
||||
assert_eq!(found.len(), 2, "{found:?}");
|
||||
for y in [50.0, -50.0] {
|
||||
assert!(found.iter().any(|c| near(c.circle.center, 30.0, y, 1e-6) && (c.circle.radius - 50.0).abs() < 1e-6), "{y}: {found:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_circle_that_crosses_the_first_segment_is_left_out() {
|
||||
// Through (50, 75) on a flat first segment, the circle touching y = -200 has radius 137.5.
|
||||
let below = [line((-500.0, -200.0), (500.0, -200.0))];
|
||||
let flat = circles_through(line((0.0, 75.0), (100.0, 75.0)), 0.5, &below);
|
||||
assert_eq!(flat.len(), 1, "{flat:?}");
|
||||
assert!((flat[0].circle.radius - 137.5).abs() < 1e-6, "{flat:?}");
|
||||
// On a hump from (0, 0) up to (50, 75) and back down to (100, 0), the same circle crosses the
|
||||
// hump's sides (its center (50, -62.5) is 80 from (0, 0)), and none on the other side reaches.
|
||||
let hump = CubicBez::new(Point::new(0.0, 0.0), Point::new(0.0, 100.0), Point::new(100.0, 100.0), Point::new(100.0, 0.0));
|
||||
assert!(near(hump.eval(0.5), 50.0, 75.0, 1e-9));
|
||||
let found = circles_through(hump, 0.5, &below);
|
||||
assert!(found.is_empty(), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn where_the_first_segment_turns_back_only_circles_that_touch_are_kept() {
|
||||
// Out to (75, 0) and back: the normal flips at the turn, and the distance to the line jumps there.
|
||||
let first = CubicBez::new(Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 0.0), Point::new(0.0, 0.0));
|
||||
let second = [line((-100.0, 40.0), (200.0, 40.0))];
|
||||
let found = circles_of_radius(first, &second, 30.0);
|
||||
for c in &found {
|
||||
assert!((c.circle.center.y - 10.0).abs() < 1e-6 || (c.circle.center.y - 70.0).abs() < 1e-6, "{c:?}");
|
||||
}
|
||||
assert!(!found.iter().any(|c| (c.circle.center.x - 75.0).abs() < 1.0), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_circle_through_a_point_touches_a_curved_segment() {
|
||||
// The second segments are a Bézier circle of radius 20 about (50, 100): touched from outside,
|
||||
// |center − (50, 100)| = r + 20 with the center at (50, r), so r = 40.
|
||||
let ring = circle(50.0, 100.0, 20.0);
|
||||
let segs: Vec<CubicBez> = (0..4).map(|i| ring.subpaths[0].segment(i)).collect();
|
||||
let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.5, &segs);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
let c = found[0];
|
||||
// Within the Bézier circle's own error (0.0273% of 20).
|
||||
assert!((c.circle.radius - 40.0).abs() < 0.01 && near(c.circle.center, 50.0, 40.0, 0.01), "{c:?}");
|
||||
assert!(near(c.second, 50.0, 80.0, 0.01), "{c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_a_curved_first_segment_the_center_is_on_the_normal() {
|
||||
// A quarter of a ring of radius 50 about (0, 0); the second segment a line far off to the right.
|
||||
let ring = circle(0.0, 0.0, 50.0);
|
||||
let mut checked = 0;
|
||||
for i in 0..4 {
|
||||
let first = ring.subpaths[0].segment(i);
|
||||
for t in [0.0, 0.3, 0.7, 1.0] {
|
||||
let found = circles_through(first, t, &[line((300.0, -500.0), (300.0, 500.0))]);
|
||||
let tangent = kurbo::ParamCurveDeriv::deriv(&first).eval(t).to_vec2();
|
||||
for c in &found {
|
||||
let to_center = c.circle.center - c.first;
|
||||
assert!(to_center.dot(tangent).abs() <= 1e-9 * to_center.hypot() * tangent.hypot(), "{i} {t}: {c:?}");
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(checked >= 8, "{checked}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_radius_fills_the_corner_between_two_lines() {
|
||||
let first = line((0.0, 0.0), (100.0, 0.0));
|
||||
let found = circles_of_radius(first, &[line((100.0, 0.0), (100.0, 100.0))], 10.0);
|
||||
// Outside the corner a circle tangent to the first line reaches the second only at the shared
|
||||
// corner, where it touches both at one point: left out.
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
let c = found[0];
|
||||
assert!(near(c.circle.center, 90.0, 10.0, 1e-6) && c.circle.radius == 10.0, "{c:?}");
|
||||
assert!(near(c.first, 90.0, 0.0, 1e-6) && near(c.second, 100.0, 10.0, 1e-6), "{c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lines_twice_the_radius_apart_give_circles_all_along() {
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[line((0.0, 40.0), (100.0, 40.0))], 20.0);
|
||||
assert!(found.len() > 100, "{}", found.len());
|
||||
assert!(found.iter().all(|c| (c.circle.center.y - 20.0).abs() < 1e-9 && c.circle.radius == 20.0), "{found:?}");
|
||||
let xs: Vec<f64> = found.iter().map(|c| c.circle.center.x).collect();
|
||||
assert!(xs.iter().any(|x| *x < 1.0) && xs.iter().any(|x| *x > 99.0), "from end to end: {xs:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_circle_that_touches_a_curve_between_the_places_tried_is_found() {
|
||||
// A ring of radius 20 about (50.3, 60): a circle of radius 20 on the line y = 0 reaches it only
|
||||
// at x = 50.3, between the places tried along the line, without the distance changing sign.
|
||||
let ring = circle(50.3, 60.0, 20.0);
|
||||
let segs: Vec<CubicBez> = (0..4).map(|i| ring.subpaths[0].segment(i)).collect();
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &segs, 20.0);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!(near(found[0].circle.center, 50.3, 20.0, 1e-3) && near(found[0].second, 50.3, 40.0, 1e-3), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_touch_half_way_between_two_places_tried_is_found() {
|
||||
// A point half way between the places tried at x = 50 and x = 50.1953125: the two samples
|
||||
// either side are equally far from a touch.
|
||||
let dot = line((50.097_656_25, 40.0), (50.097_656_25, 40.0));
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[dot], 20.0);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!(near(found[0].circle.center, 50.097_656_25, 20.0, 1e-6), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_touch_between_the_end_and_the_next_place_tried_is_found() {
|
||||
for x in [0.05, 99.95] {
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[line((x, 40.0), (x, 40.0))], 20.0);
|
||||
assert_eq!(found.len(), 1, "{x}: {found:?}");
|
||||
assert!(near(found[0].circle.center, x, 20.0, 1e-6), "{x}: {found:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_circles_between_the_same_two_places_tried_are_both_found() {
|
||||
// A point a hair less than twice the radius from the line: two circles, about 0.02 pt either
|
||||
// side of x = 50.05, both between the places tried at 50 and 50.1953125.
|
||||
let (x, y) = (50.05, 39.99999);
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[line((x, y), (x, y))], 20.0);
|
||||
assert_eq!(found.len(), 2, "{found:?}");
|
||||
let dx = (400.0 - (y - 20.0) * (y - 20.0)).sqrt();
|
||||
assert!(near(found[0].circle.center, x - dx, 20.0, 1e-6) && near(found[1].circle.center, x + dx, 20.0, 1e-6), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tiny_circle_between_long_lines_close_together_is_found() {
|
||||
let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.5, &[line((0.0, 1e-4), (100.0, 1e-4))]);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!((found[0].circle.radius - 5e-5).abs() < 1e-12 && near(found[0].circle.center, 50.0, 5e-5, 1e-12), "{found:?}");
|
||||
// A short line near a very long one: the long one's size does not hide the gap.
|
||||
let found = circles_through(line((0.0, 0.0), (100.0, 0.0)), 0.5, &[line((-5e9, 1e-3), (5e9, 1e-3))]);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!((found[0].circle.radius - 5e-4).abs() < 1e-9, "{found:?}");
|
||||
// Lines 1e-5 pt apart moved 1e9 pt to the right.
|
||||
let found = circles_through(line((1e9, 0.0), (1e9 + 100.0, 0.0)), 0.5, &[line((1e9, 1e-5), (1e9 + 100.0, 1e-5))]);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!((found[0].circle.radius - 5e-6).abs() < 1e-9, "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_segments_far_apart_give_no_circle_of_a_small_radius() {
|
||||
// Lines 1e10 pt long and 1450 pt apart: a circle of radius 500 on one stays 950 pt from the other.
|
||||
let found = circles_of_radius(line((0.0, 0.0), (1e10, 0.0)), &[line((0.0, 1450.0), (1e10, 1450.0))], 500.0);
|
||||
assert!(found.is_empty(), "{:?}", found.first());
|
||||
// A radius of 1,000,000 pt and lines 0.05 pt more than twice that apart: no touch.
|
||||
let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[line((0.0, 2_000_000.05), (100.0, 2_000_000.05))], 1e6);
|
||||
assert!(found.is_empty(), "{:?}", found.first());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_circle_when_the_radius_is_too_small_or_the_point_is_on_the_second() {
|
||||
let (first, other) = (line((0.0, 0.0), (100.0, 0.0)), line((0.0, 40.0), (100.0, 40.0)));
|
||||
assert!(circles_of_radius(first, &[other], 5.0).is_empty());
|
||||
for r in [0.0, -1.0, f64::NAN, f64::INFINITY] {
|
||||
assert!(circles_of_radius(first, &[other], r).is_empty(), "{r}");
|
||||
}
|
||||
assert!(circles_through(first, 1.0, &[line((100.0, 0.0), (100.0, 100.0))]).is_empty(), "the point is on the second");
|
||||
assert!(circles_through(first, 0.5, &[]).is_empty());
|
||||
assert!(circles_through(first, f64::NAN, &[other]).is_empty());
|
||||
let dot = line((5.0, 5.0), (5.0, 5.0));
|
||||
assert!(circles_through(dot, 0.5, &[other]).is_empty(), "no direction");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user