1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::objects::Cycle;
use crate::objects::HalfEdge;
use fj_math::Point;
use fj_math::Scalar;
use itertools::Itertools;
use super::{Validate, ValidationConfig, ValidationError};
impl Validate for Cycle {
fn validate_with_config(
&self,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
CycleValidationError::check_half_edges_disconnected(
self, config, errors,
);
CycleValidationError::check_enough_half_edges(self, config, errors);
}
}
#[derive(Clone, Debug, thiserror::Error)]
pub enum CycleValidationError {
#[error(
"Adjacent `HalfEdge`s are distinct\n\
- End position of first `HalfEdge`: {end_of_first:?}\n\
- Start position of second `HalfEdge`: {start_of_second:?}\n\
- `HalfEdge`s: {half_edges:#?}"
)]
HalfEdgesDisconnected {
end_of_first: Point<2>,
start_of_second: Point<2>,
distance: Scalar,
half_edges: Box<(HalfEdge, HalfEdge)>,
},
#[error("Expected at least one `HalfEdge`\n")]
NotEnoughHalfEdges,
}
impl CycleValidationError {
fn check_enough_half_edges(
cycle: &Cycle,
_config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
if cycle.half_edges().next().is_none() {
errors.push(Self::NotEnoughHalfEdges.into());
}
}
fn check_half_edges_disconnected(
cycle: &Cycle,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
for (first, second) in cycle.half_edges().circular_tuple_windows() {
let end_of_first = {
let [_, end] = first.boundary();
first.curve().point_from_path_coords(end)
};
let start_of_second = second.start_position();
let distance = (end_of_first - start_of_second).magnitude();
if distance > config.identical_max_distance {
errors.push(
Self::HalfEdgesDisconnected {
end_of_first,
start_of_second,
distance,
half_edges: Box::new((
first.clone_object(),
second.clone_object(),
)),
}
.into(),
);
}
}
}
}
#[cfg(test)]
mod tests {
use crate::{
builder::{CycleBuilder, HalfEdgeBuilder},
objects::Cycle,
services::Services,
validate::{cycle::CycleValidationError, Validate, ValidationError},
};
#[test]
fn half_edges_connected() -> anyhow::Result<()> {
let mut services = Services::new();
let valid = CycleBuilder::polygon([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]])
.build(&mut services.objects);
valid.validate_and_return_first_error()?;
let disconnected = {
let first =
HalfEdgeBuilder::line_segment([[0., 0.], [1., 0.]], None);
let second =
HalfEdgeBuilder::line_segment([[0., 0.], [1., 0.]], None);
CycleBuilder::new()
.add_half_edge(first)
.add_half_edge(second)
.build(&mut services.objects)
};
assert!(matches!(
disconnected.validate_and_return_first_error(),
Err(ValidationError::Cycle(
CycleValidationError::HalfEdgesDisconnected { .. }
))
));
let empty = Cycle::new([]);
assert!(matches!(
empty.validate_and_return_first_error(),
Err(ValidationError::Cycle(
CycleValidationError::NotEnoughHalfEdges
))
));
Ok(())
}
}