mesh-graph 0.6.0

Fast halfedge triangle mesh graph in pure Rust
Documentation
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
use hashbrown::HashSet;
use itertools::Itertools;
use tracing::{error, instrument};

use crate::{FaceId, HalfedgeId, MeshGraph, VertexId, error_none};

#[derive(Default)]
pub struct VertexNeighborhoodCleanup {
    pub removed_vertices: Vec<VertexId>,
    pub removed_halfedges: Vec<HalfedgeId>,
    pub removed_faces: Vec<FaceId>,

    pub added_vertices: Vec<VertexId>,
}

#[derive(Default)]
struct VertexNeighborhoodCleanupStep {
    pub removed_vertices: Vec<VertexId>,
    pub removed_halfedges: Vec<HalfedgeId>,
    pub removed_faces: Vec<FaceId>,

    pub added_duplicated_vertices: Vec<VertexId>,
    pub touched_vertices: Vec<VertexId>,
}

impl MeshGraph {
    /// Ensure that the neighborhood of a vertex is manifold.
    ///
    /// It removes flaps (two neighboring coincident triangles) and splits the given vertex
    /// in two if there are non-neighboring degenerate triangles or edges.
    ///
    /// See [Freestyle: Sculpting meshes with self-adaptive topology DOI 10.1016/j.cag.2011.03.033](https://inria.hal.science/inria-00606516v1/document)
    /// Chapters 3.2 and 5.1
    #[instrument(skip(self))]
    pub fn make_vertex_neighborhood_manifold(
        &mut self,
        vertex_id: VertexId,
    ) -> VertexNeighborhoodCleanup {
        #[cfg(feature = "rerun")]
        self.log_vert_rerun("make_neigh_manifold", vertex_id);

        let mut result = VertexNeighborhoodCleanup::default();

        let mut vertices = vec![vertex_id];

        while let Some(&v_id) = vertices.first() {
            if !self.vertices.contains_key(v_id) {
                vertices.swap_remove(0);
                continue;
            }

            let VertexNeighborhoodCleanupStep {
                added_duplicated_vertices,
                touched_vertices,
                removed_vertices,
                removed_halfedges,
                removed_faces,
            } = self.make_vertex_neighborhood_manifold_step(v_id);

            if removed_vertices.is_empty()
                && removed_halfedges.is_empty()
                && removed_faces.is_empty()
                && added_duplicated_vertices.is_empty()
                && touched_vertices.is_empty()
            {
                vertices.swap_remove(0);
            }

            vertices.extend(touched_vertices);
            for added_vertex in added_duplicated_vertices {
                vertices.push(added_vertex);

                result.added_vertices.push(added_vertex);
            }

            result.removed_vertices.extend(removed_vertices);
            result.removed_halfedges.extend(removed_halfedges);
            result.removed_faces.extend(removed_faces);

            if vertices.is_empty() {
                break;
            }
        }

        for &cancelled_v_id in
            HashSet::<VertexId>::from_iter(result.removed_vertices.iter().copied())
                .intersection(&HashSet::from_iter(result.added_vertices.iter().copied()))
        {
            result.added_vertices.retain(|&v_id| v_id != cancelled_v_id);
            result
                .removed_vertices
                .retain(|&v_id| v_id != cancelled_v_id);
        }

        result
    }

    fn make_vertex_neighborhood_manifold_step(
        &mut self,
        vertex_id: VertexId,
    ) -> VertexNeighborhoodCleanupStep {
        let mut removed_vertices = Vec::new();
        let mut removed_halfedges = Vec::new();
        let mut removed_faces = Vec::new();

        let added_duplicated_vertices = self
            .make_vertex_neighborhood_manifold_inner(
                vertex_id,
                &mut removed_vertices,
                &mut removed_halfedges,
                &mut removed_faces,
            )
            .unwrap_or_default();

        VertexNeighborhoodCleanupStep {
            added_duplicated_vertices,
            touched_vertices: vec![],
            removed_vertices,
            removed_halfedges,
            removed_faces,
        }
    }

    pub fn make_vertex_neighborhood_manifold_inner(
        &mut self,
        vertex_id: VertexId,
        removed_vertices: &mut Vec<VertexId>,
        removed_halfedges: &mut Vec<HalfedgeId>,
        removed_faces: &mut Vec<FaceId>,
    ) -> Option<Vec<VertexId>> {
        self.vertices.get(vertex_id)?;

        if self.remove_single_face(
            vertex_id,
            removed_vertices,
            removed_halfedges,
            removed_faces,
        )? {
            return Some(vec![]);
        }

        if self.remove_neighboring_flaps(
            vertex_id,
            removed_vertices,
            removed_halfedges,
            removed_faces,
        )? {
            return Some(vec![]);
        }

        if let Some(new_vertices) = self.split_disconnected_neighborhoods(vertex_id)
            && !new_vertices.is_empty()
        {
            return Some(new_vertices);
        }

        if let Some(inserted_duplicated_vertex) = self.remove_degenerate_faces(
            vertex_id,
            removed_vertices,
            removed_halfedges,
            removed_faces,
        ) {
            return Some(vec![inserted_duplicated_vertex]);
        }

        self.remove_degenerate_edges(vertex_id).map(|v| vec![v])
    }

    fn split_disconnected_neighborhoods(&mut self, vertex_id: VertexId) -> Option<Vec<VertexId>> {
        let mut new_vertices = Vec::new();

        let mut outgoing_halfedges = HashSet::<HalfedgeId>::from_iter(
            self.outgoing_halfedges
                .get(vertex_id)
                .or_else(error_none!("Outgoing halfedges not found"))?
                .iter()
                .copied(),
        );

        while let Some(&start_he_id) = outgoing_halfedges.iter().next() {
            let mut current_he_id = start_he_id;

            let mut current_outgoing_halfedges = Vec::with_capacity(outgoing_halfedges.len());

            loop {
                outgoing_halfedges.remove(&current_he_id);
                current_outgoing_halfedges.push(current_he_id);

                let cur_he = self
                    .halfedges
                    .get(current_he_id)
                    .or_else(error_none!("Halfedge not found"))?;

                let Some(next_he_id) = cur_he.cw_rotated_neighbour(self) else {
                    // TODO : handle boundary edges?
                    break;
                };

                if next_he_id == start_he_id {
                    break;
                }

                current_he_id = next_he_id;
            }

            if !outgoing_halfedges.is_empty() {
                let new_vertex_id = self
                    .duplicate_vertex_and_assign_halfedges(vertex_id, current_outgoing_halfedges)?;

                new_vertices.push(new_vertex_id);

                self.vertices
                    .get_mut(vertex_id)
                    .or_else(error_none!("Vertex not found"))?
                    .outgoing_halfedge = Some(*outgoing_halfedges.iter().next().unwrap());

                self.outgoing_halfedges
                    .insert(vertex_id, Vec::from_iter(outgoing_halfedges));

                return Some(new_vertices);
            }
        }

        Some(new_vertices)
    }

    fn duplicate_vertex_and_assign_halfedges(
        &mut self,
        vertex_id: VertexId,
        outgoing_halfedges: Vec<HalfedgeId>,
    ) -> Option<VertexId> {
        let new_vert_id = self.add_vertex(
            *self
                .positions
                .get(vertex_id)
                .or_else(error_none!("Position not found"))?
                + glam::Vec3::new(0.1, 0.0, 0.0),
        );

        tracing::info!("Duplicated {vertex_id:?}: {new_vert_id:?}");

        for &he_id in &outgoing_halfedges {
            let he = self
                .halfedges
                .get(he_id)
                .or_else(error_none!("Halfedge not found"))?;

            let twin_id = he.twin.or_else(error_none!("Twin not found"))?;

            self.halfedges
                .get_mut(twin_id)
                .or_else(error_none!("Twin not found"))?
                .end_vertex = new_vert_id;
        }

        // just created above
        self.vertices[new_vert_id].outgoing_halfedge = Some(outgoing_halfedges[0]);

        self.outgoing_halfedges
            .insert(new_vert_id, outgoing_halfedges);

        Some(new_vert_id)
    }

    fn remove_degenerate_edges(&mut self, vertex_id: VertexId) -> Option<VertexId> {
        let halfedges = self
            .vertices
            .get(vertex_id)
            .or_else(error_none!("Vertex not found"))?
            .outgoing_halfedges(self)
            .collect_vec();

        for (he_id1, he_id2) in halfedges.into_iter().tuple_combinations() {
            if self.halfedges_share_all_vertices(he_id1, he_id2) {
                let he1 = self.halfedges[he_id1];
                let twin_id1 = he1.twin.or_else(error_none!("Twin not found"))?;
                let face_id1 = self
                    .halfedges
                    .get(twin_id1)
                    .or_else(error_none!("Halfedge not found"))?
                    .face
                    .or_else(error_none!("Face not found"))?;
                let face_id2 = self
                    .halfedges
                    .get(he_id2)
                    .or_else(error_none!("Halfedge not found"))?
                    .face
                    .or_else(error_none!("Face not found"))?;

                let coincident_face_ids = self.vertices[vertex_id].faces(self).collect_vec();
                let mut start_idx = 0;

                for (idx, face_id) in coincident_face_ids.iter().enumerate() {
                    if *face_id == face_id1 {
                        start_idx = idx;
                        break;
                    }
                }

                let mut end_idx = start_idx;
                let mut side_one = vec![];

                loop {
                    let face_id = coincident_face_ids[end_idx];

                    side_one.push(face_id);

                    end_idx += 1;
                    end_idx %= coincident_face_ids.len();

                    if face_id == face_id2 {
                        break;
                    }
                }

                let mut side_two = vec![];

                while end_idx != start_idx {
                    let face_id = coincident_face_ids[end_idx];

                    side_two.push(face_id);

                    end_idx += 1;
                    end_idx %= coincident_face_ids.len();
                }

                return self.split_regions_at_edge(vertex_id, he1.end_vertex, side_one, side_two);
            }
        }

        None
    }

    #[instrument(skip(self))]
    fn split_regions_at_edge(
        &mut self,
        vertex_id: VertexId,
        other_vertex_id: VertexId,
        side_one: Vec<FaceId>,
        side_two: Vec<FaceId>,
    ) -> Option<VertexId> {
        if side_one.len() < 2 || side_two.len() < 2 {
            error!("Not enough halfedges to split");
            return None;
        }

        #[cfg(feature = "rerun")]
        {
            self.log_vert_rerun("split_regions_at_edge", vertex_id);
            self.log_faces_rerun("split_regions_at_edge/side_one", &side_one);
            self.log_faces_rerun("split_regions_at_edge/side_two", &side_two);
        }

        let new_vertex_id = self.add_vertex(
            *self
                .positions
                .get(vertex_id)
                .or_else(error_none!("Vertex position not found"))?,
        );

        // Updating vertices
        for face_id in &side_two {
            let face = self
                .faces
                .get(*face_id)
                .or_else(error_none!("Face not found"))?;

            for he_id in face.halfedges(self).collect_vec() {
                // already checked in iterator that this he exists
                let he = &mut self.halfedges[he_id];

                if he.end_vertex == vertex_id {
                    he.end_vertex = new_vertex_id;
                }
            }
        }

        self.weld_faces_at(
            vertex_id,
            other_vertex_id,
            side_one[side_one.len() - 1],
            side_one[0],
        );
        self.weld_faces_at(
            new_vertex_id,
            other_vertex_id,
            side_two[0],
            side_two[side_two.len() - 1],
        );

        self.outgoing_halfedges[vertex_id] =
            self.vertices[vertex_id].outgoing_halfedges(self).collect();
        self.outgoing_halfedges[new_vertex_id] = self.vertices[new_vertex_id]
            .outgoing_halfedges(self)
            .collect();

        Some(new_vertex_id)
    }

    #[instrument(skip_all)]
    pub fn remove_degenerate_faces(
        &mut self,
        vertex_id: VertexId,
        removed_vertices: &mut Vec<VertexId>,
        removed_halfedges: &mut Vec<HalfedgeId>,
        removed_faces: &mut Vec<FaceId>,
    ) -> Option<VertexId> {
        let faces = self
            .vertices
            .get(vertex_id)
            .or_else(error_none!("Vertex not found"))?
            .faces(self)
            .collect_vec();

        for (face_id1, face_id2) in faces.into_iter().tuple_combinations() {
            if self.faces_share_all_vertices(face_id1, face_id2) {
                let coincident_face_ids = self.vertices[vertex_id].faces(self).collect_vec();
                let mut start_idx = 0;

                for (idx, face_id) in coincident_face_ids.iter().enumerate() {
                    if *face_id == face_id1 {
                        start_idx = (idx + 1) % coincident_face_ids.len();
                        break;
                    }
                }

                let mut end_idx = start_idx;
                let mut side_one = vec![];

                loop {
                    let face_id = coincident_face_ids[end_idx];

                    end_idx += 1;
                    end_idx %= coincident_face_ids.len();

                    if face_id == face_id2 {
                        break;
                    }

                    side_one.push(face_id);
                }

                let mut side_two = vec![];

                while end_idx != start_idx {
                    let face_id = coincident_face_ids[end_idx];

                    side_two.push(face_id);

                    end_idx += 1;
                    end_idx %= coincident_face_ids.len();
                }

                side_two.pop();

                let new_vertex_id =
                    self.split_regions_at_vertex(vertex_id, side_one, side_two, removed_halfedges);

                let (del_v_ids, del_he_ids) = self.remove_face(face_id1);
                removed_vertices.extend(del_v_ids);
                removed_halfedges.extend(del_he_ids);
                removed_faces.push(face_id1);

                let (del_v_ids, del_he_ids) = self.remove_face(face_id2);
                removed_vertices.extend(del_v_ids);
                removed_halfedges.extend(del_he_ids);
                removed_faces.push(face_id2);

                return new_vertex_id;
            }
        }

        None
    }

    #[instrument(skip(self))]
    fn split_regions_at_vertex(
        &mut self,
        vertex_id: VertexId,
        side_one: Vec<FaceId>,
        side_two: Vec<FaceId>,
        removed_halfedges: &mut Vec<HalfedgeId>,
    ) -> Option<VertexId> {
        if side_one.len() < 2 || side_two.len() < 2 {
            error!("Not enough halfedges to split");
            return None;
        }

        #[cfg(feature = "rerun")]
        {
            self.log_vert_rerun("split_regions_at_vertex", vertex_id);
            self.log_faces_rerun("split_regions_at_vertex/side_one", &side_one);
            self.log_faces_rerun("split_regions_at_vertex/side_two", &side_two);
        }

        let new_vertex_id = self.add_vertex(
            *self
                .positions
                .get(vertex_id)
                .or_else(error_none!("Vertex position not found"))?,
        );

        // Updating start vertex
        for face_id in &side_two {
            let face = self
                .faces
                .get(*face_id)
                .or_else(error_none!("Face not found"))?;

            for he_id in face.halfedges(self).collect_vec() {
                // already checked in iterator that this he exists
                let he = &mut self.halfedges[he_id];
                if he.end_vertex == vertex_id {
                    he.end_vertex = new_vertex_id;
                }
            }
        }

        self.weld_faces(vertex_id, side_one[side_one.len() - 1], side_one[0]);
        self.weld_faces(new_vertex_id, side_two[0], side_two[side_two.len() - 1]);

        self.outgoing_halfedges[vertex_id] =
            self.vertices[vertex_id].outgoing_halfedges(self).collect();
        self.outgoing_halfedges[new_vertex_id] = self.vertices[new_vertex_id]
            .outgoing_halfedges(self)
            .collect();

        Some(new_vertex_id)
    }

    /// This finds the common edge between the two faces and welds them together by connecting
    /// the two `twin` relationships. The reverse `twin` pointers of the previous twins are not changed.
    ///
    /// `start_vertex_id` is shared by `face_id1` and `face_id2`.
    fn weld_faces(
        &mut self,
        start_vertex_id: VertexId,
        face_id1: FaceId,
        face_id2: FaceId,
    ) -> Option<()> {
        // #[cfg(feature = "rerun")]
        // {
        //     self.log_face_rerun("face1", face_id1);
        //     self.log_face_rerun("face2", face_id2);
        // }

        let face1 = self
            .faces
            .get(face_id1)
            .or_else(error_none!("Face not found"))?;
        let face2 = self
            .faces
            .get(face_id2)
            .or_else(error_none!("Face not found"))?;

        let face1_vertices = face1.vertices(self).collect::<HashSet<_>>();
        let face2_vertices = face2.vertices(self).collect::<HashSet<_>>();

        let mut other_common_vertex_id = None;

        for &v_id in face1_vertices.intersection(&face2_vertices) {
            if v_id != start_vertex_id {
                other_common_vertex_id = Some(v_id);
                break;
            }
        }

        let other_common_vertex_id =
            other_common_vertex_id.or_else(error_none!("No other common vertex found"))?;

        self.weld_faces_at(start_vertex_id, other_common_vertex_id, face_id1, face_id2)
    }

    #[instrument(skip(self))]
    fn weld_faces_at(
        &mut self,
        start_vertex_id: VertexId,
        other_common_vertex_id: VertexId,
        face_id1: FaceId,
        face_id2: FaceId,
    ) -> Option<()> {
        let face1 = self
            .faces
            .get(face_id1)
            .or_else(error_none!("Face not found"))?;
        let face2 = self
            .faces
            .get(face_id2)
            .or_else(error_none!("Face not found"))?;

        let he1_id = face1
            .halfedge_between(start_vertex_id, other_common_vertex_id, self)
            .or_else(error_none!("Halfedge between vertices not found"))?;

        let he2_id = face2
            .halfedge_between(start_vertex_id, other_common_vertex_id, self)
            .or_else(error_none!("Halfedge between vertices not found"))?;

        self.halfedges[he1_id].twin = Some(he2_id);
        self.halfedges[he2_id].twin = Some(he1_id);

        let (start_out_he_id, other_out_he_id) =
            if self.halfedges[he1_id].end_vertex == start_vertex_id {
                (he2_id, he1_id)
            } else {
                (he1_id, he2_id)
            };

        self.vertices
            .get_mut(start_vertex_id)
            .or_else(error_none!("Start vertex not found"))?
            .outgoing_halfedge = Some(start_out_he_id);
        self.vertices
            .get_mut(other_common_vertex_id)
            .or_else(error_none!("Other vertex not found"))?
            .outgoing_halfedge = Some(other_out_he_id);

        Some(())
    }

    pub(crate) fn remove_single_face(
        &mut self,
        vertex_id: VertexId,
        removed_vertices: &mut Vec<VertexId>,
        removed_halfedges: &mut Vec<HalfedgeId>,
        removed_faces: &mut Vec<FaceId>,
    ) -> Option<bool> {
        let face_ids = self
            .outgoing_halfedges
            .get(vertex_id)
            .or_else(error_none!(
                "Outgoing halfedges not found for vertex {vertex_id:?}"
            ))?
            .iter()
            .filter_map(|&he_id| {
                self.halfedges
                    .get(he_id)
                    .or_else(error_none!("Halfedge not found"))
                    .and_then(|he| he.face)
            })
            .collect_vec();

        if face_ids.len() == 1 {
            let face_id = face_ids[0];

            let (v_ids, he_ids) = self.remove_face(face_id);

            removed_vertices.extend(v_ids);
            removed_halfedges.extend(he_ids);
            removed_faces.push(face_id);

            Some(true)
        } else {
            Some(false)
        }
    }

    fn remove_neighboring_flaps(
        &mut self,
        vertex_id: VertexId,
        removed_vertices: &mut Vec<VertexId>,
        removed_halfedges: &mut Vec<HalfedgeId>,
        removed_faces: &mut Vec<FaceId>,
    ) -> Option<bool> {
        let faces = self
            .vertices
            .get(vertex_id)
            .or_else(error_none!("Vertex not found"))?
            .faces(self)
            .collect_vec();

        if faces.len() < 2 {
            return Some(false);
        }

        let mut face_tuples = faces.into_iter().circular_tuple_windows().collect_vec();

        while let Some((face_id1, face_id2)) = face_tuples.pop() {
            if self.faces_share_all_vertices(face_id1, face_id2) {
                #[cfg(feature = "rerun")]
                {
                    self.log_vert_rerun("flap", vertex_id);
                    self.log_face_rerun("flap1", face_id1);
                    self.log_face_rerun("flap2", face_id2);
                }

                let mut halfedges_of_faces = HashSet::<HalfedgeId>::from_iter(
                    self.halfedges.iter().filter_map(|(he_id, he)| {
                        if he.face == Some(face_id1) || he.face == Some(face_id2) {
                            Some(he_id)
                        } else {
                            None
                        }
                    }),
                );

                let (vs, hes) = self.remove_face(face_id1);
                for he_id in &hes {
                    halfedges_of_faces.remove(he_id);
                }
                removed_vertices.extend(vs);
                removed_halfedges.extend(hes);
                removed_faces.push(face_id1);

                let (vs, hes) = self.remove_face(face_id2);
                for he_id in &hes {
                    halfedges_of_faces.remove(he_id);
                }
                removed_vertices.extend(vs);
                removed_halfedges.extend(hes);
                removed_faces.push(face_id2);

                // TODO : Handle the case when there is only one halfedge?
                if halfedges_of_faces.len() >= 2 {
                    for (he_id1, he_id2) in halfedges_of_faces.iter().copied().tuple_combinations()
                    {
                        let Some(he1) = self.halfedges.get(he_id1) else {
                            // might have been deleted by prior iteration
                            continue;
                        };
                        let twin_id1 = he1.twin.or_else(error_none!("Twin 1 missing"))?;

                        let Some(he2) = self.halfedges.get(he_id2) else {
                            continue;
                        };
                        let twin_id2 = he2.twin.or_else(error_none!("Twin 2 missing"))?;

                        let start_v_id1 = he1
                            .start_vertex(self)
                            .or_else(error_none!("Start vertex 1 missing"))?;
                        let start_v_id2 = he2
                            .start_vertex(self)
                            .or_else(error_none!("Start vertex 2 missing"))?;

                        if he1.end_vertex != start_v_id2 || he2.end_vertex != start_v_id1 {
                            continue;
                        }

                        // the twin edges of the neighboring faces of the deleted faces are still there
                        // we need to remove them and re-connect (twin) their twin edges

                        self.remove_only_halfedge(he_id1);
                        self.remove_only_halfedge(he_id2);
                        removed_halfedges.push(he_id1);
                        removed_halfedges.push(he_id2);

                        {
                            let twin1 = self
                                .halfedges
                                .get_mut(twin_id1)
                                .or_else(error_none!("Twin 1 missing"))?;
                            twin1.twin = Some(twin_id2);
                        };

                        {
                            let twin2 = self
                                .halfedges
                                .get_mut(twin_id2)
                                .or_else(error_none!("Twin 2 missing"))?;
                            twin2.twin = Some(twin_id1);
                        };

                        self.vertices
                            .get_mut(start_v_id1)
                            .or_else(error_none!("Start vertex 1 missing"))?
                            .outgoing_halfedge = Some(twin_id2);

                        self.vertices
                            .get_mut(start_v_id2)
                            .or_else(error_none!("Start vertex 2 missing"))?
                            .outgoing_halfedge = Some(twin_id1);
                    }
                }

                return Some(true);
            }
        }

        Some(false)
    }
}

#[cfg(test)]
mod tests {
    use crate::ops::AddOrGetEdge;

    use super::*;
    use glam::Vec3;

    #[test]
    fn test_remove_degenerate_faces() {
        crate::utils::get_tracing_subscriber();
        let mut meshgraph = MeshGraph::new();

        let center_v_id = meshgraph.add_vertex(Vec3::new(0.0, 0.0, 1.0));

        let v1_id = meshgraph.add_vertex(Vec3::new(-0.2, 0.0, 0.0));
        let he_c_1_id = meshgraph
            .add_or_get_edge(center_v_id, v1_id)
            .start_to_end_he_id;
        let v1p_id = meshgraph.add_vertex(Vec3::new(-0.2, 0.0, 0.0));
        let AddOrGetEdge {
            start_to_end_he_id: he_c_1p_id,
            twin_he_id: he_1p_c_id,
            ..
        } = meshgraph.add_or_get_edge(center_v_id, v1p_id);

        let v2_id = meshgraph.add_vertex(Vec3::new(-1.0, 1.0, 0.0));
        let he_c_2_id = meshgraph
            .add_or_get_edge(center_v_id, v2_id)
            .start_to_end_he_id;

        let v3_id = meshgraph.add_vertex(Vec3::new(-1.0, -1.0, 0.0));
        let he_c_3_id = meshgraph
            .add_or_get_edge(center_v_id, v3_id)
            .start_to_end_he_id;

        let v4_id = meshgraph.add_vertex(Vec3::new(0.2, 0.0, 0.0));
        let he_c_4_id = meshgraph
            .add_or_get_edge(center_v_id, v4_id)
            .start_to_end_he_id;

        let v4p_id = meshgraph.add_vertex(Vec3::new(0.2, 0.0, 0.0));
        let AddOrGetEdge {
            start_to_end_he_id: he_c_4p_id,
            twin_he_id: he_4p_c_id,
            ..
        } = meshgraph.add_or_get_edge(center_v_id, v4p_id);

        let v5_id = meshgraph.add_vertex(Vec3::new(1.0, -1.0, 0.0));
        let he_c_5_id = meshgraph
            .add_or_get_edge(center_v_id, v5_id)
            .start_to_end_he_id;

        let v6_id = meshgraph.add_vertex(Vec3::new(1.0, 1.0, 0.0));
        let he_c_6_id = meshgraph
            .add_or_get_edge(center_v_id, v6_id)
            .start_to_end_he_id;

        meshgraph
            .add_face_from_halfedges(he_c_1_id, he_c_2_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_2_id, he_c_3_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_3_id, he_c_1p_id)
            .unwrap();

        meshgraph
            .add_face_from_halfedges(he_c_1p_id, he_c_4p_id)
            .unwrap();

        meshgraph
            .add_face_from_halfedges(he_c_4p_id, he_c_5_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_5_id, he_c_6_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_6_id, he_c_4_id)
            .unwrap();

        meshgraph
            .add_face_from_halfedges(he_c_1_id, he_c_4_id)
            .unwrap();

        meshgraph.halfedges[he_c_1p_id].end_vertex = v1_id;
        meshgraph.halfedges[he_c_4p_id].end_vertex = v4_id;

        // already created from face above
        let AddOrGetEdge {
            start_to_end_he_id: he_1p_4p_id,
            twin_he_id: he_4p_1p_id,
            ..
        } = meshgraph.add_or_get_edge(v1p_id, v4p_id);
        meshgraph.halfedges[he_1p_4p_id].end_vertex = v4_id;
        meshgraph.halfedges[he_4p_1p_id].end_vertex = v1_id;

        let AddOrGetEdge {
            start_to_end_he_id: he_3_1p_id,
            twin_he_id: he_1p_3_id,
            ..
        } = meshgraph.add_or_get_edge(v3_id, v1p_id);
        meshgraph.halfedges[he_3_1p_id].end_vertex = v1_id;

        let AddOrGetEdge {
            start_to_end_he_id: he_4p_5_id,
            twin_he_id: he_5_4p_id,
            ..
        } = meshgraph.add_or_get_edge(v4p_id, v5_id);
        meshgraph.halfedges[he_5_4p_id].end_vertex = v4_id;

        meshgraph.outgoing_halfedges[v1_id].push(he_1p_4p_id);
        meshgraph.outgoing_halfedges[v1_id].push(he_1p_c_id);
        meshgraph.outgoing_halfedges[v1_id].push(he_1p_3_id);
        meshgraph.outgoing_halfedges[v4_id].push(he_4p_1p_id);
        meshgraph.outgoing_halfedges[v4_id].push(he_4p_c_id);
        meshgraph.outgoing_halfedges[v4_id].push(he_4p_5_id);

        meshgraph.remove_only_vertex(v1p_id);
        meshgraph.remove_only_vertex(v4p_id);

        #[cfg(feature = "rerun")]
        meshgraph.log_rerun();

        let mut removed_vertices = vec![];
        let mut removed_halfedges = vec![];
        let mut removed_faces = vec![];

        meshgraph.remove_degenerate_faces(
            center_v_id,
            &mut removed_vertices,
            &mut removed_halfedges,
            &mut removed_faces,
        );

        #[cfg(feature = "rerun")]
        {
            meshgraph.log_rerun();
            crate::RR.flush_blocking().unwrap();
        }
    }

    #[test]
    fn test_remove_degenerate_edges() {
        crate::utils::get_tracing_subscriber();

        let mut meshgraph = MeshGraph::new();

        let center_v_id = meshgraph.add_vertex(Vec3::new(0.0, 0.0, 1.0));

        let v1_id = meshgraph.add_vertex(Vec3::new(0.0, 0.0, 0.0));
        let he_c_1_id = meshgraph
            .add_or_get_edge(center_v_id, v1_id)
            .start_to_end_he_id;

        let v1p_id = meshgraph.add_vertex(Vec3::new(0.0, 0.0, 0.0));
        let AddOrGetEdge {
            start_to_end_he_id: he_c_1p_id,
            twin_he_id: he_1p_c_id,
            ..
        } = meshgraph.add_or_get_edge(center_v_id, v1p_id);

        let v2_id = meshgraph.add_vertex(Vec3::new(-1.0, 1.0, 0.0));
        let he_c_2_id = meshgraph
            .add_or_get_edge(center_v_id, v2_id)
            .start_to_end_he_id;

        let v3_id = meshgraph.add_vertex(Vec3::new(-1.0, -1.0, 0.0));
        let he_c_3_id = meshgraph
            .add_or_get_edge(center_v_id, v3_id)
            .start_to_end_he_id;

        let v5_id = meshgraph.add_vertex(Vec3::new(1.0, -1.0, 0.0));
        let he_c_5_id = meshgraph
            .add_or_get_edge(center_v_id, v5_id)
            .start_to_end_he_id;

        let v6_id = meshgraph.add_vertex(Vec3::new(1.0, 1.0, 0.0));
        let he_c_6_id = meshgraph
            .add_or_get_edge(center_v_id, v6_id)
            .start_to_end_he_id;

        meshgraph
            .add_face_from_halfedges(he_c_1_id, he_c_2_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_2_id, he_c_3_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_3_id, he_c_1p_id)
            .unwrap();

        meshgraph
            .add_face_from_halfedges(he_c_1p_id, he_c_5_id)
            .unwrap();

        meshgraph
            .add_face_from_halfedges(he_c_5_id, he_c_6_id)
            .unwrap();
        meshgraph
            .add_face_from_halfedges(he_c_6_id, he_c_1_id)
            .unwrap();

        meshgraph.halfedges[he_c_1p_id].end_vertex = v1_id;

        // already created from face above
        let AddOrGetEdge {
            start_to_end_he_id: he_3_1p_id,
            twin_he_id: he_1p_3_id,
            ..
        } = meshgraph.add_or_get_edge(v3_id, v1p_id);
        meshgraph.halfedges[he_3_1p_id].end_vertex = v1_id;

        let AddOrGetEdge {
            start_to_end_he_id: he_1p_5_id,
            twin_he_id: he_5_1p_id,
            ..
        } = meshgraph.add_or_get_edge(v1p_id, v5_id);
        meshgraph.halfedges[he_5_1p_id].end_vertex = v1_id;

        meshgraph.outgoing_halfedges[v1_id].push(he_1p_c_id);
        meshgraph.outgoing_halfedges[v1_id].push(he_1p_5_id);
        meshgraph.outgoing_halfedges[v1_id].push(he_1p_3_id);

        meshgraph.remove_only_vertex(v1p_id);

        #[cfg(feature = "rerun")]
        meshgraph.log_rerun();

        meshgraph.remove_degenerate_edges(center_v_id);

        #[cfg(feature = "rerun")]
        {
            meshgraph.log_rerun();
            crate::RR.flush_blocking().unwrap();
        }
    }

    #[test]
    fn test_remove_flap() {
        crate::utils::get_tracing_subscriber();

        let mut mesh_graph = MeshGraph::new();

        let v1 = mesh_graph.add_vertex(Vec3::new(0.0, 0.0, 0.0));
        let v2 = mesh_graph.add_vertex(Vec3::new(1.0, 0.0, 0.0));
        let v3 = mesh_graph.add_vertex(Vec3::new(0.0, 1.0, 0.0));

        let v4 = mesh_graph.add_vertex(Vec3::new(1.0, 1.0, 0.5));
        let v5 = mesh_graph.add_vertex(Vec3::new(1.0, 1.0, -0.5));

        let edge1 = mesh_graph.add_edge(v1, v2);
        let edge2 = mesh_graph.add_edge(v2, v3);
        let edge2_d = mesh_graph.add_edge(v2, v3);

        mesh_graph.add_face_from_halfedges(edge1.start_to_end_he_id, edge2.start_to_end_he_id);
        mesh_graph.add_face_from_halfedges(edge2_d.twin_he_id, edge1.twin_he_id);

        mesh_graph.add_face_from_halfedge_and_vertex(edge2.twin_he_id, v4);
        mesh_graph.add_face_from_halfedge_and_vertex(edge2_d.start_to_end_he_id, v5);

        #[cfg(feature = "rerun")]
        mesh_graph.log_rerun();

        let mut removed_vertices = Vec::new();
        let mut removed_halfedges = Vec::new();
        let mut removed_faces = Vec::new();

        mesh_graph.remove_neighboring_flaps(
            v1,
            &mut removed_vertices,
            &mut removed_halfedges,
            &mut removed_faces,
        );

        #[cfg(feature = "rerun")]
        {
            mesh_graph.log_rerun();
            crate::RR.flush_blocking().unwrap();
        }

        assert_eq!(removed_vertices.len(), 1);
        assert_eq!(removed_halfedges.len(), 6);
        assert_eq!(removed_faces.len(), 2);
    }

    #[test]
    fn test_remove_double_flaps() {
        crate::utils::get_tracing_subscriber();

        let mut mesh_graph = MeshGraph::new();

        let v1 = mesh_graph.add_vertex(Vec3::new(0.0, 0.0, 0.0));
        let v2 = mesh_graph.add_vertex(Vec3::new(1.0, 0.0, 0.0));
        let v3 = mesh_graph.add_vertex(Vec3::new(0.0, 1.0, 0.0));
        let v4 = mesh_graph.add_vertex(Vec3::new(1.0, 1.0, 0.0));

        let v5 = mesh_graph.add_vertex(Vec3::new(0.0, -1.0, 0.0));

        let edge1 = mesh_graph.add_edge(v1, v2);
        let edge1_d = mesh_graph.add_edge(v1, v2);
        let edge2 = mesh_graph.add_edge(v2, v3);
        let edge2_d = mesh_graph.add_edge(v2, v3);

        mesh_graph.add_face_from_halfedges(edge1.start_to_end_he_id, edge2.start_to_end_he_id);
        mesh_graph.add_face_from_halfedge_and_vertex(edge2.twin_he_id, v4);

        mesh_graph.add_face_from_halfedge_and_vertex(edge2_d.start_to_end_he_id, v4);
        mesh_graph.add_face_from_halfedges(edge2_d.twin_he_id, edge1_d.twin_he_id);

        mesh_graph.add_face_from_halfedge_and_vertex(edge1_d.start_to_end_he_id, v5);

        #[cfg(feature = "rerun")]
        mesh_graph.log_rerun();

        let mut removed_vertices = Vec::new();
        let mut removed_halfedges = Vec::new();
        let mut removed_faces = Vec::new();

        mesh_graph.remove_neighboring_flaps(
            v1,
            &mut removed_vertices,
            &mut removed_halfedges,
            &mut removed_faces,
        );

        #[cfg(feature = "rerun")]
        {
            mesh_graph.log_rerun();
            crate::RR.flush_blocking().unwrap();
        }

        assert_eq!(removed_vertices.len(), 0);
        assert_eq!(removed_halfedges.len(), 6);
        assert_eq!(removed_faces.len(), 2);
    }
}