diff --git a/crates/geom/src/brep/geometry.rs b/crates/geom/src/brep/geometry.rs index f34960307..6c0b92fc3 100644 --- a/crates/geom/src/brep/geometry.rs +++ b/crates/geom/src/brep/geometry.rs @@ -12,7 +12,7 @@ //! an edge's for its pcurves, an edge's and its vertex's together where they meet, a vertex's on //! its surface. A point that cannot be evaluated is an error, never a panic. use super::eval::{CurveEval, SurfaceEval}; -use super::{Brep, Loop}; +use super::{Brep, Curve2, Loop}; use crate::Vec3; /// Where a record disagrees with itself. @@ -84,6 +84,14 @@ fn v3(p: [f64; 3]) -> Vec3 { fn unevaluable(record: Record, what: impl std::fmt::Display) -> Mismatch { Mismatch { record, problem: Problem::Unevaluable(what.to_string()) } } +/// A use's pcurve at its edge's parameter `t`. The pcurve's interval is its edge's to within +/// rounding ([`Brep::validate`], `PCURVE_INTERVAL`), so an edge parameter just past it is read at +/// the pcurve's end. +fn on_pcurve(pc: &Curve2, t: f64) -> Option<[f64; 2]> { + let [a, b] = pc.interval()?; + pc.eval(t.clamp(a.min(b), a.max(b))) +} + /// `gap` within `tolerance` (a non-finite gap never is). fn within(record: Record, what: &'static str, gap: f64, tolerance: f64) -> Result<()> { if gap <= tolerance { Ok(()) } else { Err(Mismatch { record, problem: Problem::Off { what, gap, tolerance } }) } @@ -147,7 +155,7 @@ impl Brep { let [t0, t1] = e.interval; let mut uv = [[0.0; 2]; 3]; for (k, t) in [t0, 0.5 * (t0 + t1), t1].into_iter().enumerate() { - let q = u.pcurve.eval(t).ok_or_else(|| unevaluable(at, format!("the pcurve has no point at {t}")))?; + let q = on_pcurve(&u.pcurve, t).ok_or_else(|| unevaluable(at, format!("the pcurve has no point at {t}")))?; let s = surface.point(q).ok_or_else(|| unevaluable(at, format!("the surface has no point at {q:?}")))?; let (Some(c), Some(what), Some(slot)) = (p.get(k), PCURVE_POINTS.get(k), uv.get_mut(k)) else { return Err(unevaluable(at, "pcurve sample")); @@ -181,7 +189,7 @@ impl Brep { for k in 0..AREA_SAMPLES { let f = k as f64 / AREA_SAMPLES as f64; let t = if u.forward { t0 + (t1 - t0) * f } else { t1 - (t1 - t0) * f }; - ring.push(u.pcurve.eval(t).ok_or_else(|| unevaluable(at, format!("the pcurve has no point at {t}")))?); + ring.push(on_pcurve(&u.pcurve, t).ok_or_else(|| unevaluable(at, format!("the pcurve has no point at {t}")))?); } } let area = signed_area(&ring); diff --git a/crates/geom/src/brep/tests.rs b/crates/geom/src/brep/tests.rs index 48fa9e14f..bd233a282 100644 --- a/crates/geom/src/brep/tests.rs +++ b/crates/geom/src/brep/tests.rs @@ -530,6 +530,21 @@ fn shift(c: &mut Curve2, by: [f64; 2]) { } } +/// A pcurve whose interval ends a rounding short of its edge's (as a kernel that snaps an edge's +/// end to its knot hands it back) is read at its end: the record validates and checks. +#[test] +fn a_pcurve_a_rounding_short_of_its_edge_is_read_at_its_end() { + let mut b = sheet(); + let Curve2::Spline { knots, .. } = pcurve_of(&mut b, 0, 0) else { panic!("a spline pcurve") }; + let end = *knots.last().unwrap(); + let short = end - 1e-11 * end.abs().max(1.0); + for k in knots.iter_mut().filter(|k| **k == end) { + *k = short; + } + b.validate().unwrap(); + b.check_geometry().unwrap(); +} + /// One geometric defect each, structurally valid: every one is refused, naming the record. #[test] fn geometric_defects_are_refused_by_record() {