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
use fj_math::Point;
use crate::{
objects::{Curve, Cycle, HalfEdge, Surface, SurfaceVertex, Vertex},
partial::{HasPartial, MaybePartial},
stores::{Handle, Stores},
};
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialCycle {
pub surface: Option<Handle<Surface>>,
pub half_edges: Vec<MaybePartial<HalfEdge>>,
}
impl PartialCycle {
pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
if let Some(surface) = surface {
self.surface = Some(surface);
}
self
}
pub fn with_half_edges(
mut self,
half_edge: impl IntoIterator<Item = impl Into<MaybePartial<HalfEdge>>>,
) -> Self {
self.half_edges
.extend(half_edge.into_iter().map(Into::into));
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().expect(
"Need half-edge vertices to extend cycle with poly-chain",
);
let last = last.surface_form().expect(
"Need surface vertex to extend cycle with poly-chain",
);
let vertex = last.clone();
let position = last.position().expect(
"Need surface position to extend cycle with poly-chain",
);
(position, Some(vertex))
})
.into_iter()
.chain(points.into_iter().map(|point| (point.into(), None)));
let mut previous: Option<(
Point<2>,
Option<MaybePartial<SurfaceVertex>>,
)> = None;
for (position, vertex) in iter {
if let Some((previous_position, previous_vertex)) = previous {
let surface = self
.surface
.clone()
.expect("Need surface to extend cycle with poly-chain");
let from = previous_vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(previous_position))
.into()
});
let to = vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(position))
.into()
});
previous = Some((position, Some(to.clone())));
let curve = Handle::<Curve>::partial()
.with_surface(Some(surface.clone()))
.as_line_from_points([previous_position, position]);
let [from, to] =
[(0., from), (1., to)].map(|(position, surface_form)| {
Vertex::partial()
.with_curve(Some(curve.clone()))
.with_position(Some([position]))
.with_surface_form(Some(surface_form))
});
self.half_edges.push(
HalfEdge::partial()
.with_curve(Some(curve))
.with_vertices(Some([from, to]))
.into(),
);
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();
let vertices = [first, last].map(|option| {
option.map(|half_edge| {
half_edge.vertices().expect("Need vertices to close cycle")
})
});
if let [Some([first, _]), Some([_, last])] = vertices {
let vertices = [last, first].map(|vertex| {
vertex
.surface_form()
.expect("Need surface vertex to close cycle")
.position()
.expect("Need surface position to close cycle")
});
let surface =
self.surface.clone().expect("Need surface to close cycle");
self.half_edges.push(
HalfEdge::partial()
.with_surface(Some(surface))
.as_line_segment_from_points(vertices)
.into(),
);
}
self
}
pub fn build(self, stores: &Stores) -> Cycle {
let surface = self.surface.expect("Need surface to build `Cycle`");
let surface_for_edges = surface.clone();
let half_edges = self.half_edges.into_iter().map(|half_edge| {
half_edge
.update_partial(|half_edge| {
half_edge.with_surface(Some(surface_for_edges.clone()))
})
.into_full(stores)
});
Cycle::new(surface, half_edges)
}
}
impl From<&Cycle> for PartialCycle {
fn from(cycle: &Cycle) -> Self {
Self {
surface: Some(cycle.surface().clone()),
half_edges: cycle.half_edges().cloned().map(Into::into).collect(),
}
}
}