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
use fj_interop::ext::ArrayExt;
use fj_math::{Point, Scalar};
use crate::{
objects::{
Curve, GlobalVertex, Objects, Surface, SurfaceVertex, Vertex,
VerticesInNormalizedOrder,
},
partial::{HasPartial, PartialGlobalEdge, PartialHalfEdge},
storage::Handle,
validate::ValidationError,
};
use super::{CurveBuilder, GlobalVertexBuilder};
pub trait HalfEdgeBuilder: Sized {
fn update_as_circle_from_radius(
self,
radius: impl Into<Scalar>,
objects: &Objects,
) -> Result<Self, ValidationError>;
fn update_as_line_segment_from_points(
self,
surface: Handle<Surface>,
points: [impl Into<Point<2>>; 2],
) -> Self;
fn update_as_line_segment(self) -> Self;
}
impl HalfEdgeBuilder for PartialHalfEdge {
fn update_as_circle_from_radius(
self,
radius: impl Into<Scalar>,
objects: &Objects,
) -> Result<Self, ValidationError> {
let curve = self
.curve()
.into_partial()
.with_global_form(Some(self.extract_global_curve()))
.update_as_circle_from_radius(radius);
let path = curve.path().expect("Expected path that was just created");
let [a_curve, b_curve] =
[Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));
let global_vertex = self
.global_form()
.vertices()
.map(|[global_form, _]| global_form)
.unwrap_or_else(|| {
GlobalVertex::partial()
.update_from_curve_and_position(curve.clone(), a_curve)
.into()
});
let surface_vertex = SurfaceVertex::partial()
.with_position(Some(path.point_from_path_coords(a_curve)))
.with_surface(curve.surface())
.with_global_form(Some(global_vertex))
.build(objects)?;
let [back, front] = [a_curve, b_curve].map(|point_curve| {
Vertex::partial()
.with_position(Some(point_curve))
.with_curve(curve.clone())
.with_surface_form(surface_vertex.clone())
});
Ok(self.with_curve(curve).with_vertices([back, front]))
}
fn update_as_line_segment_from_points(
self,
surface: Handle<Surface>,
points: [impl Into<Point<2>>; 2],
) -> Self {
let vertices = points.map(|point| {
let surface_form = SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(point));
Vertex::partial().with_surface_form(surface_form)
});
self.with_surface(surface)
.with_vertices(vertices)
.update_as_line_segment()
}
fn update_as_line_segment(self) -> Self {
let [from, to] = self.vertices();
let [from_surface, to_surface] =
[&from, &to].map(|vertex| vertex.surface_form());
let surface = self
.curve()
.surface()
.or_else(|| from_surface.surface())
.or_else(|| to_surface.surface())
.expect("Can't infer line segment without a surface");
let points = [&from_surface, &to_surface].map(|vertex| {
vertex
.position()
.expect("Can't infer line segment without surface position")
});
let curve = Curve::partial()
.with_global_form(Some(self.extract_global_curve()))
.with_surface(Some(surface))
.update_as_line_from_points(points);
let [back, front] = {
let vertices = [(from, 0.), (to, 1.)].map(|(vertex, position)| {
vertex.update_partial(|vertex| {
vertex
.with_position(Some([position]))
.with_curve(curve.clone())
})
});
let global_forms = {
let must_switch_order = {
let objects = Objects::new();
let vertices = vertices.clone().map(|vertex| {
vertex
.into_full(&objects)
.unwrap()
.global_form()
.clone()
});
let (_, must_switch_order) =
VerticesInNormalizedOrder::new(vertices);
must_switch_order
};
self.global_form()
.vertices()
.map(
|[a, b]| {
if must_switch_order {
[b, a]
} else {
[a, b]
}
},
)
.map(|[a, b]| [Some(a), Some(b)])
.unwrap_or([None, None])
};
vertices.zip_ext(global_forms).map(|(vertex, global_form)| {
vertex.update_partial(|vertex| {
vertex.clone().with_surface_form(
vertex.surface_form().update_partial(
|surface_vertex| {
surface_vertex.with_global_form(global_form)
},
),
)
})
})
};
self.with_curve(curve).with_vertices([back, front])
}
}
pub trait GlobalEdgeBuilder {
fn update_from_curve_and_vertices(
self,
curve: &Curve,
vertices: &[Handle<Vertex>; 2],
) -> Self;
}
impl GlobalEdgeBuilder for PartialGlobalEdge {
fn update_from_curve_and_vertices(
self,
curve: &Curve,
vertices: &[Handle<Vertex>; 2],
) -> Self {
self.with_curve(Some(curve.global_form().clone()))
.with_vertices(Some(
vertices.clone().map(|vertex| vertex.global_form().clone()),
))
}
}