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
use crate::{
builder::HalfEdgeBuilder,
objects::{Cycle, HalfEdge, Objects, Surface},
partial::{
MaybePartial, MergeWith, PartialHalfEdge, PartialVertex, Replace,
},
storage::Handle,
validate::ValidationError,
};
#[derive(Clone, Debug, Default)]
pub struct PartialCycle {
half_edges: Vec<MaybePartial<HalfEdge>>,
}
impl PartialCycle {
pub fn half_edges(&self) -> impl Iterator<Item = MaybePartial<HalfEdge>> {
self.half_edges.clone().into_iter()
}
pub fn surface(&self) -> Option<Handle<Surface>> {
self.half_edges
.first()
.and_then(|half_edge| half_edge.curve().surface())
}
pub fn with_half_edges(
mut self,
half_edges: impl IntoIterator<Item = impl Into<MaybePartial<HalfEdge>>>,
) -> Self {
let half_edges = half_edges.into_iter().map(Into::into);
let mut surface = self.surface();
for half_edge in half_edges {
surface = surface.merge_with(half_edge.curve().surface());
self.half_edges.push(half_edge);
}
self.with_surface(surface)
}
pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
if let Some(surface) = surface {
for half_edge in &mut self.half_edges {
*half_edge =
half_edge.clone().update_partial(|mut half_edge| {
half_edge.replace(surface.clone());
half_edge
});
}
}
self
}
pub fn build(
mut self,
objects: &Objects,
) -> Result<Cycle, ValidationError> {
if let (Some(first), Some(last)) =
(self.half_edges.first(), self.half_edges.last())
{
let [first, _] = first.vertices();
let [_, last] = last.vertices();
assert_eq!(
first.surface_form().position(),
last.surface_form().position(),
"Attempting to build un-closed cycle"
);
}
let mut previous_vertex = None;
for half_edge in &mut self.half_edges {
let back_vertex = previous_vertex.unwrap_or_default();
let front_vertex =
half_edge.front().surface_form().into_full(objects)?;
*half_edge = half_edge.clone().merge_with(PartialHalfEdge {
vertices: [
PartialVertex {
surface_form: back_vertex,
..Default::default()
},
PartialVertex {
surface_form: front_vertex.clone().into(),
..Default::default()
},
]
.map(Into::into),
..Default::default()
});
previous_vertex = Some(MaybePartial::from(front_vertex));
}
if let Some(half_edge) = self.half_edges.first_mut() {
let back_vertex = previous_vertex.unwrap_or_default();
*half_edge = half_edge.clone().merge_with(
PartialHalfEdge::default().with_back_vertex(PartialVertex {
surface_form: back_vertex,
..Default::default()
}),
);
}
let mut half_edges = Vec::new();
for half_edge in self.half_edges {
let half_edge = half_edge.into_full(objects)?;
half_edges.push(half_edge);
}
Ok(Cycle::new(half_edges))
}
}
impl MergeWith for PartialCycle {
fn merge_with(self, other: impl Into<Self>) -> Self {
let other = other.into();
Self {
half_edges: self.half_edges.merge_with(other.half_edges),
}
}
}
impl From<&Cycle> for PartialCycle {
fn from(cycle: &Cycle) -> Self {
Self {
half_edges: cycle.half_edges().cloned().map(Into::into).collect(),
}
}
}