diff --git a/crates/pathops/src/addons/pathtools/bridge.rs b/crates/pathops/src/addons/pathtools/bridge.rs index 3352019083..43ee629a91 100644 --- a/crates/pathops/src/addons/pathtools/bridge.rs +++ b/crates/pathops/src/addons/pathtools/bridge.rs @@ -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 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 = (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));