Skip to main content

brep_ransac/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug, PartialEq)]
4/// An error produced while validating geometry or recognizing a surface.
5pub enum RecognitionError {
6    /// The input mesh is malformed or contains invalid values.
7    InvalidMesh(String),
8    /// Recognition or mesh-analysis options are outside their valid range.
9    InvalidOptions(String),
10    /// A requested triangle or vertex selection is empty or invalid.
11    InvalidSelection(String),
12    /// The supplied geometry does not contain enough usable information.
13    DegenerateData(String),
14    /// A surface could not be fitted to otherwise valid geometry.
15    FitFailed {
16        /// The primitive type being fitted, when known.
17        surface: Option<&'static str>,
18        /// A human-readable explanation of the failure.
19        reason: String,
20    },
21}
22
23impl Display for RecognitionError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::InvalidMesh(message) => write!(f, "invalid mesh: {message}"),
27            Self::InvalidOptions(message) => write!(f, "invalid recognition options: {message}"),
28            Self::InvalidSelection(message) => write!(f, "invalid selection: {message}"),
29            Self::DegenerateData(message) => write!(f, "degenerate geometry: {message}"),
30            Self::FitFailed { surface, reason } => match surface {
31                Some(kind) => write!(f, "{kind} fit failed: {reason}"),
32                None => write!(f, "surface fit failed: {reason}"),
33            },
34        }
35    }
36}
37
38impl std::error::Error for RecognitionError {}