From d9b79174e2ada8bb2d52751f57a4d146d2b0568e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:23:25 +0000 Subject: [PATCH] fix(geom): local error floor per span; a spent trace budget ends the outer-face search --- crates/geom/src/nurbs.rs | 25 ++++++++++++++++++++----- crates/geom/src/region.rs | 13 +++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/crates/geom/src/nurbs.rs b/crates/geom/src/nurbs.rs index c8a7cc845..b02dfda91 100644 --- a/crates/geom/src/nurbs.rs +++ b/crates/geom/src/nurbs.rs @@ -529,13 +529,18 @@ impl Spline { /// sample. fn panel_length(&self, a: f64, b: f64, derivative: fn(&Spline, f64) -> Vec2) -> f64 { let speed = |t: f64| derivative(self, t).len(); - adaptive_gauss(a, b, gauss(a, b, speed), &speed, self.length_floor(), MAX_GAUSS_DEPTH) + adaptive_gauss(a, b, gauss(a, b, speed), &speed, self.length_floor(a, b), MAX_GAUSS_DEPTH) } - /// The absolute length error below which a panel counts as converged: rounding of the control - /// coordinates (1e-15 of the largest one). - fn length_floor(&self) -> f64 { - 1e-15 * self.control.iter().map(|c| c.x.abs().max(c.y.abs())).fold(0.0, f64::max) + /// The absolute length error below which the panel [a, b] counts as converged: rounding of the + /// coordinates of the control points that shape it (1e-15 of the largest), so a distant control + /// point does not relax the tolerance. Costs one span's control points, not the whole spline. + fn length_floor(&self, a: f64, b: f64) -> f64 { + let Some(n) = self.control.len().checked_sub(1) else { return 0.0 }; + let span = find_span(n, self.degree, (a + b) / 2.0, &self.knots); + let first = span.saturating_sub(self.degree); + let local = self.control.get(first..=span.min(n)).unwrap_or_default(); + 1e-15 * local.iter().map(|c| c.x.abs().max(c.y.abs())).fold(0.0, f64::max) } /// Arc length between parameters `a` and `b` (adaptive Gauss–Legendre over each knot span). @@ -683,6 +688,16 @@ mod tests { assert!(c.length() < 1e-6); } + /// A distant control point does not relax the tolerance of a span it does not shape. + #[test] + fn the_error_floor_is_local_to_the_span() { + let mut s = + Spline::from_control(vec![Vec2::new(0.0, 0.0), Vec2::new(1.0, 0.0), Vec2::new(0.2, 0.0), Vec2::new(1e13, 0.0), Vec2::new(1e13, 0.0)], 2); + s.knots = vec![0.0, 0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 2.0]; + let got = s.length_between(0.0, 1.0); + assert!((got - 41.0 / 45.0).abs() < 1e-9, "{got}"); + } + fn cubic() -> Spline { Spline::from_control(vec![Vec2::new(0.0, 0.0), Vec2::new(3.0, 3.0), Vec2::new(6.0, -3.0), Vec2::new(9.0, 0.0), Vec2::new(12.0, 2.0)], 3) } diff --git a/crates/geom/src/region.rs b/crates/geom/src/region.rs index 2efc031bc..897122823 100644 --- a/crates/geom/src/region.rs +++ b/crates/geom/src/region.rs @@ -324,7 +324,11 @@ fn trace(g: &Graph, u0: usize, v0: usize, max_steps: usize) -> Option let back = (*g.pts.get(u)? - pv).angle(); let mut best: Option<(f64, usize)> = None; let ns = g.adj.get(v)?; - g.trace_work.set(g.trace_work.get().checked_sub(ns.len() as u64)?); + let Some(left) = g.trace_work.get().checked_sub(ns.len() as u64) else { + g.trace_work.set(0); + return None; + }; + g.trace_work.set(left); for &w in ns { if w == u && g.adj.get(v).is_some_and(|l| l.len() > 1) { continue; @@ -376,7 +380,12 @@ fn outer_face(g: &Graph, p: Vec2) -> Option> { for (_, a, b) in hits.into_iter().take(64) { let (pa, pb) = (*g.pts.get(a)?, *g.pts.get(b)?); let (u, v) = if (pb - pa).cross(p - pa) > 0.0 { (a, b) } else { (b, a) }; - if let Some(ids) = trace(g, u, v, max_steps) + let traced = trace(g, u, v, max_steps); + // A spent budget ends the search: a cheaper, more distant face must not stand in for the nearer one. + if traced.is_none() && g.trace_work.get() == 0 { + return None; + } + if let Some(ids) = traced && ids.len() >= 3 && let Some(lp) = points(g, &ids) && shoelace(&lp) > 0.0