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
230
231
232
233
234
235
236
use fj_math::Point;
use crate::{
objects::{
Curve, Cycle, HalfEdge, Objects, Surface, SurfaceVertex, Vertex,
},
partial::{HasPartial, MaybePartial},
storage::Handle,
validate::ValidationError,
};
#[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(
mut self,
vertices: impl IntoIterator<Item = MaybePartial<SurfaceVertex>>,
) -> Self {
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;
for vertex_next in iter {
if let Some(vertex_prev) = previous {
let surface = self
.surface
.clone()
.expect("Need surface to extend cycle with poly-chain");
let position_prev = vertex_prev
.position()
.expect("Need surface position to extend cycle");
let position_next = vertex_next
.position()
.expect("Need surface position to extend cycle");
let from = vertex_prev.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});
let to = vertex_next.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});
previous = Some(to.clone());
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.as_line_from_points([position_prev, position_next]);
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(vertex_next);
}
self
}
pub fn with_poly_chain_from_points(
self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
self.with_poly_chain(points.into_iter().map(|position| {
SurfaceVertex::partial()
.with_position(Some(position))
.into()
}))
}
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()));
if let [Some([first, _]), Some([_, last])] = vertices {
let vertices = [last, first].map(|vertex| {
vertex
.surface_form()
.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(
mut self,
objects: &Objects,
) -> Result<Handle<Cycle>, ValidationError> {
let surface = self.surface.expect("Need surface to build `Cycle`");
let surface_for_edges = surface.clone();
let half_edges = {
let last_vertex = self
.half_edges
.last_mut()
.map(|half_edge| {
let vertex = half_edge.front();
(half_edge, vertex)
})
.map(|(half_edge, vertex)| {
let surface_vertex = vertex.surface_form();
(half_edge, vertex, surface_vertex)
})
.map(|(half_edge, vertex, surface_vertex)|
-> Result<_, ValidationError>
{
let surface_vertex = surface_vertex
.update_partial(|surface_vertex| {
surface_vertex.with_surface(Some(surface.clone()))
})
.into_full(objects)?;
*half_edge =
half_edge.clone().update_partial(|half_edge| {
half_edge.with_front_vertex(Some(
vertex.update_partial(|vertex| {
vertex.with_surface_form(Some(
surface_vertex.clone(),
))
}),
))
});
Ok(surface_vertex)
})
.transpose()?;
let (half_edges, _) = self.half_edges.into_iter().fold(
Ok((Vec::new(), last_vertex)),
|result: Result<_, ValidationError>, half_edge| {
let (mut half_edges, previous_vertex) = result?;
let half_edge = half_edge
.update_partial(|half_edge| {
let [back, _] = half_edge.vertices.clone();
let back = back.update_partial(|partial| {
partial.with_surface_form(previous_vertex)
});
half_edge
.with_surface(Some(surface_for_edges.clone()))
.with_back_vertex(Some(back))
})
.into_full(objects)?;
let front = half_edge.front().surface_form().clone();
half_edges.push(half_edge);
Ok((half_edges, Some(front)))
},
)?;
half_edges
};
Ok(objects.cycles.insert(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(),
}
}
}