-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
edge.rs
229 lines (185 loc) · 7.6 KB
/
edge.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
use fj_interop::mesh::Color;
use fj_math::{Line, Scalar, Vector};
use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
objects::{
Curve, Cycle, Face, GlobalEdge, HalfEdge, SurfaceVertex, Vertex,
},
path::SurfacePath,
stores::Stores,
};
use super::Sweep;
impl Sweep for (HalfEdge, Color) {
type Swept = Face;
fn sweep(self, path: impl Into<Vector<3>>, stores: &Stores) -> Self::Swept {
let (edge, color) = self;
let path = path.into();
let surface = edge.curve().clone().sweep(path, stores);
// We can't use the edge we're sweeping from as the bottom edge, as that
// is not defined in the right surface. Let's create a new bottom edge,
// by swapping the surface of the original.
let bottom_edge = {
let vertices = edge.vertices();
let points_curve_and_surface = vertices.clone().map(|vertex| {
(vertex.position(), [vertex.position().t, Scalar::ZERO])
});
let curve = {
// Please note that creating a line here is correct, even if the
// global curve is a circle. Projected into the side surface, it
// is going to be a line either way.
let path =
SurfacePath::Line(Line::from_points_with_line_coords(
points_curve_and_surface,
));
Curve::new(surface, path, edge.curve().global_form().clone())
};
let vertices = {
let points_surface = points_curve_and_surface
.map(|(_, point_surface)| point_surface);
// Can be cleaned up, once `zip` is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.zip
let [a_vertex, b_vertex] = vertices;
let [a_surface, b_surface] = points_surface;
let vertices_with_surface_points =
[(a_vertex, a_surface), (b_vertex, b_surface)];
vertices_with_surface_points.map(|(vertex, point_surface)| {
let surface_vertex = SurfaceVertex::new(
point_surface,
surface,
*vertex.global_form(),
);
Vertex::new(
vertex.position(),
curve.clone(),
surface_vertex,
*vertex.global_form(),
)
})
};
HalfEdge::new(curve, vertices, edge.global_form().clone())
};
let side_edges = bottom_edge
.vertices()
.clone()
.map(|vertex| (vertex, surface).sweep(path, stores));
let top_edge = {
let bottom_vertices = bottom_edge.vertices();
let global_vertices = side_edges.clone().map(|edge| {
let [_, vertex] = edge.vertices();
*vertex.global_form()
});
let points_curve_and_surface =
bottom_vertices.clone().map(|vertex| {
(vertex.position(), [vertex.position().t, Scalar::ONE])
});
let curve = {
let global = bottom_edge
.curve()
.global_form()
.clone()
.translate(path, stores);
// Please note that creating a line here is correct, even if the
// global curve is a circle. Projected into the side surface, it
// is going to be a line either way.
let path =
SurfacePath::Line(Line::from_points_with_line_coords(
points_curve_and_surface,
));
Curve::new(surface, path, global)
};
let global =
GlobalEdge::new(curve.global_form().clone(), global_vertices);
let vertices = {
let surface_points = points_curve_and_surface
.map(|(_, point_surface)| point_surface);
// Can be cleaned up, once `zip` is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.zip
let [a_vertex, b_vertex] = bottom_vertices;
let [a_surface, b_surface] = surface_points;
let [a_global, b_global] = global_vertices;
let vertices = [
(a_vertex, a_surface, a_global),
(b_vertex, b_surface, b_global),
];
vertices.map(|(vertex, point_surface, vertex_global)| {
let vertex_surface = SurfaceVertex::new(
point_surface,
surface,
vertex_global,
);
Vertex::new(
vertex.position(),
curve.clone(),
vertex_surface,
vertex_global,
)
})
};
HalfEdge::new(curve, vertices, global)
};
let cycle = {
let a = bottom_edge;
let [d, b] = side_edges;
let c = top_edge;
let mut edges = [a, b, c, d];
// Make sure that edges are oriented correctly.
let mut i = 0;
while i < edges.len() {
let j = (i + 1) % edges.len();
let [_, prev_last] = edges[i].vertices();
let [next_first, _] = edges[j].vertices();
// Need to compare surface forms here, as the global forms might
// be coincident when sweeping circles, despite the vertices
// being different!
if prev_last.surface_form() != next_first.surface_form() {
edges[j] = edges[j].clone().reverse();
}
i += 1;
}
Cycle::new(surface, edges)
};
Face::from_exterior(cycle).with_color(color)
}
}
#[cfg(test)]
mod tests {
use fj_interop::mesh::Color;
use pretty_assertions::assert_eq;
use crate::{
algorithms::{reverse::Reverse, sweep::Sweep},
objects::{Cycle, Face, HalfEdge, Surface},
partial::HasPartial,
stores::Stores,
};
#[test]
fn sweep() {
let stores = Stores::new();
let half_edge = HalfEdge::partial()
.as_line_segment_from_points(
Surface::xy_plane(),
[[0., 0.], [1., 0.]],
)
.build(&stores);
let face = (half_edge, Color::default()).sweep([0., 0., 1.], &stores);
let expected_face = {
let surface = Surface::xz_plane();
let bottom = HalfEdge::partial()
.as_line_segment_from_points(surface, [[0., 0.], [1., 0.]])
.build(&stores);
let top = HalfEdge::partial()
.as_line_segment_from_points(surface, [[0., 1.], [1., 1.]])
.build(&stores)
.reverse();
let left = HalfEdge::partial()
.as_line_segment_from_points(surface, [[0., 0.], [0., 1.]])
.build(&stores)
.reverse();
let right = HalfEdge::partial()
.as_line_segment_from_points(surface, [[1., 0.], [1., 1.]])
.build(&stores);
let cycle = Cycle::new(surface, [bottom, right, top, left]);
Face::from_exterior(cycle)
};
assert_eq!(face, expected_face);
}
}