Skip to main content

ternary_contours/
error.rs

1use crate::interpolation::InterpolationError;
2use core::fmt;
3
4/// Error returned while validating a regular ternary scalar field or constructing
5/// its cubic-alpha edge model.
6#[derive(Clone, Debug, PartialEq)]
7pub enum FieldError {
8    ZeroSubdivisions,
9    AllocationOverflow,
10    IncorrectValueCount {
11        expected: usize,
12        actual: usize,
13    },
14    NonFiniteValue {
15        index: usize,
16        value: f64,
17    },
18    InvalidLatticeCoordinate {
19        i: usize,
20        j: usize,
21        k: usize,
22        subdivisions: usize,
23    },
24    InvalidVertexIndex {
25        index: usize,
26        vertex_count: usize,
27    },
28    InsufficientStencil {
29        samples: usize,
30    },
31    CubicFeatureUnavailable,
32    Interpolation(InterpolationError),
33}
34impl fmt::Display for FieldError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::ZeroSubdivisions => {
38                write!(f, "regular ternary field subdivisions must be positive")
39            }
40            Self::AllocationOverflow => {
41                write!(f, "regular-grid allocation or index arithmetic overflowed")
42            }
43            Self::IncorrectValueCount { expected, actual } => write!(
44                f,
45                "regular ternary field requires {expected} values; received {actual}"
46            ),
47            Self::NonFiniteValue { index, value } => {
48                write!(f, "field value {index} is not finite: {value:?}")
49            }
50            Self::InvalidLatticeCoordinate {
51                i,
52                j,
53                k,
54                subdivisions,
55            } => write!(
56                f,
57                "lattice coordinate ({i},{j},{k}) does not sum to subdivision count {subdivisions}"
58            ),
59            Self::InvalidVertexIndex {
60                index,
61                vertex_count,
62            } => write!(f, "grid vertex index {index} is outside 0..{vertex_count}"),
63            Self::InsufficientStencil { samples } => write!(
64                f,
65                "cubic-alpha line requires at least three samples; received {samples}"
66            ),
67            Self::CubicFeatureUnavailable => write!(
68                f,
69                "cubic-alpha construction requires the `cubic-alpha` feature"
70            ),
71            Self::Interpolation(error) => write!(f, "cubic interpolation error: {error}"),
72        }
73    }
74}
75impl std::error::Error for FieldError {
76    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
77        match self {
78            Self::Interpolation(error) => Some(error),
79            _ => None,
80        }
81    }
82}
83impl From<InterpolationError> for FieldError {
84    fn from(value: InterpolationError) -> Self {
85        Self::Interpolation(value)
86    }
87}
88/// Error returned while evaluating a user scalar function over a regular grid.
89#[derive(Clone, Debug, PartialEq)]
90#[non_exhaustive]
91pub enum GridEvaluationError<E> {
92    /// The regular grid or resulting scalar field could not be constructed.
93    Grid(FieldError),
94    /// The user callback failed at one canonical composition-grid vertex.
95    Evaluation {
96        /// Stable canonical grid vertex identifier.
97        index: crate::GridVertexId,
98        /// Normalized semantic A/B/C composition supplied to the callback.
99        composition: [f64; 3],
100        /// Original callback error.
101        source: E,
102    },
103}
104
105impl<E: fmt::Display> fmt::Display for GridEvaluationError<E> {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        match self {
108            Self::Grid(source) => write!(f, "regular-grid construction failed: {source}"),
109            Self::Evaluation {
110                index,
111                composition,
112                source,
113            } => write!(
114                f,
115                "scalar evaluation failed at grid vertex {} for composition {:?}: {source}",
116                index.0, composition
117            ),
118        }
119    }
120}
121
122impl<E> std::error::Error for GridEvaluationError<E>
123where
124    E: std::error::Error + 'static,
125{
126    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
127        match self {
128            Self::Grid(source) => Some(source),
129            Self::Evaluation { source, .. } => Some(source),
130        }
131    }
132}
133
134impl<E> From<FieldError> for GridEvaluationError<E> {
135    fn from(source: FieldError) -> Self {
136        Self::Grid(source)
137    }
138}