honeycomb_kernels/grisubal/routines/
insert_new_edges.rs1use honeycomb_core::cmap::{CMap2, DartIdType};
6use honeycomb_core::geometry::CoordsFloat;
7use honeycomb_core::stm::atomically_with_err;
8
9use crate::cell_insertion::insert_vertices_on_edge;
10use crate::grisubal::model::{Boundary, MapEdge};
11
12pub(crate) fn insert_edges_in_map<T: CoordsFloat>(cmap: &mut CMap2<T>, edges: &[MapEdge<T>]) {
13 let n_darts_per_seg: Vec<_> = edges
16 .iter()
17 .map(|e| 2 + 2 * e.intermediates.len())
18 .collect();
19 let n_tot: usize = n_darts_per_seg.iter().sum();
20 let tmp = cmap.add_free_darts(n_tot) as usize;
21 let prefix_sum: Vec<usize> = n_darts_per_seg
25 .iter()
26 .scan(0, |state, &n_d| {
27 *state += n_d;
28 Some(*state - n_d) })
30 .collect();
31 #[allow(clippy::cast_possible_truncation)]
32 let dart_slices: Vec<Vec<DartIdType>> = n_darts_per_seg
33 .iter()
34 .zip(prefix_sum.iter())
35 .map(|(n_d, start)| {
36 ((tmp + start) as DartIdType..(tmp + start + n_d) as DartIdType).collect::<Vec<_>>()
37 })
38 .collect();
39
40 for (
42 MapEdge {
43 start,
44 intermediates,
45 end,
46 },
47 dslice,
48 ) in edges.iter().zip(dart_slices.iter())
49 {
50 let b1_start_old = cmap.beta::<1>(*start);
52 let b0_end_old = cmap.beta::<0>(*end);
53 cmap.force_unlink::<1>(*start).unwrap();
54 cmap.force_unlink::<1>(b0_end_old).unwrap();
55
56 let &[d_new, b2_d_new] = &dslice[0..2] else {
57 unreachable!()
58 };
59 cmap.force_link::<2>(d_new, b2_d_new).unwrap();
60
61 cmap.force_link::<1>(*start, d_new).unwrap();
63 cmap.force_link::<1>(b2_d_new, b1_start_old).unwrap();
64 cmap.force_link::<1>(d_new, *end).unwrap();
65 cmap.force_link::<1>(b0_end_old, b2_d_new).unwrap();
66
67 if !intermediates.is_empty() {
68 let edge_id = cmap.edge_id(d_new);
70 let new_darts = &dslice[2..];
71 atomically_with_err(|trans| {
72 insert_vertices_on_edge(
73 cmap,
74 trans,
75 edge_id,
76 new_darts,
77 &vec![T::from(0.5).unwrap(); intermediates.len()],
78 )
79 })
80 .unwrap();
81 let mut dart_id = cmap.beta::<1>(edge_id as DartIdType);
83 for v in intermediates {
84 let vid = cmap.vertex_id(dart_id);
85 let _ = cmap.force_write_vertex(vid, *v);
86 dart_id = cmap.beta::<1>(dart_id);
87 }
88 }
89
90 let mut d_boundary = cmap.beta::<1>(*start);
91 while d_boundary != *end {
92 cmap.force_write_attribute::<Boundary>(d_boundary, Boundary::Left);
93 cmap.force_write_attribute::<Boundary>(cmap.beta::<2>(d_boundary), Boundary::Right);
94 d_boundary = cmap.beta::<1>(d_boundary);
95 }
96 }
97}