oxiphysics-geometry 0.1.0

Geometric shape types for the OxiPhysics engine
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
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Triangle mesh shape with adjacency queries, vertex normals, Laplacian,
//! geodesic distance, Loop subdivision, and watertight checks.

use crate::shape::{RayHit, Shape};
use oxiphysics_core::Aabb;
use oxiphysics_core::math::{Mat3, Real, Vec3};
use std::collections::{BinaryHeap, HashMap, HashSet};

/// A triangle mesh defined by vertices and index triples.
#[derive(Debug, Clone)]
pub struct TriangleMesh {
    /// Vertex positions.
    pub vertices: Vec<Vec3>,
    /// Triangle indices (groups of three).
    pub indices: Vec<[usize; 3]>,
}

impl TriangleMesh {
    /// Create a new triangle mesh.
    pub fn new(vertices: Vec<Vec3>, indices: Vec<[usize; 3]>) -> Self {
        Self { vertices, indices }
    }

    /// Surface area: sum of triangle areas.
    #[allow(dead_code)]
    pub fn surface_area(&self) -> Real {
        let mut total = 0.0;
        for tri in &self.indices {
            let a = self.vertices[tri[0]];
            let b = self.vertices[tri[1]];
            let c = self.vertices[tri[2]];
            let ab = b - a;
            let ac = c - a;
            total += ab.cross(&ac).norm() * 0.5;
        }
        total
    }

    /// Volume via signed tetrahedral decomposition (divergence theorem).
    #[allow(dead_code)]
    pub fn volume_explicit(&self) -> Real {
        let mut vol = 0.0;
        for tri in &self.indices {
            let a = self.vertices[tri[0]];
            let b = self.vertices[tri[1]];
            let c = self.vertices[tri[2]];
            vol += a.dot(&b.cross(&c)) / 6.0;
        }
        vol.abs()
    }

    /// Center of mass via weighted tetrahedral centroids.
    #[allow(dead_code)]
    pub fn center_of_mass_explicit(&self) -> [f64; 3] {
        if self.indices.is_empty() {
            return [0.0, 0.0, 0.0];
        }
        let mut weighted = Vec3::zeros();
        let mut total_vol = 0.0;
        for tri in &self.indices {
            let a = self.vertices[tri[0]];
            let b = self.vertices[tri[1]];
            let c = self.vertices[tri[2]];
            let tet_vol = a.dot(&b.cross(&c)) / 6.0;
            let tet_center = (a + b + c) * 0.25;
            weighted += tet_center * tet_vol;
            total_vol += tet_vol;
        }
        if total_vol.abs() > 1e-12 {
            let com = weighted / total_vol;
            [com.x, com.y, com.z]
        } else {
            [0.0, 0.0, 0.0]
        }
    }

    /// Compute per-face normals (unit vectors).
    #[allow(dead_code)]
    pub fn compute_normals(&self) -> Vec<[f64; 3]> {
        self.indices
            .iter()
            .map(|tri| {
                let a = self.vertices[tri[0]];
                let b = self.vertices[tri[1]];
                let c = self.vertices[tri[2]];
                let ab = b - a;
                let ac = c - a;
                let n = ab.cross(&ac);
                let len = n.norm();
                if len < 1e-12 {
                    [0.0, 1.0, 0.0]
                } else {
                    [n.x / len, n.y / len, n.z / len]
                }
            })
            .collect()
    }

    /// Ray cast returning (t, face_index, normal) via Moller-Trumbore for all triangles.
    #[allow(dead_code)]
    pub fn ray_cast_full(
        &self,
        origin: [f64; 3],
        direction: [f64; 3],
        max_toi: f64,
    ) -> Option<(f64, usize, [f64; 3])> {
        let o = Vec3::new(origin[0], origin[1], origin[2]);
        let d = Vec3::new(direction[0], direction[1], direction[2]);
        let mut best: Option<(f64, usize, [f64; 3])> = None;

        for (face_idx, tri) in self.indices.iter().enumerate() {
            let v0 = self.vertices[tri[0]];
            let v1 = self.vertices[tri[1]];
            let v2 = self.vertices[tri[2]];

            if let Some(hit) = ray_triangle(&o, &d, &v0, &v1, &v2)
                && hit.toi >= 0.0
                && hit.toi <= max_toi
                && best.as_ref().is_none_or(|b| hit.toi < b.0)
            {
                let n = [hit.normal.x, hit.normal.y, hit.normal.z];
                best = Some((hit.toi, face_idx, n));
            }
        }
        best
    }

    /// Check whether the mesh is watertight (every edge is shared by exactly two triangles).
    #[allow(dead_code)]
    pub fn is_watertight(&self) -> bool {
        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();

        for tri in &self.indices {
            let verts = [tri[0], tri[1], tri[2]];
            for i in 0..3 {
                let a = verts[i];
                let b = verts[(i + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                *edge_count.entry(key).or_insert(0) += 1;
            }
        }

        edge_count.values().all(|&count| count == 2)
    }

    // ------------------------------------------------------------------
    // New: mesh adjacency queries
    // ------------------------------------------------------------------

    /// Build vertex-to-face adjacency: for each vertex index, the list of
    /// triangle indices that reference it.
    #[allow(dead_code)]
    pub fn vertex_to_faces(&self) -> Vec<Vec<usize>> {
        let mut v2f = vec![Vec::new(); self.vertices.len()];
        for (fi, tri) in self.indices.iter().enumerate() {
            for &vi in tri {
                v2f[vi].push(fi);
            }
        }
        v2f
    }

    /// Build face-to-face adjacency via shared edges.
    /// Returns a vec of the same length as `self.indices`. Each entry contains
    /// the indices of adjacent faces (those sharing at least one edge).
    #[allow(dead_code)]
    pub fn face_adjacency(&self) -> Vec<Vec<usize>> {
        // edge -> list of face indices
        let mut edge_faces: HashMap<(usize, usize), Vec<usize>> = HashMap::new();
        for (fi, tri) in self.indices.iter().enumerate() {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                edge_faces.entry(key).or_default().push(fi);
            }
        }
        let mut adj = vec![Vec::new(); self.indices.len()];
        for faces in edge_faces.values() {
            for i in 0..faces.len() {
                for j in (i + 1)..faces.len() {
                    adj[faces[i]].push(faces[j]);
                    adj[faces[j]].push(faces[i]);
                }
            }
        }
        // Deduplicate
        for v in &mut adj {
            v.sort_unstable();
            v.dedup();
        }
        adj
    }

    /// Return the set of unique edges as sorted (min,max) vertex index pairs.
    #[allow(dead_code)]
    pub fn unique_edges(&self) -> Vec<(usize, usize)> {
        let mut set: HashSet<(usize, usize)> = HashSet::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                set.insert(key);
            }
        }
        let mut edges: Vec<(usize, usize)> = set.into_iter().collect();
        edges.sort_unstable();
        edges
    }

    /// Build vertex-to-vertex adjacency (1-ring neighbours).
    #[allow(dead_code)]
    pub fn vertex_neighbors(&self) -> Vec<Vec<usize>> {
        let mut nbrs = vec![HashSet::<usize>::new(); self.vertices.len()];
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                nbrs[a].insert(b);
                nbrs[b].insert(a);
            }
        }
        nbrs.into_iter()
            .map(|s| {
                let mut v: Vec<usize> = s.into_iter().collect();
                v.sort_unstable();
                v
            })
            .collect()
    }

    // ------------------------------------------------------------------
    // New: edge collapse
    // ------------------------------------------------------------------

    /// Collapse an edge between vertices `v0` and `v1`, merging them to their
    /// midpoint. Triangles that degenerate (both endpoints are `v0`/`v1`) are
    /// removed.
    ///
    /// Returns `true` if the edge was found and collapsed.
    #[allow(dead_code)]
    pub fn edge_collapse(&mut self, v0: usize, v1: usize) -> bool {
        // Verify edge exists
        let edge_found = self
            .indices
            .iter()
            .any(|tri| tri.contains(&v0) && tri.contains(&v1));
        if !edge_found {
            return false;
        }

        // Midpoint
        let mid = (self.vertices[v0] + self.vertices[v1]) * 0.5;
        self.vertices[v0] = mid;

        // Replace all references to v1 with v0
        for tri in &mut self.indices {
            for idx in tri.iter_mut() {
                if *idx == v1 {
                    *idx = v0;
                }
            }
        }

        // Remove degenerate triangles (two or more identical indices)
        self.indices
            .retain(|tri| tri[0] != tri[1] && tri[1] != tri[2] && tri[0] != tri[2]);

        true
    }

    // ------------------------------------------------------------------
    // New: vertex normal computation (angle-weighted)
    // ------------------------------------------------------------------

    /// Compute per-vertex normals by accumulating face normals weighted by the
    /// interior angle at each vertex.
    #[allow(dead_code)]
    pub fn compute_vertex_normals(&self) -> Vec<[f64; 3]> {
        let mut normals = vec![Vec3::zeros(); self.vertices.len()];
        for tri in &self.indices {
            let v0 = self.vertices[tri[0]];
            let v1 = self.vertices[tri[1]];
            let v2 = self.vertices[tri[2]];
            let e01 = v1 - v0;
            let e02 = v2 - v0;
            let face_n = e01.cross(&e02);

            let angle_at = |va: Vec3, vb: Vec3, vc: Vec3| -> f64 {
                let ab = (vb - va).normalize();
                let ac = (vc - va).normalize();
                ab.dot(&ac).clamp(-1.0, 1.0).acos()
            };

            let a0 = angle_at(v0, v1, v2);
            let a1 = angle_at(v1, v2, v0);
            let a2 = angle_at(v2, v0, v1);

            normals[tri[0]] += face_n * a0;
            normals[tri[1]] += face_n * a1;
            normals[tri[2]] += face_n * a2;
        }
        normals
            .iter()
            .map(|n| {
                let len = n.norm();
                if len < 1e-12 {
                    [0.0, 1.0, 0.0]
                } else {
                    [n.x / len, n.y / len, n.z / len]
                }
            })
            .collect()
    }

    // ------------------------------------------------------------------
    // New: mesh Laplacian smoothing
    // ------------------------------------------------------------------

    /// Uniform Laplacian smoothing: move each vertex towards the average of
    /// its 1-ring neighbours by `factor` (0..1). Boundary vertices are not
    /// moved.
    #[allow(dead_code)]
    pub fn laplacian_smooth(&mut self, factor: f64, iterations: usize) {
        for _ in 0..iterations {
            let nbrs = self.vertex_neighbors();
            let mut new_pos = self.vertices.clone();
            for (vi, neighbours) in nbrs.iter().enumerate() {
                if neighbours.is_empty() {
                    continue;
                }
                let mut avg = Vec3::zeros();
                for &ni in neighbours {
                    avg += self.vertices[ni];
                }
                avg /= neighbours.len() as f64;
                let diff = avg - self.vertices[vi];
                new_pos[vi] = self.vertices[vi] + diff * factor;
            }
            self.vertices = new_pos;
        }
    }

    // ------------------------------------------------------------------
    // New: approximate geodesic distance (Dijkstra on mesh edges)
    // ------------------------------------------------------------------

    /// Compute approximate geodesic distances from `source` vertex to all
    /// other vertices using Dijkstra on edge lengths. Returns a vec of
    /// distances indexed by vertex, with `f64::INFINITY` for unreachable
    /// vertices.
    #[allow(dead_code)]
    pub fn geodesic_distance(&self, source: usize) -> Vec<f64> {
        let nbrs = self.vertex_neighbors();
        let n = self.vertices.len();
        let mut dist = vec![f64::INFINITY; n];
        dist[source] = 0.0;

        // Min-heap: (OrderedFloat(dist), vertex)
        let mut heap = BinaryHeap::new();
        heap.push(std::cmp::Reverse(OrdF64(0.0, source)));

        while let Some(std::cmp::Reverse(OrdF64(d, u))) = heap.pop() {
            if d > dist[u] {
                continue;
            }
            for &v in &nbrs[u] {
                let edge_len = (self.vertices[v] - self.vertices[u]).norm();
                let new_d = d + edge_len;
                if new_d < dist[v] {
                    dist[v] = new_d;
                    heap.push(std::cmp::Reverse(OrdF64(new_d, v)));
                }
            }
        }
        dist
    }

    // ------------------------------------------------------------------
    // New: Loop subdivision
    // ------------------------------------------------------------------

    /// Perform one iteration of Loop subdivision.
    ///
    /// Each triangle is split into four by inserting edge midpoints (for
    /// boundary edges) or Loop-weighted edge vertices (for interior edges).
    /// Existing vertices are repositioned using the Loop weighting scheme.
    #[allow(dead_code)]
    pub fn loop_subdivide(&mut self) {
        let n_verts = self.vertices.len();

        // Build edge -> face count for boundary detection
        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                *edge_count.entry(key).or_insert(0) += 1;
            }
        }

        // Edge midpoint map: canonical edge -> new vertex index
        let mut edge_verts: HashMap<(usize, usize), usize> = HashMap::new();
        let mut new_vertices = self.vertices.clone();

        // Build edge -> adjacent triangle vertex sets for Loop weights
        let mut edge_opposite: HashMap<(usize, usize), Vec<usize>> = HashMap::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let c = tri[(k + 2) % 3]; // opposite vertex
                let key = if a < b { (a, b) } else { (b, a) };
                edge_opposite.entry(key).or_default().push(c);
            }
        }

        let get_or_create_edge_vert = |a: usize,
                                       b: usize,
                                       edge_verts: &mut HashMap<(usize, usize), usize>,
                                       new_vertices: &mut Vec<Vec3>,
                                       edge_count: &HashMap<(usize, usize), usize>,
                                       edge_opposite: &HashMap<(usize, usize), Vec<usize>>,
                                       orig_verts: &[Vec3]|
         -> usize {
            let key = if a < b { (a, b) } else { (b, a) };
            if let Some(&idx) = edge_verts.get(&key) {
                return idx;
            }
            let idx = new_vertices.len();
            let count = edge_count.get(&key).copied().unwrap_or(1);
            let pos = if count == 2 {
                // Interior edge: 3/8*(a+b) + 1/8*(c+d)
                let opposites = edge_opposite.get(&key).expect("key must exist");
                if opposites.len() == 2 {
                    let c = orig_verts[opposites[0]];
                    let d = orig_verts[opposites[1]];
                    (orig_verts[a] + orig_verts[b]) * (3.0 / 8.0) + (c + d) * (1.0 / 8.0)
                } else {
                    (orig_verts[a] + orig_verts[b]) * 0.5
                }
            } else {
                // Boundary edge: simple midpoint
                (orig_verts[a] + orig_verts[b]) * 0.5
            };
            new_vertices.push(pos);
            edge_verts.insert(key, idx);
            idx
        };

        let orig_verts = self.vertices.clone();

        // Create edge vertices for each triangle
        let mut new_indices = Vec::with_capacity(self.indices.len() * 4);
        for tri in &self.indices {
            let v0 = tri[0];
            let v1 = tri[1];
            let v2 = tri[2];
            let m01 = get_or_create_edge_vert(
                v0,
                v1,
                &mut edge_verts,
                &mut new_vertices,
                &edge_count,
                &edge_opposite,
                &orig_verts,
            );
            let m12 = get_or_create_edge_vert(
                v1,
                v2,
                &mut edge_verts,
                &mut new_vertices,
                &edge_count,
                &edge_opposite,
                &orig_verts,
            );
            let m20 = get_or_create_edge_vert(
                v2,
                v0,
                &mut edge_verts,
                &mut new_vertices,
                &edge_count,
                &edge_opposite,
                &orig_verts,
            );
            // Four sub-triangles
            new_indices.push([v0, m01, m20]);
            new_indices.push([v1, m12, m01]);
            new_indices.push([v2, m20, m12]);
            new_indices.push([m01, m12, m20]);
        }

        // Reposition original vertices using Loop weights
        let nbrs = self.vertex_neighbors();
        // Determine boundary vertices
        let mut is_boundary = vec![false; n_verts];
        for (&(a, b), &cnt) in &edge_count {
            if cnt == 1 {
                is_boundary[a] = true;
                is_boundary[b] = true;
            }
        }
        for vi in 0..n_verts {
            if is_boundary[vi] {
                // Boundary: keep position (simple approach)
                // Could use 3/4 * v + 1/8 * (n1 + n2) for boundary
                continue;
            }
            let n = nbrs[vi].len();
            if n < 3 {
                continue;
            }
            let beta = if n == 3 {
                3.0 / 16.0
            } else {
                3.0 / (8.0 * n as f64)
            };
            let mut neighbour_sum = Vec3::zeros();
            for &ni in &nbrs[vi] {
                neighbour_sum += orig_verts[ni];
            }
            new_vertices[vi] = orig_verts[vi] * (1.0 - n as f64 * beta) + neighbour_sum * beta;
        }

        self.vertices = new_vertices;
        self.indices = new_indices;
    }

    // ------------------------------------------------------------------
    // New: boundary edges and Euler characteristic
    // ------------------------------------------------------------------

    /// Return boundary edges (edges shared by only one triangle).
    #[allow(dead_code)]
    pub fn boundary_edges(&self) -> Vec<(usize, usize)> {
        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                *edge_count.entry(key).or_insert(0) += 1;
            }
        }
        edge_count
            .into_iter()
            .filter(|&(_, c)| c == 1)
            .map(|(e, _)| e)
            .collect()
    }

    /// Compute the Euler characteristic: V - E + F.
    #[allow(dead_code)]
    pub fn euler_characteristic(&self) -> i64 {
        let v = self.vertices.len() as i64;
        let e = self.unique_edges().len() as i64;
        let f = self.indices.len() as i64;
        v - e + f
    }

    /// Count non-manifold edges (shared by more than 2 triangles).
    #[allow(dead_code)]
    pub fn non_manifold_edge_count(&self) -> usize {
        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                *edge_count.entry(key).or_insert(0) += 1;
            }
        }
        edge_count.values().filter(|&&c| c > 2).count()
    }

    // ------------------------------------------------------------------
    // Cotangent Laplacian
    // ------------------------------------------------------------------

    /// Compute the cotangent Laplacian weights for each directed edge (i → j).
    ///
    /// Returns a `HashMap<(usize, usize), f64>` mapping `(i, j)` to the
    /// symmetric cotangent weight `w_{ij} = (cot α + cot β) / 2`, where α
    /// and β are the angles opposite to edge (i,j) in the two incident faces.
    ///
    /// This is the standard discretisation used in geometry processing for
    /// Laplace-Beltrami operators on triangulated surfaces.
    #[allow(dead_code)]
    pub fn compute_laplacian_matrix(&self) -> HashMap<(usize, usize), f64> {
        let mut weights: HashMap<(usize, usize), f64> = HashMap::new();

        for tri in &self.indices {
            let [i0, i1, i2] = *tri;
            let p0 = self.vertices[i0];
            let p1 = self.vertices[i1];
            let p2 = self.vertices[i2];

            // Cotangent of the angle at each vertex:
            //   cot(angle at v0) is the angle between edges v0→v1 and v0→v2
            let cot_at = |a: Vec3, b: Vec3, c: Vec3| -> f64 {
                // angle at vertex a, between edges a→b and a→c
                let ab = b - a;
                let ac = c - a;
                let cos_t = ab.dot(&ac);
                let sin_t = ab.cross(&ac).norm();
                if sin_t.abs() < 1e-12 {
                    0.0
                } else {
                    cos_t / sin_t
                }
            };

            let cot0 = cot_at(p0, p1, p2); // angle at i0, opposite edge i1-i2
            let cot1 = cot_at(p1, p0, p2); // angle at i1, opposite edge i0-i2
            let cot2 = cot_at(p2, p0, p1); // angle at i2, opposite edge i0-i1

            // Edge (i1, i2): opposite angle at i0
            *weights.entry((i1, i2)).or_insert(0.0) += 0.5 * cot0;
            *weights.entry((i2, i1)).or_insert(0.0) += 0.5 * cot0;

            // Edge (i0, i2): opposite angle at i1
            *weights.entry((i0, i2)).or_insert(0.0) += 0.5 * cot1;
            *weights.entry((i2, i0)).or_insert(0.0) += 0.5 * cot1;

            // Edge (i0, i1): opposite angle at i2
            *weights.entry((i0, i1)).or_insert(0.0) += 0.5 * cot2;
            *weights.entry((i1, i0)).or_insert(0.0) += 0.5 * cot2;
        }

        weights
    }

    // ------------------------------------------------------------------
    // Heat Kernel Signature
    // ------------------------------------------------------------------

    /// Compute the Heat Kernel Signature (HKS) descriptor for each vertex
    /// at a set of time scales `t_values`.
    ///
    /// The HKS approximates the diagonal of the heat kernel K(x, x, t) using
    /// the cotangent Laplacian's (normalized) diagonal weights.  The full
    /// spectral decomposition is expensive, so this implementation uses a
    /// fast approximation: for each vertex i and time t, it computes
    ///
    ///   HKS(i, t) = exp(-t * D\[i\])
    ///
    /// where `D[i]` is the sum of cotangent weights incident on vertex i
    /// (the diagonal of the stiffness matrix, normalised by the vertex area).
    ///
    /// Returns a `Vec<Vec`f64`>` of shape `[n_vertices][t_values.len()]`.
    #[allow(dead_code)]
    pub fn compute_heat_kernel_signature(&self, t_values: &[f64]) -> Vec<Vec<f64>> {
        let n = self.vertices.len();
        if n == 0 || t_values.is_empty() {
            return vec![vec![0.0; t_values.len()]; n];
        }

        // Cotangent weights
        let weights = self.compute_laplacian_matrix();

        // Vertex areas (1/3 of the sum of incident triangle areas)
        let mut vertex_area = vec![0.0f64; n];
        for tri in &self.indices {
            let [i0, i1, i2] = *tri;
            let a = self.vertices[i0];
            let b = self.vertices[i1];
            let c = self.vertices[i2];
            let area = (b - a).cross(&(c - a)).norm() * 0.5;
            let a_third = area / 3.0;
            vertex_area[i0] += a_third;
            vertex_area[i1] += a_third;
            vertex_area[i2] += a_third;
        }

        // Row sum of cotangent weights (diagonal of the stiffness matrix)
        let mut diag = vec![0.0f64; n];
        for (&(i, _j), &w) in &weights {
            diag[i] += w;
        }

        // Effective frequency per vertex: lambda_i = diag[i] / area[i]
        let lambda: Vec<f64> = (0..n)
            .map(|i| {
                if vertex_area[i] > 1e-15 {
                    diag[i] / vertex_area[i]
                } else {
                    0.0
                }
            })
            .collect();

        // HKS(i, t) = exp(-t * lambda_i)
        (0..n)
            .map(|i| t_values.iter().map(|&t| (-t * lambda[i]).exp()).collect())
            .collect()
    }

    // ------------------------------------------------------------------
    // Laplacian smoothing (cotangent weights)
    // ------------------------------------------------------------------

    /// Iterative Laplacian smoothing using cotangent weights.
    ///
    /// Each iteration moves vertex `i` towards the weighted average of its
    /// neighbours:
    ///
    ///   v_i' = v_i + factor * Σ_j w_{ij} * (v_j - v_i) / Σ_j w_{ij}
    ///
    /// Boundary vertices (those on edges shared by only one triangle) are
    /// kept fixed.
    ///
    /// `factor` – step size in \[0, 1\]; `iterations` – number of passes.
    #[allow(dead_code)]
    pub fn smooth_laplacian(&mut self, factor: f64, iterations: usize) {
        if self.vertices.is_empty() || self.indices.is_empty() {
            return;
        }

        // Identify boundary vertices
        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();
        for tri in &self.indices {
            for k in 0..3 {
                let a = tri[k];
                let b = tri[(k + 1) % 3];
                let key = if a < b { (a, b) } else { (b, a) };
                *edge_count.entry(key).or_insert(0) += 1;
            }
        }
        let mut is_boundary = vec![false; self.vertices.len()];
        for (&(a, b), &c) in &edge_count {
            if c == 1 {
                is_boundary[a] = true;
                is_boundary[b] = true;
            }
        }

        for _ in 0..iterations {
            let weights = self.compute_laplacian_matrix();
            let mut new_vertices = self.vertices.clone();

            for i in 0..self.vertices.len() {
                if is_boundary[i] {
                    continue;
                }
                let mut weight_sum = 0.0f64;
                let mut delta = Vec3::zeros();

                for tri in &self.indices {
                    for k in 0..3 {
                        if tri[k] == i {
                            let j = tri[(k + 1) % 3];
                            let w = *weights.get(&(i, j)).unwrap_or(&0.0);
                            delta += (self.vertices[j] - self.vertices[i]) * w;
                            weight_sum += w;
                        }
                    }
                }

                if weight_sum > 1e-15 {
                    new_vertices[i] += delta * (factor / weight_sum);
                }
            }

            self.vertices = new_vertices;
        }
    }
}

// -----------------------------------------------------------------------
// Helper for Dijkstra heap ordering
// -----------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq)]
struct OrdF64(f64, usize);

impl Eq for OrdF64 {}

impl PartialOrd for OrdF64 {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for OrdF64 {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0
            .partial_cmp(&other.0)
            .unwrap_or(std::cmp::Ordering::Equal)
    }
}

// -----------------------------------------------------------------------
// Shape trait implementation
// -----------------------------------------------------------------------

impl Shape for TriangleMesh {
    fn bounding_box(&self) -> Aabb {
        if self.vertices.is_empty() {
            return Aabb::new(Vec3::zeros(), Vec3::zeros());
        }
        let mut min = self.vertices[0];
        let mut max = self.vertices[0];
        for v in &self.vertices[1..] {
            min = min.inf(v);
            max = max.sup(v);
        }
        Aabb::new(min, max)
    }

    fn support_point(&self, direction: &Vec3) -> Vec3 {
        self.vertices
            .iter()
            .max_by(|a, b| {
                a.dot(direction)
                    .partial_cmp(&b.dot(direction))
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .copied()
            .unwrap_or_else(Vec3::zeros)
    }

    fn volume(&self) -> Real {
        let mut vol = 0.0;
        for tri in &self.indices {
            let a = self.vertices[tri[0]];
            let b = self.vertices[tri[1]];
            let c = self.vertices[tri[2]];
            vol += a.dot(&b.cross(&c)) / 6.0;
        }
        vol.abs()
    }

    fn center_of_mass(&self) -> Vec3 {
        if self.indices.is_empty() {
            return Vec3::zeros();
        }
        let mut weighted = Vec3::zeros();
        let mut total_vol = 0.0;
        for tri in &self.indices {
            let a = self.vertices[tri[0]];
            let b = self.vertices[tri[1]];
            let c = self.vertices[tri[2]];
            let tet_vol = a.dot(&b.cross(&c)) / 6.0;
            let tet_center = (a + b + c) * 0.25;
            weighted += tet_center * tet_vol;
            total_vol += tet_vol;
        }
        if total_vol.abs() > 1e-12 {
            weighted / total_vol
        } else {
            Vec3::zeros()
        }
    }

    fn inertia_tensor(&self, mass: Real) -> Mat3 {
        let bb = self.bounding_box();
        let size = bb.max - bb.min;
        let k = mass / 12.0;
        Mat3::new(
            k * (size.y * size.y + size.z * size.z),
            0.0,
            0.0,
            0.0,
            k * (size.x * size.x + size.z * size.z),
            0.0,
            0.0,
            0.0,
            k * (size.x * size.x + size.y * size.y),
        )
    }

    fn ray_cast(&self, ray_origin: &Vec3, ray_direction: &Vec3, max_toi: Real) -> Option<RayHit> {
        let mut best: Option<RayHit> = None;
        for tri in &self.indices {
            let v0 = self.vertices[tri[0]];
            let v1 = self.vertices[tri[1]];
            let v2 = self.vertices[tri[2]];
            if let Some(hit) = ray_triangle(ray_origin, ray_direction, &v0, &v1, &v2)
                && hit.toi <= max_toi
                && hit.toi >= 0.0
                && best.as_ref().is_none_or(|b| hit.toi < b.toi)
            {
                best = Some(hit);
            }
        }
        best
    }
}

/// Moller-Trumbore ray-triangle intersection.
fn ray_triangle(
    origin: &Vec3,
    direction: &Vec3,
    v0: &Vec3,
    v1: &Vec3,
    v2: &Vec3,
) -> Option<RayHit> {
    let edge1 = v1 - v0;
    let edge2 = v2 - v0;
    let h = direction.cross(&edge2);
    let a = edge1.dot(&h);

    if a.abs() < 1e-10 {
        return None;
    }

    let f = 1.0 / a;
    let s = origin - v0;
    let u = f * s.dot(&h);
    if !(0.0..=1.0).contains(&u) {
        return None;
    }

    let q = s.cross(&edge1);
    let v = f * direction.dot(&q);
    if v < 0.0 || u + v > 1.0 {
        return None;
    }

    let t = f * edge2.dot(&q);
    if t < 0.0 {
        return None;
    }

    let point = origin + direction * t;
    let normal = edge1.cross(&edge2).normalize();
    Some(RayHit {
        point,
        normal,
        toi: t,
    })
}

/// Build a simple unit cube mesh (12 triangles, watertight).
#[cfg(test)]
fn unit_cube_mesh() -> TriangleMesh {
    let v = vec![
        Vec3::new(0.0, 0.0, 0.0), // 0
        Vec3::new(1.0, 0.0, 0.0), // 1
        Vec3::new(1.0, 1.0, 0.0), // 2
        Vec3::new(0.0, 1.0, 0.0), // 3
        Vec3::new(0.0, 0.0, 1.0), // 4
        Vec3::new(1.0, 0.0, 1.0), // 5
        Vec3::new(1.0, 1.0, 1.0), // 6
        Vec3::new(0.0, 1.0, 1.0), // 7
    ];
    let idx = vec![
        // -Z face (z=0)
        [0, 2, 1],
        [0, 3, 2],
        // +Z face (z=1)
        [4, 5, 6],
        [4, 6, 7],
        // -X face (x=0)
        [0, 4, 7],
        [0, 7, 3],
        // +X face (x=1)
        [1, 2, 6],
        [1, 6, 5],
        // -Y face (y=0)
        [0, 1, 5],
        [0, 5, 4],
        // +Y face (y=1)
        [3, 7, 6],
        [3, 6, 2],
    ];
    TriangleMesh::new(v, idx)
}

/// Build a simple tetrahedron mesh (4 triangles, watertight).
#[cfg(test)]
fn tetrahedron_mesh() -> TriangleMesh {
    let v = vec![
        Vec3::new(1.0, 1.0, 1.0),
        Vec3::new(-1.0, -1.0, 1.0),
        Vec3::new(-1.0, 1.0, -1.0),
        Vec3::new(1.0, -1.0, -1.0),
    ];
    let idx = vec![[0, 1, 2], [0, 3, 1], [0, 2, 3], [1, 3, 2]];
    TriangleMesh::new(v, idx)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::TriangleMesh;
    use oxiphysics_core::math::Vec3;

    #[test]
    fn test_triangle_mesh_raycast() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(-1.0, 0.0, -1.0),
                Vec3::new(1.0, 0.0, -1.0),
                Vec3::new(0.0, 0.0, 1.0),
            ],
            vec![[0, 1, 2]],
        );
        let origin = Vec3::new(0.0, 5.0, 0.0);
        let dir = Vec3::new(0.0, -1.0, 0.0);
        let hit = mesh.ray_cast(&origin, &dir, 100.0).unwrap();
        assert!((hit.toi - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_triangle_mesh_raycast_miss() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(-1.0, 0.0, -1.0),
                Vec3::new(1.0, 0.0, -1.0),
                Vec3::new(0.0, 0.0, 1.0),
            ],
            vec![[0, 1, 2]],
        );
        let origin = Vec3::new(10.0, 5.0, 0.0);
        let dir = Vec3::new(0.0, -1.0, 0.0);
        assert!(mesh.ray_cast(&origin, &dir, 100.0).is_none());
    }

    #[test]
    fn test_triangle_mesh_cube_volume() {
        let mesh = unit_cube_mesh();
        let vol = mesh.volume();
        assert!((vol - 1.0).abs() < 1e-6, "expected volume=1, got {}", vol);
    }

    #[test]
    fn test_triangle_mesh_cube_surface_area() {
        let mesh = unit_cube_mesh();
        let sa = mesh.surface_area();
        assert!((sa - 6.0).abs() < 1e-6, "expected SA=6, got {}", sa);
    }

    #[test]
    fn test_triangle_mesh_cube_is_watertight() {
        let mesh = unit_cube_mesh();
        assert!(mesh.is_watertight(), "unit cube should be watertight");
    }

    #[test]
    fn test_triangle_mesh_open_not_watertight() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        assert!(!mesh.is_watertight(), "open mesh should not be watertight");
    }

    #[test]
    fn test_triangle_mesh_compute_normals() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let normals = mesh.compute_normals();
        assert_eq!(normals.len(), 1);
        assert!((normals[0][2] - 1.0).abs() < 1e-10, "expected +Z normal");
    }

    #[test]
    fn test_triangle_mesh_ray_cast_full() {
        let mesh = unit_cube_mesh();
        let (t, _face, n) = mesh
            .ray_cast_full([0.5, 5.0, 0.5], [0.0, -1.0, 0.0], 100.0)
            .expect("should hit cube");
        assert!((t - 4.0).abs() < 1e-6, "expected t=4, got {}", t);
        assert!(n[1].abs() > 0.9, "normal should be roughly Y");
    }

    #[test]
    fn test_triangle_mesh_surface_area_triangle() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(3.0, 0.0, 0.0),
                Vec3::new(0.0, 4.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let sa = mesh.surface_area();
        assert!((sa - 6.0).abs() < 1e-10, "expected SA=6, got {}", sa);
    }

    // ------------------------------------------------------------------
    // New tests: adjacency queries
    // ------------------------------------------------------------------

    #[test]
    fn test_vertex_to_faces() {
        let mesh = unit_cube_mesh();
        let v2f = mesh.vertex_to_faces();
        assert_eq!(v2f.len(), 8);
        // Each vertex of a cube is shared by 3 faces (each face = 2 tris,
        // each vertex appears in exactly 3 faces = 6 tris / 2, but actually
        // each vertex is used by 3*2=6? Let's just check non-empty).
        for faces in &v2f {
            assert!(
                !faces.is_empty(),
                "every vertex should have at least one face"
            );
        }
        // Vertex 0 appears in -Z, -X, -Y faces (6 triangles)
        // Indices: [0,2,1],[0,3,2],[0,4,7],[0,7,3],[0,1,5],[0,5,4]
        assert_eq!(
            v2f[0].len(),
            6,
            "vertex 0 in unit cube should appear in 6 triangles"
        );
    }

    #[test]
    fn test_face_adjacency_single_triangle() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let adj = mesh.face_adjacency();
        assert_eq!(adj.len(), 1);
        assert!(adj[0].is_empty(), "single triangle has no adjacent faces");
    }

    #[test]
    fn test_face_adjacency_cube() {
        let mesh = unit_cube_mesh();
        let adj = mesh.face_adjacency();
        assert_eq!(adj.len(), 12);
        // Each triangle in a cube shares edges with other triangles
        for neighbors in &adj {
            assert!(
                !neighbors.is_empty(),
                "each cube triangle should have neighbors"
            );
        }
    }

    #[test]
    fn test_unique_edges_single_triangle() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let edges = mesh.unique_edges();
        assert_eq!(edges.len(), 3);
    }

    #[test]
    fn test_unique_edges_cube() {
        let mesh = unit_cube_mesh();
        let edges = mesh.unique_edges();
        // A cube has 12 edges + 6 face diagonals = 18
        assert_eq!(edges.len(), 18);
    }

    #[test]
    fn test_vertex_neighbors() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let nbrs = mesh.vertex_neighbors();
        assert_eq!(nbrs.len(), 3);
        assert_eq!(nbrs[0].len(), 2); // connected to 1 and 2
        assert_eq!(nbrs[1].len(), 2);
        assert_eq!(nbrs[2].len(), 2);
    }

    // ------------------------------------------------------------------
    // New tests: edge collapse
    // ------------------------------------------------------------------

    #[test]
    fn test_edge_collapse_basic() {
        // Two triangles sharing edge (0,1)
        let mut mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
                Vec3::new(0.5, -1.0, 0.0),
            ],
            vec![[0, 1, 2], [0, 3, 1]],
        );
        let ok = mesh.edge_collapse(0, 1);
        assert!(ok, "edge collapse should succeed");
        // After collapse, vertex 0 should be at midpoint (0.5, 0, 0)
        assert!((mesh.vertices[0].x - 0.5).abs() < 1e-10);
        // Triangles that degenerate should be removed
        // Both triangles had vertices 0 and 1, which are now both 0,
        // so they become degenerate -> removed? No: [0,1,2] becomes [0,0,2] -> degenerate.
        // [0,3,1] becomes [0,3,0] -> degenerate.
        assert!(mesh.indices.is_empty(), "both triangles should degenerate");
    }

    #[test]
    fn test_edge_collapse_nonexistent() {
        let mut mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        // Edge (0, 100) doesn't exist
        assert!(!mesh.edge_collapse(0, 100));
    }

    #[test]
    fn test_edge_collapse_preserves_other_faces() {
        // Three triangles, collapse edge shared by first two
        let mut mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),  // 0
                Vec3::new(2.0, 0.0, 0.0),  // 1
                Vec3::new(1.0, 1.0, 0.0),  // 2
                Vec3::new(1.0, -1.0, 0.0), // 3
                Vec3::new(3.0, 1.0, 0.0),  // 4
            ],
            vec![[0, 1, 2], [0, 3, 1], [1, 4, 2]],
        );
        let ok = mesh.edge_collapse(0, 1);
        assert!(ok);
        // Triangle [1,4,2] references v1 which is now v0
        // It becomes [0,4,2] which is non-degenerate
        assert_eq!(
            mesh.indices.len(),
            1,
            "two degenerate tris removed, one survives"
        );
    }

    // ------------------------------------------------------------------
    // New tests: vertex normals
    // ------------------------------------------------------------------

    #[test]
    fn test_vertex_normals_flat_surface() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(1.0, 1.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2], [0, 2, 3]],
        );
        let normals = mesh.compute_vertex_normals();
        assert_eq!(normals.len(), 4);
        for n in &normals {
            assert!(
                (n[2] - 1.0).abs() < 1e-6,
                "flat XY surface normal should be +Z"
            );
        }
    }

    #[test]
    fn test_vertex_normals_unit_length() {
        let mesh = unit_cube_mesh();
        let normals = mesh.compute_vertex_normals();
        for n in &normals {
            let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
            assert!(
                (len - 1.0).abs() < 1e-6,
                "vertex normal should be unit length, got {}",
                len
            );
        }
    }

    // ------------------------------------------------------------------
    // New tests: Laplacian smoothing
    // ------------------------------------------------------------------

    #[test]
    fn test_laplacian_smooth_doesnt_crash() {
        let mut mesh = unit_cube_mesh();
        mesh.laplacian_smooth(0.5, 3);
        // After smoothing the cube should still have 8 vertices and 12 faces
        assert_eq!(mesh.vertices.len(), 8);
        assert_eq!(mesh.indices.len(), 12);
    }

    #[test]
    fn test_laplacian_smooth_converges_to_center() {
        // A mesh with one interior vertex should move towards neighbors
        let mut mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0), // 0
                Vec3::new(2.0, 0.0, 0.0), // 1
                Vec3::new(1.0, 2.0, 0.0), // 2
                Vec3::new(1.0, 0.5, 0.0), // 3 (interior-ish vertex)
            ],
            vec![[0, 1, 3], [1, 2, 3], [2, 0, 3]],
        );
        let orig_v3 = mesh.vertices[3];
        mesh.laplacian_smooth(1.0, 1);
        // Vertex 3 should have moved towards the average of its neighbors (0,1,2)
        let avg =
            (Vec3::new(0.0, 0.0, 0.0) + Vec3::new(2.0, 0.0, 0.0) + Vec3::new(1.0, 2.0, 0.0)) / 3.0;
        let new_v3 = mesh.vertices[3];
        let d_new = (new_v3 - avg).norm();
        let d_old = (orig_v3 - avg).norm();
        assert!(
            d_new < d_old + 1e-10,
            "vertex should move towards neighbor average"
        );
    }

    // ------------------------------------------------------------------
    // New tests: geodesic distance
    // ------------------------------------------------------------------

    #[test]
    fn test_geodesic_distance_source() {
        let mesh = unit_cube_mesh();
        let dist = mesh.geodesic_distance(0);
        assert_eq!(dist[0], 0.0, "distance to self should be 0");
    }

    #[test]
    fn test_geodesic_distance_adjacent() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let dist = mesh.geodesic_distance(0);
        // Distance from 0 to 1 should be 1.0
        assert!(
            (dist[1] - 1.0).abs() < 1e-10,
            "expected dist=1, got {}",
            dist[1]
        );
    }

    #[test]
    fn test_geodesic_distance_symmetry() {
        let mesh = unit_cube_mesh();
        let d0 = mesh.geodesic_distance(0);
        let d6 = mesh.geodesic_distance(6);
        // Distance 0->6 should equal distance 6->0
        assert!(
            (d0[6] - d6[0]).abs() < 1e-10,
            "geodesic distance should be symmetric: {} vs {}",
            d0[6],
            d6[0]
        );
    }

    // ------------------------------------------------------------------
    // New tests: Loop subdivision
    // ------------------------------------------------------------------

    #[test]
    fn test_loop_subdivide_increases_faces() {
        let mut mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        mesh.loop_subdivide();
        assert_eq!(mesh.indices.len(), 4, "one triangle should become four");
    }

    #[test]
    fn test_loop_subdivide_cube() {
        let mut mesh = unit_cube_mesh();
        assert_eq!(mesh.indices.len(), 12);
        mesh.loop_subdivide();
        assert_eq!(mesh.indices.len(), 48, "12 tris * 4 = 48");
        // Should still have consistent structure
        assert!(
            mesh.vertices.len() > 8,
            "should have more vertices after subdivision"
        );
    }

    #[test]
    fn test_loop_subdivide_watertight_preserved() {
        let mut mesh = tetrahedron_mesh();
        assert!(mesh.is_watertight(), "tetrahedron should be watertight");
        mesh.loop_subdivide();
        assert!(
            mesh.is_watertight(),
            "subdivided tetrahedron should still be watertight"
        );
    }

    // ------------------------------------------------------------------
    // New tests: boundary edges, Euler characteristic, non-manifold
    // ------------------------------------------------------------------

    #[test]
    fn test_boundary_edges_closed_mesh() {
        let mesh = unit_cube_mesh();
        let be = mesh.boundary_edges();
        assert!(
            be.is_empty(),
            "watertight mesh should have no boundary edges"
        );
    }

    #[test]
    fn test_boundary_edges_open_mesh() {
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        );
        let be = mesh.boundary_edges();
        assert_eq!(be.len(), 3, "single triangle has 3 boundary edges");
    }

    #[test]
    fn test_euler_characteristic_cube() {
        let mesh = unit_cube_mesh();
        let chi = mesh.euler_characteristic();
        assert_eq!(chi, 2, "closed cube Euler characteristic should be 2");
    }

    #[test]
    fn test_euler_characteristic_tetrahedron() {
        let mesh = tetrahedron_mesh();
        let chi = mesh.euler_characteristic();
        assert_eq!(
            chi, 2,
            "closed tetrahedron Euler characteristic should be 2"
        );
    }

    #[test]
    fn test_non_manifold_edge_count_clean_mesh() {
        let mesh = unit_cube_mesh();
        assert_eq!(mesh.non_manifold_edge_count(), 0);
    }

    #[test]
    fn test_non_manifold_edge_count_with_extra_face() {
        // Add a third triangle sharing an edge -> non-manifold
        let mesh = TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
                Vec3::new(0.5, -1.0, 0.0),
                Vec3::new(0.5, 0.5, 1.0),
            ],
            vec![[0, 1, 2], [0, 3, 1], [0, 4, 1]], // edge (0,1) shared by 3 faces
        );
        assert_eq!(mesh.non_manifold_edge_count(), 1);
    }
}

// ---------------------------------------------------------------------------
// Tests for cotangent Laplacian, HKS, and smooth_laplacian
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests_laplacian_hks {

    use crate::TriangleMesh;
    use oxiphysics_core::math::Vec3;

    /// Flat quad made of two triangles: 4 vertices, 2 triangles.
    fn flat_quad() -> TriangleMesh {
        TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(1.0, 1.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            ],
            vec![[0, 1, 2], [0, 2, 3]],
        )
    }

    /// Single triangle.
    fn single_triangle() -> TriangleMesh {
        TriangleMesh::new(
            vec![
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0),
            ],
            vec![[0, 1, 2]],
        )
    }

    // ── compute_laplacian_matrix ─────────────────────────────────────────────

    #[test]
    fn test_laplacian_matrix_nonempty() {
        let mesh = flat_quad();
        let w = mesh.compute_laplacian_matrix();
        assert!(!w.is_empty(), "Laplacian weights should be non-empty");
    }

    #[test]
    fn test_laplacian_matrix_symmetric() {
        let mesh = flat_quad();
        let w = mesh.compute_laplacian_matrix();
        // For every (i,j) entry there should be a matching (j,i)
        for (&(i, j), &wij) in &w {
            let wji = w.get(&(j, i)).copied().unwrap_or(0.0);
            assert!(
                (wij - wji).abs() < 1e-10,
                "Laplacian not symmetric at ({i},{j}): {wij} vs {wji}"
            );
        }
    }

    #[test]
    fn test_laplacian_matrix_single_triangle_all_edges_present() {
        let mesh = single_triangle();
        let w = mesh.compute_laplacian_matrix();
        // 3 undirected edges × 2 directions = 6 entries
        assert_eq!(
            w.len(),
            6,
            "single triangle should produce 6 directed entries"
        );
    }

    #[test]
    fn test_laplacian_matrix_empty_mesh() {
        let mesh = TriangleMesh::new(vec![], vec![]);
        let w = mesh.compute_laplacian_matrix();
        assert!(w.is_empty());
    }

    // ── compute_heat_kernel_signature ────────────────────────────────────────

    #[test]
    fn test_hks_output_shape() {
        let mesh = flat_quad();
        let t_vals = vec![0.1, 1.0, 10.0];
        let hks = mesh.compute_heat_kernel_signature(&t_vals);
        assert_eq!(hks.len(), mesh.vertices.len(), "one HKS row per vertex");
        for row in &hks {
            assert_eq!(row.len(), t_vals.len(), "one entry per time scale");
        }
    }

    #[test]
    fn test_hks_values_in_range() {
        // HKS = exp(-t * lambda) is always in (0, 1] for t > 0, lambda >= 0
        let mesh = flat_quad();
        let t_vals = vec![0.01, 0.1, 1.0, 10.0];
        let hks = mesh.compute_heat_kernel_signature(&t_vals);
        for row in &hks {
            for &val in row {
                assert!(
                    val > 0.0 && val <= 1.0 + 1e-12,
                    "HKS value out of range: {val}"
                );
            }
        }
    }

    #[test]
    fn test_hks_decreasing_with_time() {
        // For any vertex, HKS(i, t1) >= HKS(i, t2) when t1 < t2
        let mesh = flat_quad();
        let t_vals = vec![0.1, 1.0, 10.0];
        let hks = mesh.compute_heat_kernel_signature(&t_vals);
        for (vi, row) in hks.iter().enumerate() {
            for k in 0..(row.len() - 1) {
                assert!(
                    row[k] >= row[k + 1] - 1e-12,
                    "HKS not decreasing for vertex {vi}: t={}, val={}; t={}, val={}",
                    t_vals[k],
                    row[k],
                    t_vals[k + 1],
                    row[k + 1]
                );
            }
        }
    }

    #[test]
    fn test_hks_empty_mesh() {
        let mesh = TriangleMesh::new(vec![], vec![]);
        let hks = mesh.compute_heat_kernel_signature(&[1.0, 2.0]);
        assert!(hks.is_empty());
    }

    // ── smooth_laplacian ────────────────────────────────────────────────────

    #[test]
    fn test_smooth_laplacian_vertex_count_unchanged() {
        let mut mesh = flat_quad();
        let n_before = mesh.vertices.len();
        mesh.smooth_laplacian(0.5, 3);
        assert_eq!(
            mesh.vertices.len(),
            n_before,
            "smoothing must not add/remove vertices"
        );
    }

    #[test]
    fn test_smooth_laplacian_triangle_count_unchanged() {
        let mut mesh = flat_quad();
        let n_before = mesh.indices.len();
        mesh.smooth_laplacian(0.5, 3);
        assert_eq!(
            mesh.indices.len(),
            n_before,
            "smoothing must not change connectivity"
        );
    }

    #[test]
    fn test_smooth_laplacian_zero_iterations_no_change() {
        let mut mesh = flat_quad();
        let verts_before: Vec<Vec3> = mesh.vertices.clone();
        mesh.smooth_laplacian(0.5, 0);
        for (a, b) in verts_before.iter().zip(mesh.vertices.iter()) {
            assert!(
                (a - b).norm() < 1e-12,
                "zero iterations should not move vertices"
            );
        }
    }

    #[test]
    fn test_smooth_laplacian_boundary_fixed() {
        // For a single triangle (all boundary) smoothing must not move any vertex
        let mut mesh = single_triangle();
        let verts_before: Vec<Vec3> = mesh.vertices.clone();
        mesh.smooth_laplacian(0.5, 5);
        for (a, b) in verts_before.iter().zip(mesh.vertices.iter()) {
            assert!(
                (a - b).norm() < 1e-12,
                "boundary vertices must not move, delta={}",
                (a - b).norm()
            );
        }
    }
}