mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
fix(geom): local error floor per span; a spent trace budget ends the outer-face search
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -324,7 +324,11 @@ fn trace(g: &Graph, u0: usize, v0: usize, max_steps: usize) -> Option<Vec<usize>
|
||||
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<Vec<usize>> {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user