mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 13:33:51 +00:00
Path Precision Tools: Smooth Bridge keeps huge circles' outlines finite
The bridge's distance and arc-center math no longer squares lengths, so circles larger than about 1e154 pt are bridged instead of refused, and an outline that would not fit in f64 is refused instead of written with non-finite anchors.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! Smooth Bridge: two circles joined into one outline by two concave arcs, each touching both
|
||||
//! circles from outside, so the outline runs on smoothly from each circle into the bridge.
|
||||
|
||||
use std::f64::consts::{FRAC_PI_2, PI, TAU};
|
||||
use std::f64::consts::{FRAC_PI_2, PI, SQRT_2, TAU};
|
||||
|
||||
use kurbo::{CubicBez, ParamCurve, Point, Shape, Vec2};
|
||||
use vectorcraft_geom::{Anchor, AnchorKind, PathData, SubPath};
|
||||
@@ -22,7 +22,7 @@ pub enum BridgeError {
|
||||
/// The circles are too far apart for the radius: the two arcs would not reach both circles,
|
||||
/// or would cross before they do.
|
||||
TooFar,
|
||||
/// A radius or circle that is not a finite size above 0.
|
||||
/// A radius or circle that is not a finite size above 0, or an outline too large for `f64`.
|
||||
Bad,
|
||||
}
|
||||
|
||||
@@ -64,18 +64,27 @@ pub fn smooth_bridge(a: Circle, b: Circle, radius: f64) -> Result<SubPath, Bridg
|
||||
return Err(BridgeError::Bad);
|
||||
}
|
||||
let axis = b.center - a.center;
|
||||
let d = axis.hypot();
|
||||
// `f64::hypot`: `Vec2::hypot` squares the length, which overflows above about 1e154 pt.
|
||||
let d = axis.x.hypot(axis.y);
|
||||
if d + a.radius.min(b.radius) <= a.radius.max(b.radius) * (1.0 + 1e-9) {
|
||||
return Err(BridgeError::Inside);
|
||||
}
|
||||
// The arcs' centers are `radius` outside both circles: where circles about the two centers,
|
||||
// `radius` larger than each, cross.
|
||||
let (ra, rb) = (radius + a.radius, radius + b.radius);
|
||||
if d > ra + rb {
|
||||
if !ra.is_finite() || !rb.is_finite() {
|
||||
return Err(BridgeError::Bad);
|
||||
}
|
||||
// Halves and no squares, so no step overflows while the lengths fit in `f64`.
|
||||
let half = ra / 2.0 + rb / 2.0;
|
||||
if d / 2.0 > half {
|
||||
return Err(BridgeError::TooFar);
|
||||
}
|
||||
let along = (d * d + ra * ra - rb * rb) / (2.0 * d);
|
||||
let across = (ra * ra - along * along).max(0.0).sqrt();
|
||||
let along = d / 2.0 + (ra - rb) / d * half;
|
||||
let across = (ra - along).max(0.0).sqrt() * (ra / 2.0 + along / 2.0).max(0.0).sqrt() * SQRT_2;
|
||||
if !along.is_finite() || !across.is_finite() {
|
||||
return Err(BridgeError::Bad);
|
||||
}
|
||||
// Each arc runs between its points on `a` and `b`, which lie towards the two centers from the
|
||||
// arc's center. Its point nearest the axis is the foot of its center on the axis when that foot
|
||||
// is between the two centers, else one of its ends, which are off the axis. So the two arcs
|
||||
@@ -98,12 +107,16 @@ pub fn smooth_bridge(a: Circle, b: Circle, radius: f64) -> Result<SubPath, Bridg
|
||||
arc(&mut segs, b.center, b.radius, b2, b1, Some(u));
|
||||
arc(&mut segs, p1, radius, b1, a1, None);
|
||||
let count = segs.len();
|
||||
let anchors = (0..count)
|
||||
let anchors: Vec<Anchor> = (0..count)
|
||||
.map(|i| {
|
||||
let (prev, seg) = (segs[(i + count - 1) % count], segs[i]);
|
||||
Anchor { p: seg.p0, h_in: prev.p2, h_out: seg.p1, kind: AnchorKind::Smooth }
|
||||
})
|
||||
.collect();
|
||||
let finite_point = |p: Point| p.x.is_finite() && p.y.is_finite();
|
||||
if !anchors.iter().all(|q| finite_point(q.p) && finite_point(q.h_in) && finite_point(q.h_out)) {
|
||||
return Err(BridgeError::Bad);
|
||||
}
|
||||
Ok(SubPath::new(anchors, true))
|
||||
}
|
||||
|
||||
@@ -318,6 +331,22 @@ mod tests {
|
||||
assert_eq!(smooth_bridge(circle(f64::INFINITY, 0.0, 10.0), b, 10.0), Err(BridgeError::Bad));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_circles_bridge_with_finite_anchors_or_are_refused() {
|
||||
// Squaring these lengths overflows f64.
|
||||
let (a, b) = (circle(0.0, 0.0, 5e153), circle(1.5e154, 0.0, 5e153));
|
||||
let sp = smooth_bridge(a, b, 5e153).unwrap();
|
||||
assert!(sp.anchors.iter().all(|q| [q.p, q.h_in, q.h_out].iter().all(|p| p.x.is_finite() && p.y.is_finite())));
|
||||
assert_smooth(&sp);
|
||||
// The two radius sums fit in f64, but not their total.
|
||||
let (a, b) = (circle(-5e307, 0.0, 1e307), circle(5e307, 0.0, 1e307));
|
||||
let sp = smooth_bridge(a, b, 1.5e308).unwrap();
|
||||
assert!(sp.anchors.iter().all(|q| [q.p, q.h_in, q.h_out].iter().all(|p| p.x.is_finite() && p.y.is_finite())));
|
||||
// The arcs' centers would lie beyond the largest f64.
|
||||
let (a, b) = (circle(0.0, 0.0, 1e308), circle(1.5e308, 0.0, 1e308));
|
||||
assert_eq!(smooth_bridge(a, b, 1e308), Err(BridgeError::Bad));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn circles_are_told_from_other_paths() {
|
||||
let round = shapes::ellipse(kurbo::Rect::new(10.0, 20.0, 50.0, 60.0));
|
||||
|
||||
Reference in New Issue
Block a user