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
use fj_math::Point;
use crate::{
objects::{Curve, Cycle, HalfEdge, Surface, SurfaceVertex, Vertex},
stores::Stores,
};
pub struct CycleBuilder<'a> {
pub stores: &'a Stores,
pub surface: Surface,
pub half_edges: Vec<HalfEdge>,
}
impl<'a> CycleBuilder<'a> {
pub fn with_half_edges(
mut self,
half_edge: impl IntoIterator<Item = HalfEdge>,
) -> Self {
self.half_edges.extend(half_edge);
self
}
pub fn with_poly_chain_from_points(
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
let iter = self
.half_edges
.last()
.map(|half_edge| {
let [_, last] = half_edge.vertices();
let vertex = *last.surface_form();
let position = last.surface_form().position();
(position, Some(vertex))
})
.into_iter()
.chain(points.into_iter().map(|point| (point.into(), None)));
let mut previous: Option<(Point<2>, Option<SurfaceVertex>)> = None;
for (position, vertex) in iter {
if let Some((previous_position, previous_vertex)) = previous {
let from = previous_vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(self.surface)
.with_position(previous_position)
.build(self.stores)
});
let to = vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(self.surface)
.with_position(position)
.build(self.stores)
});
previous = Some((position, Some(to)));
let curve = Curve::partial()
.with_surface(self.surface)
.as_line_from_points([previous_position, position])
.build(self.stores);
let [from, to] =
[(0., from), (1., to)].map(|(position, surface_form)| {
Vertex::partial()
.with_curve(curve.clone())
.with_position([position])
.with_surface_form(surface_form)
.build(self.stores)
});
self.half_edges.push(
HalfEdge::partial()
.with_curve(curve)
.with_vertices([from, to])
.build(self.stores),
);
continue;
}
previous = Some((position, vertex));
}
self
}
pub fn close_with_line_segment(mut self) -> Self {
let first = self.half_edges.first();
let last = self.half_edges.last();
if let [Some([first, _]), Some([_, last])] = [first, last]
.map(|option| option.map(|half_edge| half_edge.vertices()))
{
let vertices =
[last, first].map(|vertex| vertex.surface_form().position());
self.half_edges.push(
HalfEdge::partial()
.as_line_segment_from_points(self.surface, vertices)
.build(self.stores),
);
}
self
}
pub fn build(self) -> Cycle {
Cycle::new(self.surface, self.half_edges)
}
}