diff --git a/CHANGELOG.md b/CHANGELOG.md index b49a9851..d54a27fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Significantly improved the general stability of mesh/mesh intersection calculation. - Rename `Shape::clone_box` to `Shape::clone_dyn` (the `clone_box` method still exists but has been deprecated). +- Make `try_convex_hull` return an error instead of panicking if less than 3 input points are given. ## v0.16.1 diff --git a/crates/parry3d-f64/Cargo.toml b/crates/parry3d-f64/Cargo.toml index edb6dd94..cef9c114 100644 --- a/crates/parry3d-f64/Cargo.toml +++ b/crates/parry3d-f64/Cargo.toml @@ -57,7 +57,7 @@ num-derive = "0.4" indexmap = { version = "2", features = ["serde"], optional = true } rustc-hash = { version = "2", optional = true } cust_core = { version = "0.1", optional = true } -spade = { version = "2", optional = true } # Make this optional? +spade = { version = "2.9", optional = true } # Make this optional? rayon = { version = "1", optional = true } bytemuck = { version = "1", features = ["derive"], optional = true } rstar = "0.12.0" diff --git a/crates/parry3d/Cargo.toml b/crates/parry3d/Cargo.toml index 5603c710..c2818326 100644 --- a/crates/parry3d/Cargo.toml +++ b/crates/parry3d/Cargo.toml @@ -57,7 +57,7 @@ num-derive = "0.4" indexmap = { version = "2", features = ["serde"], optional = true } rustc-hash = { version = "2", optional = true } cust_core = { version = "0.1", optional = true } -spade = { version = "2", optional = true } # Make this optional? +spade = { version = "2.9", optional = true } # Make this optional? rayon = { version = "1", optional = true } bytemuck = { version = "1", features = ["derive"], optional = true } log = "0.4" diff --git a/src/transformation/convex_hull3/convex_hull.rs b/src/transformation/convex_hull3/convex_hull.rs index a28fa20e..be19b161 100644 --- a/src/transformation/convex_hull3/convex_hull.rs +++ b/src/transformation/convex_hull3/convex_hull.rs @@ -15,8 +15,8 @@ pub fn convex_hull(points: &[Point3]) -> (Vec>, Vec<[u32; 3]> pub fn try_convex_hull( points: &[Point3], ) -> Result<(Vec>, Vec<[u32; 3]>), ConvexHullError> { - if points.is_empty() { - return Ok((Vec::new(), Vec::new())); + if points.len() < 3 { + return Err(ConvexHullError::IncompleteInput); } // print_buildable_vec("input", points); diff --git a/src/transformation/convex_hull3/error.rs b/src/transformation/convex_hull3/error.rs index ea879c0f..5a298085 100644 --- a/src/transformation/convex_hull3/error.rs +++ b/src/transformation/convex_hull3/error.rs @@ -1,25 +1,19 @@ -#[derive(Debug, PartialEq)] +#[derive(thiserror::Error, Debug, PartialEq)] /// Errors generated by the convex-hull calculation. pub enum ConvexHullError { /// Reached an impossible configuration in the convex-hull calculation, /// likely because of a bug. + #[error("Internal error: {0}")] InternalError(&'static str), /// The convex hull calculation was unable to find a support point. /// This generally happens if the input point set contains invalid points (with NaN coordinates) /// or if they are almost coplanar. + #[error("Input points are either invalid (NaN) or are almost coplanar.")] MissingSupportPoint, + /// The convex-hull calculation failed because less than 3 points were provided. + #[error("Less than 3 points were given to the convex-hull algorithm.")] + IncompleteInput, /// Reached a piece of code we shouldn’t (internal error). + #[error("Internal error: unreachable code path")] Unreachable, } - -impl std::fmt::Display for ConvexHullError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ConvexHullError::InternalError(reason) => write!(f, "InternalError({})", reason), - ConvexHullError::MissingSupportPoint => write!(f, "MissingSupportPoint"), - ConvexHullError::Unreachable => write!(f, "Unreachable"), - } - } -} - -impl std::error::Error for ConvexHullError {}