diff --git a/crates/three-d/src/solid/rings.rs b/crates/three-d/src/solid/rings.rs index ac63fe2fab..123fc9ee21 100644 --- a/crates/three-d/src/solid/rings.rs +++ b/crates/three-d/src/solid/rings.rs @@ -19,13 +19,18 @@ fn pieces(c: vectorcraft_geom::CubicBez, tolerance: f64) -> u32 { if n.is_finite() { (n as u32).clamp(1, 32) } else { 32 } } +/// Contours thinner than this on average (twice their area over their perimeter) are dropped when +/// overlaps are resolved: the debris a boolean leaves where two curves coincide, far below any +/// art. Pathfinder's default precision would drop real hairlines too. +const DEBRIS: f64 = 1e-6; + /// `path` with its overlaps resolved under `rule`: simple contours that do not cross, /// holes running against their outlines. Type often draws one glyph as overlapping contours (a /// "k"'s arm across its stem), and [`rings`] tells a contour's side from one point of it, so a -/// contour that crosses another must be cut where they meet first. A path the boolean refuses is -/// used as it is. +/// contour that crosses another must be cut where they meet first. Every contour that fills area +/// is kept, however narrow ([`DEBRIS`]). A path the boolean refuses is used as it is. pub(super) fn resolved(path: &PathData, rule: FillRule) -> (PathData, FillRule) { - match vectorcraft_pathops::try_normalize(path, rule) { + match vectorcraft_pathops::try_unite_all_with(&[(path, rule)], DEBRIS) { Ok(p) if !p.subpaths.is_empty() => (p, FillRule::NonZero), _ => (path.clone(), rule), } diff --git a/crates/three-d/src/solid/tests.rs b/crates/three-d/src/solid/tests.rs index 859bf703d4..d2ae291c5c 100644 --- a/crates/three-d/src/solid/tests.rs +++ b/crates/three-d/src/solid/tests.rs @@ -782,3 +782,38 @@ fn crossing_contours_make_one_outline() { // The filled area is the union's: the stem, the arm past it and the leg past the arm. assert!(close(vectorcraft_pathops::area(&k, FillRule::NonZero), 20.0 * 100.0 + 40.0 * 20.0 + 20.0 * 50.0 - 10.0 * 10.0, 1e-6)); } + +/// Resolving overlaps keeps every contour that fills area, however narrow: a hairline beside a +/// large shape is art. What a boolean leaves where two curves coincide is still dropped. +#[test] +fn resolving_overlaps_keeps_narrow_contours() { + let rect = |r: Rect| { + vectorcraft_geom::SubPath::polyline(&[Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)], true) + }; + let hairline = Rect::new(120.0, 0.0, 120.005, 50.0); + let p = PathData::new(vec![rect(Rect::new(0.0, 0.0, 100.0, 100.0)), rect(hairline)]); + let b = p.bounds().unwrap(); + let filled = 100.0 * 100.0 + hairline.width() * hairline.height(); + for rule in [FillRule::NonZero, FillRule::EvenOdd] { + assert!(close(area(&plane(&p, rule, b, front()).unwrap()), filled, 1e-6), "{rule:?}: Rotate"); + assert!(close(area(&extrude(&p, rule, b, Extrude::default(), front()).unwrap()), filled, 1e-6), "{rule:?}: Extrude's cap"); + } + // A circle drawn twice, exactly and a hair off, is one outline: no sliver between the copies. + let circle = |cx: f64, cy: f64, r: f64| { + let (mut b, k) = (BezPath::new(), 0.5523 * r); + b.move_to((cx + r, cy)); + b.curve_to((cx + r, cy + k), (cx + k, cy + r), (cx, cy + r)); + b.curve_to((cx - k, cy + r), (cx - r, cy + k), (cx - r, cy)); + b.curve_to((cx - r, cy - k), (cx - k, cy - r), (cx, cy - r)); + b.curve_to((cx + k, cy - r), (cx + r, cy - k), (cx + r, cy)); + b.close_path(); + b + }; + for dx in [0.0, 1e-7] { + let mut twice = circle(50.0, 50.0, 40.0); + twice.extend(circle(50.0 + dx, 50.0, 40.0).iter()); + let p = PathData::from_bezpath(&twice); + let f = plane(&p, FillRule::NonZero, p.bounds().unwrap(), front()).unwrap(); + assert_eq!(f[0].contours.len(), 1, "{dx}"); + } +}