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
//! See [Mesh](crate::mesh::Mesh).

use crate::mesh::*;
use crate::Error;

/// # Edit
impl Mesh {
    /// Moves the vertex to the specified position.
    pub fn set_vertex_position(&mut self, vertex_id: VertexID, value: Vec3) {
        self.connectivity_info.set_position(vertex_id, value);
    }

    /// Flip the given edge such that the edge after the flip is connected to the
    /// other pair of the four vertices connected to the two adjacent faces.
    ///
    /// ```text
    ///   /\          /|\
    ///  /  \        / | \
    /// /____\  --> /  |  \
    /// \    /      \  |  /
    ///  \  /        \ | /
    ///   \/          \|/
    /// ```
    ///
    /// # Error
    ///
    /// Returns an error if trying to flip an edge on the boundary or the flip will connect two vertices that are already connected by another edge.
    ///
    pub fn flip_edge(&mut self, halfedge_id: HalfEdgeID) -> Result<(), Error> {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        let face_id = walker
            .face_id()
            .ok_or(Error::ActionWillResultInInvalidMesh(format!(
                "Trying to flip edge on boundary"
            )))?;
        let next_id = walker.next_id().unwrap();
        let previous_id = walker.previous_id().unwrap();
        let v0 = walker.vertex_id().unwrap();
        walker.as_next();
        let v3 = walker.vertex_id().unwrap();
        walker.as_previous();

        walker.as_twin();
        let twin_id = walker.halfedge_id().unwrap();
        let twin_face_id = walker
            .face_id()
            .ok_or(Error::ActionWillResultInInvalidMesh(format!(
                "Trying to flip edge on boundary"
            )))?;
        let twin_next_id = walker.next_id().unwrap();
        let twin_previous_id = walker.previous_id().unwrap();
        let v1 = walker.vertex_id().unwrap();
        let v2 = walker.as_next().vertex_id().unwrap();

        if self.connecting_edge(v2, v3).is_some() {
            Err(Error::ActionWillResultInInvalidMesh ( format!("Trying to flip edge which will connect two vertices that are already connected by another edge")))?;
        }

        self.connectivity_info
            .set_face_halfedge(face_id, previous_id);
        self.connectivity_info
            .set_face_halfedge(twin_face_id, twin_previous_id);

        self.connectivity_info
            .set_vertex_halfedge(v0, Some(next_id));
        self.connectivity_info
            .set_vertex_halfedge(v1, Some(twin_next_id));

        self.connectivity_info
            .set_halfedge_next(halfedge_id, Some(previous_id));
        self.connectivity_info
            .set_halfedge_next(next_id, Some(twin_id));
        self.connectivity_info
            .set_halfedge_next(previous_id, Some(twin_next_id));
        self.connectivity_info
            .set_halfedge_next(twin_id, Some(twin_previous_id));
        self.connectivity_info
            .set_halfedge_next(twin_next_id, Some(halfedge_id));
        self.connectivity_info
            .set_halfedge_next(twin_previous_id, Some(next_id));

        self.connectivity_info.set_halfedge_vertex(halfedge_id, v3);
        self.connectivity_info.set_halfedge_vertex(twin_id, v2);

        self.connectivity_info
            .set_halfedge_face(next_id, Some(twin_face_id));
        self.connectivity_info
            .set_halfedge_face(twin_next_id, Some(face_id));

        Ok(())
    }

    /// Split the given edge into two.
    /// Returns the id of the new vertex positioned at the given position.
    pub fn split_edge(&mut self, halfedge_id: HalfEdgeID, position: Vec3) -> VertexID {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        if walker.face_id().is_none() {
            walker.as_twin();
        }
        let split_halfedge_id = walker.halfedge_id().unwrap();

        walker.as_twin();
        let twin_halfedge_id = walker.halfedge_id().unwrap();
        let twin_vertex_id = walker.vertex_id();
        let is_boundary = walker.face_id().is_none();

        let new_vertex_id = self.connectivity_info.new_vertex(position);
        self.split_one_face(split_halfedge_id, twin_halfedge_id, new_vertex_id);

        if !is_boundary {
            self.split_one_face(twin_halfedge_id, split_halfedge_id, new_vertex_id);
        } else {
            let new_halfedge_id = self
                .connectivity_info
                .new_halfedge(twin_vertex_id, None, None);
            self.connectivity_info
                .set_halfedge_twin(split_halfedge_id, new_halfedge_id);
            self.connectivity_info
                .set_halfedge_vertex(twin_halfedge_id, new_vertex_id);
        };

        new_vertex_id
    }

    /// Split the given face into three new faces.
    /// Returns the id of the new vertex positioned at the given position.
    pub fn split_face(&mut self, face_id: FaceID, position: Vec3) -> VertexID {
        let new_vertex_id = self.connectivity_info.new_vertex(position);

        let mut walker = self.walker_from_face(face_id);
        let halfedge_id1 = walker.halfedge_id().unwrap();
        let vertex_id1 = walker.vertex_id().unwrap();

        walker.as_next();
        let halfedge_id2 = walker.halfedge_id().unwrap();
        let vertex_id2 = walker.vertex_id().unwrap();

        walker.as_next();
        let halfedge_id3 = walker.halfedge_id().unwrap();
        let vertex_id3 = walker.vertex_id().unwrap();

        let face_id1 = self.connectivity_info.create_face_with_existing_halfedge(
            vertex_id1,
            vertex_id2,
            new_vertex_id,
            halfedge_id2,
        );
        let face_id2 = self.connectivity_info.create_face_with_existing_halfedge(
            vertex_id2,
            vertex_id3,
            new_vertex_id,
            halfedge_id3,
        );

        let new_halfedge_id2 = self.connectivity_info.new_halfedge(
            Some(vertex_id3),
            Some(halfedge_id1),
            Some(face_id),
        );
        let new_halfedge_id1 = self.connectivity_info.new_halfedge(
            Some(new_vertex_id),
            Some(new_halfedge_id2),
            Some(face_id),
        );
        self.connectivity_info
            .set_halfedge_next(halfedge_id1, Some(new_halfedge_id1));
        self.connectivity_info
            .set_face_halfedge(face_id, halfedge_id1);

        // Update twin information
        let mut new_halfedge_id = unsafe { HalfEdgeID::new(0) };
        for halfedge_id in self.face_halfedge_iter(face_id1) {
            let vid = self.walker_from_halfedge(halfedge_id).vertex_id().unwrap();
            if vid == vertex_id1 {
                self.connectivity_info
                    .set_halfedge_twin(new_halfedge_id1, halfedge_id);
            } else if vid == new_vertex_id {
                new_halfedge_id = halfedge_id;
            }
        }
        for halfedge_id in self.face_halfedge_iter(face_id2) {
            let vid = self.walker_from_halfedge(halfedge_id).vertex_id().unwrap();
            if vid == vertex_id2 {
                self.connectivity_info
                    .set_halfedge_twin(new_halfedge_id, halfedge_id);
            } else if vid == new_vertex_id {
                self.connectivity_info
                    .set_halfedge_twin(new_halfedge_id2, halfedge_id);
            }
        }
        new_vertex_id
    }

    fn split_one_face(
        &mut self,
        halfedge_id: HalfEdgeID,
        twin_halfedge_id: HalfEdgeID,
        new_vertex_id: VertexID,
    ) {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        let vertex_id1 = walker.vertex_id().unwrap();
        let old_face_id = walker.face_id().unwrap();

        walker.as_next();
        let halfedge_to_reuse_vertex = walker.vertex_id().unwrap();
        let halfedge_to_reuse = walker.halfedge_id().unwrap();
        let halfedge_to_reuse_next = walker.next_id().unwrap();

        // Create new face
        let new_face_id = self.connectivity_info.create_face_with_existing_halfedge(
            vertex_id1,
            halfedge_to_reuse_vertex,
            new_vertex_id,
            halfedge_to_reuse,
        );

        // Update old face
        let new_halfedge_id = self.connectivity_info.new_halfedge(
            Some(halfedge_to_reuse_vertex),
            Some(halfedge_to_reuse_next),
            Some(old_face_id),
        );
        self.connectivity_info
            .set_halfedge_vertex(halfedge_id, new_vertex_id);
        self.connectivity_info
            .set_halfedge_next(halfedge_id, Some(new_halfedge_id));
        self.connectivity_info
            .set_face_halfedge(old_face_id, halfedge_id);

        // Update twin information
        for halfedge_id in self.face_halfedge_iter(new_face_id) {
            let vid = self.walker_from_halfedge(halfedge_id).vertex_id().unwrap();
            if vid == vertex_id1 {
                self.connectivity_info
                    .set_halfedge_twin(twin_halfedge_id, halfedge_id);
            } else if vid == new_vertex_id {
                self.connectivity_info
                    .set_halfedge_twin(new_halfedge_id, halfedge_id);
            }
        }
    }

    ///
    /// Collapses the given edge. Consequently, the to adjacent faces are removed and
    /// the two adjacent vertices are merged into one vertex
    /// which position is the average of the original vertex positions.
    /// Returns the merged vertex.
    ///
    /// **Note:** This might make some faces degenerate or produce edges and vertices that are not connected.
    ///
    pub fn collapse_edge(&mut self, halfedge_id: HalfEdgeID) -> VertexID {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        let surviving_vertex_id = walker.vertex_id().unwrap();
        walker.as_twin();
        let dying_vertex_id = walker.vertex_id().unwrap();
        let new_position = 0.5
            * (self.vertex_position(surviving_vertex_id) + self.vertex_position(dying_vertex_id));

        // Update halfedges pointing to dying vertex
        for halfedge_id in self.vertex_halfedge_iter(dying_vertex_id) {
            self.connectivity_info.set_halfedge_vertex(
                self.walker_from_halfedge(halfedge_id).twin_id().unwrap(),
                surviving_vertex_id,
            );
        }

        // Remove first face + halfedges
        let mut he_id1 = walker.halfedge_id();
        if walker.face_id().is_some() {
            walker.as_previous();
            self.connectivity_info
                .set_vertex_halfedge(surviving_vertex_id, walker.twin_id());
            walker.as_next();
        } else {
            self.connectivity_info.remove_halfedge(he_id1.unwrap());
            he_id1 = None;
        }

        // Remove second face + halfedges
        walker.as_twin();
        let he_id2 = walker.halfedge_id().unwrap();
        if walker.face_id().is_some() {
            walker.as_previous();
            self.connectivity_info
                .set_vertex_halfedge(surviving_vertex_id, walker.twin_id());
            self.remove_one_face(he_id2);
        } else {
            self.connectivity_info.remove_halfedge(he_id2);
        }

        if let Some(he_id_to_remove) = he_id1 {
            self.remove_one_face(he_id_to_remove);
        }

        // Remove dying vertex
        self.connectivity_info.remove_vertex(dying_vertex_id);

        self.move_vertex_to(surviving_vertex_id, new_position);
        surviving_vertex_id
    }

    fn remove_one_face(&mut self, halfedge_id: HalfEdgeID) {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        let face_id = walker.face_id().unwrap();

        walker.as_next();
        let halfedge_id1 = walker.halfedge_id().unwrap();
        let twin_id1 = walker.twin_id().unwrap();
        let vertex_id = walker.vertex_id().unwrap();
        walker.as_next();
        let halfedge_id2 = walker.halfedge_id().unwrap();
        let twin_id2 = walker.twin_id().unwrap();

        self.connectivity_info.remove_face(face_id);
        self.connectivity_info.remove_halfedge(halfedge_id);
        self.connectivity_info.remove_halfedge(halfedge_id1);
        self.connectivity_info.remove_halfedge(halfedge_id2);
        self.connectivity_info.set_halfedge_twin(twin_id1, twin_id2);
        self.connectivity_info
            .set_vertex_halfedge(vertex_id, Some(twin_id1));

        walker.as_twin();
    }

    ///
    /// Adds a vertex to the mesh which is not connected to anything.
    /// Usually used in combination with [Mesh::add_face].
    ///
    pub fn add_vertex(&mut self, position: Vec3) -> VertexID {
        self.connectivity_info.new_vertex(position)
    }

    ///
    /// Adds a face to the mesh and connects it to the given vertices which can be created using the [Mesh::add_vertex] method.
    /// Also creates edges between the vertices if they do not already exist.
    ///
    pub fn add_face(
        &mut self,
        vertex_id1: VertexID,
        vertex_id2: VertexID,
        vertex_id3: VertexID,
    ) -> FaceID {
        let face_id = self
            .connectivity_info
            .create_face(vertex_id1, vertex_id2, vertex_id3);
        for halfedge in self.halfedge_iter() {
            let walker = self.walker_from_halfedge(halfedge);
            let new_halfedge = self.connectivity_info.new_halfedge(
                walker.into_next().into_next().vertex_id(),
                None,
                None,
            );
            self.connectivity_info
                .set_halfedge_twin(new_halfedge, halfedge);
        }
        face_id
    }

    ///
    /// Removes the given face and also the adjacent edges and vertices if they are not connected to any other face.
    ///
    pub fn remove_face(&mut self, face_id: FaceID) {
        let edges: Vec<HalfEdgeID> = self.face_halfedge_iter(face_id).collect();
        self.remove_face_unsafe(face_id);
        for halfedge_id in edges {
            self.remove_edge_if_lonely(halfedge_id);
        }
    }

    pub(super) fn remove_face_unsafe(&mut self, face_id: FaceID) {
        let mut walker = self.walker_from_face(face_id);
        let he_id1 = walker.halfedge_id().unwrap();
        walker.as_next();
        let he_id2 = walker.halfedge_id().unwrap();
        walker.as_next();
        let he_id3 = walker.halfedge_id().unwrap();

        self.connectivity_info.set_halfedge_face(he_id1, None);
        self.connectivity_info.set_halfedge_face(he_id2, None);
        self.connectivity_info.set_halfedge_face(he_id3, None);

        self.connectivity_info.set_halfedge_next(he_id1, None);
        self.connectivity_info.set_halfedge_next(he_id2, None);
        self.connectivity_info.set_halfedge_next(he_id3, None);

        self.connectivity_info.remove_face(face_id);
    }

    /// Removes edges and vertices that are not connected to any face.
    pub fn remove_lonely_primitives(&mut self) {
        let edges: Vec<HalfEdgeID> = self.edge_iter().collect();
        for halfedge_id in edges {
            self.remove_edge_if_lonely(halfedge_id);
        }

        for vertex_id in self.vertex_iter() {
            self.remove_vertex_if_lonely(vertex_id);
        }
    }

    pub(super) fn remove_edge_if_lonely(&mut self, halfedge_id: HalfEdgeID) {
        let mut walker = self.walker_from_halfedge(halfedge_id);
        if walker.face_id().is_none() && walker.as_twin().face_id().is_none() {
            let vertex_id1 = walker.vertex_id().unwrap();
            let halfedge_id1 = walker.halfedge_id().unwrap();
            walker.as_twin();
            let vertex_id2 = walker.vertex_id().unwrap();
            let halfedge_id2 = walker.halfedge_id().unwrap();

            self.connectivity_info.remove_halfedge(halfedge_id1);
            self.connectivity_info.remove_halfedge(halfedge_id2);

            let find_new_edge_connectivity =
                |mesh: &Mesh, vertex_id: VertexID| -> Option<HalfEdgeID> {
                    for halfedge_id in mesh.halfedge_iter() {
                        let walker = mesh.walker_from_halfedge(halfedge_id);
                        if walker.vertex_id().unwrap() == vertex_id {
                            return walker.twin_id();
                        }
                    }
                    None
                };

            if self.walker_from_vertex(vertex_id1).halfedge_id().unwrap() == halfedge_id2 {
                let new_edge = find_new_edge_connectivity(&self, vertex_id1);
                self.connectivity_info
                    .set_vertex_halfedge(vertex_id1, new_edge);
                self.remove_vertex_if_lonely(vertex_id1);
            }
            if self.walker_from_vertex(vertex_id2).halfedge_id().unwrap() == halfedge_id1 {
                let new_edge = find_new_edge_connectivity(&self, vertex_id2);
                self.connectivity_info
                    .set_vertex_halfedge(vertex_id2, new_edge);
                self.remove_vertex_if_lonely(vertex_id2);
            }
        }
    }

    fn remove_vertex_if_lonely(&mut self, vertex_id: VertexID) {
        if self.connectivity_info.vertex_halfedge(vertex_id).is_none() {
            self.connectivity_info.remove_vertex(vertex_id);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use three_d_asset::{Indices, Positions, TriMesh};

    #[test]
    fn test_flip_edge() {
        let mut no_flips = 0;
        let mut mesh = crate::test_utility::square();
        let no_edges = mesh.no_halfedges();
        for halfedge_id in mesh.halfedge_iter() {
            let (v0, v1) = mesh.edge_vertices(halfedge_id);

            if mesh.flip_edge(halfedge_id).is_ok() {
                mesh.is_valid().unwrap();

                let (v2, v3) = mesh.edge_vertices(halfedge_id);
                assert_ne!(v0, v2);
                assert_ne!(v1, v2);
                assert_ne!(v0, v3);
                assert_ne!(v1, v3);

                assert!(mesh.connecting_edge(v0, v1).is_none());
                assert!(mesh.connecting_edge(v2, v3).is_some());

                let edge = mesh.connecting_edge(v2, v3).unwrap();
                let twin = mesh.walker_from_halfedge(edge).twin_id().unwrap();
                assert!(edge == halfedge_id || twin == halfedge_id,
                        "Flipped edge {} or flipped edge twin {} should be equal to before flipped edge id {}", edge, twin, halfedge_id);
                no_flips = no_flips + 1;
            }
        }
        assert_eq!(no_edges, mesh.no_halfedges());
        assert_eq!(no_flips, 2);
    }

    #[test]
    fn test_flip_multiple_edges() {
        let mut no_flips = 0;
        let mut mesh: Mesh = TriMesh::sphere(3).into();
        let no_edges = mesh.no_halfedges();
        for halfedge_id in mesh.halfedge_iter() {
            let (v0, v1) = mesh.edge_vertices(halfedge_id);

            if mesh.flip_edge(halfedge_id).is_ok() {
                mesh.is_valid().unwrap();

                let (v2, v3) = mesh.edge_vertices(halfedge_id);
                assert_ne!(v0, v2);
                assert_ne!(v1, v2);
                assert_ne!(v0, v3);
                assert_ne!(v1, v3);

                assert!(mesh.connecting_edge(v0, v1).is_none());
                assert!(mesh.connecting_edge(v2, v3).is_some());

                let edge = mesh.connecting_edge(v2, v3).unwrap();
                let twin = mesh.walker_from_halfedge(edge).twin_id().unwrap();
                assert!(edge == halfedge_id || twin == halfedge_id,
                        "Flipped edge {} or flipped edge twin {} should be equal to before flipped edge id {}", edge, twin, halfedge_id);
                no_flips = no_flips + 1;
            }
        }
        assert_eq!(no_edges, mesh.no_halfedges());
        assert!(no_flips > 0);
    }

    #[test]
    fn test_split_edge_on_boundary() {
        let mut mesh = crate::test_utility::triangle();
        for halfedge_id in mesh.halfedge_iter() {
            if mesh.walker_from_halfedge(halfedge_id).face_id().is_some() {
                mesh.split_edge(halfedge_id, vec3(-1.0, -1.0, -1.0));

                assert_eq!(mesh.no_vertices(), 4);
                assert_eq!(mesh.no_halfedges(), 2 * 3 + 4);
                assert_eq!(mesh.no_faces(), 2);

                let mut walker = mesh.walker_from_halfedge(halfedge_id);
                assert!(walker.halfedge_id().is_some());
                assert!(walker.face_id().is_some());
                assert!(walker.vertex_id().is_some());

                walker.as_twin();
                assert!(walker.halfedge_id().is_some());
                assert!(walker.face_id().is_none());
                assert!(walker.vertex_id().is_some());

                walker.as_twin().as_next().as_twin();
                assert!(walker.halfedge_id().is_some());
                assert!(walker.face_id().is_some());
                assert!(walker.vertex_id().is_some());

                walker.as_next().as_next().as_twin();
                assert!(walker.halfedge_id().is_some());
                assert!(walker.face_id().is_none());
                assert!(walker.vertex_id().is_some());

                mesh.is_valid().unwrap();

                break;
            }
        }
    }

    #[test]
    fn test_split_edge() {
        let mut mesh = crate::test_utility::square();
        for halfedge_id in mesh.halfedge_iter() {
            let mut walker = mesh.walker_from_halfedge(halfedge_id);
            if walker.face_id().is_some() && walker.as_twin().face_id().is_some() {
                let vertex_id = mesh.split_edge(halfedge_id, vec3(-1.0, -1.0, -1.0));
                assert_eq!(mesh.no_vertices(), 5);
                assert_eq!(mesh.no_halfedges(), 4 * 3 + 4);
                assert_eq!(mesh.no_faces(), 4);

                let mut w = mesh.walker_from_vertex(vertex_id);
                let start_halfedge_id = w.halfedge_id();
                let mut end_halfedge_id = w.twin_id();
                for _ in 0..4 {
                    assert!(w.halfedge_id().is_some());
                    assert!(w.twin_id().is_some());
                    assert!(w.vertex_id().is_some());
                    assert!(w.face_id().is_some());
                    w.as_previous().as_twin();
                    end_halfedge_id = w.halfedge_id();
                }
                assert_eq!(
                    start_halfedge_id, end_halfedge_id,
                    "Did not go the full round"
                );

                mesh.is_valid().unwrap();
                break;
            }
        }
    }

    #[test]
    fn test_split_face() {
        let mut mesh = crate::test_utility::triangle();
        let face_id = mesh.face_iter().next().unwrap();

        let vertex_id = mesh.split_face(face_id, vec3(-1.0, -1.0, -1.0));

        assert_eq!(mesh.no_vertices(), 4);
        assert_eq!(mesh.no_halfedges(), 3 * 3 + 3);
        assert_eq!(mesh.no_faces(), 3);

        let mut walker = mesh.walker_from_vertex(vertex_id);
        let start_edge = walker.halfedge_id().unwrap();
        let one_round_edge = walker
            .as_previous()
            .as_twin()
            .as_previous()
            .as_twin()
            .as_previous()
            .as_twin()
            .halfedge_id()
            .unwrap();
        assert_eq!(start_edge, one_round_edge);

        assert!(walker.face_id().is_some());
        walker.as_next().as_twin();
        assert!(walker.face_id().is_none());

        walker.as_twin().as_next().as_twin().as_next().as_twin();
        assert!(walker.face_id().is_none());

        walker.as_twin().as_next().as_twin().as_next().as_twin();
        assert!(walker.face_id().is_none());

        mesh.is_valid().unwrap();
    }

    #[test]
    fn test_collapse_edge_on_boundary1() {
        let mut mesh: Mesh = TriMesh {
            indices: Indices::U8(vec![0, 1, 2, 1, 3, 2, 2, 3, 4]),
            positions: Positions::F64(vec![
                vec3(0.0, 0.0, 0.0),
                vec3(0.0, 0.0, 1.0),
                vec3(1.0, 0.0, 0.0),
                vec3(1.0, 0.0, 1.0),
                vec3(2.0, 0.0, 0.5),
            ]),
            ..Default::default()
        }
        .into();

        for halfedge_id in mesh.halfedge_iter() {
            let mut walker = mesh.walker_from_halfedge(halfedge_id);
            if walker.face_id().is_none()
                && walker.as_twin().as_next().as_twin().face_id().is_some()
                && walker.as_twin().as_next().as_twin().face_id().is_some()
            {
                mesh.collapse_edge(halfedge_id);

                assert_eq!(mesh.no_vertices(), 4);
                assert_eq!(mesh.no_halfedges(), 10);
                assert_eq!(mesh.no_faces(), 2);

                mesh.is_valid().unwrap();

                break;
            }
        }
    }

    #[test]
    fn test_collapse_edge_on_boundary2() {
        let mut mesh: Mesh = TriMesh {
            indices: Indices::U8(vec![0, 2, 3, 0, 3, 1]),
            positions: Positions::F64(vec![
                vec3(0.0, 0.0, 0.0),
                vec3(0.0, 0.0, 1.0),
                vec3(1.0, 0.0, 0.0),
                vec3(1.0, 0.0, 1.0),
            ]),
            ..Default::default()
        }
        .into();
        for halfedge_id in mesh.halfedge_iter() {
            if mesh.is_edge_on_boundary(halfedge_id) {
                mesh.collapse_edge(halfedge_id);

                assert_eq!(mesh.no_vertices(), 3);
                assert_eq!(mesh.no_halfedges(), 6);
                assert_eq!(mesh.no_faces(), 1);

                mesh.is_valid().unwrap();

                break;
            }
        }
    }

    #[test]
    fn test_collapse_edge() {
        let mut mesh = crate::test_utility::subdivided_triangle();
        for halfedge_id in mesh.halfedge_iter() {
            if !mesh.is_edge_on_boundary(halfedge_id) {
                mesh.collapse_edge(halfedge_id);
                assert_eq!(mesh.no_vertices(), 3);
                assert_eq!(mesh.no_halfedges(), 6);
                assert_eq!(mesh.no_faces(), 1);

                mesh.is_valid().unwrap();
                break;
            }
        }
    }

    #[test]
    fn test_recursive_collapse_edge() {
        let mut mesh: Mesh = TriMesh {
            indices: Indices::U8(vec![0, 1, 2, 1, 3, 2, 2, 3, 4]),
            positions: Positions::F64(vec![
                vec3(0.0, 0.0, 0.0),
                vec3(0.0, 0.0, 1.0),
                vec3(1.0, 0.0, 0.0),
                vec3(1.0, 0.0, 1.0),
                vec3(2.0, 0.0, 0.5),
            ]),
            ..Default::default()
        }
        .into();

        while mesh.no_faces() > 1 {
            for halfedge_id in mesh.halfedge_iter() {
                if mesh.is_edge_on_boundary(halfedge_id) {
                    mesh.collapse_edge(halfedge_id);
                    break;
                }
            }
        }
        assert_eq!(mesh.no_vertices(), 3);
        assert_eq!(mesh.no_halfedges(), 6);
        assert_eq!(mesh.no_faces(), 1);
        mesh.is_valid().unwrap();
    }

    #[test]
    fn test_remove_face_when_unconnected() {
        let mut mesh: Mesh = TriMesh {
            positions: Positions::F64(vec![
                vec3(1.0, 0.0, 0.0),
                vec3(0.0, 0.0, 0.0),
                vec3(0.0, 0.0, -1.0),
                vec3(1.0, 0.0, 0.0),
                vec3(0.0, 0.0, 0.0),
                vec3(0.0, 0.0, -1.0),
            ]),
            ..Default::default()
        }
        .into();

        let faces: Vec<FaceID> = mesh.face_iter().into_iter().collect();

        mesh.remove_face(faces[0]);

        assert_eq!(3, mesh.no_vertices());
        assert_eq!(6, mesh.no_halfedges());
        assert_eq!(1, mesh.no_faces());
        mesh.is_valid().unwrap();
    }

    #[test]
    fn test_remove_face_when_connected() {
        let mut mesh = crate::test_utility::square();

        let face_id = mesh.face_iter().next().unwrap();

        mesh.remove_face(face_id);

        assert_eq!(3, mesh.no_vertices());
        assert_eq!(6, mesh.no_halfedges());
        assert_eq!(1, mesh.no_faces());
        mesh.is_valid().unwrap();
    }

    #[test]
    fn test_remove_face_when_three_connected_faces() {
        let mut mesh = crate::test_utility::subdivided_triangle();

        let face_id = mesh.face_iter().next().unwrap();

        mesh.remove_face(face_id);

        assert_eq!(4, mesh.no_vertices());
        assert_eq!(10, mesh.no_halfedges());
        assert_eq!(2, mesh.no_faces());
        mesh.is_valid().unwrap();
    }
}