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
use fj_math::Point;
use crate::{
objects::{HalfEdge, Surface, SurfaceVertex},
partial::{
HasPartial, MaybePartial, PartialCurve, PartialCycle, PartialHalfEdge,
PartialSurfaceVertex, PartialVertex,
},
storage::Handle,
};
use super::{CurveBuilder, HalfEdgeBuilder};
pub trait CycleBuilder {
fn with_poly_chain(
self,
vertices: impl IntoIterator<Item = impl Into<MaybePartial<SurfaceVertex>>>,
) -> Self;
fn with_poly_chain_from_points(
self,
surface: Handle<Surface>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self;
fn close_with_line_segment(self) -> Self;
}
impl CycleBuilder for PartialCycle {
fn with_poly_chain(
self,
vertices: impl IntoIterator<Item = impl Into<MaybePartial<SurfaceVertex>>>,
) -> Self {
let vertices = vertices.into_iter().map(Into::into);
let iter = self
.half_edges()
.last()
.map(|half_edge| {
let [_, last] = half_edge.vertices();
last.surface_form()
})
.into_iter()
.chain(vertices);
let mut previous: Option<MaybePartial<SurfaceVertex>> = None;
let mut half_edges = Vec::new();
for vertex_next in iter {
if let Some(vertex_prev) = previous {
let surface = vertex_prev
.surface()
.expect("Need surface to extend cycle with poly-chain");
let [position_prev, position_next] =
[&vertex_prev, &vertex_next].map(|vertex| {
vertex
.position()
.expect("Need surface position to extend cycle")
});
previous = Some(vertex_next.clone());
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve
.update_as_line_from_points([position_prev, position_next]);
let vertices = [(0., vertex_prev), (1., vertex_next)].map(
|(position, surface_form)| PartialVertex {
position: Some([position].into()),
curve: curve.clone().into(),
surface_form,
},
);
half_edges.push(PartialHalfEdge {
curve: curve.into(),
vertices: vertices.map(Into::into),
..Default::default()
});
continue;
}
previous = Some(vertex_next);
}
self.with_half_edges(half_edges)
}
fn with_poly_chain_from_points(
self,
surface: Handle<Surface>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
self.with_poly_chain(points.into_iter().map(|position| {
PartialSurfaceVertex {
position: Some(position.into()),
surface: Some(surface.clone()),
..Default::default()
}
}))
}
fn close_with_line_segment(self) -> Self {
let first = self.half_edges().next();
let last = self.half_edges().last();
let vertices = [first, last]
.map(|option| option.map(|half_edge| half_edge.vertices()));
let [Some([first, _]), Some([_, last])] = vertices else {
return self;
};
let vertices = [last, first].map(|vertex| {
vertex
.surface_form()
.position()
.expect("Need surface position to close cycle")
});
let surface = self.surface().expect("Need surface to close cycle");
self.with_half_edges(Some(
HalfEdge::partial()
.update_as_line_segment_from_points(surface, vertices),
))
}
}