From 360e1633ac0de01bda56d5c106b646caa55e87fc Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:03:23 +0100 Subject: [PATCH 1/7] Typed errors (TE-O-06): claim Co-Authored-By: Claude Code From 2f79b90095a9e5250bafde75b4a08602417687a1 Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:28:37 +0100 Subject: [PATCH 2/7] Typed errors (TE-O-06, part 1): the plane and cone failures become named causes or reviewed defects Co-Authored-By: Claude Code --- .../crates/solidcore/src/error/surface.rs | 220 +++++++++++++++++- .../crates/solidcore/src/surface/cone/p1.rs | 141 ++++++----- .../crates/solidcore/src/surface/plane/p1.rs | 70 +++++- .../crates/solidcore/src/surface/plane/p2.rs | 118 ++++++---- 4 files changed, 435 insertions(+), 114 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index 29b3474a2fd..ea80a3ee6e8 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -16,6 +16,7 @@ //! | [`SurfaceFailure::Defect`] | `SM_ERR` (or the site's, through `with_status`) | //! | the named causes of the surface solvers, [`SurfaceFailure::SolverWithout`] to [`SurfaceFailure::NothingLifted`] | the status of the phrase each replaced (`comparison_status`), or the site's through `with_status` | //! | the named causes of the volume surfaces and the implicit checks, from [`SurfaceFailure::TooFewKnots`] on | `SM_ERR`, except [`SurfaceFailure::NoNurb`]: `SM_ERR_NULL_POINTER` (a site that reported `SM_ERR` keeps it through `with_status`) | +//! | the named causes of the analytic and swept surfaces, from [`SurfaceFailure::OperationNotTaken`] on | the status of the phrase each replaced (`comparison_status`), or the site's through `with_status` | //! //! [`SurfaceFailure::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 @@ -38,7 +39,7 @@ use super::{ EntityRef, ErrorKind, Failure, Reason, SM_ERR, SM_ERR_INVALID_INPUT, - SM_ERR_NOT_WITHIN_TOLERANCE, SM_ERR_NULL_POINTER, SM_ERR_OUTSIDE_OF_DOMAIN, + SM_ERR_NOT_WITHIN_TOLERANCE, SM_ERR_NULL_POINTER, SM_ERR_OUTSIDE_OF_DOMAIN, SM_ERR_WARNING, }; /// Why a surface routine fails. @@ -257,6 +258,50 @@ pub enum SurfaceFailure { /// The canvas that encloses an analytic plane's boundary curves is degenerate or not /// finite. Class `failed`, status `SM_ERR`. PlaneCanvasNotFinite, + /// The solve operation asked for is not one the routine takes (a plane's fast drop + /// takes minimize, normalize and intersect). [`ErrorKind::InvalidInput`]. + OperationNotTaken, + /// The direction of the line given is the zero vector. [`ErrorKind::InvalidInput`]. + ZeroLineDirection, + /// The line given is not parallel to the plane within the angle test, but the + /// determinant of the crossing's linear solve is below the kernel's zero (a line + /// direction or plane scale too small to solve with). Class `failed`. + LineCrossingSingular, + /// The line where two planes meet is too nearly singular to solve for (a pivot below + /// the kernel's zero), so it is not built. Upstream's warning: status + /// `SM_ERR_WARNING`, class `failed`. + PlanesLineNearlySingular, + /// The scale given is not uniform (or, where the routine says so, not positive): an + /// analytic surface takes only a uniform scale. [`ErrorKind::InvalidInput`]. + ScaleNotUniform, + /// The trim domain given is not inside the surface's natural domain. + /// [`ErrorKind::InvalidInput`]. + TrimDomainOutside, + /// The NURB tested as a plane has parallel u and v directions at its origin, so it has + /// no plane frame (the unitize's error is the cause). Class `failed`. + NurbAxesParallel, + /// The surface's NURB form could not be made (the make's failure is the cause, where it + /// gave one). Class `failed`; whether it is a defect or a refusal is its cause's + /// ([`crate::SolidError::is_defect`] follows the chain). + NurbNotMade, + /// The evaluation of the surface's NURB form failed (its error is the cause). Class + /// `failed`; whether it is a defect or a refusal is its cause's. + NurbEvaluationFailed, + /// The radius given is not above the kernel's zero. [`ErrorKind::InvalidInput`]. + RadiusNotPositive, + /// The swept surface given has no generator curve (an empty surface whose data was + /// never read or set). [`ErrorKind::InvalidInput`]. + SurfaceWithoutGenerator, + /// The domain given reaches past the cone's apex, where its radius would be negative. + /// [`ErrorKind::InvalidInput`]. + DomainPastApex, + /// The iso curve asked for has zero length (its parameter range on the surface is + /// degenerate). [`ErrorKind::InvalidInput`]. + DegenerateIsoCurve, + /// The surface of revolution has no polar converter circle (its NURB form was never + /// made or read, or both sweep iso curves were degenerate when it was, which the make + /// does not report). Class `failed`. + NoPolarConverter, } /// What a solver lacks ([`SurfaceFailure::SolverWithout`]). @@ -291,10 +336,17 @@ impl SurfaceFailure { Self::Missing(_) | Self::SolverWithout { .. } | Self::SolveCacheNotMade - | Self::NoNurb => SM_ERR_NULL_POINTER, - Self::InvalidValue(_) | Self::LineDirectionZero => SM_ERR_INVALID_INPUT, + | Self::NoNurb + | Self::SurfaceWithoutGenerator + | Self::NoPolarConverter => SM_ERR_NULL_POINTER, + Self::InvalidValue(_) + | Self::LineDirectionZero + | Self::ZeroLineDirection + | Self::DomainPastApex + | Self::DegenerateIsoCurve => SM_ERR_INVALID_INPUT, Self::OutsideDomain(_) => SM_ERR_OUTSIDE_OF_DOMAIN, Self::OffSurface(_) => SM_ERR_NOT_WITHIN_TOLERANCE, + Self::PlanesLineNearlySingular => SM_ERR_WARNING, _ => SM_ERR, } } @@ -319,7 +371,15 @@ impl SurfaceFailure { | Self::UnknownVolumeType { .. } | Self::NoNurb | Self::CurveWithoutNurb - | Self::CurveWithoutControlPoints => ErrorKind::InvalidInput, + | Self::CurveWithoutControlPoints + | Self::OperationNotTaken + | Self::ZeroLineDirection + | Self::ScaleNotUniform + | Self::TrimDomainOutside + | Self::RadiusNotPositive + | Self::SurfaceWithoutGenerator + | Self::DomainPastApex + | Self::DegenerateIsoCurve => ErrorKind::InvalidInput, Self::BoxOutsideBendHalfSpace { .. } | Self::BoxOutsideZeroPeriod { .. } | Self::PointOutsideVolumeDomain { .. } @@ -343,7 +403,10 @@ impl SurfaceFailure { | Self::NoContinuousDomain | Self::NothingLifted | Self::NoVolumeInverse - | Self::CallFailed => ErrorKind::Failed, + | Self::CallFailed + | Self::NurbNotMade + | Self::NurbEvaluationFailed + | Self::NoPolarConverter => ErrorKind::Failed, _ => ErrorKind::of_status(self.comparison_status()), } } @@ -470,6 +533,34 @@ impl core::fmt::Display for SurfaceFailure { Self::PlaneCanvasNotFinite => { f.write_str("the analytic plane's bounding canvas is not finite") } + Self::OperationNotTaken => { + f.write_str("the solve operation is not one the routine takes") + } + Self::ZeroLineDirection => f.write_str("the line direction is the zero vector"), + Self::LineCrossingSingular => { + f.write_str("the line's crossing with the plane is singular to solve for") + } + Self::PlanesLineNearlySingular => { + f.write_str("the planes' line is nearly singular to solve for") + } + Self::ScaleNotUniform => f.write_str("an analytic surface takes only a uniform scale"), + Self::TrimDomainOutside => { + f.write_str("the trim domain is not inside the surface's natural domain") + } + Self::NurbAxesParallel => { + f.write_str("the NURB's u and v directions are parallel at its origin") + } + Self::NurbNotMade => f.write_str("the surface's NURB form could not be made"), + Self::NurbEvaluationFailed => { + f.write_str("the evaluation of the surface's NURB form failed") + } + Self::RadiusNotPositive => f.write_str("the radius is not positive"), + Self::SurfaceWithoutGenerator => f.write_str("the surface has no generator curve"), + Self::DomainPastApex => f.write_str("the domain reaches past the apex"), + Self::DegenerateIsoCurve => f.write_str("the iso curve has zero length"), + Self::NoPolarConverter => { + f.write_str("the surface of revolution has no polar converter circle") + } } } } @@ -507,6 +598,23 @@ impl crate::SolidError { Self::surface(operation, SurfaceFailure::Missing(what)) } + /// [`SurfaceFailure::NurbNotMade`] with status `status`, for a routine whose NURB form + /// is still missing after `made`, the result of the call that was to make it: its + /// error, when it failed, is the cause. + #[must_use] + pub(crate) fn surface_nurb_not_made( + operation: &'static str, + status: i32, + made: Result<(), Self>, + ) -> Self { + let failure = Failure::new(operation, SurfaceFailure::NurbNotMade).with_status(status); + match made { + Err(e) => failure.caused_by(e), + Ok(()) => failure, + } + .into_error() + } + /// The surface failure behind this error, for a [`crate::SolidError::Failed`] with a /// [`Reason::Surface`]. #[must_use] @@ -805,6 +913,105 @@ mod tests { } } + /// The named causes keep the status of the phrase each replaced, so `upstream_status` + /// (which most goldens units read) is unchanged; their class says whose the failure is, + /// and a cause chained under one decides whether it is a defect. + #[test] + fn the_named_causes_keep_their_status_and_say_their_class() { + let op = "surface::op"; + let cases = [ + ( + SurfaceFailure::OperationNotTaken, + SM_ERR, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::ZeroLineDirection, + SM_ERR_INVALID_INPUT, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::LineCrossingSingular, + SM_ERR, + ErrorKind::Failed, + ), + ( + SurfaceFailure::PlanesLineNearlySingular, + SM_ERR_WARNING, + ErrorKind::Failed, + ), + ( + SurfaceFailure::ScaleNotUniform, + SM_ERR, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::TrimDomainOutside, + SM_ERR, + ErrorKind::InvalidInput, + ), + (SurfaceFailure::NurbAxesParallel, SM_ERR, ErrorKind::Failed), + (SurfaceFailure::NurbNotMade, SM_ERR, ErrorKind::Failed), + ( + SurfaceFailure::NurbEvaluationFailed, + SM_ERR, + ErrorKind::Failed, + ), + ( + SurfaceFailure::RadiusNotPositive, + SM_ERR, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::SurfaceWithoutGenerator, + SM_ERR_NULL_POINTER, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::DomainPastApex, + SM_ERR_INVALID_INPUT, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::DerivativeOrderTooHigh { max: 3 }, + SM_ERR, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::DegenerateIsoCurve, + SM_ERR_INVALID_INPUT, + ErrorKind::InvalidInput, + ), + ( + SurfaceFailure::NoPolarConverter, + SM_ERR_NULL_POINTER, + ErrorKind::Failed, + ), + ]; + for (failure, status, kind) in cases { + let e = SolidError::surface(op, failure); + assert_eq!(e.sm_status(), Some(status), "{failure:?}"); + assert_eq!( + super::super::upstream_status(&e), + Some(status), + "{failure:?}" + ); + assert_eq!(e.kind(), kind, "{failure:?}"); + assert_eq!(e.operation(), op, "{failure:?}"); + assert_eq!(e.surface_failure(), Some(&failure)); + assert!(!e.is_defect() && !e.is_refusal(), "{failure:?}"); + } + // a NURB not made keeps the site's status, and its make's defect decides the class + let made = Err(SolidError::surface(op, SurfaceFailure::Defect { why: "x" })); + let e = SolidError::surface_nurb_not_made(op, SM_ERR_NULL_POINTER, made); + assert_eq!(e.sm_status(), Some(SM_ERR_NULL_POINTER)); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::NurbNotMade)); + assert!(e.is_defect()); + let e = SolidError::surface_nurb_not_made(op, SM_ERR, Ok(())); + assert_eq!(e.sm_status(), Some(SM_ERR)); + assert!(e.cause().is_none() && !e.is_defect()); + } + /// A reviewed defect is of class 127 with the status of the phrase cause it replaces, /// and says why. #[test] @@ -862,7 +1069,6 @@ mod tests { #[rustfmt::skip] // one file per line, so conversions in parallel merge cleanly const PINNED: &[(&str, usize)] = &[ ("solidcore/src/surface/ball_patch.rs", 8), - ("solidcore/src/surface/cone/p1.rs", 12), ("solidcore/src/surface/cone/p2.rs", 14), ("solidcore/src/surface/cone/p3.rs", 3), ("solidcore/src/surface/extend.rs", 2), @@ -871,8 +1077,6 @@ mod tests { ("solidcore/src/surface/extend/blend.rs", 6), ("solidcore/src/surface/extend/sides.rs", 2), ("solidcore/src/surface/offset_surface.rs", 27), - ("solidcore/src/surface/plane/p1.rs", 3), - ("solidcore/src/surface/plane/p2.rs", 9), ("solidcore/src/surface/procedural_blend.rs", 9), ("solidcore/src/surface/procedural_blend/convert.rs", 13), ("solidcore/src/surface/procedural_blend/offset.rs", 5), diff --git a/solidcore/crates/solidcore/src/surface/cone/p1.rs b/solidcore/crates/solidcore/src/surface/cone/p1.rs index 98990cb23bd..2f6a7f340f1 100644 --- a/solidcore/crates/solidcore/src/surface/cone/p1.rs +++ b/solidcore/crates/solidcore/src/surface/cone/p1.rs @@ -135,7 +135,9 @@ use crate::curve::curve::p2::{CIRCLE_TYPE, LINE_TYPE}; use crate::curve::curve::{Curve, CurveKind}; use crate::curve::ellipse::{Ellipse, EllipseOptions}; use crate::curve::line::{BoundedLineOptions, Line}; -use crate::error::{ignore_status, ignored, Result, SolidError}; +use crate::error::{ + ignore_status, ignored, Failure, Result, SolidError, SM_ERR, SM_ERR_NULL_POINTER, +}; use crate::geom::axis2_placement::Axis2Placement; use crate::geom::extent1d::Extent1d; use crate::geom::extent2d::Extent2d; @@ -298,9 +300,9 @@ pub(crate) fn cone_convert_uv_from_nurbs_to_step_exact( }; // convert sweep coordinate - let circle = rev.polar_curve().ok_or_else(|| { - SolidError::surface_missing(OP_TO_STEP, "the polar curve of the surface of revolution") - })?; + let circle = rev + .polar_curve() + .ok_or_else(|| SolidError::surface(OP_TO_STEP, SurfaceFailure::NoPolarConverter))?; let (x, exact) = rev.polar.convert_to_polar_parameter(k, circle, nurb_uv.x)?; // convert linear coordinate @@ -472,7 +474,7 @@ pub(crate) fn cone_iso_curve( if length < scaled_zero { return Err(SolidError::surface( OP_ISO, - SurfaceFailure::InvalidValue("the iso line has zero length"), + SurfaceFailure::DegenerateIsoCurve, )); } let scale = length / line_vec.dot(cone_z_axis); @@ -765,10 +767,15 @@ impl Cone { /// (whose query makes the cone's NURB when missing). fn adjust_gen_line_with_owner(&mut self, k: &Kernel, ivl: &Extent1d) -> Result<()> { let Some(CurveKind::Line(line)) = self.rev.gen_curve.as_mut().map(Curve::kind_mut) else { - return Err(SolidError::surface_missing( + return Err(Failure::new( OP_GEN, - "the cone's generator line", - )); + SurfaceFailure::Defect { + why: "the cone's generator is not a line, though its one caller, the cone's \ + construction, set it to a line just before", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error()); }; let dom = line.step_interval(); if ivl.is_contained_by(&dom, EFF_ZERO) && dom.is_contained_by(ivl, EFF_ZERO) { @@ -938,18 +945,16 @@ impl Cone { // Build NURB; fail cleanly if degenerate (single zero-radius apex is valid). match self.make_nurb(k) { Err(e) if e.is_unported_or_cancelled() => return Err(e), - Err(_) => { - return Err(SolidError::surface_failed( - operation, - "the cone's NURB could not be made", - )) - } + Err(e) => return Err(SolidError::surface_nurb_not_made(operation, SM_ERR, Err(e))), Ok(()) => {} } if self.rev.base.gw_nurb().is_none() { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( operation, - "the cone has no NURB", + SurfaceFailure::Defect { + why: "the cone's make_nurb returned success yet left no NURB, though on \ + success it sets the NURB of the revolution it builds", + }, )); } Ok(self) @@ -1077,9 +1082,9 @@ impl Cone { ) -> Result { // Reject non-positive radius (ctor assert is a no-op in release). if radius.partial_cmp(&EFF_ZERO) != Some(core::cmp::Ordering::Greater) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_CYL_CANONICAL, - "the cylinder's radius is not positive", + SurfaceFailure::RadiusNotPositive, )); } let cylinder = Self::new_cylinder_unbounded( @@ -1239,7 +1244,7 @@ impl Cone { self.rev .gen_curve .as_ref() - .ok_or_else(|| SolidError::surface_missing(OP_GEN, "the cone's generator curve")) + .ok_or_else(|| SolidError::surface(OP_GEN, SurfaceFailure::SurfaceWithoutGenerator)) } // ---- equality ------------------------------------------------------------------- @@ -1281,16 +1286,10 @@ impl Cone { // Method: just call the parent's version, // unless the new domain would result in going past the apex. let v_domain = new_step_uv_domain.v_interval(); - if self.radius(v_domain.min()) < -EFF_ZERO { + if self.radius(v_domain.min()) < -EFF_ZERO || self.radius(v_domain.max()) < -EFF_ZERO { return Err(SolidError::surface( OP_ADJUST, - SurfaceFailure::InvalidValue("the domain reaches past the apex"), - )); - } - if self.radius(v_domain.max()) < -EFF_ZERO { - return Err(SolidError::surface( - OP_ADJUST, - SurfaceFailure::InvalidValue("the domain reaches past the apex"), + SurfaceFailure::DomainPastApex, )); } @@ -1619,11 +1618,10 @@ impl Cone { rows } Err(e) if e.is_unported_or_cancelled() => return Err(e), - Err(_) => { - return Err(SolidError::surface_failed( - OP_EVAL, - "the cone's NURB evaluation failed", - )) + Err(e) => { + return Err(Failure::new(OP_EVAL, SurfaceFailure::NurbEvaluationFailed) + .caused_by(e) + .into_error()) } } } else { @@ -1692,9 +1690,9 @@ impl Cone { ) -> Result<()> { // check input if hu > 3 || hv > 3 { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_EVAL_STEP, - "the STEP form evaluates derivatives up to order 3 only", + SurfaceFailure::DerivativeOrderTooHigh { max: 3 }, )); // Not implemented } @@ -1974,9 +1972,9 @@ impl Cone { if let Some(s) = opt_scale { if !are_same(s.x, s.y) || !are_same(s.x, s.z) || s.x < EFF_ZERO { // Unable to scale analytical surfaces non-uniformly - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_TRANSFORM, - "an analytic surface cannot be scaled non-uniformly", + SurfaceFailure::ScaleNotUniform, )); } scale = s.x; @@ -2419,10 +2417,9 @@ mod tests { assert_eq!(e.upstream_status(), Some(SM_ERR)); assert_eq!( e.surface_failure(), - Some(&SurfaceFailure::Failed( - "the cylinder's radius is not positive" - )) + Some(&SurfaceFailure::RadiusNotPositive) ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); let c = Cone::create_canonical_cylinder(&k(), &std_placement(), 2.0).unwrap(); assert!(c.is_cylinder() && c.is_cylinder_class()); assert_eq!(c.type_code(), CYLINDER_TYPE); @@ -2488,9 +2485,11 @@ mod tests { let e = c .convert_uv_from_nurbs_to_step(&k(), Point2::new(0.5, 0.5)) .unwrap_err(); - assert_eq!(e.upstream_status(), Some(crate::error::SM_ERR_NULL_POINTER)); + assert_eq!(e.upstream_status(), Some(SM_ERR_NULL_POINTER)); assert_eq!(e.operation(), OP_TO_STEP); - assert_eq!(e.kind(), crate::error::ErrorKind::Missing); + // the degenerate sweep iso curve left no polar converter after a successful make + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::NoPolarConverter)); + assert_eq!(e.kind(), crate::error::ErrorKind::Failed); } #[test] @@ -2522,12 +2521,7 @@ mod tests { Some(crate::error::SM_ERR_INVALID_INPUT) ); assert_eq!(e.operation(), OP_ADJUST); - assert_eq!( - e.surface_failure(), - Some(&SurfaceFailure::InvalidValue( - "the domain reaches past the apex" - )) - ); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::DomainPastApex)); // the parent's AdjustSTEPUVDomain is ported (surf_of_revolution_p2) assert!(c .adjust_step_uv_domain(&k(), &Extent2d::from_bounds(0.0, 0.0, 360.0, 1.0)) @@ -2542,7 +2536,8 @@ mod tests { .unwrap_err(); assert_eq!(e.upstream_status(), Some(SM_ERR)); assert_eq!(e.operation(), OP_TRANSFORM); - assert_eq!(e.kind(), crate::error::ErrorKind::Failed); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::ScaleNotUniform)); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); assert!(c .transform( &k(), @@ -2983,29 +2978,61 @@ mod tests { } } - /// The cone's failures keep the operation, status, kind and entity of the bare - /// `Upstream` statuses they replace (a sample). + /// A cone without generator (an empty one, whose data was never read) is the caller's, + /// with upstream's `SM_ERR_NULL_POINTER`. + #[test] + fn a_cone_without_generator_is_the_callers() { + let e = Cone::empty(true).max_analytic_domain(&k()).unwrap_err(); + assert_eq!(e.sm_status(), Some(SM_ERR_NULL_POINTER)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::SurfaceWithoutGenerator) + ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); + } + + /// A STEP evaluation past order 3 is the caller's argument, refused by name with + /// upstream's `SM_ERR`. + #[test] + fn a_derivative_past_order_3_is_the_callers() { + let e = cone123() + .evaluate_step(Point2::new(10.0, 1.0), 4, 0) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(SM_ERR)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::DerivativeOrderTooHigh { max: 3 }) + ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); + } + + /// The cone's failures keep the operation, status and entity of the bare `Upstream` + /// statuses they replace (a sample); their class is the cause's. #[test] fn typed_surface_failures_keep_their_upstream_answers() { use crate::error::surface::SurfaceFailure; - let samples: [(&'static str, SurfaceFailure, i32); 3] = [ + use crate::error::ErrorKind; + let samples: [(&'static str, SurfaceFailure, i32, ErrorKind); 3] = [ ( OP_GEN, - SurfaceFailure::Missing("the cone's generator curve"), - crate::error::SM_ERR_NULL_POINTER, + SurfaceFailure::SurfaceWithoutGenerator, + SM_ERR_NULL_POINTER, + ErrorKind::InvalidInput, ), ( OP_ISO, - SurfaceFailure::InvalidValue("the iso line has zero length"), + SurfaceFailure::DegenerateIsoCurve, crate::error::SM_ERR_INVALID_INPUT, + ErrorKind::InvalidInput, ), ( OP_EVAL_STEP, - SurfaceFailure::Failed("the STEP form evaluates derivatives up to order 3 only"), + SurfaceFailure::DerivativeOrderTooHigh { max: 3 }, SM_ERR, + ErrorKind::InvalidInput, ), ]; - for (operation, failure, code) in samples { + for (operation, failure, code, kind) in samples { let typed = SolidError::surface(operation, failure); let before = SolidError::Upstream { operation, @@ -3014,7 +3041,7 @@ mod tests { }; assert_eq!(typed.sm_status(), before.sm_status(), "{typed:?}"); assert_eq!(typed.upstream_status(), Some(code), "{typed:?}"); - assert_eq!(typed.kind(), before.kind(), "{typed:?}"); + assert_eq!(typed.kind(), kind, "{typed:?}"); assert_eq!(typed.operation(), before.operation(), "{typed:?}"); assert_eq!(typed.entity(), None, "{typed:?}"); assert_eq!(typed.is_port_failure(), before.is_port_failure()); diff --git a/solidcore/crates/solidcore/src/surface/plane/p1.rs b/solidcore/crates/solidcore/src/surface/plane/p1.rs index d6479b3c4ea..e1d9b36a613 100644 --- a/solidcore/crates/solidcore/src/surface/plane/p1.rs +++ b/solidcore/crates/solidcore/src/surface/plane/p1.rs @@ -1034,9 +1034,9 @@ impl Plane { && operation != SolverOperation::Normalize && operation != SolverOperation::Intersect { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_DROP, - "the operation is neither minimize, normalize nor intersect", + SurfaceFailure::OperationNotTaken, )); } @@ -1193,7 +1193,7 @@ impl Plane { if line_vector.length_squared() < EFF_ZERO_SQ { return Err(SolidError::surface( OP_LINE, - SurfaceFailure::InvalidValue("the line direction is the zero vector"), + SurfaceFailure::ZeroLineDirection, )); } @@ -1337,10 +1337,11 @@ impl Plane { let det = pu.cross(pv).dot(lt); if det.abs() < EFF_ZERO { - // should never happen, we're in the non-parallel case here. - return Err(SolidError::surface_failed( + // the angle test above passed, so a direction or a scale too small to solve + // with makes the determinant this small + return Err(SolidError::surface( OP_LINE, - "the line is parallel to the plane", + SurfaceFailure::LineCrossingSingular, )); } @@ -2630,10 +2631,63 @@ mod tests { false, 1e-6, ); + let e = r.err().unwrap(); + assert_eq!(e.sm_status(), Some(SM_ERR_INVALID_INPUT)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::ZeroLineDirection) + ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); + } + + /// A line square to a plane whose u and v scales are 1e-7 crosses it at a determinant + /// of 1e-14, below the kernel's zero: the crossing is singular to solve for, with + /// upstream's `SM_ERR`; the fast drop refuses an operation it does not take. + #[test] + fn a_crossing_too_small_to_solve_and_an_operation_not_taken_are_named() { + use crate::geom::global_solver::SolverOperation; + use crate::surface::surface::p4::NoObject; + let tiny = Plane::new( + Point3::new(0.0, 0.0, 0.0), + Vec3::new(1.0, 0.0, 0.0), + Vec3::new(0.0, 1.0, 0.0), + Vec2::new(1e-7, 1e-7), + &Extent2d::from_bounds(0.0, 0.0, 1.0, 1.0), + ); + let e = tiny + .global_line_intersect::( + &Kernel::default(), + NoObject, + &tiny.step_uv_domain(), + Point3::new(0.0, 0.0, 1.0), + Vec3::new(0.0, 0.0, 1.0), + None, + false, + 1e-6, + ) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::LineCrossingSingular) + ); + let p = unit_plane(); + let e = p + .drop_point_fast::( + &p.step_uv_domain(), + SolverOperation::Maximize, + Point3::new(1.0, 2.0, 4.0), + 1e-6, + None, + None, + ) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); assert_eq!( - r.err().and_then(|e| e.sm_status()), - Some(SM_ERR_INVALID_INPUT) + e.surface_failure(), + Some(&SurfaceFailure::OperationNotTaken) ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); } #[test] diff --git a/solidcore/crates/solidcore/src/surface/plane/p2.rs b/solidcore/crates/solidcore/src/surface/plane/p2.rs index e3cd5912275..3561e9086bf 100644 --- a/solidcore/crates/solidcore/src/surface/plane/p2.rs +++ b/solidcore/crates/solidcore/src/surface/plane/p2.rs @@ -81,7 +81,7 @@ use crate::curve::curve::{Curve, CurveKind}; use crate::curve::ellipse::{Ellipse, EllipseOptions}; use crate::curve::line::{BoundedLineOptions, Line}; use crate::error::surface::SurfaceFailure; -use crate::error::{ignore_status, ignored, Failure, Result, SolidError, SM_ERR_WARNING}; +use crate::error::{ignore_status, ignored, Failure, Result, SolidError, SM_ERR_NULL_POINTER}; use crate::geom::axis2_placement::Axis2Placement; use crate::geom::certified::{self, next_up}; use crate::geom::extent1d::Extent1d; @@ -138,12 +138,9 @@ impl PlaneIntersectWarning { #[must_use] pub fn into_error(self) -> SolidError { match self { - Self::NearlySingular => Failure::new( - OP_PP, - SurfaceFailure::Failed("the planes' line is nearly singular to solve for"), - ) - .with_status(SM_ERR_WARNING) - .into_error(), + Self::NearlySingular => { + SolidError::surface(OP_PP, SurfaceFailure::PlanesLineNearlySingular) + } } } } @@ -315,19 +312,26 @@ impl Plane { if !(s.x == 1.0 && s.y == 1.0 && s.z == 1.0) { // scaling must be isotropic if !are_same(s.x, s.y) || !are_same(s.x, s.z) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_XFORM, - "a plane cannot be scaled non-uniformly", + SurfaceFailure::ScaleNotUniform, )); } } } // check transformed m_pNurb for planarity and gen transformed Plane parameters - let nurb = self - .base - .gw_nurb() - .ok_or_else(|| SolidError::surface_missing(OP_XFORM, "the plane's NURB"))?; + let Some(nurb) = self.base.gw_nurb() else { + return Err(Failure::new( + OP_XFORM, + SurfaceFailure::Defect { + why: "the plane's NURB is missing after the B-spline transform above \ + succeeded, which fails without a NURB", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error()); + }; let bss = BSplineSurface::from_nurb(nurb.clone(), true); let mut plane = None; for tol_scale in [1.0, 10.0, 100.0, 1000.0] { @@ -338,7 +342,14 @@ impl Plane { } // Something wrong here if scaling a plane does not produce a plane. let plane = plane.ok_or_else(|| { - SolidError::surface_failed(OP_XFORM, "the transformed NURB is not a plane") + SolidError::surface( + OP_XFORM, + SurfaceFailure::Defect { + why: "the plane's bilinear NURB, moved rigidly and scaled uniformly (both \ + checked above), no longer tests as a plane at 1000 times the \ + tolerance, though such a transform keeps every net planar", + }, + ) })?; // save the transformed plane parameters @@ -365,9 +376,9 @@ impl Plane { // Make sure trim domain in inside of natural domain if !trim_domain.is_contained_by(&natural_domain, EFF_ZERO_PARAM) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_TRIM, - "the trim domain is not inside the natural domain", + SurfaceFailure::TrimDomainOutside, )); } @@ -603,8 +614,10 @@ impl Plane { // Force XAxis and YAxis to be nicely perpendicular if x_axis.dot(y_axis).abs() > EFF_ZERO { y_axis = x_axis.cross(y_axis).cross(x_axis); - y_axis = y_axis.unitize().map_err(|_| { - SolidError::surface_failed(OP_NURB, "the y axis is parallel to the x axis") + y_axis = y_axis.unitize().map_err(|e| { + Failure::new(OP_NURB, SurfaceFailure::NurbAxesParallel) + .caused_by(e) + .into_error() })?; } @@ -668,12 +681,18 @@ impl Plane { let b = self .base_with_nurb()? .calculate_bounding_box(k, uv_domain, &req)?; - let mut bbox = b - .normal - .ok_or_else(|| SolidError::surface_missing(OP_PP, "the plane's bounding box"))?; - let mut pbox = b - .pseudo - .ok_or_else(|| SolidError::surface_missing(OP_PP, "the plane's pseudo box"))?; + let (Some(mut bbox), Some(mut pbox)) = (b.normal, b.pseudo) else { + return Err(Failure::new( + OP_PP, + SurfaceFailure::Defect { + why: "the B-spline bounding box routine, asked for the normal and pseudo \ + boxes, returned without one, though it returns every box it is asked \ + for", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error()); + }; // When asked, expand the normal and pseudo boxes by the zone tolerance bbox.expand_absolute(zone_tolerance(owner)?); pbox.expand_absolute(zone_tolerance(owner)?); @@ -794,9 +813,9 @@ impl Plane { // Arrive here when planes are not parallel. // unitize lineDir and set OriginCross = avg(midPoint1, midPoint2) - let line_dir = line_dir.unitize().map_err(|_| { - SolidError::surface_failed(OP_PP, "the planes' line direction is the zero vector") - })?; + // (its squared length is at least 1e-16 past the parallel test above, so the unit + // vector exists) + let line_dir = line_dir.unitize_or_z(); let origin_cross = if this_bounded && !other_bounded { origin_this } else if !this_bounded && other_bounded { @@ -1774,6 +1793,7 @@ pub(crate) fn global_surface_intersect_into BSplineSurface { let dx = 40.0 * shear; @@ -1816,8 +1836,12 @@ mod tests { fn transform_rejects_anisotropic_scale() { let k = &Kernel::default(); let mut p = rect(); - let r = p.transform(k, &Axis2Placement::IDENTITY, Some(Vec3::new(2.0, 1.0, 1.0))); - assert!(r.is_err()); + let e = p + .transform(k, &Axis2Placement::IDENTITY, Some(Vec3::new(2.0, 1.0, 1.0))) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::ScaleNotUniform)); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); let mut q = rect(); q.transform(k, &Axis2Placement::IDENTITY, Some(Vec3::new(2.0, 2.0, 2.0))) .unwrap(); @@ -1828,9 +1852,14 @@ mod tests { #[test] fn trim_with_domain_checks_containment() { let mut p = rect(); - assert!(p + let e = p .trim_with_domain(&Extent2d::from_bounds(-6.0, 0.0, 1.0, 1.0)) - .is_err()); + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::TrimDomainOutside) + ); p.trim_with_domain(&Extent2d::from_bounds(-1.0, 0.0, 1.0, 1.0)) .unwrap(); assert_eq!( @@ -1979,7 +2008,10 @@ mod tests { assert_eq!(e.operation(), OP_PP); assert_eq!(e.entity(), None); assert_eq!(e.kind(), crate::error::ErrorKind::Failed); - assert!(e.surface_failure().is_some()); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::PlanesLineNearlySingular) + ); assert!(e.is_status_answer()); assert!(ignored(Err::<(), _>(e)).unwrap().is_none()); @@ -2340,29 +2372,33 @@ mod tests { assert!(Plane::read_from_db(&mut db, 30).is_err()); } - /// The plane's failures keep the operation, status, kind and entity of the bare - /// `Upstream` statuses they replace (a sample). + /// The plane's failures keep the operation, status and entity of the bare `Upstream` + /// statuses they replace (a sample); their class is the cause's. #[test] fn typed_surface_failures_keep_their_upstream_answers() { use crate::error::surface::SurfaceFailure; - let samples: [(&'static str, SurfaceFailure, i32); 3] = [ + use crate::error::ErrorKind; + let samples: [(&'static str, SurfaceFailure, i32, ErrorKind); 3] = [ ( OP_XFORM, - SurfaceFailure::Failed("a plane cannot be scaled non-uniformly"), + SurfaceFailure::ScaleNotUniform, crate::error::SM_ERR, + ErrorKind::InvalidInput, ), ( OP_PP, - SurfaceFailure::Missing("the plane's bounding box"), - crate::error::SM_ERR_NULL_POINTER, + SurfaceFailure::PlanesLineNearlySingular, + SM_ERR_WARNING, + ErrorKind::Failed, ), ( OP_TRIM, - SurfaceFailure::Failed("the trim domain is not inside the natural domain"), + SurfaceFailure::TrimDomainOutside, crate::error::SM_ERR, + ErrorKind::InvalidInput, ), ]; - for (operation, failure, code) in samples { + for (operation, failure, code, kind) in samples { let typed = SolidError::surface(operation, failure); let before = SolidError::Upstream { operation, @@ -2371,7 +2407,7 @@ mod tests { }; assert_eq!(typed.sm_status(), before.sm_status(), "{typed:?}"); assert_eq!(typed.upstream_status(), Some(code), "{typed:?}"); - assert_eq!(typed.kind(), before.kind(), "{typed:?}"); + assert_eq!(typed.kind(), kind, "{typed:?}"); assert_eq!(typed.operation(), before.operation(), "{typed:?}"); assert_eq!(typed.entity(), None, "{typed:?}"); assert_eq!(typed.is_port_failure(), before.is_port_failure()); From 703e4b11e56d1eb9d0dfe1b01becc66f405f5a81 Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:58:39 +0100 Subject: [PATCH 3/7] Typed errors (TE-O-06, part 1): a zero or non-finite plane scale is the caller's A uniform zero scale passed the uniformity check, collapsed the plane's net to a point and then failed the planarity test, which reported a kernel defect whose reason ("such a transform keeps every net planar") does not hold for it. It is now ScaleNotUniform, the caller's scale, with the same status 1001; a negative uniform scale still mirrors the plane. Co-Authored-By: Claude Code --- .../crates/solidcore/src/error/surface.rs | 5 +++-- .../crates/solidcore/src/surface/plane/p2.rs | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index ea80a3ee6e8..856ff018aea 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -271,8 +271,9 @@ pub enum SurfaceFailure { /// the kernel's zero), so it is not built. Upstream's warning: status /// `SM_ERR_WARNING`, class `failed`. PlanesLineNearlySingular, - /// The scale given is not uniform (or, where the routine says so, not positive): an - /// analytic surface takes only a uniform scale. [`ErrorKind::InvalidInput`]. + /// The scale given is not uniform (or, where the routine says so, not positive, or zero + /// or not finite): an analytic surface takes only a uniform scale that keeps it a + /// surface. [`ErrorKind::InvalidInput`]. ScaleNotUniform, /// The trim domain given is not inside the surface's natural domain. /// [`ErrorKind::InvalidInput`]. diff --git a/solidcore/crates/solidcore/src/surface/plane/p2.rs b/solidcore/crates/solidcore/src/surface/plane/p2.rs index 3561e9086bf..e58222ab45d 100644 --- a/solidcore/crates/solidcore/src/surface/plane/p2.rs +++ b/solidcore/crates/solidcore/src/surface/plane/p2.rs @@ -317,6 +317,14 @@ impl Plane { SurfaceFailure::ScaleNotUniform, )); } + // a zero scale collapses the plane to a point, a non-finite one leaves no + // net: the caller's scale, not a plane the transform lost + if s.x == 0.0 || !s.x.is_finite() { + return Err(SolidError::surface( + OP_XFORM, + SurfaceFailure::ScaleNotUniform, + )); + } } } @@ -345,9 +353,10 @@ impl Plane { SolidError::surface( OP_XFORM, SurfaceFailure::Defect { - why: "the plane's bilinear NURB, moved rigidly and scaled uniformly (both \ - checked above), no longer tests as a plane at 1000 times the \ - tolerance, though such a transform keeps every net planar", + why: "the plane's bilinear NURB, moved rigidly and scaled uniformly by a \ + finite, non-zero factor (both checked above), no longer tests as a \ + plane at 1000 times the tolerance, though such a transform keeps \ + every net planar", }, ) })?; @@ -1842,6 +1851,13 @@ mod tests { assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); assert_eq!(e.surface_failure(), Some(&SurfaceFailure::ScaleNotUniform)); assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); + // a uniform zero scale collapses the plane: the caller's scale too, not a defect + let e = rect() + .transform(k, &Axis2Placement::IDENTITY, Some(Vec3::ZERO)) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::ScaleNotUniform)); + assert!(!e.is_defect()); let mut q = rect(); q.transform(k, &Axis2Placement::IDENTITY, Some(Vec3::new(2.0, 2.0, 2.0))) .unwrap(); From 87026b87b9c0688da28bcd32a82263f3f75dd774 Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:06:53 +0100 Subject: [PATCH 4/7] Typed errors (TE-O-06, part 1): a plane scaled past the kernel's resolution is degenerate A finite uniform scale so small that the plane's tangents fall below the surface's derivative limit, or that their normal's square falls to the kernel's zero squared (or so large that the squares overflow), passed the scale checks, and then the planarity test, whose thresholds are absolute, failed at every tolerance: a defect whose reason does not hold. Below the derivative limit the NURB's evaluation itself refuses the tangents. It is now PlaneTangentUnmeasurable (class degenerate, status 1001), with the evaluation's error as its cause where there is one. The classes of part 1's named causes follow the rule upstream main now states in the module doc: a trim domain outside the natural domain and a domain past the apex are outside_domain, a singular line crossing and parallel NURB axes are degenerate. Statuses are unchanged. Co-Authored-By: Claude Code --- .../crates/solidcore/src/error/surface.rs | 52 ++++++++++----- .../crates/solidcore/src/surface/plane/p2.rs | 65 +++++++++++++++++-- 2 files changed, 97 insertions(+), 20 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index 856ff018aea..b9c04a996c7 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -33,9 +33,9 @@ //! the failure's `operation` path says which routine. A failure names an entity only where the //! site it replaces named one (a blend face whose net fails its re-certification). A check of a //! caller's argument that reports no status stays [`crate::SolidError::InvalidInput`]; one that -//! reports a status is a named cause of class `invalid_input`, keeping that status. Only part of -//! `crate::surface` uses these yet; the files that still build bare statuses are listed in this -//! module's ratchet test. +//! reports a status is a named cause of the class the paragraph above gives, keeping that +//! status. Only part of `crate::surface` uses these yet; the files that still build bare +//! statuses are listed in this module's ratchet test. use super::{ EntityRef, ErrorKind, Failure, Reason, SM_ERR, SM_ERR_INVALID_INPUT, @@ -265,7 +265,7 @@ pub enum SurfaceFailure { ZeroLineDirection, /// The line given is not parallel to the plane within the angle test, but the /// determinant of the crossing's linear solve is below the kernel's zero (a line - /// direction or plane scale too small to solve with). Class `failed`. + /// direction or plane scale too small to solve with). [`ErrorKind::Degenerate`]. LineCrossingSingular, /// The line where two planes meet is too nearly singular to solve for (a pivot below /// the kernel's zero), so it is not built. Upstream's warning: status @@ -276,11 +276,17 @@ pub enum SurfaceFailure { /// surface. [`ErrorKind::InvalidInput`]. ScaleNotUniform, /// The trim domain given is not inside the surface's natural domain. - /// [`ErrorKind::InvalidInput`]. + /// [`ErrorKind::OutsideDomain`]. TrimDomainOutside, /// The NURB tested as a plane has parallel u and v directions at its origin, so it has - /// no plane frame (the unitize's error is the cause). Class `failed`. + /// no plane frame (the unitize's error is the cause). [`ErrorKind::Degenerate`]. NurbAxesParallel, + /// The transformed plane's tangents cannot be measured: its NURB's evaluation cannot + /// make them non-zero (they are below the surface's derivative limit; its error is the + /// cause), or the squared length of a tangent or of their cross product is not above the + /// kernel's zero squared, or not finite. A uniform scale so small or so large takes the + /// plane outside what the kernel resolves. [`ErrorKind::Degenerate`]. + PlaneTangentUnmeasurable, /// The surface's NURB form could not be made (the make's failure is the cause, where it /// gave one). Class `failed`; whether it is a defect or a refusal is its cause's /// ([`crate::SolidError::is_defect`] follows the chain). @@ -294,7 +300,7 @@ pub enum SurfaceFailure { /// never read or set). [`ErrorKind::InvalidInput`]. SurfaceWithoutGenerator, /// The domain given reaches past the cone's apex, where its radius would be negative. - /// [`ErrorKind::InvalidInput`]. + /// [`ErrorKind::OutsideDomain`]. DomainPastApex, /// The iso curve asked for has zero length (its parameter range on the surface is /// degenerate). [`ErrorKind::InvalidInput`]. @@ -376,22 +382,26 @@ impl SurfaceFailure { | Self::OperationNotTaken | Self::ZeroLineDirection | Self::ScaleNotUniform - | Self::TrimDomainOutside | Self::RadiusNotPositive | Self::SurfaceWithoutGenerator - | Self::DomainPastApex | Self::DegenerateIsoCurve => ErrorKind::InvalidInput, Self::BoxOutsideBendHalfSpace { .. } | Self::BoxOutsideZeroPeriod { .. } | Self::PointOutsideVolumeDomain { .. } | Self::PointOutsideGivenDomain { .. } | Self::BoxOutsideParamDomain - | Self::PseudoBoxOutsideDomain => ErrorKind::OutsideDomain, + | Self::PseudoBoxOutsideDomain + | Self::TrimDomainOutside + | Self::DomainPastApex => ErrorKind::OutsideDomain, Self::IsoCurveKindNotLiftable | Self::DerivativeCombinationUnsupported { .. } | Self::PlainVolume | Self::BSplineVolumeUnsupported => ErrorKind::Unsupported, - Self::PointInBendSingularity | Self::ZeroVolumeTangent { .. } => ErrorKind::Degenerate, + Self::PointInBendSingularity + | Self::ZeroVolumeTangent { .. } + | Self::LineCrossingSingular + | Self::NurbAxesParallel + | Self::PlaneTangentUnmeasurable => ErrorKind::Degenerate, Self::SolveCacheNotMade | Self::BlendSurfaceNotMade | Self::MergedRangeReversed { .. } @@ -551,6 +561,9 @@ impl core::fmt::Display for SurfaceFailure { Self::NurbAxesParallel => { f.write_str("the NURB's u and v directions are parallel at its origin") } + Self::PlaneTangentUnmeasurable => { + f.write_str("the transformed plane's tangents are outside what the kernel resolves") + } Self::NurbNotMade => f.write_str("the surface's NURB form could not be made"), Self::NurbEvaluationFailed => { f.write_str("the evaluation of the surface's NURB form failed") @@ -934,7 +947,7 @@ mod tests { ( SurfaceFailure::LineCrossingSingular, SM_ERR, - ErrorKind::Failed, + ErrorKind::Degenerate, ), ( SurfaceFailure::PlanesLineNearlySingular, @@ -949,9 +962,18 @@ mod tests { ( SurfaceFailure::TrimDomainOutside, SM_ERR, - ErrorKind::InvalidInput, + ErrorKind::OutsideDomain, + ), + ( + SurfaceFailure::NurbAxesParallel, + SM_ERR, + ErrorKind::Degenerate, + ), + ( + SurfaceFailure::PlaneTangentUnmeasurable, + SM_ERR, + ErrorKind::Degenerate, ), - (SurfaceFailure::NurbAxesParallel, SM_ERR, ErrorKind::Failed), (SurfaceFailure::NurbNotMade, SM_ERR, ErrorKind::Failed), ( SurfaceFailure::NurbEvaluationFailed, @@ -971,7 +993,7 @@ mod tests { ( SurfaceFailure::DomainPastApex, SM_ERR_INVALID_INPUT, - ErrorKind::InvalidInput, + ErrorKind::OutsideDomain, ), ( SurfaceFailure::DerivativeOrderTooHigh { max: 3 }, diff --git a/solidcore/crates/solidcore/src/surface/plane/p2.rs b/solidcore/crates/solidcore/src/surface/plane/p2.rs index e58222ab45d..b47f7977611 100644 --- a/solidcore/crates/solidcore/src/surface/plane/p2.rs +++ b/solidcore/crates/solidcore/src/surface/plane/p2.rs @@ -98,7 +98,7 @@ use crate::geom::tol::{are_same, is_zero, is_zero_to_tol, within_contact_tol, ZO use crate::geom::vector2d::{Point2, Vec2}; use crate::geom::vector3d::{sqrt, Point3, Vec3}; use crate::kernel::Kernel; -use crate::math::{max2, min2, EFF_ZERO, EFF_ZERO_PARAM, EFF_ZERO_SQRT, PI}; +use crate::math::{max2, min2, EFF_ZERO, EFF_ZERO_PARAM, EFF_ZERO_SQ, EFF_ZERO_SQRT, PI}; use crate::nurbs::b_spline_curve::BSplineCurve; use crate::nurbs::b_spline_curve_n_lib::TrimMode; use crate::nurbs::b_spline_surface::p2::EditNotify; @@ -348,15 +348,38 @@ impl Plane { break; } } + // The test evaluates the tangents (the evaluation refuses them below the surface's + // derivative limit), measures them and their cross product (the normal) only above + // the kernel's zero squared, and a square that overflows has no angle: a scale that + // takes them there leaves a plane the test cannot see, which is the scale's, not the + // kernel's. + if plane.is_none() { + let at = bss.natural_uv_domain().evaluate(0.0, 0.0); + let unmeasurable = Failure::new(OP_XFORM, SurfaceFailure::PlaneTangentUnmeasurable); + match evaluate_1st_derivatives(k, &bss, at, Side::Left, Side::Left) { + Ok((_, du, dv)) => { + if [du, dv, du.cross(dv)].iter().any(|d| { + let length_squared = d.length_squared(); + !(length_squared > EFF_ZERO_SQ && length_squared.is_finite()) + }) { + return Err(unmeasurable.into_error()); + } + } + Err(e) if e.is_status_answer() => { + return Err(unmeasurable.caused_by(e).into_error()); + } + Err(e) => return Err(e), + } + } // Something wrong here if scaling a plane does not produce a plane. let plane = plane.ok_or_else(|| { SolidError::surface( OP_XFORM, SurfaceFailure::Defect { why: "the plane's bilinear NURB, moved rigidly and scaled uniformly by a \ - finite, non-zero factor (both checked above), no longer tests as a \ - plane at 1000 times the tolerance, though such a transform keeps \ - every net planar", + finite, non-zero factor, with tangents the test measures (all \ + checked above), no longer tests as a plane at 1000 times the \ + tolerance, though such a transform keeps every net planar", }, ) })?; @@ -1858,6 +1881,38 @@ mod tests { assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); assert_eq!(e.surface_failure(), Some(&SurfaceFailure::ScaleNotUniform)); assert!(!e.is_defect()); + // a uniform scale that takes the tangents below the surface's derivative limit (the + // evaluation's error is the cause) or past the largest square leaves a plane the + // planarity test cannot measure: degenerate, not a defect + for factor in [1e-13, 1e-9, 1e-200, 1e200] { + let e = rect() + .transform( + k, + &Axis2Placement::IDENTITY, + Some(Vec3::new(factor, factor, factor)), + ) + .unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR), "{factor:e}"); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::PlaneTangentUnmeasurable), + "{factor:e}: {e:?}" + ); + // the evaluation's refusal of tangents below the derivative limit is the cause; + // tangents of 1e-9 evaluate but their normal's square is below the kernel's zero + // squared, and huge ones evaluate with squares that overflow + assert_eq!(e.cause().is_some(), factor < 1e-10, "{factor:e}: {e:?}"); + assert_eq!(e.kind(), crate::error::ErrorKind::Degenerate, "{factor:e}"); + assert!(!e.is_defect(), "{factor:e}"); + } + // tangents of 1e-5, whose normal's square is 1e-20, still scale + let mut q = rect(); + q.transform( + k, + &Axis2Placement::IDENTITY, + Some(Vec3::new(1e-5, 1e-5, 1e-5)), + ) + .unwrap(); let mut q = rect(); q.transform(k, &Axis2Placement::IDENTITY, Some(Vec3::new(2.0, 2.0, 2.0))) .unwrap(); @@ -2411,7 +2466,7 @@ mod tests { OP_TRIM, SurfaceFailure::TrimDomainOutside, crate::error::SM_ERR, - ErrorKind::InvalidInput, + ErrorKind::OutsideDomain, ), ]; for (operation, failure, code, kind) in samples { From 26f1180619b03aac5fe04e23da23d3f4b1901116 Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:08:13 +0100 Subject: [PATCH 5/7] Typed errors (TE-O-06, part 1): a zero line direction is #20747's LineDirectionZero #20747 on main named the same condition (the line direction given is the zero vector, status SM_ERR_INVALID_INPUT, class invalid_input) for the surface solvers. The plane's line crossing now uses it, and part 1's ZeroLineDirection is gone. The module table's row for the volume surfaces ends at CallFailed, since the analytic and swept surfaces' causes follow. Co-Authored-By: Claude Code --- solidcore/crates/solidcore/src/error/surface.rs | 12 +----------- solidcore/crates/solidcore/src/surface/plane/p1.rs | 4 ++-- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index b9c04a996c7..5e5e98b7649 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -15,7 +15,7 @@ //! | [`SurfaceFailure::OffSurface`] | `SM_ERR_NOT_WITHIN_TOLERANCE` | //! | [`SurfaceFailure::Defect`] | `SM_ERR` (or the site's, through `with_status`) | //! | the named causes of the surface solvers, [`SurfaceFailure::SolverWithout`] to [`SurfaceFailure::NothingLifted`] | the status of the phrase each replaced (`comparison_status`), or the site's through `with_status` | -//! | the named causes of the volume surfaces and the implicit checks, from [`SurfaceFailure::TooFewKnots`] on | `SM_ERR`, except [`SurfaceFailure::NoNurb`]: `SM_ERR_NULL_POINTER` (a site that reported `SM_ERR` keeps it through `with_status`) | +//! | the named causes of the volume surfaces and the implicit checks, [`SurfaceFailure::TooFewKnots`] to [`SurfaceFailure::CallFailed`] | `SM_ERR`, except [`SurfaceFailure::NoNurb`]: `SM_ERR_NULL_POINTER` (a site that reported `SM_ERR` keeps it through `with_status`) | //! | the named causes of the analytic and swept surfaces, from [`SurfaceFailure::OperationNotTaken`] on | the status of the phrase each replaced (`comparison_status`), or the site's through `with_status` | //! //! [`SurfaceFailure::Defect`] is a failure reviewed at its site as the kernel's fault, with its @@ -261,8 +261,6 @@ pub enum SurfaceFailure { /// The solve operation asked for is not one the routine takes (a plane's fast drop /// takes minimize, normalize and intersect). [`ErrorKind::InvalidInput`]. OperationNotTaken, - /// The direction of the line given is the zero vector. [`ErrorKind::InvalidInput`]. - ZeroLineDirection, /// The line given is not parallel to the plane within the angle test, but the /// determinant of the crossing's linear solve is below the kernel's zero (a line /// direction or plane scale too small to solve with). [`ErrorKind::Degenerate`]. @@ -348,7 +346,6 @@ impl SurfaceFailure { | Self::NoPolarConverter => SM_ERR_NULL_POINTER, Self::InvalidValue(_) | Self::LineDirectionZero - | Self::ZeroLineDirection | Self::DomainPastApex | Self::DegenerateIsoCurve => SM_ERR_INVALID_INPUT, Self::OutsideDomain(_) => SM_ERR_OUTSIDE_OF_DOMAIN, @@ -380,7 +377,6 @@ impl SurfaceFailure { | Self::CurveWithoutNurb | Self::CurveWithoutControlPoints | Self::OperationNotTaken - | Self::ZeroLineDirection | Self::ScaleNotUniform | Self::RadiusNotPositive | Self::SurfaceWithoutGenerator @@ -547,7 +543,6 @@ impl core::fmt::Display for SurfaceFailure { Self::OperationNotTaken => { f.write_str("the solve operation is not one the routine takes") } - Self::ZeroLineDirection => f.write_str("the line direction is the zero vector"), Self::LineCrossingSingular => { f.write_str("the line's crossing with the plane is singular to solve for") } @@ -939,11 +934,6 @@ mod tests { SM_ERR, ErrorKind::InvalidInput, ), - ( - SurfaceFailure::ZeroLineDirection, - SM_ERR_INVALID_INPUT, - ErrorKind::InvalidInput, - ), ( SurfaceFailure::LineCrossingSingular, SM_ERR, diff --git a/solidcore/crates/solidcore/src/surface/plane/p1.rs b/solidcore/crates/solidcore/src/surface/plane/p1.rs index e1d9b36a613..cd27bd961d9 100644 --- a/solidcore/crates/solidcore/src/surface/plane/p1.rs +++ b/solidcore/crates/solidcore/src/surface/plane/p1.rs @@ -1193,7 +1193,7 @@ impl Plane { if line_vector.length_squared() < EFF_ZERO_SQ { return Err(SolidError::surface( OP_LINE, - SurfaceFailure::ZeroLineDirection, + SurfaceFailure::LineDirectionZero, )); } @@ -2635,7 +2635,7 @@ mod tests { assert_eq!(e.sm_status(), Some(SM_ERR_INVALID_INPUT)); assert_eq!( e.surface_failure(), - Some(&SurfaceFailure::ZeroLineDirection) + Some(&SurfaceFailure::LineDirectionZero) ); assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput); } From 7ad6d8d77788c5811ae573db160d5060a3334bf9 Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:41:23 +0100 Subject: [PATCH 6/7] Typed errors (TE-O-06, part 2): the cone rebuild's failures become named causes or reviewed defects Co-Authored-By: Claude Code --- .../crates/solidcore/src/error/surface.rs | 96 ++++++++++- .../crates/solidcore/src/surface/cone/p2.rs | 155 +++++++++++++----- 2 files changed, 211 insertions(+), 40 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index 5e5e98b7649..ec3be99e8d5 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -307,6 +307,65 @@ pub enum SurfaceFailure { /// made or read, or both sweep iso curves were degenerate when it was, which the make /// does not report). Class `failed`. NoPolarConverter, + /// The surface is not a cylinder with a radius above the kernel's zero, which the + /// routine takes. [`ErrorKind::Unsupported`]. + NotACylinder, + /// The cone the routine would build passes its apex within the surface's span: its + /// radius at a control point of the net is not above the kernel's zero. Class `failed`. + ConeClosesWithinSpan, + /// The NURB is not of the analytic kind the routine tests it for or rebuilds from it + /// (an edited NURB that is no longer its surface's kind, or a NURB recognised as one): + /// `check` says which test it failed. [`ErrorKind::Unsupported`]. + NurbNotAnalytic { + /// The test of the analytic kind the NURB failed. + check: AnalyticCheck, + }, +} + +/// The test of an analytic kind a NURB failed ([`SurfaceFailure::NurbNotAnalytic`]). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum AnalyticCheck { + /// Its degrees and rationality are not the kind's (a cone's are 1 by 2, rational). + DegreeAndRationality, + /// Its iso curves at the two ends are not circular arcs. + EndsNotCircular, + /// Its end arcs' start or end angles differ by more than the tolerance. + EndArcsTwisted, + /// The distance between its end arcs' centres is degenerate. + HeightDegenerate, + /// The plane of its bottom arc is not perpendicular to the axis. + BottomNotSquareToAxis, + /// The plane of its top arc is not perpendicular to the axis. + TopNotSquareToAxis, + /// It is a cylinder's, and its end radii differ by more than the cylinder's tolerance. + RadiiDiffer, + /// Moved onto the mean-radius cylinder, its net would have a radius not above zero at + /// a control point. + NetRadiusNotPositive, + /// Moved onto the mean-radius cylinder, its net is not on that cylinder (its end + /// circles are turned against each other or off the axis). + NetOffCylinder, +} + +impl core::fmt::Display for AnalyticCheck { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.write_str(match self { + Self::DegreeAndRationality => "its degrees or rationality are not the kind's", + Self::EndsNotCircular => "its end iso curves are not circular", + Self::EndArcsTwisted => "its end arcs' angles differ by more than the tolerance", + Self::HeightDegenerate => "its height is degenerate", + Self::BottomNotSquareToAxis => { + "its bottom arc's plane is not perpendicular to the axis" + } + Self::TopNotSquareToAxis => "its top arc's plane is not perpendicular to the axis", + Self::RadiiDiffer => "its top and bottom radii differ", + Self::NetRadiusNotPositive => { + "its net has no positive radius at a control point on the mean-radius cylinder" + } + Self::NetOffCylinder => "its moved net is not on the mean-radius cylinder", + }) + } } /// What a solver lacks ([`SurfaceFailure::SolverWithout`]). @@ -392,7 +451,9 @@ impl SurfaceFailure { Self::IsoCurveKindNotLiftable | Self::DerivativeCombinationUnsupported { .. } | Self::PlainVolume - | Self::BSplineVolumeUnsupported => ErrorKind::Unsupported, + | Self::BSplineVolumeUnsupported + | Self::NotACylinder + | Self::NurbNotAnalytic { .. } => ErrorKind::Unsupported, Self::PointInBendSingularity | Self::ZeroVolumeTangent { .. } | Self::LineCrossingSingular @@ -570,6 +631,15 @@ impl core::fmt::Display for SurfaceFailure { Self::NoPolarConverter => { f.write_str("the surface of revolution has no polar converter circle") } + Self::NotACylinder => { + f.write_str("the surface is not a cylinder with a positive radius") + } + Self::ConeClosesWithinSpan => { + f.write_str("the cone would pass its apex within the surface's span") + } + Self::NurbNotAnalytic { check } => { + write!(f, "the NURB is not of the analytic kind: {check}") + } } } } @@ -1000,6 +1070,19 @@ mod tests { SM_ERR_NULL_POINTER, ErrorKind::Failed, ), + (SurfaceFailure::NotACylinder, SM_ERR, ErrorKind::Unsupported), + ( + SurfaceFailure::ConeClosesWithinSpan, + SM_ERR, + ErrorKind::Failed, + ), + ( + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::EndArcsTwisted, + }, + SM_ERR, + ErrorKind::Unsupported, + ), ]; for (failure, status, kind) in cases { let e = SolidError::surface(op, failure); @@ -1014,6 +1097,16 @@ mod tests { assert_eq!(e.surface_failure(), Some(&failure)); assert!(!e.is_defect() && !e.is_refusal(), "{failure:?}"); } + assert_eq!( + SolidError::surface( + op, + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::HeightDegenerate + } + ) + .to_string(), + "surface::op: the NURB is not of the analytic kind: its height is degenerate" + ); // a NURB not made keeps the site's status, and its make's defect decides the class let made = Err(SolidError::surface(op, SurfaceFailure::Defect { why: "x" })); let e = SolidError::surface_nurb_not_made(op, SM_ERR_NULL_POINTER, made); @@ -1082,7 +1175,6 @@ mod tests { #[rustfmt::skip] // one file per line, so conversions in parallel merge cleanly const PINNED: &[(&str, usize)] = &[ ("solidcore/src/surface/ball_patch.rs", 8), - ("solidcore/src/surface/cone/p2.rs", 14), ("solidcore/src/surface/cone/p3.rs", 3), ("solidcore/src/surface/extend.rs", 2), ("solidcore/src/surface/extend/analytic.rs", 25), diff --git a/solidcore/crates/solidcore/src/surface/cone/p2.rs b/solidcore/crates/solidcore/src/surface/cone/p2.rs index 8ebd2bfd61f..f814b9cb1b0 100644 --- a/solidcore/crates/solidcore/src/surface/cone/p2.rs +++ b/solidcore/crates/solidcore/src/surface/cone/p2.rs @@ -96,7 +96,8 @@ use super::Cone; use crate::curve::curve::Curve; use crate::curve::ellipse::{Ellipse, EllipseOptions}; use crate::curve::line::{BoundedLineOptions, Line}; -use crate::error::{ignore_status, ignored, Result, SolidError}; +use crate::error::surface::{AnalyticCheck, SurfaceFailure}; +use crate::error::{ignore_status, ignored, Failure, Result, SolidError, SM_ERR_NULL_POINTER}; use crate::geom::axis2_placement::Axis2Placement; use crate::geom::extent1d::Extent1d; use crate::geom::extent2d::Extent2d; @@ -579,17 +580,12 @@ impl Cone { /// `SM_ERR` unless this is a cylinder with a NURB whose radius stays positive; those of /// the control point edits and of the rebuild. pub fn lean_cylinder(&self, k: &Kernel, slope: f64, neutral_height: f64) -> Result { - let err = || { - SolidError::surface_failed( - "surface::cone::p2::Cone::lean_cylinder", - "the surface is not a cylinder with a NURB whose radius stays positive", - ) - }; + const OP_LEAN: &str = "surface::cone::p2::Cone::lean_cylinder"; let canonical = self.canonical(); if !self.is_cylinder() || canonical.radius.partial_cmp(&EFF_ZERO) != Some(core::cmp::Ordering::Greater) { - return Err(err()); + return Err(SolidError::surface(OP_LEAN, SurfaceFailure::NotACylinder)); } let origin = canonical.origin.origin(); let axis = canonical.origin.x_axis().cross(canonical.origin.y_axis()); @@ -614,7 +610,10 @@ impl Cone { let height = d.dot(axis); let radius = canonical.radius + slope * (height - neutral_height); if radius.partial_cmp(&EFF_ZERO) != Some(core::cmp::Ordering::Greater) { - return Err(err()); + return Err(SolidError::surface( + OP_LEAN, + SurfaceFailure::ConeClosesWithinSpan, + )); } let moved = origin + axis * height + (d - axis * height) * (radius / canonical.radius); @@ -649,11 +648,26 @@ impl Cone { /// [`crate::surface::surf_of_revolution::SurfOfRevolution::reparameterize`]. pub fn reparameterize(&mut self, k: &Kernel, trim_domain: &Extent2d) -> Result<()> { // GetOrCreateGwNurbPointer (the virtual MakeNurb with the cone's iso curves) + let mut made = Ok(()); if self.rev.base.gw_nurb().is_none() { - ignore_status(self.make_nurb(k))?; + made = self.make_nurb(k); + ignore_status(made.clone())?; } if self.rev.base.gw_nurb().is_none() { - return Err(SolidError::surface_missing(OP_REPARAM, "the cone's NURB")); + return Err(match made { + Err(e) => { + SolidError::surface_nurb_not_made(OP_REPARAM, SM_ERR_NULL_POINTER, Err(e)) + } + Ok(()) => Failure::new( + OP_REPARAM, + SurfaceFailure::Defect { + why: "the NURB is missing after make_nurb returned success, which on \ + success sets the NURB", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error(), + }); } let inside_out = self.inside_out; self.rev @@ -860,7 +874,15 @@ impl Cone { tgt_nurb_domain.v_interval() }; let gen = off_cone.rev.gen_curve.as_mut().ok_or_else(|| { - SolidError::surface_missing(OP_OFFSET, "the offset cone's generator curve") + Failure::new( + OP_OFFSET, + SurfaceFailure::Defect { + why: "the offset cone has no generator, though create_canonical \ + built it above with its generator line", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error() })?; ignore_status(gen.edit_parameterization(k, &tgt_gen_nurb_ivl))?; @@ -958,8 +980,9 @@ impl Cone { // translate the NewCone->GenCurve from current center line (status ignored) let mut mv = Axis2Placement::IDENTITY; mv.translate(off_vec); + // (the offset cone is a copy of this one: its generator is this cone's) let gen = off_cone.rev.gen_curve.as_mut().ok_or_else(|| { - SolidError::surface_missing(OP_OFFSET, "the offset cone's generator curve") + SolidError::surface(OP_OFFSET, SurfaceFailure::SurfaceWithoutGenerator) })?; ignore_status(gen.transform(k, &mv, None))?; @@ -1210,8 +1233,8 @@ impl Cone { /// # Errors /// /// Upstream's `SER` statuses (returned as a `TRUE` result without cylinder): those - /// of the iso curves; those of the degeneracy and arc tests; - /// `SM_ERR_NULL_POINTER` for a surface without NURB (upstream copies the null NURB). + /// of the iso curves; those of the degeneracy and arc tests. A surface without NURB + /// is not a cylinder (`Ok(None)`). pub fn is_nurb_surface_cylinder( k: &Kernel, test_surface: &BSplineSurface, @@ -1449,7 +1472,15 @@ impl Cone { // the cylinder's NURB is a copy of the tested one let nurb = test_surface.gw_nurb().ok_or_else(|| { - SolidError::surface_missing(OP_NURB_CYLINDER, "the tested surface's NURB") + Failure::new( + OP_NURB_CYLINDER, + SurfaceFailure::Defect { + why: "the tested surface has no NURB, though its degrees summed to 3 \ + above, which a surface without NURB cannot", + }, + ) + .with_status(SM_ERR_NULL_POINTER) + .into_error() })?; cylinder.rev.base.nurb = Some(copy_nurb_surface(nurb)).into(); cylinder.rev.base.nurb_is_borrowed = false; @@ -1505,9 +1536,11 @@ impl Cone { let h = d.dot(z); let here = bot_radius + (top_radius - bot_radius) * (h / height); if !(here > 0.0 && here.is_finite()) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the cylinder's net has no positive radius at a control point", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::NetRadiusNotPositive, + }, )); } moved.push((i, j, centre + z * h + (d - z * h) * (radius / here), w)); @@ -1548,10 +1581,11 @@ impl Cone { tan_angle: 0.0, }; if !implicit::lies_on(&base, &cylinder)? { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the cylinder's moved net is not on the mean-radius cylinder (its end \ - circles are turned against each other or off the axis)", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::NetOffCylinder, + }, )); } Ok(base) @@ -1594,9 +1628,11 @@ impl Cone { || linear_iso_param == SurfParamType::Neither { // Modified Nurb SmCone: no longer has degree 1 and 2 basis functions - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the NURB is no longer of degree 1 by 2 and rational", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::DegreeAndRationality, + }, )); } @@ -1643,18 +1679,22 @@ impl Cone { )? else { // Modified Nurb SmCone: no longer has circular top and bottom IsoParamCurves - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the NURB's top and bottom iso curves are not circular", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::EndsNotCircular, + }, )); }; // not a cone - top/bot start angles or top/bot end angles vary by more than // tolerance if twisted(&top, &bot, ang_tol)? { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the top and bottom arcs' angles differ by more than the tolerance", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::EndArcsTwisted, + }, )); } @@ -1667,18 +1707,22 @@ impl Cone { // not a cone - height of cone is degenerate (cone is a disk) if height < Self::MIN_HEIGHT { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the cone's height is degenerate", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::HeightDegenerate, + }, )); } nurb_z_axis = nurb_z_axis.unitize_or_z(); // not a cone - bottom arc's plane is not perpendicular to cone axis if !nurb_z_axis.is_parallel_to(bot.frame.z_axis(), ang_tol) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the bottom arc's plane is not perpendicular to the axis", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::BottomNotSquareToAxis, + }, )); } // Not upstream (#16982 review): nor the top arc's. `twisted` compares only the @@ -1688,9 +1732,11 @@ impl Cone { // circle's tilt from the centre line is below `ang_tol` degrees (the far circle's // from the recorded axis, below `2 ang_tol`) if !nurb_z_axis.is_parallel_to(top.frame.z_axis(), ang_tol) { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the top arc's plane is not perpendicular to the axis", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::TopNotSquareToAxis, + }, )); } @@ -1719,9 +1765,11 @@ impl Cone { bot.radius = mean; top.radius = mean; } else { - return Err(SolidError::surface_failed( + return Err(SolidError::surface( OP_REBUILD, - "the cylinder's top and bottom radii differ", + SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::RadiiDiffer, + }, )); } } @@ -2773,7 +2821,13 @@ mod tests { let top = radius + sign * EFF_ZERO_SQRT * (1.0 + radius); let mut c = edited_cylinder(&k, radius, top, swap, inside); let before = c.clone(); - assert!(c.rebuild_step_from_nurb_parameters(&k).is_err()); + let e = c.rebuild_step_from_nurb_parameters(&k).unwrap_err(); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::RadiiDiffer + }) + ); assert_eq!(c.base_radius(), before.base_radius()); assert_eq!(c.semi_angle_deg(), before.semi_angle_deg()); assert_eq!(c.rev().position(), before.rev().position()); @@ -2814,6 +2868,21 @@ mod tests { let before = c.clone(); let result = c.rebuild_step_from_nurb_parameters(&k); assert_eq!(result.is_err(), refused, "{tilt_deg} {swap} {inside}"); + // the tilted row is the NURB's last; with swap and inside the rebuild + // reads it as its bottom or its top arc, so one of the two + // square-to-axis checks refuses it + if let Err(e) = &result { + assert!( + matches!( + e.surface_failure(), + Some(&SurfaceFailure::NurbNotAnalytic { + check: AnalyticCheck::BottomNotSquareToAxis + | AnalyticCheck::TopNotSquareToAxis + }) + ), + "{swap} {inside}: {e}" + ); + } if refused { assert_eq!(c.base_radius(), before.base_radius()); assert_eq!(c.semi_angle_deg(), before.semi_angle_deg()); @@ -3353,8 +3422,18 @@ mod tests { assert!(d_old.cross(d_new).length() < 1e-9 * 25.0, "{uv:?}"); } } - // a radius that would go through the axis is refused - assert!(cylinder.lean_cylinder(&k, 2.0, 4.0).is_err()); + // a radius that would go through the axis is refused, and so is a surface that is + // not a cylinder + let e = cylinder.lean_cylinder(&k, 2.0, 4.0).unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::ConeClosesWithinSpan) + ); + let e = cone.lean_cylinder(&k, slope, 4.0).unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); + assert_eq!(e.surface_failure(), Some(&SurfaceFailure::NotACylinder)); + assert_eq!(e.kind(), crate::error::ErrorKind::Unsupported); } /// Two end arcs of a short cylinder differ by two ulps at one control point. Their From 33247785280d2ae3e6f91fea9f28389a88838b4c Mon Sep 17 00:00:00 2001 From: Markus Reichart <52602624+MarkusReichart@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:08:40 +0100 Subject: [PATCH 7/7] Typed errors (TE-O-06, part 2): a non-finite lean is the caller's A NaN or infinite slope or neutral height made every radius of the lean NaN, so the positive-radius test failed and the lean reported ConeClosesWithinSpan, a cone through its axis that it never found. It is now LeanNotFinite (class invalid_input, status 1001), as is a finite pair whose radius law overflows. ConeClosesWithinSpan is class degenerate, as the module doc's class rule on main now says for degenerate geometry. Co-Authored-By: Claude Code --- .../crates/solidcore/src/error/surface.rs | 22 ++++++++++--- .../crates/solidcore/src/surface/cone/p2.rs | 32 +++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/solidcore/crates/solidcore/src/error/surface.rs b/solidcore/crates/solidcore/src/error/surface.rs index ec3be99e8d5..dcd56f6dd59 100644 --- a/solidcore/crates/solidcore/src/error/surface.rs +++ b/solidcore/crates/solidcore/src/error/surface.rs @@ -311,8 +311,12 @@ pub enum SurfaceFailure { /// routine takes. [`ErrorKind::Unsupported`]. NotACylinder, /// The cone the routine would build passes its apex within the surface's span: its - /// radius at a control point of the net is not above the kernel's zero. Class `failed`. + /// radius at a control point of the net is not above the kernel's zero. + /// [`ErrorKind::Degenerate`]. ConeClosesWithinSpan, + /// The lean's slope or neutral height is NaN or infinite, or the radius law they give + /// overflows at a control point of the net. [`ErrorKind::InvalidInput`]. + LeanNotFinite, /// The NURB is not of the analytic kind the routine tests it for or rebuilds from it /// (an edited NURB that is no longer its surface's kind, or a NURB recognised as one): /// `check` says which test it failed. [`ErrorKind::Unsupported`]. @@ -439,7 +443,8 @@ impl SurfaceFailure { | Self::ScaleNotUniform | Self::RadiusNotPositive | Self::SurfaceWithoutGenerator - | Self::DegenerateIsoCurve => ErrorKind::InvalidInput, + | Self::DegenerateIsoCurve + | Self::LeanNotFinite => ErrorKind::InvalidInput, Self::BoxOutsideBendHalfSpace { .. } | Self::BoxOutsideZeroPeriod { .. } | Self::PointOutsideVolumeDomain { .. } @@ -458,7 +463,8 @@ impl SurfaceFailure { | Self::ZeroVolumeTangent { .. } | Self::LineCrossingSingular | Self::NurbAxesParallel - | Self::PlaneTangentUnmeasurable => ErrorKind::Degenerate, + | Self::PlaneTangentUnmeasurable + | Self::ConeClosesWithinSpan => ErrorKind::Degenerate, Self::SolveCacheNotMade | Self::BlendSurfaceNotMade | Self::MergedRangeReversed { .. } @@ -637,6 +643,9 @@ impl core::fmt::Display for SurfaceFailure { Self::ConeClosesWithinSpan => { f.write_str("the cone would pass its apex within the surface's span") } + Self::LeanNotFinite => { + f.write_str("the lean's slope, neutral height or radius is not finite") + } Self::NurbNotAnalytic { check } => { write!(f, "the NURB is not of the analytic kind: {check}") } @@ -1074,7 +1083,12 @@ mod tests { ( SurfaceFailure::ConeClosesWithinSpan, SM_ERR, - ErrorKind::Failed, + ErrorKind::Degenerate, + ), + ( + SurfaceFailure::LeanNotFinite, + SM_ERR, + ErrorKind::InvalidInput, ), ( SurfaceFailure::NurbNotAnalytic { diff --git a/solidcore/crates/solidcore/src/surface/cone/p2.rs b/solidcore/crates/solidcore/src/surface/cone/p2.rs index f814b9cb1b0..c6be5b6a6a0 100644 --- a/solidcore/crates/solidcore/src/surface/cone/p2.rs +++ b/solidcore/crates/solidcore/src/surface/cone/p2.rs @@ -577,8 +577,9 @@ impl Cone { /// /// # Errors /// - /// `SM_ERR` unless this is a cylinder with a NURB whose radius stays positive; those of - /// the control point edits and of the rebuild. + /// `SM_ERR` unless this is a cylinder with a NURB whose radius stays positive and finite + /// under a finite `slope` and `neutral_height`; those of the control point edits and of + /// the rebuild. pub fn lean_cylinder(&self, k: &Kernel, slope: f64, neutral_height: f64) -> Result { const OP_LEAN: &str = "surface::cone::p2::Cone::lean_cylinder"; let canonical = self.canonical(); @@ -587,6 +588,10 @@ impl Cone { { return Err(SolidError::surface(OP_LEAN, SurfaceFailure::NotACylinder)); } + // a NaN or infinite radius law is the caller's, not a cone that crosses its axis + if !(slope.is_finite() && neutral_height.is_finite()) { + return Err(SolidError::surface(OP_LEAN, SurfaceFailure::LeanNotFinite)); + } let origin = canonical.origin.origin(); let axis = canonical.origin.x_axis().cross(canonical.origin.y_axis()); let axis = axis / axis.length(); @@ -609,6 +614,10 @@ impl Cone { let d = p - origin; let height = d.dot(axis); let radius = canonical.radius + slope * (height - neutral_height); + // finite factors whose product overflows + if !radius.is_finite() { + return Err(SolidError::surface(OP_LEAN, SurfaceFailure::LeanNotFinite)); + } if radius.partial_cmp(&EFF_ZERO) != Some(core::cmp::Ordering::Greater) { return Err(SolidError::surface( OP_LEAN, @@ -3430,6 +3439,25 @@ mod tests { e.surface_failure(), Some(&SurfaceFailure::ConeClosesWithinSpan) ); + assert_eq!(e.kind(), crate::error::ErrorKind::Degenerate); + // a NaN or infinite slope or neutral height, or a finite pair whose radius law + // overflows, is the caller's: invalid input, not a cone through the axis + for (s, h) in [ + (f64::NAN, 4.0), + (slope, f64::NAN), + (f64::INFINITY, 4.0), + (slope, f64::NEG_INFINITY), + (1e308, -1e308), + ] { + let e = cylinder.lean_cylinder(&k, s, h).unwrap_err(); + assert_eq!(e.sm_status(), Some(crate::error::SM_ERR), "{s} {h}"); + assert_eq!( + e.surface_failure(), + Some(&SurfaceFailure::LeanNotFinite), + "{s} {h}" + ); + assert_eq!(e.kind(), crate::error::ErrorKind::InvalidInput, "{s} {h}"); + } let e = cone.lean_cylinder(&k, slope, 4.0).unwrap_err(); assert_eq!(e.sm_status(), Some(crate::error::SM_ERR)); assert_eq!(e.surface_failure(), Some(&SurfaceFailure::NotACylinder));