1use std::fmt;
2
3use crate::{FieldError, interpolation::InterpolationError};
4
5#[derive(Clone, Debug, PartialEq)]
6#[non_exhaustive]
7pub enum ContourError {
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 NonFiniteLevel {
29 index: usize,
30 value: f64,
31 },
32 DuplicateLevel {
33 first: usize,
34 second: usize,
35 value: f64,
36 },
37 NonFiniteBandBreak {
38 index: usize,
39 value: f64,
40 },
41 DuplicateBandBreak {
42 first: usize,
43 second: usize,
44 value: f64,
45 },
46 UnorderedBandBreak {
47 previous_index: usize,
48 index: usize,
49 previous: f64,
50 value: f64,
51 },
52 UnsupportedFilledInterpolation,
53 UnclosedBandBoundary,
54 InvalidTolerance {
55 value_tolerance: f64,
56 geometry_tolerance: f64,
57 },
58 InvalidAdaptiveOptions {
59 max_depth: u8,
60 flatness_tolerance: f64,
61 },
62 InvalidRegularizationSpacing {
63 spacing: f64,
64 },
65 InvalidProjectionOptions {
66 tolerance: f64,
67 iterations: usize,
68 max_step: f64,
69 },
70 CubicFeatureUnavailable,
71 InsufficientStencil {
72 samples: usize,
73 },
74 FlatTriangle {
75 triangle: usize,
76 level: f64,
77 },
78 BranchingTopology {
79 degree: usize,
80 },
81 ZeroLengthPath,
82 InvalidClosedLoop,
83 ProjectionZeroGradient {
84 residual: f64,
85 },
86 ProjectionNonConvergence {
87 residual: f64,
88 iterations: usize,
89 },
90 PointOutsideGrid {
91 a: f64,
92 b: f64,
93 c: f64,
94 },
95 Interpolation(InterpolationError),
96}
97
98impl fmt::Display for ContourError {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 match self {
101 Self::ZeroSubdivisions => {
102 write!(f, "regular ternary field subdivisions must be positive")
103 }
104 Self::AllocationOverflow => {
105 write!(f, "regular-grid allocation or index arithmetic overflowed")
106 }
107 Self::IncorrectValueCount { expected, actual } => write!(
108 f,
109 "regular ternary field requires {expected} values; received {actual}"
110 ),
111 Self::NonFiniteValue { index, value } => {
112 write!(f, "field value {index} is not finite: {value:?}")
113 }
114 Self::InvalidLatticeCoordinate {
115 i,
116 j,
117 k,
118 subdivisions,
119 } => write!(
120 f,
121 "lattice coordinate ({i},{j},{k}) does not sum to subdivision count {subdivisions}"
122 ),
123 Self::InvalidVertexIndex {
124 index,
125 vertex_count,
126 } => write!(f, "grid vertex index {index} is outside 0..{vertex_count}"),
127 Self::NonFiniteLevel { index, value } => {
128 write!(f, "contour level {index} is not finite: {value:?}")
129 }
130 Self::DuplicateLevel {
131 first,
132 second,
133 value,
134 } => write!(f, "contour levels {first} and {second} duplicate {value:?}"),
135 Self::NonFiniteBandBreak { index, value } => {
136 write!(f, "contour band break {index} is not finite: {value:?}")
137 }
138 Self::DuplicateBandBreak {
139 first,
140 second,
141 value,
142 } => write!(
143 f,
144 "contour band breaks {first} and {second} duplicate {value:?}"
145 ),
146 Self::UnorderedBandBreak {
147 previous_index,
148 index,
149 previous,
150 value,
151 } => write!(
152 f,
153 "band break {index} ({value}) must be greater than break {previous_index} ({previous})"
154 ),
155 Self::UnsupportedFilledInterpolation => write!(
156 f,
157 "filled contour bands currently support only piecewise-linear interpolation"
158 ),
159 Self::UnclosedBandBoundary => write!(
160 f,
161 "filled-band fragments did not assemble into a closed boundary"
162 ),
163 Self::InvalidTolerance {
164 value_tolerance,
165 geometry_tolerance,
166 } => write!(
167 f,
168 "contour tolerances must be finite and positive: value={value_tolerance:?}, geometry={geometry_tolerance:?}"
169 ),
170 Self::InvalidAdaptiveOptions {
171 max_depth,
172 flatness_tolerance,
173 } => write!(
174 f,
175 "invalid adaptive options: max_depth={max_depth}, flatness={flatness_tolerance:?}"
176 ),
177 Self::InvalidRegularizationSpacing { spacing } => write!(
178 f,
179 "contour regularization spacing must be finite and positive: {spacing:?}"
180 ),
181 Self::InvalidProjectionOptions {
182 tolerance,
183 iterations,
184 max_step,
185 } => write!(
186 f,
187 "invalid projection options: tolerance={tolerance:?}, iterations={iterations}, max_step={max_step:?}"
188 ),
189 Self::CubicFeatureUnavailable => write!(
190 f,
191 "cubic-alpha contour interpolation requires the `cubic-alpha` feature"
192 ),
193 Self::InsufficientStencil { samples } => write!(
194 f,
195 "cubic-alpha line requires at least three samples; received {samples}"
196 ),
197 Self::FlatTriangle { triangle, level } => write!(
198 f,
199 "triangle {triangle} is entirely coincident with contour level {level:?}"
200 ),
201 Self::BranchingTopology { degree } => {
202 write!(f, "contour endpoint graph has non-manifold degree {degree}")
203 }
204 Self::ZeroLengthPath => write!(f, "contour extraction produced a zero-length path"),
205 Self::InvalidClosedLoop => write!(
206 f,
207 "closed contour does not contain at least three distinct points"
208 ),
209 Self::ProjectionZeroGradient { residual } => write!(
210 f,
211 "implicit contour projection encountered a zero gradient at residual {residual:?}"
212 ),
213 Self::ProjectionNonConvergence {
214 residual,
215 iterations,
216 } => write!(
217 f,
218 "implicit contour projection did not converge after {iterations} iterations; residual={residual:?}"
219 ),
220 Self::PointOutsideGrid { a, b, c } => write!(
221 f,
222 "composition ({a:?},{b:?},{c:?}) lies outside the regular ternary grid"
223 ),
224 Self::Interpolation(error) => write!(f, "cubic interpolation error: {error}"),
225 }
226 }
227}
228impl std::error::Error for ContourError {
229 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
230 match self {
231 Self::Interpolation(error) => Some(error),
232 _ => None,
233 }
234 }
235}
236impl From<InterpolationError> for ContourError {
237 fn from(value: InterpolationError) -> Self {
238 Self::Interpolation(value)
239 }
240}
241impl From<FieldError> for ContourError {
242 fn from(value: FieldError) -> Self {
243 match value {
244 FieldError::ZeroSubdivisions => Self::ZeroSubdivisions,
245 FieldError::AllocationOverflow => Self::AllocationOverflow,
246 FieldError::IncorrectValueCount { expected, actual } => {
247 Self::IncorrectValueCount { expected, actual }
248 }
249 FieldError::NonFiniteValue { index, value } => Self::NonFiniteValue { index, value },
250 FieldError::InvalidLatticeCoordinate {
251 i,
252 j,
253 k,
254 subdivisions,
255 } => Self::InvalidLatticeCoordinate {
256 i,
257 j,
258 k,
259 subdivisions,
260 },
261 FieldError::InvalidVertexIndex {
262 index,
263 vertex_count,
264 } => Self::InvalidVertexIndex {
265 index,
266 vertex_count,
267 },
268 FieldError::InsufficientStencil { samples } => Self::InsufficientStencil { samples },
269 FieldError::CubicFeatureUnavailable => Self::CubicFeatureUnavailable,
270 FieldError::Interpolation(error) => Self::Interpolation(error),
271 }
272 }
273}