honeycomb_core/cmap/dim2/
link_and_sew.rs

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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! (Un)sew and (un)link implementations
//!
//! This module contains code used to implement sew, unsew, link and unlink operations in all
//! dimensions for which they are defined (1, 2) for a [`CMap2`].

// ------ IMPORTS

use crate::prelude::{CMap2, DartIdType, NULL_DART_ID};
use crate::{
    attributes::{AttributeStorage, UnknownAttributeStorage},
    geometry::CoordsFloat,
};
use stm::{atomically, StmError, Transaction};

// ------ CONTENT

/// **Sew and unsew operations**
impl<T: CoordsFloat> CMap2<T> {
    /// 1-sew operation.
    ///
    /// This operation corresponds to *coherently linking* two darts via the *β<sub>1</sub>*
    /// function. For a thorough explanation of this operation (and implied hypothesis &
    /// consequences), refer to the [user guide][UG].
    ///
    /// [UG]: https://lihpc-computational-geometry.github.io/honeycomb/
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    /// - `policy: SewPolicy` -- Geometrical sewing policy to follow.
    ///
    /// After the sewing operation, these darts will verify
    /// *β<sub>1</sub>(`lhs_dart`) = `rhs_dart`*. The *β<sub>0</sub>* function is also updated.
    ///
    /// # Panics
    ///
    /// The method may panic if the two darts are not 1-sewable.
    ///
    pub fn one_sew(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        // this operation only makes sense if lhs_dart is associated to a fully defined edge, i.e.
        // its image through beta2 is defined & has a valid associated vertex (we assume the second
        // condition is valid if the first one is)
        // if that is not the case, the sewing operation becomes a linking operation
        let b2lhs_dart_id = self.beta::<2>(lhs_dart_id);
        if b2lhs_dart_id == NULL_DART_ID {
            self.one_link(lhs_dart_id, rhs_dart_id);
        } else {
            // fetch vertices ID before topology update
            let b2lhs_vid_old = self.vertex_id(b2lhs_dart_id);
            let rhs_vid_old = self.vertex_id(rhs_dart_id);
            // update the topology
            self.one_link(lhs_dart_id, rhs_dart_id);
            // merge vertices & attributes from the old IDs to the new one
            // FIXME: VertexIdentifier should be cast to DartIdentifier
            self.vertices
                .merge(self.vertex_id(rhs_dart_id), b2lhs_vid_old, rhs_vid_old);
            self.attributes.merge_vertex_attributes(
                self.vertex_id(rhs_dart_id),
                b2lhs_vid_old,
                rhs_vid_old,
            );
        }
    }

    /// Atomically 1-sew two darts.
    pub fn atomically_one_sew(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        atomically(|trans| {
            let b2lhs_dart_id = self.betas[(2, lhs_dart_id)].read(trans)?;
            if b2lhs_dart_id == NULL_DART_ID {
                self.one_link_core(trans, lhs_dart_id, rhs_dart_id)
            } else {
                let b2lhs_vid_old = self.vertex_id_transac(trans, b2lhs_dart_id)?;
                let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;

                self.one_link_core(trans, lhs_dart_id, rhs_dart_id)?;

                let new_vid = self.vertex_id_transac(trans, rhs_dart_id)?;

                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.vertices
                    .merge_core(trans, new_vid, b2lhs_vid_old, rhs_vid_old)?;
                self.attributes.merge_vertex_attributes_transac(
                    trans,
                    new_vid,
                    b2lhs_vid_old,
                    rhs_vid_old,
                )?;
                Ok(())
            }
        });
    }

    /// 2-sew operation.
    ///
    /// This operation corresponds to *coherently linking* two darts via the *β<sub>2</sub>*
    /// function. For a thorough explanation of this operation (and implied hypothesis &
    /// consequences), refer to the [user guide][UG].
    ///
    /// [UG]: https://lihpc-computational-geometry.github.io/honeycomb/
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    /// - `policy: SewPolicy` -- Geometrical sewing policy to follow.
    ///
    /// After the sewing operation, these darts will verify
    /// *β<sub>2</sub>(`lhs_dart`) = `rhs_dart`* and *β<sub>2</sub>(`rhs_dart`) = `lhs_dart`*.
    ///
    /// # Panics
    ///
    /// The method may panic if:
    /// - the two darts are not 2-sewable,
    /// - the method cannot resolve orientation issues.
    ///
    pub fn two_sew(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        let b1lhs_dart_id = self.beta::<1>(lhs_dart_id);
        let b1rhs_dart_id = self.beta::<1>(rhs_dart_id);
        // match (is lhs 1-free, is rhs 1-free)
        match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
            // trivial case, no update needed
            (true, true) => {
                self.two_link(lhs_dart_id, rhs_dart_id);
            }
            // update vertex associated to b1rhs/lhs
            (true, false) => {
                // fetch vertices ID before topology update
                let lhs_eid_old = self.edge_id(lhs_dart_id);
                let rhs_eid_old = self.edge_id(b1rhs_dart_id);
                let lhs_vid_old = self.vertex_id(lhs_dart_id);
                let b1rhs_vid_old = self.vertex_id(b1rhs_dart_id);
                // update the topology
                self.two_link(lhs_dart_id, rhs_dart_id);
                // merge vertices & attributes from the old IDs to the new one
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.vertices
                    .merge(self.vertex_id(lhs_dart_id), lhs_vid_old, b1rhs_vid_old);
                self.attributes.merge_vertex_attributes(
                    self.vertex_id(lhs_dart_id),
                    lhs_vid_old,
                    b1rhs_vid_old,
                );
                self.attributes.merge_edge_attributes(
                    self.edge_id(lhs_dart_id),
                    lhs_eid_old,
                    rhs_eid_old,
                );
            }
            // update vertex associated to b1lhs/rhs
            (false, true) => {
                // fetch vertices ID before topology update
                let lhs_eid_old = self.edge_id(lhs_dart_id);
                let rhs_eid_old = self.edge_id(b1rhs_dart_id);
                let b1lhs_vid_old = self.vertex_id(b1lhs_dart_id);
                let rhs_vid_old = self.vertex_id(rhs_dart_id);
                // update the topology
                self.two_link(lhs_dart_id, rhs_dart_id);
                // merge vertices & attributes from the old IDs to the new one
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.vertices
                    .merge(self.vertex_id(rhs_dart_id), b1lhs_vid_old, rhs_vid_old);
                self.attributes.merge_vertex_attributes(
                    self.vertex_id(rhs_dart_id),
                    b1lhs_vid_old,
                    rhs_vid_old,
                );
                self.attributes.merge_edge_attributes(
                    self.edge_id(lhs_dart_id),
                    lhs_eid_old,
                    rhs_eid_old,
                );
            }
            // update both vertices making up the edge
            (false, false) => {
                // fetch vertices ID before topology update
                let lhs_eid_old = self.edge_id(lhs_dart_id);
                let rhs_eid_old = self.edge_id(b1rhs_dart_id);
                // (lhs/b1rhs) vertex
                let lhs_vid_old = self.vertex_id(lhs_dart_id);
                let b1rhs_vid_old = self.vertex_id(b1rhs_dart_id);
                // (b1lhs/rhs) vertex
                let b1lhs_vid_old = self.vertex_id(b1lhs_dart_id);
                let rhs_vid_old = self.vertex_id(rhs_dart_id);

                // check orientation
                // FIXME: using `get` is suboptimal because read ops imply a copy in our collections
                // FIXME: maybe we should directly read into the storage instead of using its API
                #[rustfmt::skip]
                if let (
                    Some(l_vertex), Some(b1r_vertex), // (lhs/b1rhs) vertices
                    Some(b1l_vertex), Some(r_vertex), // (b1lhs/rhs) vertices
                ) = (
                    self.vertices.get(lhs_vid_old), self.vertices.get(b1rhs_vid_old),// (lhs/b1rhs)
                    self.vertices.get(b1lhs_vid_old), self.vertices.get(rhs_vid_old) // (b1lhs/rhs)
                )
                {
                    let lhs_vector = b1l_vertex - l_vertex;
                    let rhs_vector = b1r_vertex - r_vertex;
                    // dot product should be negative if the two darts have opposite direction
                    // we could also put restriction on the angle made by the two darts to prevent
                    // drastic deformation
                    // FIXME: should we crash in case of inconsistent orientation?
                    assert!(
                        lhs_vector.dot(&rhs_vector) < T::zero(),
                        "{}",
                        format!("Dart {lhs_dart_id} and {rhs_dart_id} do not have consistent orientation for 2-sewing"),
                    );
                };

                // update the topology
                self.two_link(lhs_dart_id, rhs_dart_id);
                // merge vertices & attributes from the old IDs to the new one
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.vertices
                    .merge(self.vertex_id(lhs_dart_id), lhs_vid_old, b1rhs_vid_old);
                self.vertices
                    .merge(self.vertex_id(rhs_dart_id), b1lhs_vid_old, rhs_vid_old);
                self.attributes.merge_vertex_attributes(
                    self.vertex_id(lhs_dart_id),
                    lhs_vid_old,
                    b1rhs_vid_old,
                );
                self.attributes.merge_vertex_attributes(
                    self.vertex_id(rhs_dart_id),
                    b1lhs_vid_old,
                    rhs_vid_old,
                );
                self.attributes.merge_edge_attributes(
                    self.edge_id(lhs_dart_id),
                    lhs_eid_old,
                    rhs_eid_old,
                );
            }
        }
    }

    #[allow(clippy::missing_panics_doc)]
    /// Atomically 2-sew two darts.
    #[allow(clippy::too_many_lines)]
    pub fn atomically_two_sew(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        atomically(|trans| {
            let b1lhs_dart_id = self.betas[(1, lhs_dart_id)].read(trans)?;
            let b1rhs_dart_id = self.betas[(1, rhs_dart_id)].read(trans)?;
            // match (is lhs 1-free, is rhs 1-free)
            match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
                // trivial case, no update needed
                (true, true) => {
                    self.two_link_core(trans, lhs_dart_id, rhs_dart_id)?;
                }
                // update vertex associated to b1rhs/lhs
                (true, false) => {
                    // fetch vertices ID before topology update
                    let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
                    let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
                    let b1rhs_vid_old = self.vertex_id_transac(trans, b1rhs_dart_id)?;
                    // update the topology
                    self.two_link_core(trans, lhs_dart_id, rhs_dart_id)?;
                    // merge vertices & attributes from the old IDs to the new one
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    let lhs_vid_new = self.vertex_id_transac(trans, lhs_dart_id)?;
                    let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
                    self.vertices
                        .merge_core(trans, lhs_vid_new, lhs_vid_old, b1rhs_vid_old)?;
                    self.attributes.merge_vertex_attributes_transac(
                        trans,
                        lhs_vid_new,
                        lhs_vid_old,
                        b1rhs_vid_old,
                    )?;
                    self.attributes.merge_edge_attributes_transac(
                        trans,
                        eid_new,
                        lhs_eid_old,
                        rhs_eid_old,
                    )?;
                }
                // update vertex associated to b1lhs/rhs
                (false, true) => {
                    // fetch vertices ID before topology update
                    let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
                    let b1lhs_vid_old = self.vertex_id_transac(trans, b1lhs_dart_id)?;
                    let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
                    // update the topology
                    self.two_link_core(trans, lhs_dart_id, rhs_dart_id)?;
                    // merge vertices & attributes from the old IDs to the new one
                    let rhs_vid_new = self.vertex_id_transac(trans, rhs_dart_id)?;
                    let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
                    self.vertices
                        .merge_core(trans, rhs_vid_new, b1lhs_vid_old, rhs_vid_old)?;
                    self.attributes.merge_vertex_attributes_transac(
                        trans,
                        rhs_vid_new,
                        b1lhs_vid_old,
                        rhs_vid_old,
                    )?;
                    self.attributes.merge_edge_attributes_transac(
                        trans,
                        eid_new,
                        lhs_eid_old,
                        rhs_eid_old,
                    )?;
                }
                // update both vertices making up the edge
                (false, false) => {
                    // fetch vertices ID before topology update
                    let lhs_eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let rhs_eid_old = self.edge_id_transac(trans, b1rhs_dart_id)?;
                    // (lhs/b1rhs) vertex
                    let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
                    let b1rhs_vid_old = self.vertex_id_transac(trans, b1rhs_dart_id)?;
                    // (b1lhs/rhs) vertex
                    let b1lhs_vid_old = self.vertex_id_transac(trans, b1lhs_dart_id)?;
                    let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;

                    // check orientation
                    #[rustfmt::skip]
                    if let (
                        Some(l_vertex), Some(b1r_vertex), // (lhs/b1rhs) vertices
                        Some(b1l_vertex), Some(r_vertex), // (b1lhs/rhs) vertices
                    ) = (
                        self.vertices.get(lhs_vid_old), self.vertices.get(b1rhs_vid_old),// (lhs/b1rhs)
                        self.vertices.get(b1lhs_vid_old), self.vertices.get(rhs_vid_old) // (b1lhs/rhs)
                    )
                    {
                        let lhs_vector = b1l_vertex - l_vertex;
                        let rhs_vector = b1r_vertex - r_vertex;
                        // dot product should be negative if the two darts have opposite direction
                        // we could also put restriction on the angle made by the two darts to prevent
                        // drastic deformation
                        assert!(
                            lhs_vector.dot(&rhs_vector) < T::zero(),
                            "{}",
                            format!("Dart {lhs_dart_id} and {rhs_dart_id} do not have consistent orientation for 2-sewing"),
                        );
                    };

                    // update the topology
                    self.two_link_core(trans, lhs_dart_id, rhs_dart_id)?;
                    // merge vertices & attributes from the old IDs to the new one
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    let lhs_vid_new = self.vertex_id_transac(trans, lhs_dart_id)?;
                    let rhs_vid_new = self.vertex_id_transac(trans, rhs_dart_id)?;
                    let eid_new = self.edge_id_transac(trans, lhs_dart_id)?;
                    self.vertices
                        .merge_core(trans, lhs_vid_new, lhs_vid_old, b1rhs_vid_old)?;
                    self.vertices
                        .merge_core(trans, rhs_vid_new, b1lhs_vid_old, rhs_vid_old)?;
                    self.attributes.merge_vertex_attributes_transac(
                        trans,
                        lhs_vid_new,
                        lhs_vid_old,
                        b1rhs_vid_old,
                    )?;
                    self.attributes.merge_vertex_attributes_transac(
                        trans,
                        rhs_vid_new,
                        b1lhs_vid_old,
                        rhs_vid_old,
                    )?;
                    self.attributes.merge_edge_attributes_transac(
                        trans,
                        eid_new,
                        lhs_eid_old,
                        rhs_eid_old,
                    )?;
                }
            }
            Ok(())
        });
    }

    /// 1-unsew operation.
    ///
    /// This operation corresponds to *coherently separating* two darts linked via the
    /// *β<sub>1</sub>* function. For a thorough explanation of this operation (and implied
    /// hypothesis & consequences), refer to the [user guide][UG].
    ///
    /// [UG]: https://lihpc-computational-geometry.github.io/honeycomb/
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to separate.
    /// - `policy: UnsewPolicy` -- Geometrical unsewing policy to follow.
    ///
    /// Note that we do not need to take two darts as arguments since the second dart can be
    /// obtained through the *β<sub>1</sub>* function. The *β<sub>0</sub>* function is also updated.
    ///
    /// # Panics
    ///
    /// The method may panic if there's a missing attribute at the splitting step. While the
    /// implementation could fall back to a simple unlink operation, it probably should have been
    /// called by the user, instead of unsew, in the first place.
    pub fn one_unsew(&self, lhs_dart_id: DartIdType) {
        let b2lhs_dart_id = self.beta::<2>(lhs_dart_id);
        if b2lhs_dart_id == NULL_DART_ID {
            self.one_unlink(lhs_dart_id);
        } else {
            // fetch IDs before topology update
            let rhs_dart_id = self.beta::<1>(lhs_dart_id);
            let vid_old = self.vertex_id(rhs_dart_id);
            // update the topology
            self.one_unlink(lhs_dart_id);
            // split vertices & attributes from the old ID to the new ones
            // FIXME: VertexIdentifier should be cast to DartIdentifier
            self.vertices.split(
                self.vertex_id(b2lhs_dart_id),
                self.vertex_id(rhs_dart_id),
                vid_old,
            );
            self.attributes.split_vertex_attributes(
                self.vertex_id(b2lhs_dart_id),
                self.vertex_id(rhs_dart_id),
                vid_old,
            );
        }
    }

    /// Atomically 1-unsew two darts.
    pub fn atomically_one_unsew(&self, lhs_dart_id: DartIdType) {
        atomically(|trans| {
            let b2lhs_dart_id = self.betas[(2, lhs_dart_id)].read(trans)?;
            if b2lhs_dart_id == NULL_DART_ID {
                self.one_unlink_core(trans, lhs_dart_id)?;
            } else {
                // fetch IDs before topology update
                let rhs_dart_id = self.betas[(1, lhs_dart_id)].read(trans)?;
                let vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
                // update the topology
                self.one_unlink_core(trans, lhs_dart_id)?;
                // split vertices & attributes from the old ID to the new ones
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                let (new_lhs, new_rhs) = (
                    self.vertex_id_transac(trans, b2lhs_dart_id)?,
                    self.vertex_id_transac(trans, rhs_dart_id)?,
                );
                self.vertices.split_core(trans, new_lhs, new_rhs, vid_old)?;
                self.attributes
                    .split_vertex_attributes_transac(trans, new_lhs, new_rhs, vid_old)?;
            }
            Ok(())
        });
    }

    /// 2-unsew operation.
    ///
    /// This operation corresponds to *coherently separating* two darts linked via the
    /// *β<sub>2</sub>* function. For a thorough explanation of this operation (and implied
    /// hypothesis & consequences), refer to the [user guide][UG].
    ///
    /// [UG]: https://lihpc-computational-geometry.github.io/honeycomb/
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to separate.
    /// - `policy: UnsewPolicy` -- Geometrical unsewing policy to follow.
    ///
    /// Note that we do not need to take two darts as arguments since the second dart can be
    /// obtained through the *β<sub>2</sub>* function.
    ///
    /// # Panics
    ///
    /// The method may panic if there's a missing attribute at the splitting step. While the
    /// implementation could fall back to a simple unlink operation, it probably should have been
    /// called by the user, instead of unsew, in the first place.
    pub fn two_unsew(&self, lhs_dart_id: DartIdType) {
        let rhs_dart_id = self.beta::<2>(lhs_dart_id);
        let b1lhs_dart_id = self.beta::<1>(lhs_dart_id);
        let b1rhs_dart_id = self.beta::<1>(rhs_dart_id);
        // match (is lhs 1-free, is rhs 1-free)
        match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
            (true, true) => {
                // fetch IDs before topology update
                let eid_old = self.edge_id(lhs_dart_id);
                // update the topology
                self.two_unlink(lhs_dart_id);
                // split attributes from the old ID to the new ones
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.attributes
                    .split_edge_attributes(lhs_dart_id, rhs_dart_id, eid_old);
            }
            (true, false) => {
                // fetch IDs before topology update
                let eid_old = self.edge_id(lhs_dart_id);
                let lhs_vid_old = self.vertex_id(lhs_dart_id);
                // update the topology
                self.two_unlink(lhs_dart_id);
                // split vertex & attributes from the old ID to the new ones
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.attributes
                    .split_edge_attributes(lhs_dart_id, rhs_dart_id, eid_old);
                let (new_lv_lhs, new_lv_rhs) =
                    (self.vertex_id(lhs_dart_id), self.vertex_id(b1rhs_dart_id));
                self.attributes
                    .split_vertex_attributes(new_lv_lhs, new_lv_rhs, lhs_vid_old);
            }
            (false, true) => {
                // fetch IDs before topology update
                let eid_old = self.edge_id(lhs_dart_id);
                let rhs_vid_old = self.vertex_id(rhs_dart_id);
                // update the topology
                self.two_unlink(lhs_dart_id);
                // split vertex & attributes from the old ID to the new ones
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.attributes
                    .split_edge_attributes(lhs_dart_id, rhs_dart_id, eid_old);
                let (new_rv_lhs, new_rv_rhs) =
                    (self.vertex_id(b1lhs_dart_id), self.vertex_id(rhs_dart_id));
                self.attributes
                    .split_vertex_attributes(new_rv_lhs, new_rv_rhs, rhs_vid_old);
            }
            (false, false) => {
                // fetch IDs before topology update
                let eid_old = self.edge_id(lhs_dart_id);
                let lhs_vid_old = self.vertex_id(lhs_dart_id);
                let rhs_vid_old = self.vertex_id(rhs_dart_id);
                // update the topology
                self.two_unlink(lhs_dart_id);
                // split vertices & attributes from the old ID to the new ones
                // FIXME: VertexIdentifier should be cast to DartIdentifier
                self.attributes
                    .split_edge_attributes(lhs_dart_id, rhs_dart_id, eid_old);
                let (new_lv_lhs, new_lv_rhs) =
                    (self.vertex_id(lhs_dart_id), self.vertex_id(b1rhs_dart_id));
                let (new_rv_lhs, new_rv_rhs) =
                    (self.vertex_id(b1lhs_dart_id), self.vertex_id(rhs_dart_id));
                self.attributes
                    .split_vertex_attributes(new_lv_lhs, new_lv_rhs, lhs_vid_old);
                self.attributes
                    .split_vertex_attributes(new_rv_lhs, new_rv_rhs, rhs_vid_old);
            }
        }
    }

    /// Atomically 2-unsew two darts.
    pub fn atomically_two_unsew(&self, lhs_dart_id: DartIdType) {
        atomically(|trans| {
            let rhs_dart_id = self.betas[(2, lhs_dart_id)].read(trans)?;
            let b1lhs_dart_id = self.betas[(1, lhs_dart_id)].read(trans)?;
            let b1rhs_dart_id = self.betas[(1, rhs_dart_id)].read(trans)?;
            // match (is lhs 1-free, is rhs 1-free)
            match (b1lhs_dart_id == NULL_DART_ID, b1rhs_dart_id == NULL_DART_ID) {
                (true, true) => {
                    // fetch IDs before topology update
                    let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    // update the topology
                    self.two_unlink_core(trans, lhs_dart_id)?;
                    // split attributes from the old ID to the new ones
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    self.attributes.split_edge_attributes_transac(
                        trans,
                        lhs_dart_id,
                        rhs_dart_id,
                        eid_old,
                    )?;
                }
                (true, false) => {
                    // fetch IDs before topology update
                    let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
                    // update the topology
                    self.two_unlink_core(trans, lhs_dart_id)?;
                    // split vertex & attributes from the old ID to the new ones
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    self.attributes.split_edge_attributes_transac(
                        trans,
                        lhs_dart_id,
                        rhs_dart_id,
                        eid_old,
                    )?;
                    let (new_lv_lhs, new_lv_rhs) = (
                        self.vertex_id_transac(trans, lhs_dart_id)?,
                        self.vertex_id_transac(trans, b1rhs_dart_id)?,
                    );
                    self.attributes.split_vertex_attributes_transac(
                        trans,
                        new_lv_lhs,
                        new_lv_rhs,
                        lhs_vid_old,
                    )?;
                }
                (false, true) => {
                    // fetch IDs before topology update
                    let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
                    // update the topology
                    self.two_unlink_core(trans, lhs_dart_id)?;
                    // split vertex & attributes from the old ID to the new ones
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    self.attributes.split_edge_attributes_transac(
                        trans,
                        lhs_dart_id,
                        rhs_dart_id,
                        eid_old,
                    )?;
                    let (new_rv_lhs, new_rv_rhs) = (
                        self.vertex_id_transac(trans, b1lhs_dart_id)?,
                        self.vertex_id_transac(trans, rhs_dart_id)?,
                    );
                    self.attributes.split_vertex_attributes_transac(
                        trans,
                        new_rv_lhs,
                        new_rv_rhs,
                        rhs_vid_old,
                    )?;
                }
                (false, false) => {
                    // fetch IDs before topology update
                    let eid_old = self.edge_id_transac(trans, lhs_dart_id)?;
                    let lhs_vid_old = self.vertex_id_transac(trans, lhs_dart_id)?;
                    let rhs_vid_old = self.vertex_id_transac(trans, rhs_dart_id)?;
                    // update the topology
                    self.two_unlink_core(trans, lhs_dart_id)?;
                    // split vertices & attributes from the old ID to the new ones
                    // FIXME: VertexIdentifier should be cast to DartIdentifier
                    self.attributes.split_edge_attributes_transac(
                        trans,
                        lhs_dart_id,
                        rhs_dart_id,
                        eid_old,
                    )?;
                    let (new_lv_lhs, new_lv_rhs) = (
                        self.vertex_id_transac(trans, lhs_dart_id)?,
                        self.vertex_id_transac(trans, b1rhs_dart_id)?,
                    );
                    let (new_rv_lhs, new_rv_rhs) = (
                        self.vertex_id_transac(trans, b1lhs_dart_id)?,
                        self.vertex_id_transac(trans, rhs_dart_id)?,
                    );
                    self.attributes.split_vertex_attributes_transac(
                        trans,
                        new_lv_lhs,
                        new_lv_rhs,
                        lhs_vid_old,
                    )?;
                    self.attributes.split_vertex_attributes_transac(
                        trans,
                        new_rv_lhs,
                        new_rv_rhs,
                        rhs_vid_old,
                    )?;
                }
            }
            Ok(())
        });
    }
}

/// **Link and unlink operations**
impl<T: CoordsFloat> CMap2<T> {
    /// 1-link operation.
    ///
    /// This operation corresponds to linking two darts via the *β<sub>1</sub>* function. Unlike
    /// its sewing counterpart, this method does not contain any code to update the attributes or
    /// geometrical data of the affected cell(s). The *β<sub>0</sub>* function is also updated.
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    ///
    /// # Panics
    ///
    /// This method may panic if `lhs_dart_id` isn't 1-free or `rhs_dart_id` isn't 0-free.
    ///
    pub fn one_link(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        atomically(|trans| self.one_link_core(trans, lhs_dart_id, rhs_dart_id));
    }

    /// 2-link operation.
    ///
    /// This operation corresponds to linking two darts via the *β<sub>2</sub>* function. Unlike
    /// its sewing counterpart, this method does not contain any code to update the attributes or
    /// geometrical data of the affected cell(s).
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` or `rhs_dart_id` isn't 2-free.
    pub fn two_link(&self, lhs_dart_id: DartIdType, rhs_dart_id: DartIdType) {
        atomically(|trans| self.two_link_core(trans, lhs_dart_id, rhs_dart_id));
    }

    /// 1-unlink operation.
    ///
    /// This operation corresponds to unlinking two darts that are linked via the *β<sub>1</sub>*
    /// function. Unlike its sewing counterpart, this method does not contain any code to update
    /// the attributes or geometrical data of the affected cell(s). The *β<sub>0</sub>* function is
    /// also updated.
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to unlink.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` is already 1-free.
    pub fn one_unlink(&self, lhs_dart_id: DartIdType) {
        atomically(|trans| self.one_unlink_core(trans, lhs_dart_id));
    }

    /// 2-unlink operation.
    ///
    /// This operation corresponds to unlinking two darts that are linked via the *β<sub>2</sub>*
    /// function. Unlike its sewing counterpart, this method does not contain any code to update
    /// the attributes or geometrical data of the affected cell(s).
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to unlink.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` is already 2-free.
    pub fn two_unlink(&self, lhs_dart_id: DartIdType) {
        atomically(|trans| self.two_unlink_core(trans, lhs_dart_id));
    }
}

#[doc(hidden)]
/// **Link and unlink core operations**
impl<T: CoordsFloat> CMap2<T> {
    /// 1-link operation.
    ///
    /// This operation corresponds to linking two darts via the *β<sub>1</sub>* function. Unlike
    /// its sewing counterpart, this method does not contain any code to update the attributes or
    /// geometrical data of the affected cell(s). The *β<sub>0</sub>* function is also updated.
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    ///
    /// # Panics
    ///
    /// This method may panic if `lhs_dart_id` isn't 1-free or `rhs_dart_id` isn't 0-free.
    ///
    pub(crate) fn one_link_core(
        &self,
        trans: &mut Transaction,
        lhs_dart_id: DartIdType,
        rhs_dart_id: DartIdType,
    ) -> Result<(), StmError> {
        // we could technically overwrite the value, but these assertions
        // makes it easier to assert algorithm correctness
        assert!(self.is_i_free::<1>(lhs_dart_id));
        assert!(self.is_i_free::<0>(rhs_dart_id));
        // set beta_1(lhs_dart) to rhs_dart
        self.betas[(1, lhs_dart_id)].write(trans, rhs_dart_id)?;
        // set beta_0(rhs_dart) to lhs_dart
        self.betas[(0, rhs_dart_id)].write(trans, lhs_dart_id)?;
        Ok(())
    }

    /// 2-link operation.
    ///
    /// This operation corresponds to linking two darts via the *β<sub>2</sub>* function. Unlike
    /// its sewing counterpart, this method does not contain any code to update the attributes or
    /// geometrical data of the affected cell(s).
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the first dart to be linked.
    /// - `rhs_dart_id: DartIdentifier` -- ID of the second dart to be linked.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` or `rhs_dart_id` isn't 2-free.
    pub(crate) fn two_link_core(
        &self,
        trans: &mut Transaction,
        lhs_dart_id: DartIdType,
        rhs_dart_id: DartIdType,
    ) -> Result<(), StmError> {
        // we could technically overwrite the value, but these assertions
        // make it easier to assert algorithm correctness
        assert!(self.is_i_free::<2>(lhs_dart_id));
        assert!(self.is_i_free::<2>(rhs_dart_id));
        // set beta_2(lhs_dart) to rhs_dart
        self.betas[(2, lhs_dart_id)].write(trans, rhs_dart_id)?;
        // set beta_2(rhs_dart) to lhs_dart
        self.betas[(2, rhs_dart_id)].write(trans, lhs_dart_id)?;
        Ok(())
    }

    /// 1-unlink operation.
    ///
    /// This operation corresponds to unlinking two darts that are linked via the *β<sub>1</sub>*
    /// function. Unlike its sewing counterpart, this method does not contain any code to update
    /// the attributes or geometrical data of the affected cell(s). The *β<sub>0</sub>* function is
    /// also updated.
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to unlink.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` is already 1-free.
    pub(crate) fn one_unlink_core(
        &self,
        trans: &mut Transaction,
        lhs_dart_id: DartIdType,
    ) -> Result<(), StmError> {
        // set beta_1(lhs_dart) to NullDart
        let rhs_dart_id = self.betas[(1, lhs_dart_id)].replace(trans, NULL_DART_ID)?;
        assert_ne!(rhs_dart_id, NULL_DART_ID);
        // set beta_0(rhs_dart) to NullDart
        self.betas[(0, rhs_dart_id)].write(trans, NULL_DART_ID)?;
        Ok(())
    }

    /// 2-unlink operation.
    ///
    /// This operation corresponds to unlinking two darts that are linked via the *β<sub>2</sub>*
    /// function. Unlike its sewing counterpart, this method does not contain any code to update
    /// the attributes or geometrical data of the affected cell(s).
    ///
    /// # Arguments
    ///
    /// - `lhs_dart_id: DartIdentifier` -- ID of the dart to unlink.
    ///
    /// # Panics
    ///
    /// This method may panic if one of `lhs_dart_id` is already 2-free.
    pub(crate) fn two_unlink_core(
        &self,
        trans: &mut Transaction,
        lhs_dart_id: DartIdType,
    ) -> Result<(), StmError> {
        // set beta_2(dart) to NullDart
        let rhs_dart_id = self.betas[(2, lhs_dart_id)].replace(trans, NULL_DART_ID)?;
        assert_ne!(rhs_dart_id, NULL_DART_ID);
        // set beta_2(beta_2(dart)) to NullDart
        self.betas[(2, rhs_dart_id)].write(trans, NULL_DART_ID)?;
        Ok(())
    }
}