-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
face.rs
161 lines (128 loc) · 4.74 KB
/
face.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
use fj_math::{Scalar, Vector};
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
objects::{Face, Shell},
path::GlobalPath,
stores::Stores,
};
use super::Sweep;
impl Sweep for Face {
type Swept = Shell;
fn sweep(self, path: impl Into<Vector<3>>, stores: &Stores) -> Self::Swept {
let path = path.into();
let mut faces = Vec::new();
let is_negative_sweep = {
let u = match self.surface().u() {
GlobalPath::Circle(_) => todo!(
"Sweeping from faces defined in round surfaces is not \
supported"
),
GlobalPath::Line(line) => line.direction(),
};
let v = self.surface().v();
let normal = u.cross(&v);
normal.dot(&path) < Scalar::ZERO
};
let bottom_face = {
if is_negative_sweep {
self.clone()
} else {
self.clone().reverse()
}
};
faces.push(bottom_face);
let top_face = {
let mut face = self.clone().translate(path, stores);
if is_negative_sweep {
face = face.reverse();
};
face
};
faces.push(top_face);
for cycle in self.all_cycles() {
for half_edge in cycle.half_edges() {
let edge = if is_negative_sweep {
half_edge.clone().reverse()
} else {
half_edge.clone()
};
let face = (edge, self.color()).sweep(path, stores);
faces.push(face);
}
}
Shell::new().with_faces(faces)
}
}
#[cfg(test)]
mod tests {
use fj_interop::mesh::Color;
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
objects::{Face, HalfEdge, Sketch, Surface},
partial::HasPartial,
stores::Stores,
};
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 stores = Stores::new();
let surface = Surface::xy_plane();
let solid = Sketch::builder(&stores, surface)
.build_polygon_from_points(TRIANGLE)
.sweep(UP, &stores);
let bottom = Face::builder(&stores, surface)
.with_exterior_polygon_from_points(TRIANGLE)
.build()
.reverse();
let top = Face::builder(&stores, surface.translate(UP, &stores))
.with_exterior_polygon_from_points(TRIANGLE)
.build();
assert!(solid.find_face(&bottom).is_some());
assert!(solid.find_face(&top).is_some());
let mut side_faces = TRIANGLE.windows(2).map(|window| {
// Can't panic, as we passed `2` to `windows`.
//
// Can be cleaned up, once `array_windows` is stable:
// https://doc.rust-lang.org/std/primitive.slice.html#method.array_windows
let [a, b] = [window[0], window[1]];
let half_edge = HalfEdge::partial()
.as_line_segment_from_points(Surface::xy_plane(), [a, b])
.build(&stores);
(half_edge, Color::default()).sweep(UP, &stores)
});
assert!(side_faces.all(|face| solid.find_face(&face).is_some()));
}
#[test]
fn sweep_down() {
let stores = Stores::new();
let surface = Surface::xy_plane();
let solid = Sketch::builder(&stores, surface)
.build_polygon_from_points(TRIANGLE)
.sweep(DOWN, &stores);
let bottom = Face::builder(&stores, surface.translate(DOWN, &stores))
.with_exterior_polygon_from_points(TRIANGLE)
.build()
.reverse();
let top = Face::builder(&stores, surface)
.with_exterior_polygon_from_points(TRIANGLE)
.build();
assert!(solid.find_face(&bottom).is_some());
assert!(solid.find_face(&top).is_some());
let mut side_faces = TRIANGLE.windows(2).map(|window| {
// Can't panic, as we passed `2` to `windows`.
//
// Can be cleaned up, once `array_windows` is stable:
// https://doc.rust-lang.org/std/primitive.slice.html#method.array_windows
let [a, b] = [window[0], window[1]];
let half_edge = HalfEdge::partial()
.as_line_segment_from_points(Surface::xy_plane(), [a, b])
.build(&stores)
.reverse();
(half_edge, Color::default()).sweep(DOWN, &stores)
});
assert!(side_faces.all(|face| solid.find_face(&face).is_some()));
}
}