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
use fj_math::Point;
use crate::{
objects::{HalfEdge, Surface},
partial::{Partial, PartialCycle},
};
use super::HalfEdgeBuilder;
pub trait CycleBuilder {
fn update_as_polygon_from_points(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>>;
fn update_as_polygon(&mut self);
fn add_half_edge(&mut self) -> Partial<HalfEdge>;
fn add_half_edge_from_point_to_start(
&mut self,
point: impl Into<Point<2>>,
) -> Partial<HalfEdge>;
}
impl CycleBuilder for PartialCycle {
fn update_as_polygon_from_points(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>> {
let surface = surface.into();
let mut points = points.into_iter().map(Into::into);
let mut half_edges = Vec::new();
if let Some(point) = points.next() {
let mut half_edge = self.add_half_edge_from_point_to_start(point);
half_edge.write().replace_surface(surface);
half_edges.push(half_edge);
}
for point in points {
let half_edge = self.add_half_edge_from_point_to_start(point);
half_edges.push(half_edge);
}
self.update_as_polygon();
half_edges
}
fn update_as_polygon(&mut self) {
for half_edge in &mut self.half_edges {
half_edge.write().update_as_line_segment();
}
}
fn add_half_edge(&mut self) -> Partial<HalfEdge> {
let mut new_half_edge = Partial::<HalfEdge>::new();
let (first_half_edge, mut last_half_edge) =
match self.half_edges.first() {
Some(first_half_edge) => {
let first_half_edge = first_half_edge.clone();
let last_half_edge = self
.half_edges
.last()
.cloned()
.unwrap_or_else(|| first_half_edge.clone());
(first_half_edge, last_half_edge)
}
None => (new_half_edge.clone(), new_half_edge.clone()),
};
{
let shared_surface_vertex =
new_half_edge.read().back().read().surface_form.clone();
let mut last_half_edge = last_half_edge.write();
last_half_edge.front_mut().write().surface_form =
shared_surface_vertex;
last_half_edge.infer_global_form();
}
{
let shared_surface_vertex =
first_half_edge.read().back().read().surface_form.clone();
let shared_surface = shared_surface_vertex.read().surface.clone();
let mut new_half_edge = new_half_edge.write();
new_half_edge.front_mut().write().surface_form =
shared_surface_vertex;
new_half_edge.replace_surface(shared_surface);
new_half_edge.infer_global_form();
}
self.half_edges.push(new_half_edge.clone());
new_half_edge
}
fn add_half_edge_from_point_to_start(
&mut self,
point: impl Into<Point<2>>,
) -> Partial<HalfEdge> {
let mut half_edge = self.add_half_edge();
half_edge
.write()
.back_mut()
.write()
.surface_form
.write()
.position = Some(point.into());
half_edge
}
}