oxihuman-mesh 0.2.1

Mesh processing, topology, and geometry algorithms for OxiHuman
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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Mesh hole detection and hole-filling (triangulated polygon patches).
//!
//! Provides algorithms to find open boundary loops in a mesh and fill them
//! with new triangulated geometry using fan, ear-clip, or minimum-area strategies.

#![allow(dead_code)]

use std::collections::HashMap;

use crate::mesh::MeshBuffers;

// ---------------------------------------------------------------------------
// Math helpers
// ---------------------------------------------------------------------------

#[inline]
fn sub3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}

#[inline]
fn add3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}

#[inline]
fn scale3(v: [f32; 3], s: f32) -> [f32; 3] {
    [v[0] * s, v[1] * s, v[2] * s]
}

#[inline]
fn cross3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [
        a[1] * b[2] - a[2] * b[1],
        a[2] * b[0] - a[0] * b[2],
        a[0] * b[1] - a[1] * b[0],
    ]
}

#[inline]
fn dot3(a: [f32; 3], b: [f32; 3]) -> f32 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

#[inline]
fn len3(v: [f32; 3]) -> f32 {
    (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
}

#[inline]
fn normalize3(v: [f32; 3]) -> [f32; 3] {
    let l = len3(v);
    if l < 1e-10 {
        [0.0, 1.0, 0.0]
    } else {
        [v[0] / l, v[1] / l, v[2] / l]
    }
}

#[inline]
fn len2(v: [f32; 2]) -> f32 {
    (v[0] * v[0] + v[1] * v[1]).sqrt()
}

// ---------------------------------------------------------------------------
// Public data types
// ---------------------------------------------------------------------------

/// A hole: an ordered boundary loop of vertex indices.
#[derive(Debug, Clone)]
pub struct MeshHole {
    /// Ordered boundary vertices forming a closed loop.
    pub loop_vertices: Vec<u32>,
    /// Approximate planar area of the hole.
    pub area_estimate: f32,
}

impl MeshHole {
    /// Number of vertices in the boundary loop.
    pub fn vertex_count(&self) -> usize {
        self.loop_vertices.len()
    }

    /// Compute the perimeter length of the boundary loop.
    pub fn perimeter(&self, positions: &[[f32; 3]]) -> f32 {
        let n = self.loop_vertices.len();
        if n < 2 {
            return 0.0;
        }
        let mut total = 0.0f32;
        for i in 0..n {
            let a = self.loop_vertices[i] as usize;
            let b = self.loop_vertices[(i + 1) % n] as usize;
            if a < positions.len() && b < positions.len() {
                let d = sub3(positions[b], positions[a]);
                total += len3(d);
            }
        }
        total
    }

    /// Compute the centroid of the boundary loop vertices.
    pub fn centroid(&self, positions: &[[f32; 3]]) -> [f32; 3] {
        let n = self.loop_vertices.len();
        if n == 0 {
            return [0.0, 0.0, 0.0];
        }
        let mut sum = [0.0f32; 3];
        let mut count = 0usize;
        for &vi in &self.loop_vertices {
            let idx = vi as usize;
            if idx < positions.len() {
                sum = add3(sum, positions[idx]);
                count += 1;
            }
        }
        if count == 0 {
            return [0.0, 0.0, 0.0];
        }
        let inv = 1.0 / count as f32;
        scale3(sum, inv)
    }

    /// Estimate the normal of the hole polygon using Newell's method.
    pub fn normal_estimate(&self, positions: &[[f32; 3]]) -> [f32; 3] {
        let n = self.loop_vertices.len();
        if n < 3 {
            return [0.0, 1.0, 0.0];
        }
        let mut normal = [0.0f32; 3];
        for i in 0..n {
            let a = self.loop_vertices[i] as usize;
            let b = self.loop_vertices[(i + 1) % n] as usize;
            if a >= positions.len() || b >= positions.len() {
                continue;
            }
            let pa = positions[a];
            let pb = positions[b];
            normal[0] += (pa[1] - pb[1]) * (pa[2] + pb[2]);
            normal[1] += (pa[2] - pb[2]) * (pa[0] + pb[0]);
            normal[2] += (pa[0] - pb[0]) * (pa[1] + pb[1]);
        }
        normalize3(normal)
    }
}

/// Result of hole filling.
#[derive(Debug, Clone)]
pub struct PatchResult {
    /// The resulting mesh with holes filled.
    pub mesh: MeshBuffers,
    /// Number of holes that were filled.
    pub holes_filled: usize,
    /// Number of new vertices added.
    pub new_vertices: usize,
    /// Number of new triangles added.
    pub new_triangles: usize,
}

/// Hole filling strategy.
#[derive(Debug, Clone, PartialEq)]
pub enum PatchStrategy {
    /// Triangle fan from centroid (fast, works for convex holes).
    Fan,
    /// Ear-clipping triangulation (works for any simple polygon).
    EarClip,
    /// Minimum total triangle area (greedy).
    MinArea,
}

// ---------------------------------------------------------------------------
// Internal: build boundary edge map
// ---------------------------------------------------------------------------

fn build_boundary_edge_map(mesh: &MeshBuffers) -> HashMap<(u32, u32), u32> {
    let mut map: HashMap<(u32, u32), u32> = HashMap::new();
    let indices = &mesh.indices;
    let face_count = indices.len() / 3;
    for f in 0..face_count {
        let a = indices[f * 3];
        let b = indices[f * 3 + 1];
        let c = indices[f * 3 + 2];
        for &(p, q) in &[(a, b), (b, c), (c, a)] {
            let key = (p.min(q), p.max(q));
            *map.entry(key).or_insert(0) += 1;
        }
    }
    map
}

/// Get directed boundary edges: edges used exactly once in the mesh,
/// keeping the direction as they appear in the winding order.
fn directed_boundary_edges(mesh: &MeshBuffers) -> Vec<(u32, u32)> {
    let edge_count = build_boundary_edge_map(mesh);
    let indices = &mesh.indices;
    let face_count = indices.len() / 3;
    let mut directed = Vec::new();
    for f in 0..face_count {
        let a = indices[f * 3];
        let b = indices[f * 3 + 1];
        let c = indices[f * 3 + 2];
        for &(p, q) in &[(a, b), (b, c), (c, a)] {
            let key = (p.min(q), p.max(q));
            if edge_count.get(&key).copied().unwrap_or(0) == 1 {
                directed.push((p, q));
            }
        }
    }
    directed
}

/// Walk directed boundary edges into ordered loops.
fn walk_boundary_loops(directed_edges: &[(u32, u32)]) -> Vec<Vec<u32>> {
    if directed_edges.is_empty() {
        return Vec::new();
    }

    // Build a map from start vertex -> end vertex for directed edges.
    let mut next_map: HashMap<u32, u32> = HashMap::new();
    for &(a, b) in directed_edges {
        next_map.insert(a, b);
    }

    let mut visited: HashMap<u32, bool> = next_map.keys().map(|&k| (k, false)).collect();
    let mut loops = Vec::new();

    for &start in next_map.keys() {
        if visited.get(&start).copied().unwrap_or(true) {
            continue;
        }

        let mut loop_verts = vec![start];
        *visited.entry(start).or_insert(true) = true;
        let mut current = start;

        loop {
            match next_map.get(&current) {
                None => break,
                Some(&next) => {
                    if next == start {
                        // Closed loop complete
                        loops.push(loop_verts);
                        break;
                    }
                    if visited.get(&next).copied().unwrap_or(false) {
                        // Hit visited vertex — close off
                        loop_verts.push(next);
                        loops.push(loop_verts);
                        break;
                    }
                    *visited.entry(next).or_insert(true) = true;
                    loop_verts.push(next);
                    current = next;
                }
            }
        }
    }

    loops
}

// ---------------------------------------------------------------------------
// Planar area estimate for a polygon loop
// ---------------------------------------------------------------------------

fn estimate_polygon_area(loop_verts: &[u32], positions: &[[f32; 3]]) -> f32 {
    let n = loop_verts.len();
    if n < 3 {
        return 0.0;
    }
    // Compute centroid
    let mut c = [0.0f32; 3];
    let mut count = 0usize;
    for &vi in loop_verts {
        let idx = vi as usize;
        if idx < positions.len() {
            c = add3(c, positions[idx]);
            count += 1;
        }
    }
    if count == 0 {
        return 0.0;
    }
    let inv = 1.0 / count as f32;
    c = scale3(c, inv);

    // Sum triangle areas from centroid
    let mut area = 0.0f32;
    for i in 0..n {
        let a = loop_verts[i] as usize;
        let b = loop_verts[(i + 1) % n] as usize;
        if a >= positions.len() || b >= positions.len() {
            continue;
        }
        let ab = sub3(positions[b], positions[a]);
        let ac = sub3(c, positions[a]);
        let cr = cross3(ab, ac);
        area += len3(cr) * 0.5;
    }
    area
}

// ---------------------------------------------------------------------------
// Public API: find holes
// ---------------------------------------------------------------------------

/// Find all holes (open boundary loops) in a mesh.
pub fn find_holes(mesh: &MeshBuffers) -> Vec<MeshHole> {
    let directed = directed_boundary_edges(mesh);
    let loops = walk_boundary_loops(&directed);

    loops
        .into_iter()
        .filter(|lp| lp.len() >= 3)
        .map(|loop_vertices| {
            let area_estimate = estimate_polygon_area(&loop_vertices, &mesh.positions);
            MeshHole {
                loop_vertices,
                area_estimate,
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Public API: is_watertight / hole_count
// ---------------------------------------------------------------------------

/// Check mesh for any open boundary edges; returns true if there are none.
pub fn is_watertight(mesh: &MeshBuffers) -> bool {
    let edge_map = build_boundary_edge_map(mesh);
    edge_map.values().all(|&count| count == 2)
}

/// Count the number of distinct hole loops in the mesh.
pub fn hole_count(mesh: &MeshBuffers) -> usize {
    find_holes(mesh).len()
}

// ---------------------------------------------------------------------------
// Public API: fan_patch
// ---------------------------------------------------------------------------

/// Fan patch: add centroid vertex, fan triangles from it to boundary.
///
/// Returns `(centroid_position, triangles)` where triangles are indices into
/// a vertex array formed by appending the centroid after `positions`.
pub fn fan_patch(positions: &[[f32; 3]], loop_verts: &[u32]) -> ([f32; 3], Vec<u32>) {
    let n = loop_verts.len();
    if n < 3 {
        return ([0.0, 0.0, 0.0], Vec::new());
    }

    // Compute centroid
    let mut c = [0.0f32; 3];
    let mut count = 0usize;
    for &vi in loop_verts {
        let idx = vi as usize;
        if idx < positions.len() {
            c = add3(c, positions[idx]);
            count += 1;
        }
    }
    if count > 0 {
        let inv = 1.0 / count as f32;
        c = scale3(c, inv);
    }

    // Centroid index = positions.len() (will be appended to the combined array)
    let centroid_idx = positions.len() as u32;
    let mut triangles = Vec::with_capacity(n * 3);

    for i in 0..n {
        let a = loop_verts[i];
        let b = loop_verts[(i + 1) % n];
        // Fan from centroid: centroid, a, b  (CCW when boundary is CCW)
        triangles.push(centroid_idx);
        triangles.push(a);
        triangles.push(b);
    }

    (c, triangles)
}

// ---------------------------------------------------------------------------
// Public API: project_polygon_2d
// ---------------------------------------------------------------------------

/// Project 3D polygon vertices onto a best-fit 2D plane defined by `normal`.
///
/// Builds a local (u, v) coordinate frame perpendicular to `normal` and
/// returns the 2-D projections in that frame.
pub fn project_polygon_2d(pts: &[[f32; 3]], normal: &[f32; 3]) -> Vec<[f32; 2]> {
    if pts.is_empty() {
        return Vec::new();
    }

    let n = normalize3(*normal);

    // Build tangent u: pick an arbitrary vector not parallel to n
    let arbitrary = if n[0].abs() < 0.9 {
        [1.0f32, 0.0, 0.0]
    } else {
        [0.0f32, 1.0, 0.0]
    };
    let u = normalize3(cross3(n, arbitrary));
    let v = cross3(n, u);

    // Project centroid to origin
    let centroid = {
        let mut s = [0.0f32; 3];
        for p in pts {
            s = add3(s, *p);
        }
        scale3(s, 1.0 / pts.len() as f32)
    };

    pts.iter()
        .map(|&p| {
            let d = sub3(p, centroid);
            [dot3(d, u), dot3(d, v)]
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Public API: polygon_signed_area_2d
// ---------------------------------------------------------------------------

/// Compute the signed area of a 2D polygon using the shoelace formula.
/// Positive area means CCW winding.
pub fn polygon_signed_area_2d(pts: &[[f32; 2]]) -> f32 {
    let n = pts.len();
    if n < 3 {
        return 0.0;
    }
    let mut area = 0.0f32;
    for i in 0..n {
        let j = (i + 1) % n;
        area += pts[i][0] * pts[j][1];
        area -= pts[j][0] * pts[i][1];
    }
    area * 0.5
}

// ---------------------------------------------------------------------------
// Public API: is_ear
// ---------------------------------------------------------------------------

/// Check if vertex `i` in `loop_verts` forms an "ear".
///
/// An ear is a triangle (prev, i, next) that is:
/// - locally convex (correct winding), and
/// - contains no other loop vertices inside it.
pub fn is_ear(i: usize, loop_verts: &[u32], positions: &[[f32; 3]]) -> bool {
    let n = loop_verts.len();
    if n < 3 {
        return false;
    }

    let prev = loop_verts[(i + n - 1) % n] as usize;
    let curr = loop_verts[i] as usize;
    let next = loop_verts[(i + 1) % n] as usize;

    if prev >= positions.len() || curr >= positions.len() || next >= positions.len() {
        return false;
    }

    // Compute normal to determine overall polygon orientation
    let hole_normal = {
        let mut normal = [0.0f32; 3];
        for j in 0..n {
            let a = loop_verts[j] as usize;
            let b = loop_verts[(j + 1) % n] as usize;
            if a >= positions.len() || b >= positions.len() {
                continue;
            }
            let pa = positions[a];
            let pb = positions[b];
            normal[0] += (pa[1] - pb[1]) * (pa[2] + pb[2]);
            normal[1] += (pa[2] - pb[2]) * (pa[0] + pb[0]);
            normal[2] += (pa[0] - pb[0]) * (pa[1] + pb[1]);
        }
        normalize3(normal)
    };

    let pa = positions[prev];
    let pb = positions[curr];
    let pc = positions[next];

    // Check convexity: cross(pb-pa, pc-pa) should have same direction as hole_normal
    let ab = sub3(pb, pa);
    let ac = sub3(pc, pa);
    let cr = cross3(ab, ac);
    let convexity = dot3(cr, hole_normal);
    if convexity <= 0.0 {
        return false; // reflex vertex — not an ear
    }

    // Check that no other loop vertex is inside triangle (pa, pb, pc)
    for (j, &lv) in loop_verts.iter().enumerate() {
        if j == (i + n - 1) % n || j == i || j == (i + 1) % n {
            continue;
        }
        let vj = lv as usize;
        if vj >= positions.len() {
            continue;
        }
        let p = positions[vj];
        if point_in_triangle_3d(p, pa, pb, pc, hole_normal) {
            return false;
        }
    }

    true
}

/// Test if point `p` is inside triangle (a, b, c) using barycentric coords projected to 2D.
fn point_in_triangle_3d(
    p: [f32; 3],
    a: [f32; 3],
    b: [f32; 3],
    c: [f32; 3],
    normal: [f32; 3],
) -> bool {
    // Use sign of cross products in the plane defined by `normal`
    let ab = sub3(b, a);
    let bc = sub3(c, b);
    let ca = sub3(a, c);

    let ap = sub3(p, a);
    let bp = sub3(p, b);
    let cp = sub3(p, c);

    let d1 = dot3(cross3(ab, ap), normal);
    let d2 = dot3(cross3(bc, bp), normal);
    let d3 = dot3(cross3(ca, cp), normal);

    let has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0);
    let has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0);

    !(has_neg && has_pos)
}

// ---------------------------------------------------------------------------
// Public API: ear_clip
// ---------------------------------------------------------------------------

/// Ear-clipping triangulation of a boundary loop.
///
/// Returns list of triangle index triples (global vertex indices from `loop_verts`).
pub fn ear_clip(loop_verts: &[u32], positions: &[[f32; 3]]) -> Vec<[u32; 3]> {
    let n = loop_verts.len();
    if n < 3 {
        return Vec::new();
    }
    if n == 3 {
        return vec![[loop_verts[0], loop_verts[1], loop_verts[2]]];
    }

    let mut remaining: Vec<u32> = loop_verts.to_vec();
    let mut triangles = Vec::with_capacity(n - 2);

    let max_iter = n * n + n;
    let mut iter_count = 0usize;

    while remaining.len() > 3 {
        iter_count += 1;
        if iter_count > max_iter {
            break; // degenerate polygon; avoid infinite loop
        }

        let len = remaining.len();
        let mut ear_found = false;

        for i in 0..len {
            if is_ear(i, &remaining, positions) {
                let prev = remaining[(i + len - 1) % len];
                let curr = remaining[i];
                let next = remaining[(i + 1) % len];
                triangles.push([prev, curr, next]);
                remaining.remove(i);
                ear_found = true;
                break;
            }
        }

        if !ear_found {
            // Fallback: just clip the first vertex (handles degenerate cases)
            let len = remaining.len();
            let prev = remaining[len - 1];
            let curr = remaining[0];
            let next = remaining[1];
            triangles.push([prev, curr, next]);
            remaining.remove(0);
        }
    }

    // Last triangle
    if remaining.len() == 3 {
        triangles.push([remaining[0], remaining[1], remaining[2]]);
    }

    triangles
}

// ---------------------------------------------------------------------------
// MinArea greedy triangulation
// ---------------------------------------------------------------------------

/// Greedy minimum-area triangulation of a polygon loop.
fn min_area_triangulate(loop_verts: &[u32], positions: &[[f32; 3]]) -> Vec<[u32; 3]> {
    let n = loop_verts.len();
    if n < 3 {
        return Vec::new();
    }
    if n == 3 {
        return vec![[loop_verts[0], loop_verts[1], loop_verts[2]]];
    }

    let mut remaining: Vec<u32> = loop_verts.to_vec();
    let mut triangles = Vec::with_capacity(n - 2);

    while remaining.len() > 3 {
        let len = remaining.len();
        let mut best_idx = 0;
        let mut best_area = f32::MAX;

        for i in 0..len {
            let prev = remaining[(i + len - 1) % len] as usize;
            let curr = remaining[i] as usize;
            let next = remaining[(i + 1) % len] as usize;

            if prev >= positions.len() || curr >= positions.len() || next >= positions.len() {
                continue;
            }

            let ab = sub3(positions[curr], positions[prev]);
            let ac = sub3(positions[next], positions[prev]);
            let cr = cross3(ab, ac);
            let area = len3(cr) * 0.5;

            if area < best_area {
                best_area = area;
                best_idx = i;
            }
        }

        let len = remaining.len();
        let prev = remaining[(best_idx + len - 1) % len];
        let curr = remaining[best_idx];
        let next = remaining[(best_idx + 1) % len];
        triangles.push([prev, curr, next]);
        remaining.remove(best_idx);
    }

    if remaining.len() == 3 {
        triangles.push([remaining[0], remaining[1], remaining[2]]);
    }

    triangles
}

// ---------------------------------------------------------------------------
// Public API: fill_hole
// ---------------------------------------------------------------------------

/// Fill a specific hole using the given strategy.
///
/// Returns `(new_positions, new_triangle_indices)` where triangle indices are
/// into the combined vertex array (original positions + new_positions appended).
pub fn fill_hole(
    mesh: &MeshBuffers,
    hole: &MeshHole,
    strategy: PatchStrategy,
) -> (Vec<[f32; 3]>, Vec<u32>) {
    let loop_verts = &hole.loop_vertices;
    let positions = &mesh.positions;

    if loop_verts.len() < 3 {
        return (Vec::new(), Vec::new());
    }

    match strategy {
        PatchStrategy::Fan => {
            let (centroid, tris) = fan_patch(positions, loop_verts);
            (vec![centroid], tris)
        }
        PatchStrategy::EarClip => {
            let tri_verts = ear_clip(loop_verts, positions);
            let mut flat = Vec::with_capacity(tri_verts.len() * 3);
            for t in tri_verts {
                flat.push(t[0]);
                flat.push(t[1]);
                flat.push(t[2]);
            }
            (Vec::new(), flat)
        }
        PatchStrategy::MinArea => {
            let tri_verts = min_area_triangulate(loop_verts, positions);
            let mut flat = Vec::with_capacity(tri_verts.len() * 3);
            for t in tri_verts {
                flat.push(t[0]);
                flat.push(t[1]);
                flat.push(t[2]);
            }
            (Vec::new(), flat)
        }
    }
}

// ---------------------------------------------------------------------------
// Public API: fill_holes
// ---------------------------------------------------------------------------

/// Fill all holes in a mesh using the given strategy.
pub fn fill_holes(mesh: &MeshBuffers, strategy: PatchStrategy) -> PatchResult {
    let holes = find_holes(mesh);
    let holes_filled = holes.len();

    if holes_filled == 0 {
        return PatchResult {
            mesh: mesh.clone(),
            holes_filled: 0,
            new_vertices: 0,
            new_triangles: 0,
        };
    }

    // Build combined mesh
    let mut new_positions = mesh.positions.clone();
    let mut new_normals = mesh.normals.clone();
    let mut new_tangents = mesh.tangents.clone();
    let mut new_uvs = mesh.uvs.clone();
    let mut new_indices = mesh.indices.clone();

    let mut total_new_verts = 0usize;
    let mut total_new_tris = 0usize;

    for hole in &holes {
        // Temporarily build a helper mesh with current positions for fill_hole calls
        let helper_mesh = MeshBuffers {
            positions: new_positions.clone(),
            normals: new_normals.clone(),
            tangents: new_tangents.clone(),
            uvs: new_uvs.clone(),
            indices: new_indices.clone(),
            colors: None,
            has_suit: mesh.has_suit,
        };

        let (patch_positions, patch_indices) = fill_hole(&helper_mesh, hole, strategy.clone());

        let patch_tri_count = patch_indices.len() / 3;

        // Append new vertices
        for &pos in &patch_positions {
            new_positions.push(pos);
            // Estimate normal from hole
            let normal = hole.normal_estimate(&helper_mesh.positions);
            new_normals.push(normal);
            new_tangents.push([1.0, 0.0, 0.0, 1.0]);
            new_uvs.push([0.0, 0.0]);
        }

        // Append new triangle indices
        new_indices.extend_from_slice(&patch_indices);

        total_new_verts += patch_positions.len();
        total_new_tris += patch_tri_count;
    }

    let result_mesh = MeshBuffers {
        positions: new_positions,
        normals: new_normals,
        tangents: new_tangents,
        uvs: new_uvs,
        indices: new_indices,
        colors: mesh.colors.clone(),
        has_suit: mesh.has_suit,
    };

    PatchResult {
        mesh: result_mesh,
        holes_filled,
        new_vertices: total_new_verts,
        new_triangles: total_new_tris,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use oxihuman_morph::engine::MeshBuffers as MB;

    fn make_mesh(positions: Vec<[f32; 3]>, indices: Vec<u32>) -> MeshBuffers {
        let n = positions.len();
        MeshBuffers::from_morph(MB {
            positions,
            normals: vec![[0.0, 0.0, 1.0]; n],
            uvs: vec![[0.0, 0.0]; n],
            indices,
            has_suit: false,
        })
    }

    /// Build a flat disc mesh with a hole: outer ring of 8 verts,
    /// each consecutive pair connected to center (but center removed),
    /// leaving a boundary loop.
    fn open_disc_mesh() -> MeshBuffers {
        // 8-sided ring mesh: verts 0-7 on a unit circle, no center vertex.
        // Each triangle: (i, (i+1)%8, (i+2)%8) creates a ring with one open boundary.
        // Simpler: two triangles forming a quad strip around a square, with top open.
        //
        // Actually: make a simple mesh with an obvious hole.
        // Mesh: a "C" shape — two triangles with a shared edge, leaving 4 boundary verts.
        //
        // Vertices:
        //   0: (0,0,0)  1: (1,0,0)  2: (2,0,0)
        //   3: (0,1,0)  4: (1,1,0)  5: (2,1,0)
        //   6: (0,2,0)  7: (1,2,0)  8: (2,2,0)
        //
        // Top face: 6-7-8 (closed region). Bottom face: 0-1-2 (closed region).
        // Side faces: left (0-3-6, 0-6-3), right (2-5-8, ...)
        // but leave out the top-open boundary.
        //
        // Simpler: a flat plane with the center quad removed.
        // Let's use a donut-like: outer 4 verts, inner 4 verts (hole).
        //   Outer: 0=(0,0,0), 1=(2,0,0), 2=(2,2,0), 3=(0,2,0)
        //   Inner: 4=(0.5,0.5,0), 5=(1.5,0.5,0), 6=(1.5,1.5,0), 7=(0.5,1.5,0)
        // Triangulate the ring between outer and inner.
        let positions = vec![
            [0.0, 0.0, 0.0], // 0
            [2.0, 0.0, 0.0], // 1
            [2.0, 2.0, 0.0], // 2
            [0.0, 2.0, 0.0], // 3
            [0.5, 0.5, 0.0], // 4
            [1.5, 0.5, 0.0], // 5
            [1.5, 1.5, 0.0], // 6
            [0.5, 1.5, 0.0], // 7
        ];
        // Ring triangles (outer -> inner)
        let indices = vec![
            0, 1, 5, 0, 5, 4, // bottom side
            1, 2, 6, 1, 6, 5, // right side
            2, 3, 7, 2, 7, 6, // top side
            3, 0, 4, 3, 4, 7, // left side
        ];
        make_mesh(positions, indices)
    }

    /// A simple closed tetrahedron (no holes).
    fn closed_tetrahedron() -> MeshBuffers {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.5, 1.0, 0.0],
            [0.5, 0.333, 1.0],
        ];
        let indices = vec![
            0, 1, 2, // base
            0, 1, 3, // front
            1, 2, 3, // right
            2, 0, 3, // left
        ];
        make_mesh(positions, indices)
    }

    /// A mesh with two holes: bottom and top openings of a cylinder-like shape.
    fn cylinder_open_both_ends() -> MeshBuffers {
        // 4-sided prism, no caps
        let positions = vec![
            [0.0, 0.0, 0.0], // 0: bottom ring
            [1.0, 0.0, 0.0], // 1
            [1.0, 1.0, 0.0], // 2
            [0.0, 1.0, 0.0], // 3
            [0.0, 0.0, 1.0], // 4: top ring
            [1.0, 0.0, 1.0], // 5
            [1.0, 1.0, 1.0], // 6
            [0.0, 1.0, 1.0], // 7
        ];
        // Side faces only (CCW from outside)
        let indices = vec![
            0, 1, 5, 0, 5, 4, // front
            1, 2, 6, 1, 6, 5, // right
            2, 3, 7, 2, 7, 6, // back
            3, 0, 4, 3, 4, 7, // left
        ];
        make_mesh(positions, indices)
    }

    // -----------------------------------------------------------------------
    // Test 1: closed mesh is watertight
    // -----------------------------------------------------------------------
    #[test]
    fn test_closed_mesh_is_watertight() {
        let mesh = closed_tetrahedron();
        assert!(is_watertight(&mesh), "tetrahedron should be watertight");
    }

    // -----------------------------------------------------------------------
    // Test 2: open mesh is not watertight
    // -----------------------------------------------------------------------
    #[test]
    fn test_open_mesh_not_watertight() {
        let mesh = open_disc_mesh();
        assert!(!is_watertight(&mesh), "ring mesh should not be watertight");
    }

    // -----------------------------------------------------------------------
    // Test 3: find_holes returns correct count for donut mesh
    // -----------------------------------------------------------------------
    #[test]
    fn test_find_holes_donut_count() {
        let mesh = open_disc_mesh();
        let holes = find_holes(&mesh);
        // The ring mesh has two boundary loops: outer (0,1,2,3) and inner (4,5,6,7)
        assert!(
            !holes.is_empty(),
            "donut mesh should have at least one hole"
        );
    }

    // -----------------------------------------------------------------------
    // Test 4: find_holes returns 0 for closed mesh
    // -----------------------------------------------------------------------
    #[test]
    fn test_find_holes_closed_mesh_zero() {
        let mesh = closed_tetrahedron();
        let holes = find_holes(&mesh);
        assert_eq!(holes.len(), 0, "closed mesh should have no holes");
    }

    // -----------------------------------------------------------------------
    // Test 5: hole_count matches find_holes
    // -----------------------------------------------------------------------
    #[test]
    fn test_hole_count_matches_find_holes() {
        let mesh = open_disc_mesh();
        assert_eq!(hole_count(&mesh), find_holes(&mesh).len());
    }

    // -----------------------------------------------------------------------
    // Test 6: MeshHole vertex_count
    // -----------------------------------------------------------------------
    #[test]
    fn test_mesh_hole_vertex_count() {
        let hole = MeshHole {
            loop_vertices: vec![0, 1, 2, 3],
            area_estimate: 1.0,
        };
        assert_eq!(hole.vertex_count(), 4);
    }

    // -----------------------------------------------------------------------
    // Test 7: MeshHole perimeter
    // -----------------------------------------------------------------------
    #[test]
    fn test_mesh_hole_perimeter() {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let hole = MeshHole {
            loop_vertices: vec![0, 1, 2, 3],
            area_estimate: 1.0,
        };
        let p = hole.perimeter(&positions);
        assert!(
            (p - 4.0).abs() < 1e-5,
            "square perimeter should be 4.0, got {p}"
        );
    }

    // -----------------------------------------------------------------------
    // Test 8: MeshHole centroid
    // -----------------------------------------------------------------------
    #[test]
    fn test_mesh_hole_centroid() {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [2.0, 0.0, 0.0],
            [2.0, 2.0, 0.0],
            [0.0, 2.0, 0.0],
        ];
        let hole = MeshHole {
            loop_vertices: vec![0, 1, 2, 3],
            area_estimate: 4.0,
        };
        let c = hole.centroid(&positions);
        assert!((c[0] - 1.0).abs() < 1e-5);
        assert!((c[1] - 1.0).abs() < 1e-5);
    }

    // -----------------------------------------------------------------------
    // Test 9: fan_patch generates correct triangle count
    // -----------------------------------------------------------------------
    #[test]
    fn test_fan_patch_triangle_count() {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let loop_verts = vec![0u32, 1, 2, 3];
        let (centroid, tris) = fan_patch(&positions, &loop_verts);
        // 4 boundary verts => 4 triangles => 12 indices
        assert_eq!(
            tris.len(),
            12,
            "fan_patch should produce 4 triangles for 4-vert loop"
        );
        // Centroid should be at (0.5, 0.5, 0.0)
        assert!((centroid[0] - 0.5).abs() < 1e-5);
        assert!((centroid[1] - 0.5).abs() < 1e-5);
    }

    // -----------------------------------------------------------------------
    // Test 10: ear_clip produces correct triangle count
    // -----------------------------------------------------------------------
    #[test]
    fn test_ear_clip_triangle_count() {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [3.0, 0.0, 0.0],
            [3.0, 2.0, 0.0],
            [0.0, 2.0, 0.0],
        ];
        let loop_verts = vec![0u32, 1, 2, 3];
        let tris = ear_clip(&loop_verts, &positions);
        // A quadrilateral should produce 2 triangles
        assert_eq!(tris.len(), 2, "ear_clip should produce 2 tris for quad");
    }

    // -----------------------------------------------------------------------
    // Test 11: polygon_signed_area_2d square
    // -----------------------------------------------------------------------
    #[test]
    fn test_polygon_signed_area_2d_square() {
        let pts = vec![[0.0f32, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
        let area = polygon_signed_area_2d(&pts);
        assert!(
            (area - 0.5).abs() < 1e-5 || (area - 1.0).abs() < 1e-5,
            "CCW unit square signed area should be 0.5 (shoelace) or 1.0, got {area}"
        );
        // The shoelace formula for unit square CCW gives +0.5 * 2 = 1.0
        // Let me check: (0,0)->(1,0)->(1,1)->(0,1)
        // = (0*0 - 1*0) + (1*1 - 1*0) + (1*1 - 0*1) + (0*0 - 0*1)
        // Shoelace: sum(x_i * y_{i+1} - x_{i+1} * y_i) / 2
        // = (0*0-1*0) + (1*1-1*0) + (1*1-0*1) + (0*0-0*1) = 0 + 1 + 1 + 0 = 2 => area = 1.0
        assert!(
            (area.abs() - 1.0).abs() < 1e-5,
            "unit square area magnitude should be 1.0, got {area}"
        );
    }

    // -----------------------------------------------------------------------
    // Test 12: fill_holes Fan strategy increases triangle count
    // -----------------------------------------------------------------------
    #[test]
    fn test_fill_holes_fan_increases_triangles() {
        let mesh = cylinder_open_both_ends();
        let orig_tri_count = mesh.face_count();
        let result = fill_holes(&mesh, PatchStrategy::Fan);
        assert!(
            result.holes_filled > 0,
            "cylinder should have fillable holes"
        );
        assert!(
            result.mesh.face_count() > orig_tri_count,
            "filled mesh should have more triangles"
        );
        assert!(result.new_triangles > 0);
    }

    // -----------------------------------------------------------------------
    // Test 13: fill_holes EarClip strategy
    // -----------------------------------------------------------------------
    #[test]
    fn test_fill_holes_earclip() {
        let mesh = cylinder_open_both_ends();
        let result = fill_holes(&mesh, PatchStrategy::EarClip);
        assert!(result.holes_filled > 0, "EarClip should fill holes");
        assert!(result.new_triangles > 0, "EarClip should add triangles");
    }

    // -----------------------------------------------------------------------
    // Test 14: fill_holes MinArea strategy
    // -----------------------------------------------------------------------
    #[test]
    fn test_fill_holes_min_area() {
        let mesh = cylinder_open_both_ends();
        let result = fill_holes(&mesh, PatchStrategy::MinArea);
        assert!(result.holes_filled > 0, "MinArea should fill holes");
        assert!(result.new_triangles > 0, "MinArea should add triangles");
    }

    // -----------------------------------------------------------------------
    // Test 15: project_polygon_2d preserves relative distances
    // -----------------------------------------------------------------------
    #[test]
    fn test_project_polygon_2d_unit_square() {
        let pts = vec![
            [0.0f32, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let normal = [0.0f32, 0.0, 1.0];
        let proj = project_polygon_2d(&pts, &normal);
        assert_eq!(proj.len(), 4);
        // Check that projected distances match 3D distances
        let d3d = len3(sub3(pts[1], pts[0]));
        let d2d = len2([proj[1][0] - proj[0][0], proj[1][1] - proj[0][1]]);
        assert!(
            (d3d - d2d).abs() < 1e-5,
            "projected distance should match 3D: {d3d} vs {d2d}"
        );
    }

    // -----------------------------------------------------------------------
    // Test 16: is_ear detects ear on convex quad
    // -----------------------------------------------------------------------
    #[test]
    fn test_is_ear_convex_quad() {
        // A flat CCW square in XY plane
        let positions = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let loop_verts = vec![0u32, 1, 2, 3];
        // All vertices of a convex polygon should be ears
        let ears: Vec<bool> = (0..4).map(|i| is_ear(i, &loop_verts, &positions)).collect();
        assert!(
            ears.iter().any(|&e| e),
            "convex quad should have at least one ear, got: {ears:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Test 17: normal_estimate for flat horizontal loop points up
    // -----------------------------------------------------------------------
    #[test]
    fn test_normal_estimate_flat_loop() {
        let positions = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let hole = MeshHole {
            loop_vertices: vec![0, 1, 2, 3],
            area_estimate: 1.0,
        };
        let n = hole.normal_estimate(&positions);
        // Should point in +Z or -Z direction
        assert!(
            n[2].abs() > 0.9,
            "flat loop normal should point in Z, got {n:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Test 18: write results to /tmp/ for inspection
    // -----------------------------------------------------------------------
    #[test]
    fn test_write_results_to_tmp() {
        let mesh = cylinder_open_both_ends();
        let result = fill_holes(&mesh, PatchStrategy::Fan);

        let report = format!(
            "holes_filled: {}\nnew_vertices: {}\nnew_triangles: {}\ntotal_verts: {}\ntotal_tris: {}\n",
            result.holes_filled,
            result.new_vertices,
            result.new_triangles,
            result.mesh.vertex_count(),
            result.mesh.face_count(),
        );

        let tmp = std::env::temp_dir().join("mesh_patch_report.txt");
        std::fs::write(&tmp, &report).ok();

        // Verify the file was written
        let content = std::fs::read_to_string(&tmp).expect("should succeed");
        assert!(content.contains("holes_filled"));
    }
}