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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::vec;
use fj_math::Point;
use crate::objects::{Curve, Face};
use super::CurveEdgeIntersection;
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CurveFaceIntersection {
pub intervals: Vec<CurveFaceIntersectionInterval>,
}
impl CurveFaceIntersection {
pub fn from_intervals(
intervals: impl IntoIterator<
Item = impl Into<CurveFaceIntersectionInterval>,
>,
) -> Self {
let intervals = intervals.into_iter().map(Into::into).collect();
Self { intervals }
}
pub fn compute(curve: &Curve, face: &Face) -> Self {
let half_edges = face.all_cycles().flat_map(|cycle| cycle.half_edges());
let mut intersections = Vec::new();
for half_edge in half_edges {
let intersection = CurveEdgeIntersection::compute(curve, half_edge);
if let Some(intersection) = intersection {
match intersection {
CurveEdgeIntersection::Point { point_on_curve } => {
intersections.push(point_on_curve);
}
CurveEdgeIntersection::Coincident { points_on_curve } => {
intersections.extend(points_on_curve);
}
}
}
}
assert!(intersections.len() % 2 == 0);
intersections.sort();
let intervals = intersections
.chunks(2)
.map(|chunk| {
CurveFaceIntersectionInterval {
start: chunk[0],
end: chunk[1],
}
})
.collect();
CurveFaceIntersection { intervals }
}
pub fn merge(&self, other: &Self) -> Self {
let mut self_intervals = self.intervals.iter().copied();
let mut other_interval = other.intervals.iter().copied();
let mut next_self = self_intervals.next();
let mut next_other = other_interval.next();
let mut intervals = Vec::new();
while let (Some(self_), Some(other)) = (next_self, next_other) {
let overlap_start = self_.start.max(other.start);
let overlap_end = self_.end.min(other.end);
if overlap_start < overlap_end {
intervals.push(CurveFaceIntersectionInterval {
start: overlap_start,
end: overlap_end,
});
}
if self_.end <= overlap_end {
next_self = self_intervals.next();
}
if other.end <= overlap_end {
next_other = other_interval.next();
}
}
Self { intervals }
}
pub fn is_empty(&self) -> bool {
self.intervals.is_empty()
}
}
impl IntoIterator for CurveFaceIntersection {
type Item = CurveFaceIntersectionInterval;
type IntoIter = vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.intervals.into_iter()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CurveFaceIntersectionInterval {
pub start: Point<1>,
pub end: Point<1>,
}
impl<P> From<[P; 2]> for CurveFaceIntersectionInterval
where
P: Into<Point<1>>,
{
fn from(interval: [P; 2]) -> Self {
let [start, end] = interval.map(Into::into);
CurveFaceIntersectionInterval { start, end }
}
}
#[cfg(test)]
mod tests {
use crate::{
objects::{Curve, Face, Surface},
partial::HasPartial,
stores::{Handle, Stores},
};
use super::CurveFaceIntersection;
#[test]
fn compute() {
let stores = Stores::new();
let surface = stores.surfaces.insert(Surface::xy_plane());
let curve = Handle::<Curve>::partial()
.with_surface(Some(surface.clone()))
.as_line_from_points([[-3., 0.], [-2., 0.]])
.build(&stores);
#[rustfmt::skip]
let exterior = [
[-2., -2.],
[ 2., -2.],
[ 2., 2.],
[-2., 2.],
];
#[rustfmt::skip]
let interior = [
[-1., -1.],
[-1., 1.],
[ 1., 1.],
[ 1., -1.],
];
let face = Face::builder(&stores, surface)
.with_exterior_polygon_from_points(exterior)
.with_interior_polygon_from_points(interior)
.build();
let expected =
CurveFaceIntersection::from_intervals([[[1.], [2.]], [[4.], [5.]]]);
assert_eq!(CurveFaceIntersection::compute(&curve, &face), expected);
}
#[test]
fn merge() {
let a = CurveFaceIntersection::from_intervals([
[[0.], [1.]], [[2.], [5.]], [[7.], [8.]], [[9.], [11.]], [[14.], [16.]], [[18.], [21.]], [[23.], [25.]], [[26.], [28.]], [[31.], [35.]], [[36.], [38.]], [[39.], [40.]], [[41.], [45.]], [[48.], [49.]], [[50.], [52.]], [[53.], [58.]], [[60.], [61.]], [[62.], [63.]], [[65.], [66.]], ]);
let b = CurveFaceIntersection::from_intervals([
[[0.], [1.]], [[3.], [4.]], [[6.], [9.]], [[10.], [12.]], [[13.], [15.]], [[17.], [19.]], [[20.], [22.]], [[24.], [27.]], [[30.], [32.]], [[33.], [34.]], [[37.], [41.]], [[42.], [43.]], [[44.], [46.]], [[47.], [51.]], [[54.], [55.]], [[56.], [57.]], [[59.], [64.]], ]);
let merged = a.merge(&b);
let expected = CurveFaceIntersection::from_intervals([
[[0.], [1.]], [[3.], [4.]], [[7.], [8.]], [[10.], [11.]], [[14.], [15.]], [[18.], [19.]], [[20.], [21.]], [[24.], [25.]], [[26.], [27.]], [[31.], [32.]], [[33.], [34.]], [[37.], [38.]], [[39.], [40.]], [[42.], [43.]], [[44.], [45.]], [[48.], [49.]], [[50.], [51.]], [[54.], [55.]], [[56.], [57.]], [[60.], [61.]], [[62.], [63.]], ]);
assert_eq!(merged, expected);
}
}