Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions solidcore/crates/solidcore/src/curve/plane_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//! Deviations: none (no upstream counterpart). A line is read as its segment from `start` to
//! `end`. No recursion.

use crate::error::query::QueryFailure;
use crate::error::{Result, SolidError};
use crate::eval::Curve;
use crate::math::{Point3, Vec3};
Expand Down Expand Up @@ -54,7 +55,8 @@ pub struct PlaneOf {
///
/// `InvalidInput` for no curves, a tolerance not finite and positive, numbers not finite, or
/// a rational spline with a weight not positive; `Unsupported` for a traced intersection
/// curve; a query failure for curves so large their chords or normal overflow.
/// curve; [`QueryFailure::BoundOverflows`] for curves so large their chords or normal
/// overflow.
pub fn plane_of(curves: &[Curve], tol: f64) -> Result<Option<PlaneOf>> {
if curves.is_empty() {
return Err(SolidError::invalid_input(OP, "no curves"));
Expand Down Expand Up @@ -108,7 +110,7 @@ pub fn plane_of(curves: &[Curve], tol: f64) -> Result<Option<PlaneOf>> {
};
// A size past the floating range (a chord or a cross product overflowing) leaves no
// normal to bound with: refused, never read as a plane. `f64::max` would drop a NaN.
let overflow = || SolidError::query_failed(OP, "the curves' size overflows the bound");
let overflow = || SolidError::query(OP, QueryFailure::BoundOverflows);
if !(reach.is_finite() && normal.x.is_finite() && normal.y.is_finite() && normal.z.is_finite())
{
return Err(overflow());
Expand Down Expand Up @@ -275,10 +277,9 @@ mod tests {
let huge = Curve::Polyline {
points: vec![[0.0; 3], [1e200, 0.0, 0.0], [0.0, 1e200, 0.0]],
};
assert!(matches!(
plane_of(&[huge], 1e-6),
Err(SolidError::Failed(_))
));
let e = plane_of(&[huge], 1e-6).unwrap_err();
assert_eq!(e.query_failure(), Some(&QueryFailure::BoundOverflows));
assert_eq!(e.sm_status(), Some(crate::error::SM_ERR));
}

#[test]
Expand Down
90 changes: 71 additions & 19 deletions solidcore/crates/solidcore/src/error/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
//! | [`QueryFailure::Missing`] | `SM_ERR_NULL_POINTER` |
//! | [`QueryFailure::InvalidValue`] | `SM_ERR_INVALID_INPUT` |
//! | [`QueryFailure::Defect`] | `SM_ERR` (or the site's, through `with_status`) |
//! | the named causes, from [`QueryFailure::BoundOverflows`] on | the status of the phrase each replaced (`comparison_status`), or the site's through `with_status` |
//!
//! [`QueryFailure::Defect`] is a failure reviewed at its site as the kernel's fault, with its
//! reason: a defect ([`ErrorKind::StageFailed`], class 127). The phrase causes are sites
//! not yet converted (the conversion guide: `docs/failure-codes-and-reproduction.md` §6).
//!
//! Deviations: not upstream. A failure says in a phrase what went wrong; the failure's
//! `operation` path says which routine. A failure names an entity only where the site
//! it replaces did (the point classification's seam failure, at its face). The failures
//! that are checks of a caller's arguments stay [`crate::SolidError::InvalidInput`].
//! Deviations: not upstream. A failure names what went wrong (a named cause, or a phrase
//! where a site is not yet converted); the failure's `operation` path says which routine.
//! A failure names an entity only where the site it replaces did (the point
//! classification's seam failure, at its face). A check of a caller's argument or body
//! that upstream answers with a status is a named cause of kind
//! [`ErrorKind::InvalidInput`] that keeps the status (for example
//! [`QueryFailure::FaceWithoutSurface`]); the checks that report no status stay
//! [`crate::SolidError::InvalidInput`].

use super::{ErrorKind, Failure, Reason, SM_ERR, SM_ERR_INVALID_INPUT, SM_ERR_NULL_POINTER};

Expand All @@ -48,26 +53,52 @@ pub enum QueryFailure {
/// The step that failed, and why that is the kernel's fault.
why: &'static str,
},
/// A bound or size the query computes overflows the floating range, though every number
/// given is finite: the inputs' size, or how far apart they are, reaches past it. Class
/// `failed`.
BoundOverflows,
/// No station of the first curve dropped onto the other curve or the surface, so no
/// deviation was measured. Class `failed`.
NoStationDropped,
/// No patch of the surface overlaps the UV box given: the box is within the rounding of
/// the patches' knot range, but past every patch. [`ErrorKind::InvalidInput`].
NoPatchInBox,
/// A face of the caller's body has no surface. [`ErrorKind::InvalidInput`].
FaceWithoutSurface,
/// The surface the query reads (a B-spline surface, or the B-spline base of an analytic
/// one) has no NURBS net: none was made for it, or it could not be made (a NURB that
/// cannot be made stays missing), so the query has no net to read.
/// [`ErrorKind::Unsupported`].
SurfaceWithoutNurbs,
}

impl QueryFailure {
/// The status the C++ reports for the failure (a comparison detail).
pub(crate) const fn comparison_status(&self) -> i32 {
match self {
Self::Failed(_) | Self::Defect { .. } => SM_ERR,
Self::Missing(_) => SM_ERR_NULL_POINTER,
Self::Failed(_)
| Self::Defect { .. }
| Self::BoundOverflows
| Self::NoStationDropped
| Self::NoPatchInBox
| Self::SurfaceWithoutNurbs => SM_ERR,
Self::Missing(_) | Self::FaceWithoutSurface => SM_ERR_NULL_POINTER,
Self::InvalidValue(_) => SM_ERR_INVALID_INPUT,
}
}

/// The failure's stable [`ErrorKind`]: [`ErrorKind::Missing`] for
/// [`QueryFailure::Missing`], [`ErrorKind::InvalidInput`] for
/// [`QueryFailure::InvalidValue`], [`ErrorKind::Failed`] otherwise.
/// The failure's stable [`ErrorKind`]: [`ErrorKind::InvalidInput`] for
/// [`QueryFailure::InvalidValue`] and for a check of the caller's argument or body,
/// [`ErrorKind::Unsupported`] for a surface or object the routine does not take,
/// [`ErrorKind::Missing`] for [`QueryFailure::Missing`], otherwise the kind of the status
/// it replaced.
/// [`QueryFailure::Defect`] is [`ErrorKind::StageFailed`], a defect.
#[must_use]
pub const fn kind(&self) -> ErrorKind {
match self {
Self::Defect { .. } => ErrorKind::StageFailed,
Self::SurfaceWithoutNurbs => ErrorKind::Unsupported,
Self::NoPatchInBox | Self::FaceWithoutSurface => ErrorKind::InvalidInput,
_ => ErrorKind::of_status(self.comparison_status()),
}
}
Expand All @@ -79,6 +110,11 @@ impl core::fmt::Display for QueryFailure {
Self::Failed(what) | Self::InvalidValue(what) => f.write_str(what),
Self::Missing(what) => write!(f, "{what} is missing"),
Self::Defect { why } => write!(f, "{}: {why}", ErrorKind::StageFailed),
Self::BoundOverflows => f.write_str("the bound overflows the floating range"),
Self::NoStationDropped => f.write_str("no station dropped onto the other"),
Self::NoPatchInBox => f.write_str("no patch of the surface overlaps the UV box"),
Self::FaceWithoutSurface => f.write_str("the face has no surface"),
Self::SurfaceWithoutNurbs => f.write_str("the surface has no NURBS net"),
}
}
}
Expand Down Expand Up @@ -179,6 +215,32 @@ pub(super) mod tests {
assert_eq!(SolidError::sm_err("query::op").query_failure(), None);
}

/// Each named cause reports the status of the phrase it replaced, and its kind.
#[test]
fn the_named_query_causes_keep_the_status_they_replaced() {
use ErrorKind as K;
use QueryFailure as F;
let cases = [
(F::BoundOverflows, SM_ERR, K::Failed),
(F::NoStationDropped, SM_ERR, K::Failed),
(F::NoPatchInBox, SM_ERR, K::InvalidInput),
(F::FaceWithoutSurface, SM_ERR_NULL_POINTER, K::InvalidInput),
(F::SurfaceWithoutNurbs, SM_ERR, K::Unsupported),
];
for (failure, code, kind) in cases {
let e = SolidError::query("query::op", failure);
assert_eq!(e.sm_status(), Some(code), "{failure:?}");
assert_eq!(super::super::upstream_status(&e), Some(code), "{failure:?}");
assert_eq!(e.kind(), kind, "{failure:?}");
assert_eq!(e.query_failure(), Some(&failure));
assert!(!e.is_defect(), "{failure:?}");
}
assert_eq!(
SolidError::query("query::op", F::FaceWithoutSurface).to_string(),
"query::op: the face has no surface"
);
}

/// The query routines describe their failures: their library code builds no bare
/// `Upstream` status, except the files listed (with the bare sites each may still hold)
/// whose conversion waits on other open work.
Expand Down Expand Up @@ -242,25 +304,15 @@ pub(super) mod tests {
};
#[rustfmt::skip] // one file per line, so conversions in parallel merge cleanly
const PINNED: &[(&str, usize)] = &[
("solidcore/src/curve/plane_of.rs", 1),
("solidcore/src/nurbs/crv_deviation.rs", 12),
("solidcore/src/nurbs/srf_deviation.rs", 3),
("solidcore/src/query/api_queries.rs", 36),
("solidcore/src/query/brep.rs", 2),
("solidcore/src/query/brep/ray.rs", 2),
("solidcore/src/query/brep/ray/live.rs", 4),
("solidcore/src/query/contact.rs", 3),
("solidcore/src/query/continuity.rs", 5),
("solidcore/src/query/curve.rs", 1),
("solidcore/src/query/deviation.rs", 2),
("solidcore/src/query/distance_bound.rs", 4),
("solidcore/src/query/edge.rs", 10),
("solidcore/src/query/face/p1.rs", 2),
("solidcore/src/query/face/p2.rs", 19),
("solidcore/src/query/face/p3.rs", 2),
("solidcore/src/query/face/p4.rs", 2),
("solidcore/src/query/face_surface.rs", 2),
("solidcore/src/query/normal.rs", 2),
("solidcore/src/query/poly.rs", 13),
("solidcore/src/query/poly/copy.rs", 11),
("solidcore/src/query/poly_properties.rs", 2),
Expand Down
74 changes: 40 additions & 34 deletions solidcore/crates/solidcore/src/nurbs/crv_deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
//! Deviations: none (no upstream counterpart). Non-rational 3D curves only, of degree 1 to
//! [`MAX_DEGREE`]; their knot vectors clamped, on one domain for [`certified_distance`].

use crate::error::query::QueryFailure;
use crate::error::{Result, SolidError};
use crate::math::Vec3;
use crate::nurbs::crv_basic::Curve;
Expand All @@ -59,8 +60,8 @@ const OP_FIT: &str = "nurbs::crv_deviation::interpolation_bound";
///
/// `InvalidInput` for a rational or 2D curve, a parameter count other than the points', or a
/// number not finite;
/// a query failure when the bound is not finite; those of the evaluation (a parameter
/// outside the knots).
/// [`QueryFailure::BoundOverflows`] when the bound overflows the floating range; those of the
/// evaluation (a parameter outside the knots).
pub fn interpolation_bound(c: &Curve, params: &[f64], points: &[Vec3]) -> Result<f64> {
if c.cpts().iter().any(|p| p.w != NOW || p.z == NOZ) {
return Err(SolidError::invalid_input(OP_FIT, "a rational or 2D curve"));
Expand Down Expand Up @@ -95,15 +96,15 @@ pub fn interpolation_bound(c: &Curve, params: &[f64], points: &[Vec3]) -> Result
.derivs(u, Side::Left, 0)?
.first()
.copied()
.ok_or_else(|| SolidError::query_failed(OP_FIT, "no point evaluated"))?;
.ok_or_else(|| SolidError::query(OP_FIT, QueryFailure::Defect { why: "the curve's evaluation of order 0 returned no point, though an evaluation that succeeds returns the point it is asked for" }))?;
let d = norm_up(p.x - q.x, p.y - q.y, p.z - q.z)
.ok_or_else(|| SolidError::query_failed(OP_FIT, "the bound is not finite"))?;
.ok_or_else(|| SolidError::query(OP_FIT, QueryFailure::BoundOverflows))?;
largest = largest.max(d);
}
let eval_err = gamma(c.degree().saturating_mul(12).saturating_add(4)) * size;
let bound = (largest + 2.0 * eval_err) * (1.0 + gamma(2));
if !bound.is_finite() {
return Err(SolidError::query_failed(OP_FIT, "the bound is not finite"));
return Err(SolidError::query(OP_FIT, QueryFailure::BoundOverflows));
}
Ok(bound)
}
Expand All @@ -117,8 +118,8 @@ pub fn interpolation_bound(c: &Curve, params: &[f64], points: &[Vec3]) -> Result
/// # Errors
///
/// `InvalidInput` for a rational or 2D curve, a number not finite, knots not clamped, or two
/// curves on different domains; a query failure when their pieces do not share their spans or the bound is not
/// finite; those of the refinement, the extraction and the elevation.
/// curves on different domains; [`QueryFailure::BoundOverflows`] when the bound overflows the
/// floating range; those of the refinement, the extraction and the elevation.
pub fn certified_distance(a: &Curve, b: &Curve) -> Result<f64> {
check(a, OP)?;
check(b, OP)?;
Expand All @@ -139,7 +140,7 @@ pub fn certified_distance(a: &Curve, b: &Curve) -> Result<f64> {
let largest = largest_gap(&pa, &pb, OP)?;
let bound = (largest + 2.0 * (ea + eb)) * (1.0 + gamma(2));
if !bound.is_finite() {
return Err(SolidError::query_failed(OP, "the bound is not finite"));
return Err(SolidError::query(OP, QueryFailure::BoundOverflows));
}
Ok(bound)
}
Expand Down Expand Up @@ -209,10 +210,7 @@ pub fn certified_hausdorff(a: &Curve, ia: [f64; 2], b: &Curve, ib: [f64; 2]) ->
};
let (ca, cb) = (over(&pa, c0, c1), over(&pb, c0, c1));
if ca.is_empty() {
return Err(SolidError::query_failed(
OP_HAUSDORFF,
"the curves' pieces do not share their spans",
));
return Err(SolidError::query(OP_HAUSDORFF, QueryFailure::Defect { why: "every interval end is a breakpoint of both curves' pieces and the intervals overlap past a point, so a piece lies in their common interval" }));
}
let largest = largest_gap(&ca, &cb, OP_HAUSDORFF)?;
// the largest distance of a piece past the common interval from the curve's point there,
Expand All @@ -221,20 +219,16 @@ pub fn certified_hausdorff(a: &Curve, ia: [f64; 2], b: &Curve, ib: [f64; 2]) ->
let first = common.first().and_then(|x| x.cpts().first());
let last = common.last().and_then(|x| x.cpts().last());
let (Some(&at_c0), Some(&at_c1)) = (first, last) else {
return Err(SolidError::query_failed(
OP_HAUSDORFF,
"a piece without points",
));
return Err(SolidError::query(OP_HAUSDORFF, QueryFailure::Defect { why: "the common pieces were checked to pair up, and a Bézier piece of degree 1 or more has at least two control points" }));
};
let mut s = None;
for (lo, hi, end) in [(lo, c0, at_c0), (c1, hi, at_c1)] {
if lo >= hi {
continue;
}
for r in over(p, lo, hi).iter().flat_map(Curve::cpts) {
let d = norm_up(r.x - end.x, r.y - end.y, r.z - end.z).ok_or_else(|| {
SolidError::query_failed(OP_HAUSDORFF, "the bound is not finite")
})?;
let d = norm_up(r.x - end.x, r.y - end.y, r.z - end.z)
.ok_or_else(|| SolidError::query(OP_HAUSDORFF, QueryFailure::BoundOverflows))?;
s = Some(s.map_or(d, |m: f64| m.max(d)));
}
}
Expand All @@ -249,9 +243,9 @@ pub fn certified_hausdorff(a: &Curve, ia: [f64; 2], b: &Curve, ib: [f64; 2]) ->
}
};
if !bound.is_finite() {
return Err(SolidError::query_failed(
return Err(SolidError::query(
OP_HAUSDORFF,
"the bound is not finite",
QueryFailure::BoundOverflows,
));
}
Ok(bound)
Expand Down Expand Up @@ -279,28 +273,19 @@ fn in_own_frame(a: &Curve, b: &Curve, op: &'static str) -> Result<(Curve, Curve)
/// degree on the same spans (computed, each rounded up by `γ_8`).
fn largest_gap(pa: &[Curve], pb: &[Curve], op: &'static str) -> Result<f64> {
if pa.len() != pb.len() {
return Err(SolidError::query_failed(
op,
"the curves' pieces do not share their spans",
));
return Err(SolidError::query(op, QueryFailure::Defect { why: "both curves are cut into Bézier pieces at the union of their breakpoints, so their pieces pair up span by span" }));
}
let mut largest = 0.0_f64;
for (x, y) in pa.iter().zip(pb) {
if x.knots().first() != y.knots().first() || x.knots().last() != y.knots().last() {
return Err(SolidError::query_failed(
op,
"the curves' pieces do not share their spans",
));
return Err(SolidError::query(op, QueryFailure::Defect { why: "both curves are cut into Bézier pieces at the union of their breakpoints, so their pieces pair up span by span" }));
}
if x.cpts().len() != y.cpts().len() {
return Err(SolidError::query_failed(
op,
"the curves' pieces are not of one degree",
));
return Err(SolidError::query(op, QueryFailure::Defect { why: "both curves' pieces are raised to the higher of the two degrees, so paired pieces have as many control points" }));
}
for (p, r) in x.cpts().iter().zip(y.cpts()) {
let d = norm_up(p.x - r.x, p.y - r.y, p.z - r.z)
.ok_or_else(|| SolidError::query_failed(op, "the bound is not finite"))?;
.ok_or_else(|| SolidError::query(op, QueryFailure::BoundOverflows))?;
largest = largest.max(d);
}
}
Expand Down Expand Up @@ -573,6 +558,27 @@ mod tests {
));
}

/// Finite curves too far apart for the bound: each difference of control points is
/// finite, its length is not.
#[test]
fn curves_too_far_apart_for_the_bound_overflow_by_name() {
use crate::error::query::QueryFailure;
let a = spline(&[[8e307, 8e307, 0.0], [8e307, 8e307, 1.0]], 1);
let b = spline(&[[-8e307, -8e307, 0.0], [-8e307, -8e307, 1.0]], 1);
for e in [
certified_distance(&a, &b).unwrap_err(),
certified_hausdorff(&a, [0.0, 1.0], &b, [0.0, 1.0]).unwrap_err(),
] {
assert_eq!(
e.query_failure(),
Some(&QueryFailure::BoundOverflows),
"{e}"
);
assert_eq!(e.sm_status(), Some(crate::error::SM_ERR));
assert!(!e.is_defect());
}
}

#[test]
fn an_extreme_degree_is_refused_not_overflowed() {
let c = spline(&WAVE, 3);
Expand Down
Loading
Loading