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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Step 1 implementation
//!
//! The aim of this step is to build an exhaustive list of the segments making up
//! the geometry intersected with the grid: For each segment, if both vertices
//! do not belong to the same cell, we break it into sub-segments until it is the case.
use std::{
cmp::{max, min},
collections::VecDeque,
};
use honeycomb_core::cmap::{CMap2, DartIdType, NULL_DART_ID};
use honeycomb_core::geometry::{CoordsFloat, Vertex2};
use crate::grisubal::model::{Geometry2, GeometryVertex, GridCellId};
use super::Segments;
macro_rules! make_geometry_vertex {
($g: ident, $vid: ident) => {
if $g.poi.contains(&$vid) {
GeometryVertex::PoI($vid)
} else {
GeometryVertex::Regular($vid)
}
};
}
macro_rules! left_intersec {
($va: ident, $vb: ident, $vdart:ident, $cy: ident) => {{
let s = ($vdart.x() - $va.x()) / ($vb.x() - $va.x());
(s, ($vdart.y() - $va.y() - ($vb.y() - $va.y()) * s) / $cy)
}};
}
macro_rules! right_intersec {
($va: ident, $vb: ident, $vdart:ident, $cy: ident) => {{
let s = ($vdart.x() - $va.x()) / ($vb.x() - $va.x());
(s, (($vb.y() - $va.y()) * s - ($vdart.y() - $va.y())) / $cy)
}};
}
macro_rules! down_intersec {
($va: ident, $vb: ident, $vdart:ident, $cx: ident) => {{
let s = ($vdart.y() - $va.y()) / ($vb.y() - $va.y());
(s, (($vb.x() - $va.x()) * s - ($vdart.x() - $va.x())) / $cx)
}};
}
macro_rules! up_intersec {
($va: ident, $vb: ident, $vdart:ident, $cx: ident) => {{
let s = ($vdart.y() - $va.y()) / ($vb.y() - $va.y());
(s, (($vdart.x() - $va.x()) - ($vb.x() - $va.x()) * s) / $cx)
}};
}
#[allow(
clippy::too_many_lines,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]
pub(crate) fn generate_intersection_data<T: CoordsFloat>(
cmap: &CMap2<T>,
geometry: &Geometry2<T>,
[nx, _ny]: [usize; 2],
[cx, cy]: [T; 2],
origin: Vertex2<T>,
) -> (Segments, Vec<(DartIdType, T)>) {
let tmp: Vec<_> = geometry
.segments
.iter()
.map(|&(v1_id, v2_id)| {
// fetch vertices of the segment
let Vertex2(ox, oy) = origin;
let (v1, v2) = (&geometry.vertices[v1_id], &geometry.vertices[v2_id]);
// compute their position in the grid
// we assume that the origin of the grid is at (0., 0.)
let (c1, c2) = (
GridCellId(
((v1.x() - ox) / cx).floor().to_usize().unwrap(),
((v1.y() - oy) / cy).floor().to_usize().unwrap(),
),
GridCellId(
((v2.x() - ox) / cx).floor().to_usize().unwrap(),
((v2.y() - oy) / cy).floor().to_usize().unwrap(),
),
);
(
GridCellId::l1_dist(&c1, &c2),
GridCellId::offset(&c1, &c2),
v1,
v2,
v1_id,
v2_id,
c1,
)
})
.collect();
// total number of intersection
let n_intersec: usize = tmp.iter().map(|(dist, _, _, _, _, _, _)| dist).sum();
// we're using the prefix sum to compute an offset from the start. that's why we need a 0 at the front
// we'll cut off the last element later
let prefix_sum = tmp
.iter()
.map(|(dist, _, _, _, _, _, _)| dist)
.scan(0, |state, &dist| {
*state += dist;
Some(*state - dist) // we want an offset, not the actual sum
});
// preallocate the intersection vector
let mut intersection_metadata = vec![(NULL_DART_ID, T::nan()); n_intersec];
let new_segments: Segments = tmp.iter().zip(prefix_sum).flat_map(|(&(dist, diff, v1, v2, v1_id, v2_id, c1), start)| {
let transform = Box::new(|seg: &[GeometryVertex]| {
assert_eq!(seg.len(), 2);
(seg[0].clone(), seg[1].clone())
});
// check neighbor status
match dist {
// trivial case:
// v1 & v2 belong to the same cell
0 => {
vec![(make_geometry_vertex!(geometry, v1_id), make_geometry_vertex!(geometry, v2_id))]
}
// ok case:
// v1 & v2 belong to neighboring cells
1 => {
// fetch base dart of the cell of v1
#[allow(clippy::cast_possible_truncation)]
let d_base = (1 + 4 * c1.0 + nx * 4 * c1.1) as DartIdType;
// which dart does this correspond to?
#[rustfmt::skip]
let dart_id = match diff {
(-1, 0) => d_base + 3,
( 1, 0) => d_base + 1,
( 0, -1) => d_base ,
( 0, 1) => d_base + 2,
_ => unreachable!(),
};
// what's the vertex associated to the dart?
let v_dart = cmap
.read_vertex(cmap.vertex_id(dart_id))
.expect("E: found a topological vertex with no associated coordinates");
// compute relative position of the intersection on the intersecting edges
// `s` is relative to the segment `v1v2`, `t` to the grid's segment (the origin being `v_dart`)
#[rustfmt::skip]
let (_s, t) = match diff {
(-1, 0) => left_intersec!(v1, v2, v_dart, cy),
( 1, 0) => right_intersec!(v1, v2, v_dart, cy),
( 0, -1) => down_intersec!(v1, v2, v_dart, cx),
( 0, 1) => up_intersec!(v1, v2, v_dart, cx),
_ => unreachable!(),
};
let id = start;
intersection_metadata[id] = (dart_id, t);
vec![
(make_geometry_vertex!(geometry, v1_id), GeometryVertex::Intersec(id)),
(GeometryVertex::Intersec(id), make_geometry_vertex!(geometry, v2_id)),
]
}
// highly annoying case:
// v1 & v2 do not belong to neighboring cell
_ => {
// pure vertical / horizontal traversal are treated separately because it ensures we're not trying
// to compute intersections of parallel segments (which results at best in a division by 0)
let i_ids = start..start+dist;
match diff {
(i, 0) => {
// we can solve the intersection equation
// for each vertical edge of the grid we cross (i times)
let i_base = c1.0 as isize;
let tmp =
// the range is either
// i > 0: i_base..i_base + i
// or
// i < 0: i_base + 1 + i..i_base + 1
(min(i_base, i_base + 1 + i)..max(i_base + i, i_base + 1)).zip(i_ids).map(|(x, id)| {
// cell base dart
let d_base =
(1 + 4 * x + (nx * 4 * c1.1) as isize) as DartIdType;
// intersected dart
let dart_id = if i.is_positive() {
d_base + 1
} else {
d_base + 3
};
// vertex associated to the intersected dart
let v_dart = cmap.read_vertex(cmap.vertex_id(dart_id))
.expect("E: found a topological vertex with no associated coordinates");
// compute intersection
let (_s, t) = if i.is_positive() {
right_intersec!(v1, v2, v_dart, cy)
} else {
left_intersec!(v1, v2, v_dart, cy)
};
intersection_metadata[id] = (dart_id, t);
GeometryVertex::Intersec(id)
});
// because of how the range is written, we need to reverse the iterator in one case
// to keep intersection ordered from v1 to v2 (i.e. ensure the segments we build are correct)
let mut vs: VecDeque<GeometryVertex> = if i > 0 {
tmp.collect()
} else {
tmp.rev().collect()
};
// complete the vertex list
vs.push_front(make_geometry_vertex!(geometry, v1_id));
vs.push_back(make_geometry_vertex!(geometry, v2_id));
vs.make_contiguous()
.windows(2)
.map(transform)
.collect::<Vec<_>>()
}
(0, j) => {
// we can solve the intersection equation
// for each horizontal edge of the grid we cross (j times)
let j_base = c1.1 as isize;
let tmp =
// the range is either
// j > 0: j_base..j_base + j
// or
// j < 0: j_base + 1 + j..j_base + 1
(min(j_base, j_base + 1 + j)..max(j_base + j, j_base + 1)).zip(i_ids).map(|(y, id)| {
// cell base dart
let d_base = (1 + 4 * c1.0 + nx * 4 * y as usize) as DartIdType;
// intersected dart
let dart_id = if j.is_positive() { d_base + 2 } else { d_base };
// vertex associated to the intersected dart
let v_dart = cmap.read_vertex(cmap.vertex_id(dart_id))
.expect("E: found a topological vertex with no associated coordinates");
// compute intersection
let (_s, t) = if j.is_positive() {
up_intersec!(v1, v2, v_dart, cx)
} else {
down_intersec!(v1, v2, v_dart, cx)
};
intersection_metadata[id] = (dart_id, t);
GeometryVertex::Intersec(id)
});
// because of how the range is written, we need to reverse the iterator in one case
// to keep intersection ordered from v1 to v2 (i.e. ensure the segments we build are correct)
let mut vs: VecDeque<GeometryVertex> = if j > 0 {
tmp.collect()
} else {
tmp.rev().collect()
};
// complete the vertex list
vs.push_front(make_geometry_vertex!(geometry, v1_id));
vs.push_back(make_geometry_vertex!(geometry, v2_id));
vs.make_contiguous()
.windows(2)
.map(transform)
.collect::<Vec<_>>()
}
(i, j) => {
// in order to process this, we'll consider a "sub-grid" & use the direction of the segment to
// deduce which pair of dart we are supposed to intersect
// we also have to consider corner traversal; this corresponds to intersecting both darts of
// the pair at respective relative positions 1 and 0 (or 0 and 1)
let i_base = c1.0 as isize;
let j_base = c1.1 as isize;
let i_cell_range = min(i_base, i_base + i)..=max(i_base + i, i_base);
let j_cell_range = min(j_base, j_base + j)..=max(j_base + j, j_base);
let subgrid_cells =
i_cell_range.flat_map(|x| j_cell_range.clone().map(move |y| (x, y)));
let mut intersec_data: Vec<(T, T, DartIdType)> = subgrid_cells
.map(|(x, y)| {
// cell base dart
let d_base = (1 + 4 * x + nx as isize * 4 * y) as DartIdType;
// (potentially) intersected darts
let vdart_id = if i.is_positive() {
d_base + 1
} else {
d_base + 3
};
let hdart_id = if j.is_positive() { d_base + 2 } else { d_base };
// associated vertices
let v_vdart = cmap.read_vertex(cmap.vertex_id(vdart_id))
.expect("E: found a topological vertex with no associated coordinates");
let v_hdart = cmap.read_vertex(cmap.vertex_id(hdart_id))
.expect("E: found a topological vertex with no associated coordinates");
// compute (potential) intersections
let v_coeffs = if i.is_positive() {
right_intersec!(v1, v2, v_vdart, cy)
} else {
left_intersec!(v1, v2, v_vdart, cy)
};
let h_coeffs = if j.is_positive() {
up_intersec!(v1, v2, v_hdart, cx)
} else {
down_intersec!(v1, v2, v_hdart, cx)
};
(hdart_id, vdart_id, v_coeffs, h_coeffs)
})
.filter_map(|(hdart_id, vdart_id, (vs, vt), (hs, ht))| {
let zero = T::zero();
let one = T::one();
// there is one corner intersection to check per (i, j) quadrant
match (i.is_positive(), j.is_positive()) {
// check
(true, true) | (false, false) => {
if ((vt - one).abs() < T::epsilon())
&& (ht.abs() < T::epsilon())
{
return Some((hs, zero, hdart_id));
}
}
(false, true) | (true, false) => {
if (vt.abs() < T::epsilon())
&& ((ht - one).abs() < T::epsilon())
{
return Some((vs, zero, vdart_id));
}
}
}
// we can deduce if and which side is intersected using s and t values
// these should be comprised strictly between 0 and 1 for regular intersections
if (T::epsilon() <= vs)
& (vs <= one - T::epsilon())
& (T::epsilon() <= vt)
& (vt <= one - T::epsilon())
{
return Some((vs, vt, vdart_id)); // intersect vertical side
}
if (T::epsilon() < hs)
& (hs <= one - T::epsilon())
& (T::epsilon() <= ht)
& (ht <= one - T::epsilon())
{
return Some((hs, ht, hdart_id)); // intersect horizontal side
}
// intersect none; this is possible since we're looking at cells of a subgrid,
// not following through the segment's intersections
None
})
.collect();
// sort intersections from v1 to v2
intersec_data.retain(|(s, _, _)| (T::zero() <= *s) && (*s <= T::one()));
// panic unreachable because of the retain above; there's no s s.t. s == NaN
intersec_data.sort_by(|(s1, _, _), (s2, _, _)| s1.partial_cmp(s2)
.expect("E: unreachable"));
// collect geometry vertices
let mut vs = vec![make_geometry_vertex!(geometry, v1_id)];
vs.extend(intersec_data.iter_mut().zip(i_ids).map(|((_, t, dart_id), id)| {
if t.is_zero() {
// we assume that the segment fully goes through the corner and does not land exactly
// on it, this allows us to compute directly the dart from which the next segment
// should start: the one incident to the vertex in the opposite quadrant
// in that case, the preallocated intersection metadata slot will stay as (0, Nan)
// this is ok, we can simply ignore the entry when processing the data later
let dart_in = *dart_id;
GeometryVertex::IntersecCorner(dart_in)
} else {
intersection_metadata[id] = (*dart_id, *t);
GeometryVertex::Intersec(id)
}
}));
vs.push(make_geometry_vertex!(geometry, v2_id));
vs.windows(2)
.map(transform)
.collect::<Vec<_>>()
}
}
}
}
}).collect();
(new_segments, intersection_metadata)
}