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
103 changes: 92 additions & 11 deletions solidcore/crates/solidcore/src/error/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,45 @@ pub enum QueryFailure {
/// No ray fired from the point found the region it lies in (every ray, both ways, failed
/// or found none). Class `failed`.
RegionNotFound,
/// A bounding box asked of the edge's curve could not be found (the first box's error is
/// the cause). Class `failed`; whether it is a defect or a refusal is its cause's
/// ([`crate::SolidError::is_defect`] follows the chain).
BoundingBoxNotFound,
/// A vector the query unitizes or measures an angle with has zero length, so it has no
/// direction. Upstream returns `SM_ERR_INVALID_INPUT` as a status here, so its kind is
/// [`ErrorKind::InvalidInput`].
ZeroLengthVector,
/// A drop of a point onto a curve or a surface found no foot point. Class `failed`.
NoFoot,
/// The sector classification stepped off a face on its first step, where it has no
/// earlier step to fall back to. Class `failed`.
SteppedOffOnFirstStep,
/// The curve classified against the edge's radial sector runs along the edge there: its
/// tangent is parallel to the edge's, so it has no direction across the sector. Class
/// `failed`.
TangentAlongEdge,
/// The region has no shells, so it has no box. Class `failed`.
RegionWithoutShells,
/// No edge was given where the routine needs one. [`ErrorKind::InvalidInput`].
NoEdgeGiven,
/// The UV line of the classification could not be made (its error is the cause: the
/// line's one error is ends that coincide, the caller's points). Status
/// `SM_ERR_NULL_POINTER`, [`ErrorKind::InvalidInput`].
UvLineNotMade,
/// The classification curve's touch list holds no entry to start the loop walk from:
/// no touch, or in a cyclic walk no exit followed by an entry. Class `failed`.
NoLoopTouches,
/// The surface cache has no poly B-rep: phase 1 has not run on its face.
/// [`ErrorKind::InvalidInput`].
NoPolyBrep,
/// The poly edge's segment meets no node of the cache's subdivision tree. Class `failed`.
SegmentOutsideTree,
/// No tessellation driver was given where the test needs one.
/// [`ErrorKind::InvalidInput`].
NoTessDriver,
/// The cache's poly B-rep has no face (the caller's poly B-rep: phase 1 gives it one).
/// [`ErrorKind::InvalidInput`].
PolyBrepWithoutFace,
}

impl QueryFailure {
Expand All @@ -128,11 +167,23 @@ impl QueryFailure {
| Self::ClosestPointNotEvaluated
| Self::InvalidReach
| Self::CrossingOffEdge
| Self::RegionNotFound => SM_ERR,
Self::Missing(_) | Self::FaceWithoutSurface | Self::EdgeWithoutCurve => {
SM_ERR_NULL_POINTER
}
Self::InvalidValue(_) => SM_ERR_INVALID_INPUT,
| Self::RegionNotFound
| Self::BoundingBoxNotFound
| Self::NoFoot
| Self::SteppedOffOnFirstStep
| Self::TangentAlongEdge
| Self::RegionWithoutShells
| Self::NoLoopTouches
| Self::SegmentOutsideTree => SM_ERR,
Self::Missing(_)
| Self::FaceWithoutSurface
| Self::EdgeWithoutCurve
| Self::NoEdgeGiven
| Self::UvLineNotMade
| Self::NoPolyBrep
| Self::NoTessDriver
| Self::PolyBrepWithoutFace => SM_ERR_NULL_POINTER,
Self::InvalidValue(_) | Self::ZeroLengthVector => SM_ERR_INVALID_INPUT,
}
}

Expand All @@ -152,7 +203,12 @@ impl QueryFailure {
| Self::EdgeWithoutCurve
| Self::EdgeuseWithoutEdge
| Self::EdgeuseOrientationUnset
| Self::InvalidReach => ErrorKind::InvalidInput,
| Self::InvalidReach
| Self::NoEdgeGiven
| Self::UvLineNotMade
| Self::NoPolyBrep
| Self::NoTessDriver
| Self::PolyBrepWithoutFace => ErrorKind::InvalidInput,
_ => ErrorKind::of_status(self.comparison_status()),
}
}
Expand Down Expand Up @@ -189,6 +245,23 @@ impl core::fmt::Display for QueryFailure {
f.write_str("the crossing on the face's surface is off the edge")
}
Self::RegionNotFound => f.write_str("no ray fired from the point found its region"),
Self::BoundingBoxNotFound => f.write_str("the curve's bounding box could not be found"),
Self::ZeroLengthVector => f.write_str("a vector of zero length has no direction"),
Self::NoFoot => f.write_str("the drop found no foot point"),
Self::SteppedOffOnFirstStep => f.write_str("the first step stepped off the surface"),
Self::TangentAlongEdge => {
f.write_str("the curve's tangent is parallel to the edge's tangent")
}
Self::RegionWithoutShells => f.write_str("the region has no shells"),
Self::NoEdgeGiven => f.write_str("no edge was given"),
Self::UvLineNotMade => f.write_str("the UV line could not be made"),
Self::NoLoopTouches => {
f.write_str("the touch-direction list is empty or its index is unset")
}
Self::NoPolyBrep => f.write_str("the surface cache has no poly B-rep"),
Self::SegmentOutsideTree => f.write_str("the segment meets no subdivision tree node"),
Self::NoTessDriver => f.write_str("no tessellation driver was given"),
Self::PolyBrepWithoutFace => f.write_str("the poly B-rep has no face"),
}
}
}
Expand Down Expand Up @@ -314,6 +387,19 @@ pub(super) mod tests {
(F::InvalidReach, SM_ERR, K::InvalidInput),
(F::CrossingOffEdge, SM_ERR, K::Failed),
(F::RegionNotFound, SM_ERR, K::Failed),
(F::BoundingBoxNotFound, SM_ERR, K::Failed),
(F::ZeroLengthVector, SM_ERR_INVALID_INPUT, K::InvalidInput),
(F::NoFoot, SM_ERR, K::Failed),
(F::SteppedOffOnFirstStep, SM_ERR, K::Failed),
(F::TangentAlongEdge, SM_ERR, K::Failed),
(F::RegionWithoutShells, SM_ERR, K::Failed),
(F::NoEdgeGiven, SM_ERR_NULL_POINTER, K::InvalidInput),
(F::UvLineNotMade, SM_ERR_NULL_POINTER, K::InvalidInput),
(F::NoLoopTouches, SM_ERR, K::Failed),
(F::NoPolyBrep, SM_ERR_NULL_POINTER, K::InvalidInput),
(F::SegmentOutsideTree, SM_ERR, K::Failed),
(F::NoTessDriver, SM_ERR_NULL_POINTER, K::InvalidInput),
(F::PolyBrepWithoutFace, SM_ERR_NULL_POINTER, K::InvalidInput),
];
for (failure, code, kind) in cases {
let e = SolidError::query("query::op", failure);
Expand Down Expand Up @@ -394,15 +480,10 @@ pub(super) mod tests {
const PINNED: &[(&str, usize)] = &[
("solidcore/src/query/brep/ray.rs", 2),
("solidcore/src/query/brep/ray/live.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/poly.rs", 13),
("solidcore/src/query/poly/copy.rs", 11),
("solidcore/src/query/poly_properties.rs", 2),
("solidcore/src/query/tess.rs", 6),
];
assert_pinned("query phrase-cause sites", &crate_sites(&PHRASES), PINNED);
}
Expand Down
87 changes: 57 additions & 30 deletions solidcore/crates/solidcore/src/query/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
use crate::curve::curve::p1::GlobalPointSolveOptions;
use crate::curve::curve::p2::euclidian_step_off;
use crate::curve::curve::Curve;
use crate::error::query::QueryFailure;
use crate::error::{ignored, Result, SolidError};
use crate::geom::extent1d::Extent1d;
use crate::geom::extent3d::Extent3d;
Expand Down Expand Up @@ -229,15 +230,15 @@ pub fn calculate_bounding_box(

// when asked for a tight Axis Aligned Box
if tight {
let mut ok1 = true;
let mut ok2 = true;
// the first box's error, which the failure passes on
let mut failed: Option<SolidError> = None;

if req.normal {
match calculate_tight_bounding_box(k, brep, edge) {
Ok(Some(b)) => out.normal = Some(b),
Ok(None) => {}
Err(err) if err.is_unported_or_cancelled() => return Err(err),
Err(_) => ok1 = false,
Err(err) => failed = Some(err),
}
}

Expand All @@ -253,18 +254,21 @@ pub fn calculate_bounding_box(
out.polar = b.polar;
}
Err(err) if err.is_unported_or_cancelled() => return Err(err),
Err(_) => ok2 = false,
Err(err) => {
failed.get_or_insert(err);
}
}
}

// all done
if ok1 && ok2 {
let Some(err) = failed else {
return Ok(out);
}
return Err(SolidError::query_failed(
OP_BBOX,
"the curve's bounding box could not be found",
));
};
return Err(
crate::error::Failure::new(OP_BBOX, QueryFailure::BoundingBoxNotFound)
.caused_by(err)
.into_error(),
);
}

// get all the bounding boxes from the curve
Expand Down Expand Up @@ -310,7 +314,7 @@ pub fn calculate_tight_bounding_box(
.calculate_bounding_box(k, &e.interval, req, true)?
.normal
.ok_or_else(|| {
SolidError::query_failed(OP_TIGHT, "the curve's bounding box has no normal box")
SolidError::query(OP_TIGHT, QueryFailure::Defect { why: "the curve's bounding box was asked for with its normal box, which the box of every curve kind computes when asked" })
})?;

let tol = EFF_ZERO * (1.0 + curve_bbox.max_dimension());
Expand Down Expand Up @@ -529,11 +533,14 @@ fn evaluate_binormal_step_off(

/// The status of a vector operation upstream checks with `SER` (`Unitize`,
/// `AngleBetween` of a zero-length vector): both return `SM_ERR_INVALID_INPUT` as a
/// status (not a null dereference), which `SER` passes on unchanged.
/// status (not a null dereference), which `SER` passes on unchanged. The operation's
/// error stays the cause, so a port invariant below still reads as one.
fn vec_status<T>(r: Result<T>, operation: &'static str) -> Result<T> {
r.map_err(|e| match e {
SolidError::InvalidInput { .. } | SolidError::PortInvariant { .. } => {
SolidError::query_invalid_value(operation, "a vector of zero length has no direction")
crate::error::Failure::new(operation, QueryFailure::ZeroLengthVector)
.caused_by(e)
.into_error()
}
e => e,
})
Expand Down Expand Up @@ -656,7 +663,7 @@ fn holds_coincidence(surfaces: Surfaces, gap: f64) -> bool {
///
/// `SM_ERR` when the point cannot be dropped onto the other edge or the first step-off
/// leaves a face; `SM_ERR_INVALID_INPUT` (as
/// [`QueryFailure::InvalidValue`](crate::error::query::QueryFailure::InvalidValue)) when the first
/// [`QueryFailure::ZeroLengthVector`]) when the first
/// step-off's tangent or a binormal to measure is zero-length (a failed first step-off
/// leaves the tangent uninitialised, which `Unitize` rejects before the step-off
/// failure is looked at); [`SolidError::InvalidInput`] where upstream dereferences a null
Expand Down Expand Up @@ -712,15 +719,16 @@ pub fn radial_sector_classify_between(
// classify edge locals
let same_brep = other_brep.is_none();
let other_ref: &Brep = other_brep.as_deref().unwrap_or(&*brep);
let other_edge = other_ref
.edgeuse(classify_eu)?
.edge
.ok_or_else(|| SolidError::query_missing(OP_RADIAL, "the other edge use's edge"))?;
let other_edge = other_ref.edgeuse(classify_eu)?.edge.ok_or_else(|| {
crate::error::Failure::new(OP_RADIAL, QueryFailure::EdgeuseWithoutEdge)
.with_status(crate::error::SM_ERR_NULL_POINTER)
.into_error()
})?;
let other = other_ref.edge(other_edge)?;
let other_curve = other
.curve
.as_ref()
.ok_or_else(|| SolidError::query_missing(OP_RADIAL, "the other edge's curve"))?;
.ok_or_else(|| SolidError::query(OP_RADIAL, QueryFailure::EdgeWithoutCurve))?;
let tolerance = other.tolerance + this.tolerance;

// get the corresponding classify edge parameter value
Expand Down Expand Up @@ -749,10 +757,7 @@ pub fn radial_sector_classify_between(
}
if !success {
// Unable to drop point to other curve.
return Err(SolidError::query_failed(
OP_RADIAL,
"the point could not be dropped onto the other curve",
));
return Err(SolidError::query(OP_RADIAL, QueryFailure::NoFoot));
}
param
};
Expand Down Expand Up @@ -909,9 +914,9 @@ pub fn radial_sector_classify_between(
if stepped_off_surface {
// its an error to step off on first pass
if count == 0 {
return Err(SolidError::query_failed(
return Err(SolidError::query(
OP_RADIAL,
"the first step stepped off the surface",
QueryFailure::SteppedOffOnFirstStep,
));
}

Expand Down Expand Up @@ -1194,7 +1199,7 @@ pub fn radial_sector_classify_between(
/// # Errors
///
/// `SM_ERR` when the curve's projected direction vanishes; `SM_ERR_INVALID_INPUT` (as
/// [`QueryFailure::InvalidValue`](crate::error::query::QueryFailure::InvalidValue)) for a zero-length vector to unitize or measure;
/// [`QueryFailure::ZeroLengthVector`]) for a zero-length vector to unitize or measure;
/// [`SolidError::InvalidInput`]
/// for a sector edge use without radial partner; those of the step-offs and
/// evaluations; [`SolidError::Unported`] for the step-offs
Expand Down Expand Up @@ -1265,13 +1270,13 @@ pub fn radial_sector_of_curve_classify(
.unwrap_or(start_param);
let pv = curve.evaluate(k, step_param, 1, true, true)?;
let mut tan = *pv.get(1).ok_or_else(|| {
SolidError::query_failed(OP_RADIAL_CURVE, "the curve evaluation has no tangent")
SolidError::query(OP_RADIAL_CURVE, QueryFailure::Defect { why: "the curve evaluation, asked for the first derivative, returned fewer, though an evaluation that succeeds returns every order it is asked for" })
})?;
if orientation == OrientType::Opposite {
tan = -tan;
}
let pnt3 = *pv.first().ok_or_else(|| {
SolidError::query_failed(OP_RADIAL_CURVE, "the curve evaluation has no point")
SolidError::query(OP_RADIAL_CURVE, QueryFailure::Defect { why: "the curve evaluation, asked for the point and the first derivative, returned no point, though an evaluation that succeeds returns every order it is asked for" })
})?;

// Project binormals into plane which is perpendicular to edge
Expand All @@ -1280,9 +1285,9 @@ pub fn radial_sector_of_curve_classify(
let bin2 = tan1.cross(s2.binormal).cross(tan1);
let bin3 = tan1.cross(tan).cross(tan1);
if bin3.length_squared() < EFF_ZERO_SQ {
return Err(SolidError::query_failed(
return Err(SolidError::query(
OP_RADIAL_CURVE,
"the curve's tangent is parallel to the edge's tangent",
QueryFailure::TangentAlongEdge,
));
}

Expand Down Expand Up @@ -1658,4 +1663,26 @@ mod tests {
assert_eq!(RadialSectorClass::ZeroSectorMate.code(), 7);
assert_eq!(CurveSectorClass::CoincidentRadial.code(), 3);
}

#[test]
fn a_vector_operations_error_stays_the_cause() {
// a zero-length vector: the status `SER` passes on, over the operation's error
let unit = Vec3::ZERO.unitize().unwrap_err();
let e = vec_status(Vec3::ZERO.unitize(), OP_RADIAL).unwrap_err();
assert_eq!(e.query_failure(), Some(&QueryFailure::ZeroLengthVector));
assert_eq!(e.sm_status(), Some(crate::error::SM_ERR_INVALID_INPUT));
assert_eq!(e.cause(), Some(&unit));
assert!(e.is_status_answer(), "{e:?}");
let angle = Vec3::X.angle_between(Vec3::ZERO).unwrap_err();
let e = vec_status(Vec3::X.angle_between(Vec3::ZERO), OP_RADIAL_CURVE).unwrap_err();
assert_eq!(e.cause(), Some(&angle));
// a port invariant below keeps its class: the status is no answer
let port = SolidError::port_invariant("inner", "an adapter failed");
let e = vec_status::<f64>(Err(port.clone()), OP_RADIAL).unwrap_err();
assert_eq!(e.cause(), Some(&port));
assert!(e.is_port_failure() && !e.is_status_answer(), "{e:?}");
// any other error passes through unchanged
let other = SolidError::cancelled("inner");
assert_eq!(vec_status::<f64>(Err(other.clone()), OP_RADIAL), Err(other));
}
}
10 changes: 6 additions & 4 deletions solidcore/crates/solidcore/src/query/face/p1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use crate::curve::crv_on_surf::CrvOnSurf;
use crate::curve::curve::{Curve, CurveKind};
use crate::error::ignored;
use crate::error::query::QueryFailure;
use crate::geom::certified::{above, gamma};
use crate::geom::extent1d::Extent1d;
use crate::geom::extent3d::Extent3d;
Expand Down Expand Up @@ -752,7 +753,7 @@ pub fn calculate_shell_bounding_box(
/// # Errors
///
/// `SM_ERR`
/// ([`QueryFailure::Failed`](crate::error::query::QueryFailure::Failed)) for a region without shells; those of the shell
/// ([`QueryFailure::RegionWithoutShells`]) for a region without shells; those of the shell
/// boxes. `output` then holds the union of the shells before the failing one.
pub fn calculate_region_bounding_box_into(
k: &mut Kernel,
Expand All @@ -765,9 +766,9 @@ pub fn calculate_region_bounding_box_into(

let shells = b.region(id)?.get_shells(None, |_, _| Ok(false))?;
if shells.is_empty() {
return Err(SolidError::query_failed(
return Err(SolidError::query(
OP_REGION_BOX,
"the region has no shells",
QueryFailure::RegionWithoutShells,
));
}
let mut this_box = Extent3d::new();
Expand Down Expand Up @@ -1259,7 +1260,7 @@ pub fn edge_curve_on_classify(
curve: &Curve,
opts: &EdgeCurveOnOptions,
) -> Result<CurveOnClassification> {
let edge = edge.ok_or_else(|| SolidError::query_missing(OP_EDGE_CURVE, "the edge"))?;
let edge = edge.ok_or_else(|| SolidError::query(OP_EDGE_CURVE, QueryFailure::NoEdgeGiven))?;

// init outputs
let mut out = CurveOnClassification::default();
Expand Down Expand Up @@ -1401,6 +1402,7 @@ mod tests {
let mut out = Extent3d::from_point(Vec3::new(1., 2., 3.));
let e = calculate_region_bounding_box_into(&mut k, &b, r, false, &mut out).unwrap_err();
assert_eq!(e.upstream_status(), Some(crate::error::SM_ERR), "{e:?}");
assert_eq!(e.query_failure(), Some(&QueryFailure::RegionWithoutShells));
assert_eq!(out, Extent3d::new());
}

Expand Down
Loading
Loading