-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
sweep.rs
363 lines (291 loc) · 10.2 KB
/
sweep.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::collections::HashMap;
use fj_math::{Scalar, Transform, Triangle, Vector};
use crate::{
geometry::{Surface, SweptCurve},
shape::{Handle, Mapping, Shape, ValidationError, ValidationResult},
topology::{Cycle, Edge, Face, Vertex},
};
use super::{transform_shape, CycleApprox, Tolerance};
/// Create a new shape by sweeping an existing one
pub fn sweep_shape(
source: Shape,
path: Vector<3>,
tolerance: Tolerance,
color: [u8; 4],
) -> Result<Shape, ValidationError> {
let mut sweep = Sweep::init(source, path, tolerance, color);
sweep.create_top_and_bottom_faces()?;
sweep.create_side_faces()?;
Ok(sweep.target)
}
struct Sweep {
source: Shape,
target: Shape,
bottom: Shape,
top: Shape,
source_to_bottom: Mapping,
source_to_top: Mapping,
path: Vector<3>,
translation: Transform,
is_sweep_along_negative_direction: bool,
tolerance: Tolerance,
color: [u8; 4],
}
impl Sweep {
fn init(
source: Shape,
path: Vector<3>,
tolerance: Tolerance,
color: [u8; 4],
) -> Self {
let target = Shape::new();
let (bottom, source_to_bottom) = source.clone_shape();
let (top, source_to_top) = source.clone_shape();
let translation = Transform::translation(path);
let is_sweep_along_negative_direction =
path.dot(&Vector::from([0., 0., 1.])) < Scalar::ZERO;
Self {
source,
target,
bottom,
top,
source_to_bottom,
source_to_top,
path,
translation,
is_sweep_along_negative_direction,
tolerance,
color,
}
}
fn create_top_and_bottom_faces(&mut self) -> Result<(), ValidationError> {
if self.is_sweep_along_negative_direction {
reverse_surfaces(&mut self.top)?;
} else {
reverse_surfaces(&mut self.bottom)?;
}
transform_shape(&mut self.top, &self.translation)?;
self.target.merge_shape(&self.bottom)?;
self.target.merge_shape(&self.top)?;
Ok(())
}
fn create_side_faces(&mut self) -> Result<(), ValidationError> {
for cycle_source in self.source.cycles() {
if cycle_source.get().edges.len() == 1 {
// If there's only one edge in the cycle, it must be a
// continuous edge that connects to itself. By sweeping that, we
// create a continuous face.
//
// Continuous faces aren't currently supported by the
// approximation code, and hence can't be triangulated. To
// address that, we fall back to the old and almost obsolete
// triangle representation to create the face.
//
// This is the last piece of code that still uses the triangle
// representation.
create_continuous_side_face_fallback(
&cycle_source.get(),
&self.translation,
self.tolerance,
self.color,
&mut self.target,
)?;
continue;
}
// If there's no continuous edge, we can create the non-continuous
// faces using boundary representation.
let mut vertex_bottom_to_edge = HashMap::new();
for edge_source in &cycle_source.get().edges {
let edge_source = edge_source.canonical();
let edge_bottom = self.source_to_bottom.edge(&edge_source);
let edge_top = self.source_to_top.edge(&edge_source);
let surface = create_side_surface(self, &edge_bottom);
let cycle = create_side_cycle(
&mut self.target,
edge_bottom,
edge_top,
&mut vertex_bottom_to_edge,
)?;
create_side_face(self, surface, cycle)?;
}
}
Ok(())
}
}
fn reverse_surfaces(shape: &mut Shape) -> Result<(), ValidationError> {
shape
.update()
.update_all(|surface: &mut Surface| *surface = surface.reverse())
.validate()
}
fn create_continuous_side_face_fallback(
cycle_source: &Cycle<3>,
translation: &Transform,
tolerance: Tolerance,
color: [u8; 4],
target: &mut Shape,
) -> Result<(), ValidationError> {
let approx = CycleApprox::new(cycle_source, tolerance);
let mut quads = Vec::new();
for segment in approx.segments() {
let [v0, v1] = segment.points();
let [v3, v2] = {
let segment = translation.transform_segment(&segment);
segment.points()
};
quads.push([v0, v1, v2, v3]);
}
let mut side_face: Vec<(Triangle<3>, _)> = Vec::new();
for [v0, v1, v2, v3] in quads {
side_face.push(([v0, v1, v2].into(), color));
side_face.push(([v0, v2, v3].into(), color));
}
target.insert(Face::Triangles(side_face))?;
Ok(())
}
fn create_side_surface(
sweep: &Sweep,
edge_bottom: &Handle<Edge<3>>,
) -> Surface {
let mut surface = Surface::SweptCurve(SweptCurve {
curve: edge_bottom.get().curve(),
path: sweep.path,
});
if sweep.is_sweep_along_negative_direction {
surface = surface.reverse();
}
surface
}
fn create_side_cycle(
target: &mut Shape,
edge_bottom: Handle<Edge<3>>,
edge_top: Handle<Edge<3>>,
vertex_bottom_to_edge: &mut HashMap<Handle<Vertex>, Handle<Edge<3>>>,
) -> ValidationResult<Cycle<3>> {
// Can't panic. We already ruled out the "continuous edge" case above, so
// these edges must have vertices.
let [vertices_bottom, vertices_top] = [&edge_bottom, &edge_top]
.map(|edge| edge.get().vertices.expect_vertices());
// Can be simplified, once `zip` is stabilized:
// https://doc.rust-lang.org/std/primitive.array.html#method.zip
let [b_a, b_b] = vertices_bottom;
let [t_a, t_b] = vertices_top;
let vertices = [(b_a, t_a), (b_b, t_b)];
// Create (or retrieve from the cache, `vertex_bottom_to_edge`) side edges
// from the vertices of this source/bottom edge.
//
// Can be cleaned up, once `try_map` is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.try_map
let side_edges = vertices.map(|(vertex_bottom, vertex_top)| {
// We only need to create the edge, if it hasn't already been created
// for a neighboring side face. Let's check our cache, to see if that's
// the case.
let edge = vertex_bottom_to_edge
.get(&vertex_bottom.canonical())
.cloned();
if let Some(edge) = edge {
return Ok(edge);
}
let points = [vertex_bottom.clone(), vertex_top]
.map(|vertex| vertex.canonical().get().point());
let edge =
Edge::builder(target).build_line_segment_from_points(points)?;
vertex_bottom_to_edge.insert(vertex_bottom.canonical(), edge.clone());
Ok(edge)
});
let [a, b]: [Result<_, ValidationError>; 2] = side_edges;
let [edge_side_a, edge_side_b] = [a?, b?];
target.merge(Cycle::new([
edge_bottom,
edge_top,
edge_side_a,
edge_side_b,
]))
}
fn create_side_face(
sweep: &mut Sweep,
surface: Surface,
cycle: Handle<Cycle<3>>,
) -> Result<(), ValidationError> {
let surface = sweep.target.insert(surface)?;
sweep.target.insert(Face::new(
surface,
vec![cycle],
Vec::new(),
sweep.color,
))?;
Ok(())
}
#[cfg(test)]
mod tests {
use fj_math::{Point, Scalar, Vector};
use crate::{
algorithms::Tolerance,
geometry::Surface,
shape::{Handle, Shape},
topology::{Cycle, Edge, Face},
};
use super::sweep_shape;
#[test]
fn sweep() -> anyhow::Result<()> {
let tolerance = Tolerance::from_scalar(Scalar::ONE)?;
let sketch =
Triangle::new([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]], false)?;
let swept = sweep_shape(
sketch.shape,
Vector::from([0., 0., 1.]),
tolerance,
[255, 0, 0, 255],
)?;
let bottom_face =
Triangle::new([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]], true)?
.face
.get();
let top_face =
Triangle::new([[0., 0., 1.], [1., 0., 1.], [0., 1., 1.]], false)?
.face
.get();
let mut contains_bottom_face = false;
let mut contains_top_face = false;
for face in swept.faces() {
if face.get().clone() == bottom_face {
contains_bottom_face = true;
}
if face.get().clone() == top_face {
contains_top_face = true;
}
}
assert!(contains_bottom_face);
assert!(contains_top_face);
// Side faces are not tested, as those use triangle representation. The
// plan is to start testing them, as they are transitioned to b-rep.
Ok(())
}
pub struct Triangle {
shape: Shape,
face: Handle<Face>,
}
impl Triangle {
fn new(
points: [impl Into<Point<3>>; 3],
reverse: bool,
) -> anyhow::Result<Self> {
let mut shape = Shape::new();
let [a, b, c] = points.map(|point| point.into());
let ab = Edge::builder(&mut shape)
.build_line_segment_from_points([a, b])?;
let bc = Edge::builder(&mut shape)
.build_line_segment_from_points([b, c])?;
let ca = Edge::builder(&mut shape)
.build_line_segment_from_points([c, a])?;
let cycles = shape.insert(Cycle::new(vec![ab, bc, ca]))?;
let surface = Surface::plane_from_points([a, b, c]);
let surface = if reverse { surface.reverse() } else { surface };
let surface = shape.insert(surface)?;
let abc =
Face::new(surface, vec![cycles], Vec::new(), [255, 0, 0, 255]);
let face = shape.insert(abc)?;
Ok(Self { shape, face })
}
}
}