-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
solid.rs
178 lines (161 loc) · 5.88 KB
/
solid.rs
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
168
169
170
171
172
173
174
175
176
177
178
use std::iter::repeat;
use crate::{
geometry::{repr::tri_mesh::convert_point_surface_to_global, Geometry},
storage::Handle,
topology::{Cycle, Face, HalfEdge, Region, Shell, Solid, Vertex},
validation::{checks::MultipleReferencesToObject, ValidationCheck},
};
use fj_math::Point;
use super::{Validate, ValidationConfig, ValidationError};
impl Validate for Solid {
fn validate(
&self,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
geometry: &Geometry,
) {
errors.extend(
MultipleReferencesToObject::<Face, Shell>::check(
self, geometry, config,
)
.map(Into::into),
);
errors.extend(
MultipleReferencesToObject::<Region, Face>::check(
self, geometry, config,
)
.map(Into::into),
);
errors.extend(
MultipleReferencesToObject::<Cycle, Region>::check(
self, geometry, config,
)
.map(Into::into),
);
errors.extend(
MultipleReferencesToObject::<HalfEdge, Cycle>::check(
self, geometry, config,
)
.map(Into::into),
);
SolidValidationError::check_vertices(self, geometry, config, errors);
}
}
/// [`Solid`] validation failed
#[derive(Clone, Debug, thiserror::Error)]
pub enum SolidValidationError {
/// [`Solid`] contains vertices that are coincident, but not identical
#[error(
"Solid contains Vertices that are coincident but not identical\n
Vertex 1: {vertex_a:#?} ({position_a:?})
Vertex 2: {vertex_b:#?} ({position_b:?})"
)]
DistinctVerticesCoincide {
/// The first vertex
vertex_a: Handle<Vertex>,
/// The second vertex
vertex_b: Handle<Vertex>,
/// Position of first vertex
position_a: Point<3>,
/// Position of second vertex
position_b: Point<3>,
},
/// [`Solid`] contains vertices that are identical, but do not coincide
#[error(
"Solid contains Vertices that are identical but do not coincide\n
Vertex 1: {vertex_a:#?} ({position_a:?})
Vertex 2: {vertex_b:#?} ({position_b:?})"
)]
IdenticalVerticesNotCoincident {
/// The first vertex
vertex_a: Handle<Vertex>,
/// The second vertex
vertex_b: Handle<Vertex>,
/// Position of first vertex
position_a: Point<3>,
/// Position of second vertex
position_b: Point<3>,
},
}
impl SolidValidationError {
fn check_vertices(
solid: &Solid,
geometry: &Geometry,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
let vertices: Vec<(Point<3>, Handle<Vertex>)> = solid
.shells()
.iter()
.flat_map(|s| s.faces())
.flat_map(|face| {
face.region()
.all_cycles()
.flat_map(|cycle| cycle.half_edges().iter().cloned())
.zip(repeat(face.surface()))
})
.filter_map(|(h, s)| {
let Some(local_curve_geometry) =
geometry.of_curve(h.curve()).unwrap().local_on(s)
else {
// If the curve geometry has no local definition,
// there's nothing we can check.
return None;
};
Some((
convert_point_surface_to_global(
&geometry.of_surface_2(s).unwrap().generator,
local_curve_geometry.path.point_from_path_coords(
geometry
.of_vertex(h.start_vertex())
.unwrap()
.local_on(h.curve())
.unwrap()
.position,
),
config.tolerance,
geometry,
),
h.start_vertex().clone(),
))
})
.collect();
// This is O(N^2) which isn't great, but we can't use a HashMap since we
// need to deal with float inaccuracies. Maybe we could use some smarter
// data-structure like an octree.
for (position_a, vertex_a) in &vertices {
for (position_b, vertex_b) in &vertices {
let vertices_are_identical = vertex_a.id() == vertex_b.id();
let vertices_are_not_identical = !vertices_are_identical;
let too_far_to_be_identical = position_a
.distance_to(position_b)
> config.identical_max_distance;
let too_close_to_be_distinct = position_a
.distance_to(position_b)
< config.distinct_min_distance;
if vertices_are_identical && too_far_to_be_identical {
errors.push(
Self::IdenticalVerticesNotCoincident {
vertex_a: vertex_a.clone(),
vertex_b: vertex_b.clone(),
position_a: *position_a,
position_b: *position_b,
}
.into(),
)
}
if vertices_are_not_identical && too_close_to_be_distinct {
errors.push(
Self::DistinctVerticesCoincide {
vertex_a: vertex_a.clone(),
vertex_b: vertex_b.clone(),
position_a: *position_a,
position_b: *position_b,
}
.into(),
)
}
}
}
}
}