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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::collections::BTreeMap;
use fj_interop::ext::ArrayExt;
use fj_math::Point;
use crate::{
geometry::CurveBoundary,
objects::{Curve, Face, HalfEdge, Shell, Surface, Vertex},
operations::{
build::{BuildFace, BuildHalfEdge, BuildSurface, Polygon},
geometry::UpdateHalfEdgeGeometry,
insert::{Insert, IsInserted, IsInsertedNo, IsInsertedYes},
join::JoinCycle,
reverse::ReverseCurveCoordinateSystems,
update::{
UpdateCycle, UpdateFace, UpdateHalfEdge, UpdateRegion, UpdateShell,
},
},
Core,
};
/// Build a [`Shell`]
///
/// See [module-level documentation] for context.
///
/// [module-level documentation]: super
pub trait BuildShell {
/// Build an empty shell
fn empty() -> Shell {
Shell::new([])
}
/// Build a polyhedron by specifying its vertices and indices
fn from_vertices_and_indices(
vertices: impl IntoIterator<Item = impl Into<Point<3>>>,
indices: impl IntoIterator<Item = [usize; 3]>,
core: &mut Core,
) -> Shell {
let vertices = vertices
.into_iter()
.enumerate()
.map(|(index, position)| {
let vertex = Vertex::new().insert(core);
let position = position.into();
(index, (vertex, position))
})
.collect::<BTreeMap<_, _>>();
let mut curves = BTreeMap::new();
let faces = indices
.into_iter()
.map(|indices| {
let [(a, a_pos), (b, b_pos), (c, c_pos)] = indices
.map(|index| vertices.get(&index).expect("Invalid index"));
let (surface, _) = Surface::plane_from_points(
[a_pos, b_pos, c_pos].map(Clone::clone),
core,
);
let curves_and_boundaries =
[[a, b], [b, c], [c, a]].map(|vertices| {
let vertices = vertices.map(Clone::clone);
let vertices = CurveBoundary::<Vertex>::from(vertices);
curves
.get(&vertices.clone().reverse())
.cloned()
.unwrap_or_else(|| {
let curve = Curve::new().insert(core);
let boundary =
CurveBoundary::<Point<1>>::from([
[0.],
[1.],
]);
curves.insert(
vertices,
(curve.clone(), boundary),
);
(curve, boundary.reverse())
})
});
let half_edges = {
let vertices = [a, b, c].map(Clone::clone);
let [a, b, c] = [[0., 0.], [1., 0.], [0., 1.]];
vertices
.zip_ext([[a, b], [b, c], [c, a]])
.zip_ext(curves_and_boundaries)
.map(|((vertex, positions), (curve, boundary))| {
let half_edge = HalfEdge::line_segment(
positions,
Some(boundary.reverse().inner),
core,
);
half_edge
.update_start_vertex(|_, _| vertex, core)
.update_curve(|_, _| curve, core)
.insert(core)
.set_path(
core.layers
.geometry
.of_half_edge(&half_edge)
.path,
&mut core.layers.geometry,
)
})
};
Face::unbound(surface, core).update_region(
|region, core| {
region.update_exterior(
|cycle, core| {
cycle.add_half_edges(half_edges, core)
},
core,
)
},
core,
)
})
.collect::<Vec<_>>();
Shell::empty().add_faces(faces, core)
}
/// Build a tetrahedron from the provided points
///
/// Accepts 4 points, naturally. For the purposes of the following
/// discussion, let's call those `a`, `b`, `c`, and `d`, and assume that the
/// order they are listed in here matches the order they are provided in
/// within the array.
///
/// Assumes that `a`, `b`, and `c` form a triangle in counter-clockwise
/// order, when arranging the viewpoint such that it is on the opposite side
/// of the triangle from `d`. If this assumption is met, the orientation of
/// all faces of the tetrahedron will be valid, meaning their
/// counter-clockwise sides are outside.
///
/// # Implementation Note
///
/// In principle, this method doesn't need to make assumptions about the
/// order of the points provided. It could, given some extra effort, just
/// build a correct tetrahedron, regardless of that order.
fn tetrahedron(
points: [impl Into<Point<3>>; 4],
core: &mut Core,
) -> TetrahedronShell {
let [a, b, c, d] = points.map(Into::into);
let abc = Face::triangle([a, b, c], core);
let bad = Face::triangle([b, a, d], core).update_region(
|region, core| {
region.update_exterior(
|cycle, core| {
cycle
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.join_to(
abc.face.region().exterior(),
0..=0,
0..=0,
core,
)
},
core,
)
},
core,
);
let dac = Face::triangle([d, a, c], core).update_region(
|region, core| {
region.update_exterior(
|cycle, core| {
cycle
.update_half_edge(
cycle.half_edges().nth_circular(1),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.join_to(
abc.face.region().exterior(),
1..=1,
2..=2,
core,
)
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.join_to(
bad.face.region().exterior(),
0..=0,
1..=1,
core,
)
},
core,
)
},
core,
);
let cbd = Face::triangle([c, b, d], core).update_region(
|region, core| {
region.update_exterior(
|cycle, core| {
cycle
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.update_half_edge(
cycle.half_edges().nth_circular(1),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.update_half_edge(
cycle.half_edges().nth_circular(2),
|edge, core| {
[edge
.reverse_curve_coordinate_systems(core)]
},
core,
)
.join_to(
abc.face.region().exterior(),
0..=0,
1..=1,
core,
)
.join_to(
bad.face.region().exterior(),
1..=1,
2..=2,
core,
)
.join_to(
dac.face.region().exterior(),
2..=2,
2..=2,
core,
)
},
core,
)
},
core,
);
let triangles =
[abc, bad, dac, cbd].map(|triangle| triangle.insert(core));
let shell =
Shell::new(triangles.iter().map(|triangle| triangle.face.clone()));
let [abc, bad, dac, cbd] = triangles;
TetrahedronShell {
shell,
abc,
bad,
dac,
cbd,
}
}
}
impl BuildShell for Shell {}
/// A tetrahedron
///
/// A tetrahedron is constructed from 4 points and has 4 faces. For the purpose
/// of naming the fields of this struct, the points are named `a`, `b`, `c`, and
/// `d`, in the order in which they are passed.
///
/// Returned by [`BuildShell::tetrahedron`].
pub struct TetrahedronShell<I: IsInserted = IsInsertedNo> {
/// The shell that forms the tetrahedron
pub shell: I::T<Shell>,
/// The face formed by the points `a`, `b`, and `c`.
pub abc: Polygon<3, IsInsertedYes>,
/// The face formed by the points `b`, `a`, and `d`.
pub bad: Polygon<3, IsInsertedYes>,
/// The face formed by the points `d`, `a`, and `c`.
pub dac: Polygon<3, IsInsertedYes>,
/// The face formed by the points `c`, `b`, and `d`.
pub cbd: Polygon<3, IsInsertedYes>,
}