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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use fj_math::Winding;
use crate::{
objects::{Cycle, Face, Surface},
storage::Handle,
};
use super::{Validate, ValidationConfig};
impl Validate for Face {
type Error = FaceValidationError;
fn validate_with_config(
&self,
_: &ValidationConfig,
) -> Result<(), Self::Error> {
FaceValidationError::check_surface_identity(self)?;
FaceValidationError::check_interior_winding(self)?;
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum FaceValidationError {
#[error(
"`Surface` of an interior `Cycle` doesn't match `Face`'s `Surface`\n\
- `Surface` of the `Face`: {surface:#?}\n\
- Invalid interior `Cycle`: {interior:#?}\n\
- `Face`: {face:#?}"
)]
SurfaceMismatch {
surface: Handle<Surface>,
interior: Handle<Cycle>,
face: Face,
},
#[error(
"Interior of `Face` has invalid winding; must be opposite of exterior\n\
- Winding of exterior cycle: {exterior_winding:#?}\n\
- Winding of interior cycle: {interior_winding:#?}\n\
- `Face`: {face:#?}"
)]
InvalidInteriorWinding {
exterior_winding: Winding,
interior_winding: Winding,
face: Face,
},
}
impl FaceValidationError {
fn check_surface_identity(face: &Face) -> Result<(), Self> {
let surface = face.surface();
for interior in face.interiors() {
if surface.id() != interior.surface().id() {
return Err(Self::SurfaceMismatch {
surface: surface.clone(),
interior: interior.clone(),
face: face.clone(),
});
}
}
Ok(())
}
fn check_interior_winding(face: &Face) -> Result<(), Self> {
let exterior_winding = face.exterior().winding();
for interior in face.interiors() {
let interior_winding = interior.winding();
if exterior_winding == interior_winding {
return Err(Self::InvalidInteriorWinding {
exterior_winding,
interior_winding,
face: face.clone(),
});
}
assert_ne!(
exterior_winding,
interior.winding(),
"Interior cycles must have opposite winding of exterior cycle"
);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{
algorithms::reverse::Reverse,
builder::{CycleBuilder, FaceBuilder},
insert::Insert,
objects::{Cycle, Face, Objects},
partial::HasPartial,
validate::Validate,
};
#[test]
fn face_surface_mismatch() -> anyhow::Result<()> {
let objects = Objects::new();
let valid = Face::partial()
.with_surface(objects.surfaces.xy_plane())
.with_exterior_polygon_from_points([[0., 0.], [3., 0.], [0., 3.]])
.with_interior_polygon_from_points([[1., 1.], [1., 2.], [2., 1.]])
.build(&objects)?;
let invalid = {
let interiors = [Cycle::partial()
.with_poly_chain_from_points(
objects.surfaces.xz_plane(),
[[1., 1.], [1., 2.], [2., 1.]],
)
.close_with_line_segment()
.build(&objects)?
.insert(&objects)?];
Face::new(valid.exterior().clone(), interiors, valid.color())
};
assert!(valid.validate().is_ok());
assert!(invalid.validate().is_err());
Ok(())
}
#[test]
fn face_invalid_interior_winding() -> anyhow::Result<()> {
let objects = Objects::new();
let valid = Face::partial()
.with_surface(objects.surfaces.xy_plane())
.with_exterior_polygon_from_points([[0., 0.], [3., 0.], [0., 3.]])
.with_interior_polygon_from_points([[1., 1.], [1., 2.], [2., 1.]])
.build(&objects)?;
let invalid = {
let interiors = valid
.interiors()
.cloned()
.map(|cycle| cycle.reverse(&objects))
.collect::<Result<Vec<_>, _>>()?;
Face::new(valid.exterior().clone(), interiors, valid.color())
};
assert!(valid.validate().is_ok());
assert!(invalid.validate().is_err());
Ok(())
}
}