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
use fj_math::{Scalar, Vector};
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
geometry::path::GlobalPath,
objects::{Face, Objects, Shell},
services::Service,
storage::Handle,
};
use super::{Sweep, SweepCache};
impl Sweep for Handle<Face> {
type Swept = Handle<Shell>;
fn sweep_with_cache(
self,
path: impl Into<Vector<3>>,
cache: &mut SweepCache,
objects: &mut Service<Objects>,
) -> Self::Swept {
let path = path.into();
let mut faces = Vec::new();
let is_negative_sweep = {
let u = match self.surface().geometry().u {
GlobalPath::Circle(_) => todo!(
"Sweeping from faces defined in round surfaces is not \
supported"
),
GlobalPath::Line(line) => line.direction(),
};
let v = self.surface().geometry().v;
let normal = u.cross(&v);
normal.dot(&path) < Scalar::ZERO
};
let bottom_face = {
if is_negative_sweep {
self.clone()
} else {
self.clone().reverse(objects)
}
};
faces.push(bottom_face);
let top_face = {
let mut face = self.clone().translate(path, objects);
if is_negative_sweep {
face = face.reverse(objects);
};
face
};
faces.push(top_face);
for cycle in self.all_cycles() {
for half_edge in cycle.half_edges() {
let half_edge = if is_negative_sweep {
half_edge.clone().reverse(objects)
} else {
half_edge.clone()
};
let face = (half_edge, self.color())
.sweep_with_cache(path, cache, objects);
faces.push(face);
}
}
Shell::builder().with_faces(faces).build(objects)
}
}
#[cfg(test)]
mod tests {
use fj_interop::{ext::SliceExt, mesh::Color};
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
builder::{FaceBuilder, HalfEdgeBuilder},
insert::Insert,
objects::{Face, HalfEdge, Objects, Sketch},
partial::HasPartial,
services::State,
};
use super::Sweep;
const TRIANGLE: [[f64; 2]; 3] = [[0., 0.], [1., 0.], [0., 1.]];
const UP: [f64; 3] = [0., 0., 1.];
const DOWN: [f64; 3] = [0., 0., -1.];
#[test]
fn sweep_up() {
let mut objects = Objects::new().into_service();
let surface = objects.surfaces.xy_plane();
let solid = Sketch::builder()
.with_surface(surface.clone())
.with_polygon_from_points(TRIANGLE, &mut objects)
.build(&mut objects)
.sweep(UP, &mut objects);
let bottom = Face::partial()
.with_surface(surface.clone())
.with_exterior_polygon_from_points(TRIANGLE)
.build(&mut objects)
.insert(&mut objects)
.reverse(&mut objects);
let top = Face::partial()
.with_surface(surface.translate(UP, &mut objects))
.with_exterior_polygon_from_points(TRIANGLE)
.build(&mut objects)
.insert(&mut objects);
assert!(solid.find_face(&bottom).is_some());
assert!(solid.find_face(&top).is_some());
let triangle = TRIANGLE.as_slice();
let side_faces = triangle.array_windows_ext().map(|&[a, b]| {
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
objects.surfaces.xy_plane(),
[a, b],
)
.build(&mut objects)
.insert(&mut objects);
(half_edge, Color::default()).sweep(UP, &mut objects)
});
assert!(side_faces
.into_iter()
.all(|face| solid.find_face(&face).is_some()));
}
#[test]
fn sweep_down() {
let mut objects = Objects::new().into_service();
let surface = objects.surfaces.xy_plane();
let solid = Sketch::builder()
.with_surface(surface.clone())
.with_polygon_from_points(TRIANGLE, &mut objects)
.build(&mut objects)
.sweep(DOWN, &mut objects);
let bottom = Face::partial()
.with_surface(surface.clone().translate(DOWN, &mut objects))
.with_exterior_polygon_from_points(TRIANGLE)
.build(&mut objects)
.insert(&mut objects)
.reverse(&mut objects);
let top = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points(TRIANGLE)
.build(&mut objects)
.insert(&mut objects);
assert!(solid.find_face(&bottom).is_some());
assert!(solid.find_face(&top).is_some());
let triangle = TRIANGLE.as_slice();
let side_faces = triangle.array_windows_ext().map(|&[a, b]| {
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
objects.surfaces.xy_plane(),
[a, b],
)
.build(&mut objects)
.insert(&mut objects)
.reverse(&mut objects);
(half_edge, Color::default()).sweep(DOWN, &mut objects)
});
assert!(side_faces
.into_iter()
.all(|face| solid.find_face(&face).is_some()));
}
}