parry2d 0.26.0

2 dimensional collision detection library in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
use crate::bounding_volume::Aabb;
#[cfg(feature = "dim3")]
use crate::math::VectorExt;
use crate::math::{Pose, Vector};
use crate::partitioning::{Bvh, BvhBuildStrategy};
use crate::shape::{FeatureId, Shape, Triangle, TrianglePseudoNormals, TypedCompositeShape};
use crate::utils::HashablePartialEq;
use alloc::{vec, vec::Vec};
use core::fmt;
#[cfg(feature = "dim3")]
use {crate::shape::Cuboid, crate::utils::SortedPair};

use {
    crate::shape::composite_shape::CompositeShape,
    crate::utils::hashmap::{Entry, HashMap},
    crate::utils::hashset::HashSet,
};

#[cfg(feature = "dim2")]
use crate::transformation::ear_clipping::triangulate_ear_clipping;

use crate::query::details::NormalConstraints;

/// Errors that occur when computing or validating triangle mesh topology.
///
/// Triangle mesh topology describes the connectivity and adjacency relationships between
/// vertices, edges, and triangles. When constructing a mesh with [`TriMeshFlags::HALF_EDGE_TOPOLOGY`],
/// Parry validates these relationships and returns this error if inconsistencies are found.
///
/// # When This Occurs
///
/// Topology errors typically occur when:
/// - The mesh is non-manifold (edges with more than 2 adjacent faces)
/// - Adjacent triangles have inconsistent winding order
/// - Triangles have degenerate geometry (duplicate vertices)
///
/// [`TriMeshFlags::HALF_EDGE_TOPOLOGY`]: crate::shape::TriMeshFlags::HALF_EDGE_TOPOLOGY
#[derive(thiserror::Error, Copy, Clone, Debug, PartialEq, Eq)]
pub enum TopologyError {
    /// A triangle has two or three identical vertices (degenerate triangle).
    ///
    /// This error indicates that a triangle in the mesh references the same vertex
    /// multiple times, creating a degenerate triangle (zero area). For example,
    /// a triangle with indices `[5, 5, 7]` or `[1, 2, 1]`.
    ///
    /// Degenerate triangles cannot be part of a valid mesh topology because they
    /// don't have proper edges or face normals.
    ///
    //    /// TODO: figure out why this doc-test fails.
    //    /// # How to Fix
    //    ///
    //    /// ```
    //    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    //    /// # use parry3d::shape::{TriMesh, TriMeshFlags};
    //    /// # use parry3d::math::Vector;
    //    /// let vertices = vec![
    //    ///     Vector::ZERO,
    //    ///     Vector::new(1.0, 0.0, 0.0),
    //    ///     Vector::new(0.0, 1.0, 0.0),
    //    /// ];
    //    ///
    //    /// // BAD: Triangle with duplicate vertices
    //    /// let bad_indices = vec![[0, 0, 1]];  // vertex 0 appears twice!
    //    ///
    //    /// let result = TriMesh::with_flags(
    //    ///     vertices.clone(),
    //    ///     bad_indices,
    //    ///     TriMeshFlags::HALF_EDGE_TOPOLOGY
    //    /// );
    //    /// assert!(result.is_err());
    //    ///
    //    /// // GOOD: All three vertices are distinct
    //    /// let good_indices = vec![[0, 1, 2]];
    //    /// let mesh = TriMesh::with_flags(
    //    ///     vertices,
    //    ///     good_indices,
    //    ///     TriMeshFlags::HALF_EDGE_TOPOLOGY
    //    /// ).expect("Valid mesh");
    //    /// # }
    //    /// ```
    ///
    /// Alternatively, use [`TriMeshFlags::DELETE_DEGENERATE_TRIANGLES`] to automatically
    /// remove degenerate triangles:
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// # use parry3d::shape::{TriMesh, TriMeshFlags};
    /// # use parry3d::math::Vector;
    /// # let vertices = vec![Vector::ZERO, Vector::new(1.0, 0.0, 0.0), Vector::new(0.0, 1.0, 0.0)];
    /// # let indices = vec![[0, 0, 1], [0, 1, 2]];
    /// let flags = TriMeshFlags::HALF_EDGE_TOPOLOGY
    ///     | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES;
    ///
    /// let mesh = TriMesh::with_flags(vertices, indices, flags)
    ///     .expect("Bad triangles removed");
    /// # }
    /// ```
    #[error("the triangle {0} has at least two identical vertices.")]
    BadTriangle(u32),

    /// Two adjacent triangles have opposite orientations (inconsistent winding).
    ///
    /// For a manifold mesh with consistent normals, adjacent triangles must have
    /// compatible orientations. If two triangles share an edge, they must traverse
    /// that edge in opposite directions.
    ///
    /// This error reports:
    /// - `triangle1`, `triangle2`: The indices of the two conflicting triangles
    /// - `edge`: The shared edge as a pair of vertex indices `(v1, v2)`
    ///
    /// # Example of the Problem
    ///
    /// ```text
    /// CORRECT (opposite winding on shared edge):
    ///   Triangle 1: [a, b, c]  ->  edge (a,b)
    ///   Triangle 2: [b, a, d]  ->  edge (b,a)  ✓ opposite direction
    ///
    /// INCORRECT (same winding on shared edge):
    ///   Triangle 1: [a, b, c]  ->  edge (a,b)
    ///   Triangle 2: [a, b, d]  ->  edge (a,b)  ✗ same direction!
    /// ```
    ///
    //    /// TODO: figure out why this doc test fails?
    //    /// # How to Fix
    //    ///
    //    /// You need to reverse the winding order of one of the triangles:
    //    ///
    //    /// ```
    //    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    //    /// # use parry3d::shape::{TriMesh, TriMeshFlags};
    //    /// # use parry3d::math::Vector;
    //    /// let vertices = vec![
    //    ///     Vector::ZERO,  // vertex 0
    //    ///     Vector::new(1.0, 0.0, 0.0),  // vertex 1
    //    ///     Vector::new(0.5, 1.0, 0.0),  // vertex 2
    //    ///     Vector::new(0.5, -1.0, 0.0), // vertex 3
    //    /// ];
    //    ///
    //    /// // BAD: Both triangles traverse edge (0,1) in the same direction
    //    /// let bad_indices = vec![
    //    ///     [0, 1, 2],  // edge (0,1)
    //    ///     [0, 1, 3],  // edge (0,1) - same direction!
    //    /// ];
    //    ///
    //    /// let result = TriMesh::with_flags(
    //    ///     vertices.clone(),
    //    ///     bad_indices,
    //    ///     TriMeshFlags::HALF_EDGE_TOPOLOGY
    //    /// );
    //    /// assert!(result.is_err());
    //    ///
    //    /// // GOOD: Second triangle reversed to [1, 0, 3]
    //    /// let good_indices = vec![
    //    ///     [0, 1, 2],  // edge (0,1)
    //    ///     [1, 0, 3],  // edge (1,0) - opposite direction!
    //    /// ];
    //    ///
    //    /// let mesh = TriMesh::with_flags(
    //    ///     vertices,
    //    ///     good_indices,
    //    ///     TriMeshFlags::HALF_EDGE_TOPOLOGY
    //    /// ).expect("Valid mesh");
    //    /// # }
    //    /// ```
    ///
    /// # Common Causes
    ///
    /// - Mixing clockwise and counter-clockwise triangle definitions
    /// - Incorrectly constructed mesh from modeling software
    /// - Manual mesh construction with inconsistent winding
    /// - Merging separate meshes with different conventions
    #[error("the triangles {triangle1} and {triangle2} sharing the edge {edge:?} have opposite orientations.")]
    BadAdjacentTrianglesOrientation {
        /// The first triangle, with an orientation opposite to the second triangle.
        triangle1: u32,
        /// The second triangle, with an orientation opposite to the first triangle.
        triangle2: u32,
        /// The edge shared between the two triangles.
        edge: (u32, u32),
    },
}

/// Errors that occur when creating a triangle mesh.
///
/// When constructing a [`TriMesh`] using [`TriMesh::new`] or [`TriMesh::with_flags`],
/// various validation checks are performed. This error type describes what went wrong.
///
/// # Common Usage
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # use parry3d::shape::{TriMesh, TriMeshBuilderError, TopologyError};
/// # use parry3d::math::Vector;
/// let vertices = vec![Vector::ZERO];
/// let indices = vec![];  // Empty!
///
/// match TriMesh::new(vertices, indices) {
///     Err(TriMeshBuilderError::EmptyIndices) => {
///         println!("Cannot create a mesh with no triangles");
///     }
///     Err(TriMeshBuilderError::TopologyError(topo_err)) => {
///         println!("Mesh topology is invalid: {}", topo_err);
///     }
///     Ok(mesh) => {
///         println!("Mesh created successfully");
///     }
/// }
/// # }
/// ```
///
/// [`TriMesh`]: crate::shape::TriMesh
/// [`TriMesh::new`]: crate::shape::TriMesh::new
/// [`TriMesh::with_flags`]: crate::shape::TriMesh::with_flags
#[derive(thiserror::Error, Copy, Clone, Debug, PartialEq, Eq)]
pub enum TriMeshBuilderError {
    /// The index buffer is empty (no triangles provided).
    ///
    /// A triangle mesh must contain at least one triangle. An empty index buffer
    /// is not valid because there's nothing to render or use for collision detection.
    ///
    /// # How to Fix
    ///
    /// Ensure your index buffer has at least one triangle:
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// # use parry3d::shape::TriMesh;
    /// # use parry3d::math::Vector;
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    ///
    /// // BAD: No triangles
    /// let empty_indices = vec![];
    /// assert!(TriMesh::new(vertices.clone(), empty_indices).is_err());
    ///
    /// // GOOD: At least one triangle
    /// let indices = vec![[0, 1, 2]];
    /// assert!(TriMesh::new(vertices, indices).is_ok());
    /// # }
    /// ```
    #[error("A triangle mesh must contain at least one triangle.")]
    EmptyIndices,

    /// The mesh topology is invalid.
    ///
    /// This wraps a [`TopologyError`] that provides details about the specific
    /// topology problem. This only occurs when creating a mesh with topology
    /// validation enabled (e.g., [`TriMeshFlags::HALF_EDGE_TOPOLOGY`]).
    ///
    /// See [`TopologyError`] for details on specific topology problems and how
    /// to fix them.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// # use parry3d::shape::{TriMesh, TriMeshFlags, TriMeshBuilderError};
    /// # use parry3d::math::Vector;
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    ///
    /// // Triangle with duplicate vertices
    /// let bad_indices = vec![[0, 0, 1]];
    ///
    /// match TriMesh::with_flags(vertices, bad_indices, TriMeshFlags::HALF_EDGE_TOPOLOGY) {
    ///     Err(TriMeshBuilderError::TopologyError(topo_err)) => {
    ///         println!("Topology error: {}", topo_err);
    ///         // Handle the specific topology issue
    ///     }
    ///     _ => {}
    /// }
    /// # }
    /// ```
    ///
    /// [`TriMeshFlags::HALF_EDGE_TOPOLOGY`]: crate::shape::TriMeshFlags::HALF_EDGE_TOPOLOGY
    #[error("Topology Error: {0}")]
    TopologyError(TopologyError),
}

/// The set of pseudo-normals of a triangle mesh.
///
/// These pseudo-normals are used for the inside-outside test of a
/// point on the triangle, as described in the paper:
/// "Signed distance computation using the angle weighted pseudonormal", Baerentzen, et al.
/// DOI: 10.1109/TVCG.2005.49
#[derive(Default, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
#[cfg(feature = "dim3")]
pub struct TriMeshPseudoNormals {
    /// The pseudo-normals of the vertices.
    pub vertices_pseudo_normal: Vec<Vector>,
    /// The pseudo-normals of the edges.
    pub edges_pseudo_normal: Vec<[Vector; 3]>,
}

/// The connected-components of a triangle mesh.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct TriMeshConnectedComponents {
    /// The `face_colors[i]` gives the connected-component index
    /// of the i-th face.
    pub face_colors: Vec<u32>,
    /// The set of faces grouped by connected components.
    pub grouped_faces: Vec<u32>,
    /// The range of connected components. `self.grouped_faces[self.ranges[i]..self.ranges[i + 1]]`
    /// contains the indices of all the faces part of the i-th connected component.
    pub ranges: Vec<usize>,
}

impl TriMeshConnectedComponents {
    /// The total number of connected components.
    pub fn num_connected_components(&self) -> usize {
        self.ranges.len() - 1
    }

    /// Convert the connected-component description into actual meshes (returned as raw index and
    /// vertex buffers).
    ///
    /// The `mesh` must be the one used to generate `self`, otherwise it might panic or produce an
    /// unexpected result.
    pub fn to_mesh_buffers(&self, mesh: &TriMesh) -> Vec<(Vec<Vector>, Vec<[u32; 3]>)> {
        let mut result = vec![];
        let mut new_vtx_index: Vec<_> = vec![u32::MAX; mesh.vertices.len()];

        for ranges in self.ranges.windows(2) {
            let num_faces = ranges[1] - ranges[0];

            if num_faces == 0 {
                continue;
            }

            let mut vertices = Vec::with_capacity(num_faces);
            let mut indices = Vec::with_capacity(num_faces);

            for fid in ranges[0]..ranges[1] {
                let vids = mesh.indices[self.grouped_faces[fid] as usize];
                let new_vids = vids.map(|id| {
                    if new_vtx_index[id as usize] == u32::MAX {
                        vertices.push(mesh.vertices[id as usize]);
                        new_vtx_index[id as usize] = vertices.len() as u32 - 1;
                    }

                    new_vtx_index[id as usize]
                });
                indices.push(new_vids);
            }

            result.push((vertices, indices));
        }

        result
    }

    /// Convert the connected-component description into actual meshes.
    ///
    /// The `mesh` must be the one used to generate `self`, otherwise it might panic or produce an
    /// unexpected result.
    ///
    /// All the meshes are constructed with the given `flags`.
    pub fn to_meshes(
        &self,
        mesh: &TriMesh,
        flags: TriMeshFlags,
    ) -> Vec<Result<TriMesh, TriMeshBuilderError>> {
        self.to_mesh_buffers(mesh)
            .into_iter()
            .map(|(vtx, idx)| TriMesh::with_flags(vtx, idx, flags))
            .collect()
    }
}

/// A vertex of a triangle-mesh’s half-edge topology.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct TopoVertex {
    /// One of the half-edge with this vertex as endpoint.
    pub half_edge: u32,
}

/// A face of a triangle-mesh’s half-edge topology.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct TopoFace {
    /// The half-edge adjacent to this face, with a starting point equal
    /// to the first point of this face.
    pub half_edge: u32,
}

/// A half-edge of a triangle-mesh’s half-edge topology.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct TopoHalfEdge {
    /// The next half-edge.
    pub next: u32,
    /// This half-edge twin on the adjacent triangle.
    ///
    /// This is `u32::MAX` if there is no twin.
    pub twin: u32,
    /// The first vertex of this edge.
    pub vertex: u32,
    /// The face associated to this half-edge.
    pub face: u32,
}

/// The half-edge topology information of a triangle mesh.
#[derive(Default, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
pub struct TriMeshTopology {
    /// The vertices of this half-edge representation.
    pub vertices: Vec<TopoVertex>,
    /// The faces of this half-edge representation.
    pub faces: Vec<TopoFace>,
    /// The half-edges of this half-edge representation.
    pub half_edges: Vec<TopoHalfEdge>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// Controls how a [`TriMesh`] should be loaded.
pub struct TriMeshFlags(u16);

bitflags::bitflags! {
    impl TriMeshFlags: u16 {
        /// If set, the half-edge topology of the trimesh will be computed if possible.
        const HALF_EDGE_TOPOLOGY = 1;
        /// If set, the connected components of the trimesh will be computed.
        const CONNECTED_COMPONENTS = 1 << 1;
        /// If set, any triangle that results in a failing half-hedge topology computation will be deleted.
        const DELETE_BAD_TOPOLOGY_TRIANGLES = 1 << 2;
        /// If set, the trimesh will be assumed to be oriented (with outward normals).
        ///
        /// The pseudo-normals of its vertices and edges will be computed.
        const ORIENTED = 1 << 3;
        /// If set, the duplicate vertices of the trimesh will be merged.
        ///
        /// Two vertices with the exact same coordinates will share the same entry on the
        /// vertex buffer and the index buffer is adjusted accordingly.
        const MERGE_DUPLICATE_VERTICES = 1 << 4;
        /// If set, the triangles sharing two vertices with identical index values will be removed.
        ///
        /// Because of the way it is currently implemented, this methods implies that duplicate
        /// vertices will be merged. It will no longer be the case in the future once we decouple
        /// the computations.
        const DELETE_DEGENERATE_TRIANGLES = 1 << 5;
        /// If set, two triangles sharing three vertices with identical index values (in any order)
        /// will be removed.
        ///
        /// Because of the way it is currently implemented, this methods implies that duplicate
        /// vertices will be merged. It will no longer be the case in the future once we decouple
        /// the computations.
        const DELETE_DUPLICATE_TRIANGLES = 1 << 6;
        /// If set, a special treatment will be applied to contact manifold calculation to eliminate
        /// or fix contacts normals that could lead to incorrect bumps in physics simulation
        /// (especially on flat surfaces).
        ///
        /// This is achieved by taking into account adjacent triangle normals when computing contact
        /// points for a given triangle.
        const FIX_INTERNAL_EDGES = (1 << 7) | Self::MERGE_DUPLICATE_VERTICES.bits();
    }
}

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
#[derive(Clone)]
/// A triangle mesh.
pub struct TriMesh {
    bvh: Bvh,
    vertices: Vec<Vector>,
    indices: Vec<[u32; 3]>,
    #[cfg(feature = "dim3")]
    pub(crate) pseudo_normals: Option<TriMeshPseudoNormals>,
    topology: Option<TriMeshTopology>,
    connected_components: Option<TriMeshConnectedComponents>,
    flags: TriMeshFlags,
}

impl fmt::Debug for TriMesh {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "GenericTriMesh")
    }
}

impl TriMesh {
    /// Creates a new triangle mesh from a vertex buffer and an index buffer.
    ///
    /// This is the most common way to construct a `TriMesh`. The mesh is created with
    /// default settings (no topology computation, no pseudo-normals, etc.). For more
    /// control over these optional features, use [`TriMesh::with_flags`] instead.
    ///
    /// # Arguments
    ///
    /// * `vertices` - A vector of 3D points representing the mesh vertices
    /// * `indices` - A vector of triangles, where each triangle is represented by
    ///   three indices into the vertex buffer. Indices should be in counter-clockwise
    ///   order for outward-facing normals.
    ///
    /// # Returns
    ///
    /// * `Ok(TriMesh)` - If the mesh was successfully created
    /// * `Err(TriMeshBuilderError)` - If the mesh is invalid (e.g., empty index buffer)
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    /// - The index buffer is empty (at least one triangle is required)
    ///
    /// # Performance
    ///
    /// This function builds a BVH (Bounding Volume Hierarchy) acceleration structure
    /// for the mesh, which takes O(n log n) time where n is the number of triangles.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// // Create a simple triangle mesh (a single triangle)
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    ///
    /// let trimesh = TriMesh::new(vertices, indices).expect("Invalid mesh");
    /// assert_eq!(trimesh.num_triangles(), 1);
    /// # }
    /// ```
    ///
    /// ```
    /// # #[cfg(all(feature = "dim2", feature = "f32"))] {
    /// use parry2d::shape::TriMesh;
    /// use parry2d::math::Vector;
    ///
    /// // Create a quad (two triangles) in 2D
    /// let vertices = vec![
    ///     Vector::ZERO,  // bottom-left
    ///     Vector::new(1.0, 0.0),  // bottom-right
    ///     Vector::new(1.0, 1.0),  // top-right
    ///     Vector::new(0.0, 1.0),  // top-left
    /// ];
    /// let indices = vec![
    ///     [0, 1, 2],  // first triangle
    ///     [0, 2, 3],  // second triangle
    /// ];
    ///
    /// let quad = TriMesh::new(vertices, indices).unwrap();
    /// assert_eq!(quad.num_triangles(), 2);
    /// assert_eq!(quad.vertices().len(), 4);
    /// # }
    /// ```
    pub fn new(vertices: Vec<Vector>, indices: Vec<[u32; 3]>) -> Result<Self, TriMeshBuilderError> {
        Self::with_flags(vertices, indices, TriMeshFlags::empty())
    }

    /// Creates a new triangle mesh from a vertex buffer and an index buffer, and flags controlling optional properties.
    ///
    /// This is the most flexible way to create a `TriMesh`, allowing you to specify exactly
    /// which optional features should be computed. Use this when you need:
    /// - Half-edge topology for adjacency information
    /// - Connected components analysis
    /// - Pseudo-normals for robust inside/outside tests
    /// - Automatic merging of duplicate vertices
    /// - Removal of degenerate or duplicate triangles
    ///
    /// # Arguments
    ///
    /// * `vertices` - A vector of 3D points representing the mesh vertices
    /// * `indices` - A vector of triangles, where each triangle is represented by
    ///   three indices into the vertex buffer
    /// * `flags` - A combination of [`TriMeshFlags`] controlling which optional features to compute
    ///
    /// # Returns
    ///
    /// * `Ok(TriMesh)` - If the mesh was successfully created
    /// * `Err(TriMeshBuilderError)` - If the mesh is invalid or topology computation failed
    ///
    /// # Errors
    ///
    /// This function returns an error if:
    /// - The index buffer is empty
    /// - [`TriMeshFlags::HALF_EDGE_TOPOLOGY`] is set but the topology is invalid
    ///   (e.g., non-manifold edges or inconsistent orientations)
    ///
    /// # Performance
    ///
    /// - Base construction: O(n log n) for BVH building
    /// - `HALF_EDGE_TOPOLOGY`: O(n) where n is the number of triangles
    /// - `CONNECTED_COMPONENTS`: O(n) with union-find
    /// - `ORIENTED` or `FIX_INTERNAL_EDGES`: O(n) for pseudo-normal computation
    /// - `MERGE_DUPLICATE_VERTICES`: O(n) with hash map lookups
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// // Create vertices for a simple mesh
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [1, 3, 2]];
    ///
    /// // Create a mesh with half-edge topology
    /// let flags = TriMeshFlags::HALF_EDGE_TOPOLOGY;
    /// let mesh = TriMesh::with_flags(vertices.clone(), indices.clone(), flags)
    ///     .expect("Failed to create mesh");
    ///
    /// // The topology information is now available
    /// assert!(mesh.topology().is_some());
    /// # }
    /// ```
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// # let vertices = vec![
    /// #     Vector::ZERO,
    /// #     Vector::new(1.0, 0.0, 0.0),
    /// #     Vector::new(0.0, 1.0, 0.0),
    /// # ];
    /// # let indices = vec![[0, 1, 2]];
    /// // Combine multiple flags for advanced features
    /// let flags = TriMeshFlags::HALF_EDGE_TOPOLOGY
    ///     | TriMeshFlags::MERGE_DUPLICATE_VERTICES
    ///     | TriMeshFlags::DELETE_DEGENERATE_TRIANGLES;
    ///
    /// let mesh = TriMesh::with_flags(vertices, indices, flags)
    ///     .expect("Failed to create mesh");
    /// # }
    /// ```
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// # let vertices = vec![
    /// #     Vector::ZERO,
    /// #     Vector::new(1.0, 0.0, 0.0),
    /// #     Vector::new(0.0, 1.0, 0.0),
    /// # ];
    /// # let indices = vec![[0, 1, 2]];
    /// // For robust point containment tests in 3D
    /// let flags = TriMeshFlags::ORIENTED;
    /// let mesh = TriMesh::with_flags(vertices, indices, flags)
    ///     .expect("Failed to create mesh");
    ///
    /// // Pseudo-normals are now computed for accurate inside/outside tests
    /// assert!(mesh.pseudo_normals().is_some());
    /// # }
    /// ```
    pub fn with_flags(
        vertices: Vec<Vector>,
        indices: Vec<[u32; 3]>,
        flags: TriMeshFlags,
    ) -> Result<Self, TriMeshBuilderError> {
        if indices.is_empty() {
            return Err(TriMeshBuilderError::EmptyIndices);
        }

        let mut result = Self {
            bvh: Bvh::new(),
            vertices,
            indices,
            #[cfg(feature = "dim3")]
            pseudo_normals: None,
            topology: None,
            connected_components: None,
            flags: TriMeshFlags::empty(),
        };

        let _ = result.set_flags(flags);

        if result.bvh.is_empty() {
            // The BVH hasn’t been computed by `.set_flags`.
            result.rebuild_bvh();
        }

        Ok(result)
    }

    /// Sets the flags of this triangle mesh, controlling its optional associated data.
    pub fn set_flags(&mut self, flags: TriMeshFlags) -> Result<(), TopologyError> {
        let mut result = Ok(());
        let prev_indices_len = self.indices.len();

        if !flags.contains(TriMeshFlags::HALF_EDGE_TOPOLOGY) {
            self.topology = None;
        }

        #[cfg(feature = "dim3")]
        if !flags.intersects(TriMeshFlags::ORIENTED | TriMeshFlags::FIX_INTERNAL_EDGES) {
            self.pseudo_normals = None;
        }

        if !flags.contains(TriMeshFlags::CONNECTED_COMPONENTS) {
            self.connected_components = None;
        }

        let difference = flags & !self.flags;

        if difference.intersects(
            TriMeshFlags::MERGE_DUPLICATE_VERTICES
                | TriMeshFlags::DELETE_DEGENERATE_TRIANGLES
                | TriMeshFlags::DELETE_DUPLICATE_TRIANGLES,
        ) {
            self.merge_duplicate_vertices(
                flags.contains(TriMeshFlags::DELETE_DEGENERATE_TRIANGLES),
                flags.contains(TriMeshFlags::DELETE_DUPLICATE_TRIANGLES),
            )
        }

        if difference.intersects(
            TriMeshFlags::HALF_EDGE_TOPOLOGY | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES,
        ) {
            result =
                self.compute_topology(flags.contains(TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES));
        }

        #[cfg(feature = "std")]
        if difference.intersects(TriMeshFlags::CONNECTED_COMPONENTS) {
            self.compute_connected_components();
        }

        #[cfg(feature = "dim3")]
        if difference.intersects(TriMeshFlags::ORIENTED | TriMeshFlags::FIX_INTERNAL_EDGES) {
            self.compute_pseudo_normals();
        }

        if prev_indices_len != self.indices.len() {
            self.rebuild_bvh();
        }

        self.flags = flags;
        result
    }

    // TODO: support a crate like get_size2 (will require support on nalgebra too)?
    /// An approximation of the memory usage (in bytes) for this struct plus
    /// the memory it allocates dynamically.
    pub fn total_memory_size(&self) -> usize {
        size_of::<Self>() + self.heap_memory_size()
    }

    /// An approximation of the memory dynamically-allocated by this struct.
    pub fn heap_memory_size(&self) -> usize {
        // NOTE: if a new field is added to `Self`, adjust this function result.
        let Self {
            bvh,
            vertices,
            indices,
            topology,
            connected_components,
            flags: _,
            #[cfg(feature = "dim3")]
            pseudo_normals,
        } = self;
        let sz_bvh = bvh.heap_memory_size();
        let sz_vertices = vertices.capacity() * size_of::<Vector>();
        let sz_indices = indices.capacity() * size_of::<[u32; 3]>();
        #[cfg(feature = "dim3")]
        let sz_pseudo_normals = pseudo_normals
            .as_ref()
            .map(|pn| {
                pn.vertices_pseudo_normal.capacity() * size_of::<Vector>()
                    + pn.edges_pseudo_normal.capacity() * size_of::<[Vector; 3]>()
            })
            .unwrap_or(0);
        #[cfg(feature = "dim2")]
        let sz_pseudo_normals = 0;
        let sz_topology = topology
            .as_ref()
            .map(|t| {
                t.vertices.capacity() * size_of::<TopoVertex>()
                    + t.faces.capacity() * size_of::<TopoFace>()
                    + t.half_edges.capacity() * size_of::<TopoHalfEdge>()
            })
            .unwrap_or(0);
        let sz_connected_components = connected_components
            .as_ref()
            .map(|c| {
                c.face_colors.capacity() * size_of::<u32>()
                    + c.grouped_faces.capacity() * size_of::<f32>()
                    + c.ranges.capacity() * size_of::<usize>()
            })
            .unwrap_or(0);

        sz_bvh
            + sz_vertices
            + sz_indices
            + sz_pseudo_normals
            + sz_topology
            + sz_connected_components
    }

    /// Transforms in-place the vertices of this triangle mesh.
    ///
    /// Applies a rigid transformation (rotation and translation) to all vertices
    /// of the mesh. This is useful for positioning or orienting a mesh in world space.
    /// The transformation also updates the BVH and pseudo-normals (if present).
    ///
    /// # Arguments
    ///
    /// * `transform` - The isometry (rigid transformation) to apply to all vertices
    ///
    /// # Performance
    ///
    /// This operation is O(n) for transforming vertices, plus O(n log n) for
    /// rebuilding the BVH, where n is the number of triangles.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::{Vector, Pose};
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mut mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// // Translate the mesh by (10, 0, 0)
    /// let transform = Pose::translation(10.0, 0.0, 0.0);
    /// mesh.transform_vertices(&transform);
    ///
    /// // All vertices are now shifted
    /// assert_eq!(mesh.vertices()[0], Vector::new(10.0, 0.0, 0.0));
    /// assert_eq!(mesh.vertices()[1], Vector::new(11.0, 0.0, 0.0));
    /// assert_eq!(mesh.vertices()[2], Vector::new(10.0, 1.0, 0.0));
    /// # }
    /// ```
    pub fn transform_vertices(&mut self, transform: &Pose) {
        self.vertices
            .iter_mut()
            .for_each(|pt| *pt = transform * *pt);
        self.rebuild_bvh();

        // The pseudo-normals must be rotated too.
        #[cfg(feature = "dim3")]
        if let Some(pseudo_normals) = &mut self.pseudo_normals {
            pseudo_normals
                .vertices_pseudo_normal
                .iter_mut()
                .for_each(|n| *n = transform.rotation * *n);
            pseudo_normals.edges_pseudo_normal.iter_mut().for_each(|n| {
                n[0] = transform.rotation * n[0];
                n[1] = transform.rotation * n[1];
                n[2] = transform.rotation * n[2];
            });
        }
    }

    /// Returns a scaled version of this triangle mesh.
    ///
    /// Creates a new mesh with all vertices scaled by the given per-axis scale factors.
    /// Unlike rigid transformations, scaling can change the shape of the mesh (when
    /// scale factors differ between axes).
    ///
    /// The scaling also updates pseudo-normals (if present) to remain valid after
    /// the transformation, and efficiently scales the BVH structure.
    ///
    /// # Arguments
    ///
    /// * `scale` - The scale factors for each axis (x, y, z in 3D)
    ///
    /// # Returns
    ///
    /// A new `TriMesh` with scaled vertices
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// // Uniform scaling: double all dimensions
    /// let scaled = mesh.clone().scaled(Vector::new(2.0, 2.0, 2.0));
    /// assert_eq!(scaled.vertices()[1], Vector::new(2.0, 0.0, 0.0));
    ///
    /// // Non-uniform scaling: stretch along X axis
    /// let stretched = mesh.scaled(Vector::new(3.0, 1.0, 1.0));
    /// assert_eq!(stretched.vertices()[1], Vector::new(3.0, 0.0, 0.0));
    /// assert_eq!(stretched.vertices()[2], Vector::new(0.0, 1.0, 0.0));
    /// # }
    /// ```
    pub fn scaled(mut self, scale: Vector) -> Self {
        self.vertices.iter_mut().for_each(|pt| *pt *= scale);

        #[cfg(feature = "dim3")]
        if let Some(pn) = &mut self.pseudo_normals {
            pn.vertices_pseudo_normal.iter_mut().for_each(|n| {
                *n *= scale;
                *n = n.normalize_or(*n);
            });
            pn.edges_pseudo_normal.iter_mut().for_each(|n| {
                n[0] *= scale;
                n[1] *= scale;
                n[2] *= scale;

                n[0] = n[0].normalize_or(n[0]);
                n[1] = n[1].normalize_or(n[1]);
                n[2] = n[2].normalize_or(n[2]);
            });
        }

        let mut bvh = self.bvh.clone();
        bvh.scale(scale);

        Self {
            bvh,
            vertices: self.vertices,
            indices: self.indices,
            #[cfg(feature = "dim3")]
            pseudo_normals: self.pseudo_normals,
            topology: self.topology,
            connected_components: self.connected_components,
            flags: self.flags,
        }
    }

    /// Appends a second triangle mesh to this triangle mesh.
    ///
    /// This combines two meshes into one by adding all vertices and triangles from
    /// the `rhs` mesh to this mesh. The vertex indices in the appended triangles
    /// are automatically adjusted to reference the correct vertices in the combined
    /// vertex buffer.
    ///
    /// After appending, all optional features (topology, pseudo-normals, etc.) are
    /// recomputed according to this mesh's current flags.
    ///
    /// # Arguments
    ///
    /// * `rhs` - The mesh to append to this mesh
    ///
    /// # Performance
    ///
    /// This operation rebuilds the entire mesh with all its features, which can be
    /// expensive for large meshes. Time complexity is O(n log n) where n is the
    /// total number of triangles after appending.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// // Create first mesh (a triangle)
    /// let vertices1 = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices1 = vec![[0, 1, 2]];
    /// let mut mesh1 = TriMesh::new(vertices1, indices1).unwrap();
    ///
    /// // Create second mesh (another triangle, offset in space)
    /// let vertices2 = vec![
    ///     Vector::new(2.0, 0.0, 0.0),
    ///     Vector::new(3.0, 0.0, 0.0),
    ///     Vector::new(2.0, 1.0, 0.0),
    /// ];
    /// let indices2 = vec![[0, 1, 2]];
    /// let mesh2 = TriMesh::new(vertices2, indices2).unwrap();
    ///
    /// // Append second mesh to first
    /// mesh1.append(&mesh2);
    ///
    /// assert_eq!(mesh1.num_triangles(), 2);
    /// assert_eq!(mesh1.vertices().len(), 6);
    /// # }
    /// ```
    pub fn append(&mut self, rhs: &TriMesh) {
        let base_id = self.vertices.len() as u32;
        self.vertices.extend_from_slice(rhs.vertices());
        self.indices.extend(
            rhs.indices()
                .iter()
                .map(|idx| [idx[0] + base_id, idx[1] + base_id, idx[2] + base_id]),
        );

        let vertices = core::mem::take(&mut self.vertices);
        let indices = core::mem::take(&mut self.indices);
        *self = TriMesh::with_flags(vertices, indices, self.flags).unwrap();
    }

    /// Create a `TriMesh` from a set of points assumed to describe a counter-clockwise non-convex polygon.
    ///
    /// This function triangulates a 2D polygon using ear clipping algorithm. The polygon
    /// can be convex or concave, but must be simple (no self-intersections) and have
    /// counter-clockwise winding order.
    ///
    /// # Arguments
    ///
    /// * `vertices` - The vertices of the polygon in counter-clockwise order
    ///
    /// # Returns
    ///
    /// * `Some(TriMesh)` - If triangulation succeeded
    /// * `None` - If the polygon is invalid (self-intersecting, degenerate, etc.)
    ///
    /// # Requirements
    ///
    /// - Polygon must be simple (no self-intersections)
    /// - Vertices must be in counter-clockwise order
    /// - Polygon must have non-zero area
    /// - Only available in 2D (`dim2` feature)
    ///
    /// # Performance
    ///
    /// Ear clipping has O(n²) time complexity in the worst case, where n is the
    /// number of vertices.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim2", feature = "f32"))] {
    /// use parry2d::shape::TriMesh;
    /// use parry2d::math::Vector;
    ///
    /// // Create a simple concave polygon (L-shape)
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(2.0, 0.0),
    ///     Vector::new(2.0, 1.0),
    ///     Vector::new(1.0, 1.0),
    ///     Vector::new(1.0, 2.0),
    ///     Vector::new(0.0, 2.0),
    /// ];
    ///
    /// let mesh = TriMesh::from_polygon(vertices)
    ///     .expect("Failed to triangulate polygon");
    ///
    /// // The polygon has been triangulated
    /// assert!(mesh.num_triangles() > 0);
    /// # }
    /// ```
    #[cfg(feature = "dim2")]
    pub fn from_polygon(vertices: Vec<Vector>) -> Option<Self> {
        triangulate_ear_clipping(&vertices).map(|indices| Self::new(vertices, indices).unwrap())
    }

    /// A flat view of the index buffer of this mesh.
    ///
    /// Returns the triangle indices as a flat array of `u32` values, where every
    /// three consecutive values form a triangle. This is useful for interfacing
    /// with graphics APIs that expect flat index buffers.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [1, 3, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// let flat = mesh.flat_indices();
    /// assert_eq!(flat, &[0, 1, 2, 1, 3, 2]);
    /// assert_eq!(flat.len(), mesh.num_triangles() * 3);
    /// # }
    /// ```
    pub fn flat_indices(&self) -> &[u32] {
        unsafe {
            let len = self.indices.len() * 3;
            let data = self.indices.as_ptr() as *const u32;
            core::slice::from_raw_parts(data, len)
        }
    }

    fn rebuild_bvh(&mut self) {
        let leaves = self.indices.iter().enumerate().map(|(i, idx)| {
            let aabb = Triangle::new(
                self.vertices[idx[0] as usize],
                self.vertices[idx[1] as usize],
                self.vertices[idx[2] as usize],
            )
            .local_aabb();
            (i, aabb)
        });

        self.bvh = Bvh::from_iter(BvhBuildStrategy::Binned, leaves)
    }

    /// Reverse the orientation of the triangle mesh.
    ///
    /// This flips the winding order of all triangles, effectively turning the mesh
    /// "inside-out". If triangles had counter-clockwise winding (outward normals),
    /// they will have clockwise winding (inward normals) after this operation.
    ///
    /// This is useful when:
    /// - A mesh was imported with incorrect orientation
    /// - You need to flip normals for rendering or physics
    /// - Creating the "back side" of a mesh
    ///
    /// # Performance
    ///
    /// This operation modifies triangles in-place (O(n)), but recomputes topology
    /// and pseudo-normals if they were present, which can be expensive for large meshes.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    ///
    /// let mut mesh = TriMesh::new(vertices, indices).unwrap();
    /// let original_triangle = mesh.triangle(0);
    ///
    /// // Reverse the mesh orientation
    /// mesh.reverse();
    ///
    /// let reversed_triangle = mesh.triangle(0);
    ///
    /// // The first two vertices are swapped
    /// assert_eq!(original_triangle.a, reversed_triangle.b);
    /// assert_eq!(original_triangle.b, reversed_triangle.a);
    /// assert_eq!(original_triangle.c, reversed_triangle.c);
    /// # }
    /// ```
    pub fn reverse(&mut self) {
        self.indices.iter_mut().for_each(|idx| idx.swap(0, 1));

        // NOTE: the BVH, and connected components are not changed by this operation.
        //       The pseudo-normals just have to be flipped.
        //       The topology must be recomputed.

        #[cfg(feature = "dim3")]
        if let Some(pseudo_normals) = &mut self.pseudo_normals {
            for n in &mut pseudo_normals.vertices_pseudo_normal {
                *n = -*n;
            }

            for n in pseudo_normals.edges_pseudo_normal.iter_mut() {
                n[0] = -n[0];
                n[1] = -n[1];
                n[2] = -n[2];
            }
        }

        if self.flags.contains(TriMeshFlags::HALF_EDGE_TOPOLOGY) {
            // TODO: this could be done more efficiently.
            let _ = self.compute_topology(false);
        }
    }

    /// Merge all duplicate vertices and adjust the index buffer accordingly.
    ///
    /// If `delete_degenerate_triangles` is set to true, any triangle with two
    /// identical vertices will be removed.
    ///
    /// This is typically used to recover a vertex buffer from which we can deduce
    /// adjacency information. between triangles by observing how the vertices are
    /// shared by triangles based on the index buffer.
    fn merge_duplicate_vertices(
        &mut self,
        delete_degenerate_triangles: bool,
        delete_duplicate_triangles: bool,
    ) {
        let mut vtx_to_id = HashMap::default();
        let mut new_vertices = Vec::with_capacity(self.vertices.len());
        let mut new_indices = Vec::with_capacity(self.indices.len());
        let mut triangle_set = HashSet::default();

        fn resolve_coord_id(
            coord: Vector,
            vtx_to_id: &mut HashMap<HashablePartialEq<Vector>, u32>,
            new_vertices: &mut Vec<Vector>,
        ) -> u32 {
            let key = HashablePartialEq::new(coord);
            let id = match vtx_to_id.entry(key) {
                Entry::Occupied(entry) => entry.into_mut(),
                Entry::Vacant(entry) => entry.insert(new_vertices.len() as u32),
            };

            if *id == new_vertices.len() as u32 {
                new_vertices.push(coord);
            }

            *id
        }

        for t in self.indices.iter() {
            let va = resolve_coord_id(
                self.vertices[t[0] as usize],
                &mut vtx_to_id,
                &mut new_vertices,
            );

            let vb = resolve_coord_id(
                self.vertices[t[1] as usize],
                &mut vtx_to_id,
                &mut new_vertices,
            );

            let vc = resolve_coord_id(
                self.vertices[t[2] as usize],
                &mut vtx_to_id,
                &mut new_vertices,
            );

            let is_degenerate = va == vb || va == vc || vb == vc;

            if !is_degenerate || !delete_degenerate_triangles {
                if delete_duplicate_triangles {
                    let (c, b, a) = crate::utils::sort3(&va, &vb, &vc);
                    if triangle_set.insert((*a, *b, *c)) {
                        new_indices.push([va, vb, vc])
                    }
                } else {
                    new_indices.push([va, vb, vc]);
                }
            }
        }

        new_vertices.shrink_to_fit();

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

        // Vertices and indices changed: the pseudo-normals are no longer valid.
        #[cfg(feature = "dim3")]
        if self.pseudo_normals.is_some() {
            self.compute_pseudo_normals();
        }

        // Vertices and indices changed: the topology no longer valid.
        #[cfg(feature = "dim3")]
        if self.topology.is_some() {
            let _ = self.compute_topology(false);
        }
    }

    #[cfg(feature = "dim3")]
    /// Computes the pseudo-normals used for solid point-projection.
    ///
    /// This computes the pseudo-normals needed by the point containment test described in
    /// "Signed distance computation using the angle weighted pseudonormal", Baerentzen, et al.
    /// DOI: 10.1109/TVCG.2005.49
    ///
    /// For the point-containment test to properly detect the inside of the trimesh (i.e. to return
    /// `proj.is_inside = true`), the trimesh must:
    /// - Be manifold (closed, no t-junctions, etc.)
    /// - Be oriented with outward normals.
    ///
    /// If the trimesh is correctly oriented, but is manifold everywhere except at its boundaries,
    /// then the computed pseudo-normals will provide correct point-containment test results except
    /// for points closest to the boundary of the mesh.
    ///
    /// It may be useful to call `self.remove_duplicate_vertices()` before this method, in order to fix the
    /// index buffer if some of the vertices of this trimesh are duplicated.
    fn compute_pseudo_normals(&mut self) {
        let mut vertices_pseudo_normal = vec![Vector::ZERO; self.vertices().len()];
        let mut edges_pseudo_normal = HashMap::default();
        let mut edges_multiplicity = HashMap::default();

        for idx in self.indices() {
            let vtx = self.vertices();
            let tri = Triangle::new(
                vtx[idx[0] as usize],
                vtx[idx[1] as usize],
                vtx[idx[2] as usize],
            );

            if let Some(n) = tri.normal() {
                let ang1 = (tri.b - tri.a).angle(tri.c - tri.a);
                let ang2 = (tri.a - tri.b).angle(tri.c - tri.b);
                let ang3 = (tri.b - tri.c).angle(tri.a - tri.c);

                vertices_pseudo_normal[idx[0] as usize] += n * ang1;
                vertices_pseudo_normal[idx[1] as usize] += n * ang2;
                vertices_pseudo_normal[idx[2] as usize] += n * ang3;

                let edges = [
                    SortedPair::new(idx[0], idx[1]),
                    SortedPair::new(idx[0], idx[2]),
                    SortedPair::new(idx[1], idx[2]),
                ];

                for edge in &edges {
                    let edge_n = edges_pseudo_normal.entry(*edge).or_insert(Vector::ZERO);
                    *edge_n += n; // NOTE: there is no need to multiply by the incident angle since it is always equal to PI for all the edges.
                    let edge_mult = edges_multiplicity.entry(*edge).or_insert(0);
                    *edge_mult += 1;
                }
            }
        }

        let edges_pseudo_normal = self
            .indices()
            .iter()
            .map(|idx| {
                let e0 = SortedPair::new(idx[0], idx[1]);
                let e1 = SortedPair::new(idx[1], idx[2]);
                let e2 = SortedPair::new(idx[2], idx[0]);
                let default = Vector::ZERO;
                [
                    edges_pseudo_normal.get(&e0).copied().unwrap_or(default),
                    edges_pseudo_normal.get(&e1).copied().unwrap_or(default),
                    edges_pseudo_normal.get(&e2).copied().unwrap_or(default),
                ]
            })
            .collect();

        self.pseudo_normals = Some(TriMeshPseudoNormals {
            vertices_pseudo_normal,
            edges_pseudo_normal,
        })
    }

    fn delete_bad_topology_triangles(&mut self) {
        let mut half_edge_set = HashSet::default();
        let mut deleted_any = false;

        // First, create three half-edges for each face.
        self.indices.retain(|idx| {
            if idx[0] == idx[1] || idx[0] == idx[2] || idx[1] == idx[2] {
                deleted_any = true;
                return false;
            }

            for k in 0..3 {
                let edge_key = (idx[k as usize], idx[(k as usize + 1) % 3]);
                if half_edge_set.contains(&edge_key) {
                    deleted_any = true;
                    return false;
                }
            }

            for k in 0..3 {
                let edge_key = (idx[k as usize], idx[(k as usize + 1) % 3]);
                let _ = half_edge_set.insert(edge_key);
            }

            true
        });
    }

    /// Computes half-edge topological information for this triangle mesh, based on its index buffer only.
    ///
    /// This computes the half-edge representation of this triangle mesh’s topology. This is useful for advanced
    /// geometric operations like trimesh-trimesh intersection geometry computation.
    ///
    /// It may be useful to call `self.merge_duplicate_vertices(true, true)` before this method, in order to fix the
    /// index buffer if some of the vertices of this trimesh are duplicated.
    ///
    /// # Return
    /// Returns `true` if the computation succeeded. Returns `false` if this mesh can’t have an half-edge representation
    /// because at least three faces share the same edge.
    fn compute_topology(&mut self, delete_bad_triangles: bool) -> Result<(), TopologyError> {
        if delete_bad_triangles {
            self.delete_bad_topology_triangles();
        }

        let mut topology = TriMeshTopology::default();
        let mut half_edge_map = HashMap::default();

        topology.vertices.resize(
            self.vertices.len(),
            TopoVertex {
                half_edge: u32::MAX,
            },
        );

        // First, create three half-edges for each face.
        for (fid, idx) in self.indices.iter().enumerate() {
            let half_edge_base_id = topology.half_edges.len() as u32;

            if idx[0] == idx[1] || idx[0] == idx[2] || idx[1] == idx[2] {
                return Err(TopologyError::BadTriangle(fid as u32));
            }

            for k in 0u32..3 {
                let half_edge = TopoHalfEdge {
                    next: half_edge_base_id + (k + 1) % 3,
                    // We don’t know which one it is yet.
                    // If the twin doesn’t exist, we use `u32::MAX` as
                    // it’s (invalid) index. This value can be relied on
                    // by other algorithms.
                    twin: u32::MAX,
                    vertex: idx[k as usize],
                    face: fid as u32,
                };
                topology.half_edges.push(half_edge);

                let edge_key = (idx[k as usize], idx[(k as usize + 1) % 3]);

                if let Some(existing) = half_edge_map.insert(edge_key, half_edge_base_id + k) {
                    // If the same edge already exists (with the same vertex order) then
                    // we have two triangles sharing the same but with opposite incompatible orientations.
                    return Err(TopologyError::BadAdjacentTrianglesOrientation {
                        edge: edge_key,
                        triangle1: topology.half_edges[existing as usize].face,
                        triangle2: fid as u32,
                    });
                }

                topology.vertices[idx[k as usize] as usize].half_edge = half_edge_base_id + k;
            }

            topology.faces.push(TopoFace {
                half_edge: half_edge_base_id,
            })
        }

        // Second, identify twins.
        for (key, he1) in &half_edge_map {
            if key.0 < key.1 {
                // Test, to avoid checking the same pair twice.
                if let Some(he2) = half_edge_map.get(&(key.1, key.0)) {
                    topology.half_edges[*he1 as usize].twin = *he2;
                    topology.half_edges[*he2 as usize].twin = *he1;
                }
            }
        }

        self.topology = Some(topology);

        Ok(())
    }

    // NOTE: this is private because that calculation is controlled by TriMeshFlags::CONNECTED_COMPONENTS
    // TODO: we should remove the CONNECTED_COMPONENTS flags and just have this be a free function.
    // TODO: this should be no_std compatible once ena is or once we have an alternative for it.
    #[cfg(feature = "std")]
    fn compute_connected_components(&mut self) {
        use ena::unify::{InPlaceUnificationTable, UnifyKey};

        #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
        struct IntKey(u32);

        impl UnifyKey for IntKey {
            type Value = ();
            fn index(&self) -> u32 {
                self.0
            }
            fn from_index(u: u32) -> IntKey {
                IntKey(u)
            }
            fn tag() -> &'static str {
                "IntKey"
            }
        }

        let mut ufind: InPlaceUnificationTable<IntKey> = InPlaceUnificationTable::new();
        let mut face_colors = vec![u32::MAX; self.indices.len()];
        let mut ranges = vec![0];
        let mut vertex_to_range = vec![u32::MAX; self.vertices.len()];
        let mut grouped_faces = vec![u32::MAX; self.indices.len()];
        let mut vertex_to_key = vec![IntKey(u32::MAX); self.vertices.len()];

        let mut vertex_key = |id: u32, ufind: &mut InPlaceUnificationTable<IntKey>| {
            if vertex_to_key[id as usize].0 == u32::MAX {
                let new_key = ufind.new_key(());
                vertex_to_key[id as usize] = new_key;
                new_key
            } else {
                vertex_to_key[id as usize]
            }
        };

        for idx in self.indices() {
            let keys = idx.map(|i| vertex_key(i, &mut ufind));
            ufind.union(keys[0], keys[1]);
            ufind.union(keys[1], keys[2]);
            ufind.union(keys[2], keys[0]);
        }

        for (idx, face_color) in self.indices().iter().zip(face_colors.iter_mut()) {
            debug_assert_eq!(
                ufind.find(vertex_to_key[idx[0] as usize]),
                ufind.find(vertex_to_key[idx[1] as usize])
            );
            debug_assert_eq!(
                ufind.find(vertex_to_key[idx[0] as usize]),
                ufind.find(vertex_to_key[idx[2] as usize])
            );

            let group_index = ufind.find(vertex_to_key[idx[0] as usize]).0 as usize;

            if vertex_to_range[group_index] == u32::MAX {
                // Additional range
                ranges.push(0);
                vertex_to_range[group_index] = ranges.len() as u32 - 1;
            }

            let range_id = vertex_to_range[group_index];
            ranges[range_id as usize] += 1;
            // NOTE: the range_id points to the range upper bound. The face color is the range lower bound.
            *face_color = range_id - 1;
        }

        // Cumulated sum on range indices, to find the first index faces need to be inserted into
        // for each range.
        for i in 1..ranges.len() {
            ranges[i] += ranges[i - 1];
        }

        debug_assert_eq!(*ranges.last().unwrap(), self.indices().len());

        // Group faces.
        let mut insertion_in_range_index = ranges.clone();
        for (face_id, face_color) in face_colors.iter().enumerate() {
            let insertion_index = &mut insertion_in_range_index[*face_color as usize];
            grouped_faces[*insertion_index] = face_id as u32;
            *insertion_index += 1;
        }

        self.connected_components = Some(TriMeshConnectedComponents {
            face_colors,
            grouped_faces,
            ranges,
        })
    }

    #[allow(dead_code)] // Useful for testing.
    pub(crate) fn assert_half_edge_topology_is_valid(&self) {
        let topo = self
            .topology
            .as_ref()
            .expect("No topology information found.");
        assert_eq!(self.vertices.len(), topo.vertices.len());
        assert_eq!(self.indices.len(), topo.faces.len());

        for (face_id, (face, idx)) in topo.faces.iter().zip(self.indices.iter()).enumerate() {
            let he0 = topo.half_edges[face.half_edge as usize];
            assert_eq!(he0.face, face_id as u32);
            assert_eq!(he0.vertex, idx[0]);
            let he1 = topo.half_edges[he0.next as usize];
            assert_eq!(he1.face, face_id as u32);
            assert_eq!(he1.vertex, idx[1]);
            let he2 = topo.half_edges[he1.next as usize];
            assert_eq!(he2.face, face_id as u32);
            assert_eq!(he2.vertex, idx[2]);
            assert_eq!(he2.next, face.half_edge);
        }

        for he in &topo.half_edges {
            let idx = &self.indices[he.face as usize];
            assert!(he.vertex == idx[0] || he.vertex == idx[1] || he.vertex == idx[2]);
        }
    }

    /// An iterator through all the triangles of this mesh.
    ///
    /// Returns an iterator that yields [`Triangle`] shapes representing each
    /// triangle in the mesh. Each triangle contains the actual 3D coordinates
    /// of its three vertices (not indices).
    ///
    /// # Performance
    ///
    /// The iterator performs vertex lookups on-the-fly, so iterating through
    /// all triangles is O(n) where n is the number of triangles.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [1, 3, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// // Iterate through all triangles
    /// for triangle in mesh.triangles() {
    ///     println!("Triangle: {:?}, {:?}, {:?}", triangle.a, triangle.b, triangle.c);
    /// }
    ///
    /// // Count triangles with specific properties
    /// let count = mesh.triangles()
    ///     .filter(|tri| tri.area() > 0.1)
    ///     .count();
    /// # }
    /// ```
    pub fn triangles(&self) -> impl ExactSizeIterator<Item = Triangle> + '_ {
        self.indices.iter().map(move |ids| {
            Triangle::new(
                self.vertices[ids[0] as usize],
                self.vertices[ids[1] as usize],
                self.vertices[ids[2] as usize],
            )
        })
    }

    #[cfg(feature = "dim3")]
    /// Gets the normal of the triangle represented by `feature`.
    pub fn feature_normal(&self, feature: FeatureId) -> Option<Vector> {
        match feature {
            FeatureId::Face(i) => self
                .triangle(i % self.num_triangles() as u32)
                .feature_normal(FeatureId::Face(0)),
            _ => None,
        }
    }
}

impl TriMesh {
    /// The flags of this triangle mesh.
    ///
    /// Returns the [`TriMeshFlags`] that were used to construct this mesh,
    /// indicating which optional features are enabled.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    ///
    /// let flags = TriMeshFlags::HALF_EDGE_TOPOLOGY;
    /// let mesh = TriMesh::with_flags(vertices, indices, flags).unwrap();
    ///
    /// assert!(mesh.flags().contains(TriMeshFlags::HALF_EDGE_TOPOLOGY));
    /// # }
    /// ```
    pub fn flags(&self) -> TriMeshFlags {
        self.flags
    }

    /// Compute the axis-aligned bounding box of this triangle mesh.
    ///
    /// Returns the AABB that tightly bounds all triangles of the mesh after
    /// applying the given isometry transformation.
    ///
    /// # Arguments
    ///
    /// * `pos` - The position/orientation of the mesh in world space
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::{Vector, Pose};
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// let identity = Pose::identity();
    /// let aabb = mesh.aabb(&identity);
    ///
    /// // The AABB contains all vertices
    /// assert!(aabb.contains_local_point(Vector::new(0.5, 0.5, 0.0)));
    /// # }
    /// ```
    pub fn aabb(&self, pos: &Pose) -> Aabb {
        self.bvh.root_aabb().transform_by(pos)
    }

    /// Gets the local axis-aligned bounding box of this triangle mesh.
    ///
    /// Returns the AABB in the mesh's local coordinate system (without any
    /// transformation applied). This is faster than [`TriMesh::aabb`] when
    /// no transformation is needed.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::new(-1.0, -1.0, 0.0),
    ///     Vector::new(1.0, -1.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// let aabb = mesh.local_aabb();
    /// assert_eq!(aabb.mins.x, -1.0);
    /// assert_eq!(aabb.maxs.x, 1.0);
    /// # }
    /// ```
    pub fn local_aabb(&self) -> Aabb {
        self.bvh.root_aabb()
    }

    /// The acceleration structure used by this triangle-mesh.
    ///
    /// Returns a reference to the BVH (Bounding Volume Hierarchy) that
    /// accelerates spatial queries on this mesh. The BVH is used internally
    /// for ray casting, collision detection, and other geometric queries.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// let bvh = mesh.bvh();
    /// // The BVH can be used for advanced spatial queries
    /// assert!(!bvh.is_empty());
    /// # }
    /// ```
    pub fn bvh(&self) -> &Bvh {
        &self.bvh
    }

    /// The number of triangles forming this mesh.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [1, 3, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// assert_eq!(mesh.num_triangles(), 2);
    /// # }
    /// ```
    pub fn num_triangles(&self) -> usize {
        self.indices.len()
    }

    /// Does the given feature ID identify a backface of this trimesh?
    pub fn is_backface(&self, feature: FeatureId) -> bool {
        if let FeatureId::Face(i) = feature {
            i >= self.indices.len() as u32
        } else {
            false
        }
    }

    /// Get the `i`-th triangle of this mesh.
    ///
    /// Returns a [`Triangle`] shape with the actual vertex coordinates (not indices)
    /// of the requested triangle. The triangle index must be less than the number
    /// of triangles in the mesh.
    ///
    /// # Arguments
    ///
    /// * `i` - The index of the triangle to retrieve (0-based)
    ///
    /// # Panics
    ///
    /// Panics if `i >= self.num_triangles()`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// let triangle = mesh.triangle(0);
    /// assert_eq!(triangle.a, Vector::ZERO);
    /// assert_eq!(triangle.b, Vector::new(1.0, 0.0, 0.0));
    /// assert_eq!(triangle.c, Vector::new(0.0, 1.0, 0.0));
    /// # }
    /// ```
    pub fn triangle(&self, i: u32) -> Triangle {
        let idx = self.indices[i as usize];
        Triangle::new(
            self.vertices[idx[0] as usize],
            self.vertices[idx[1] as usize],
            self.vertices[idx[2] as usize],
        )
    }

    /// Returns the pseudo-normals of one of this mesh’s triangles, if it was computed.
    ///
    /// This returns `None` if the pseudo-normals of this triangle were not computed.
    /// To have its pseudo-normals computed, be sure to set the [`TriMeshFlags`] so that
    /// they contain the [`TriMeshFlags::FIX_INTERNAL_EDGES`] flag.
    #[cfg(feature = "dim3")]
    pub fn triangle_normal_constraints(&self, i: u32) -> Option<TrianglePseudoNormals> {
        if self.flags.contains(TriMeshFlags::FIX_INTERNAL_EDGES) {
            let triangle = self.triangle(i);
            let pseudo_normals = self.pseudo_normals.as_ref()?;
            let edges_pseudo_normals = pseudo_normals.edges_pseudo_normal[i as usize];

            // TODO: could the pseudo-normal be pre-normalized instead of having to renormalize
            //       every time we need them?
            Some(TrianglePseudoNormals {
                face: triangle.normal()?,
                edges: [
                    (edges_pseudo_normals[0]).try_normalize()?,
                    (edges_pseudo_normals[1]).try_normalize()?,
                    (edges_pseudo_normals[2]).try_normalize()?,
                ],
            })
        } else {
            None
        }
    }

    #[cfg(feature = "dim2")]
    #[doc(hidden)]
    pub fn triangle_normal_constraints(&self, _i: u32) -> Option<TrianglePseudoNormals> {
        None
    }

    /// The vertex buffer of this mesh.
    ///
    /// Returns a slice containing all vertex positions as 3D points.
    /// The vertices are stored in the order they were provided during construction.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// assert_eq!(mesh.vertices().len(), 3);
    /// assert_eq!(mesh.vertices()[0], Vector::ZERO);
    /// # }
    /// ```
    pub fn vertices(&self) -> &[Vector] {
        &self.vertices
    }

    /// The index buffer of this mesh.
    ///
    /// Returns a slice of triangles, where each triangle is represented as an
    /// array of three vertex indices. The indices reference positions in the
    /// vertex buffer returned by [`TriMesh::vertices`].
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::TriMesh;
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     Vector::new(1.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [1, 3, 2]];
    /// let mesh = TriMesh::new(vertices, indices).unwrap();
    ///
    /// assert_eq!(mesh.indices().len(), 2);
    /// assert_eq!(mesh.indices()[0], [0, 1, 2]);
    /// assert_eq!(mesh.indices()[1], [1, 3, 2]);
    /// # }
    /// ```
    pub fn indices(&self) -> &[[u32; 3]] {
        &self.indices
    }

    /// Returns the topology information of this trimesh, if it has been computed.
    ///
    /// Topology information includes half-edge data structures that describe
    /// adjacency relationships between vertices, edges, and faces. This is
    /// computed when the [`TriMeshFlags::HALF_EDGE_TOPOLOGY`] flag is set.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// let vertices = vec![
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2]];
    ///
    /// // Without topology flag
    /// let mesh = TriMesh::new(vertices.clone(), indices.clone()).unwrap();
    /// assert!(mesh.topology().is_none());
    ///
    /// // With topology flag
    /// let mesh = TriMesh::with_flags(
    ///     vertices,
    ///     indices,
    ///     TriMeshFlags::HALF_EDGE_TOPOLOGY
    /// ).unwrap();
    /// assert!(mesh.topology().is_some());
    /// # }
    /// ```
    pub fn topology(&self) -> Option<&TriMeshTopology> {
        self.topology.as_ref()
    }

    /// Returns the connected-component information of this trimesh, if it has been computed.
    ///
    /// Connected components represent separate, non-connected parts of the mesh.
    /// This is computed when the [`TriMeshFlags::CONNECTED_COMPONENTS`] flag is set
    /// (requires the `std` feature).
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// # #[cfg(feature = "std")] {
    /// use parry3d::shape::{TriMesh, TriMeshFlags};
    /// use parry3d::math::Vector;
    ///
    /// // Create two separate triangles (not connected)
    /// let vertices = vec![
    ///     // First triangle
    ///     Vector::ZERO,
    ///     Vector::new(1.0, 0.0, 0.0),
    ///     Vector::new(0.0, 1.0, 0.0),
    ///     // Second triangle (separate)
    ///     Vector::new(5.0, 0.0, 0.0),
    ///     Vector::new(6.0, 0.0, 0.0),
    ///     Vector::new(5.0, 1.0, 0.0),
    /// ];
    /// let indices = vec![[0, 1, 2], [3, 4, 5]];
    ///
    /// let mesh = TriMesh::with_flags(
    ///     vertices,
    ///     indices,
    ///     TriMeshFlags::CONNECTED_COMPONENTS
    /// ).unwrap();
    ///
    /// if let Some(cc) = mesh.connected_components() {
    ///     assert_eq!(cc.num_connected_components(), 2);
    /// }
    /// # }
    /// # }
    /// ```
    pub fn connected_components(&self) -> Option<&TriMeshConnectedComponents> {
        self.connected_components.as_ref()
    }

    /// Returns the connected-component of this mesh.
    ///
    /// The connected-components are returned as a set of `TriMesh` build with the given `flags`.
    pub fn connected_component_meshes(
        &self,
        flags: TriMeshFlags,
    ) -> Option<Vec<Result<TriMesh, TriMeshBuilderError>>> {
        self.connected_components()
            .map(|cc| cc.to_meshes(self, flags))
    }

    /// The pseudo-normals of this triangle mesh, if they have been computed.
    #[cfg(feature = "dim3")]
    pub fn pseudo_normals(&self) -> Option<&TriMeshPseudoNormals> {
        self.pseudo_normals.as_ref()
    }

    /// The pseudo-normals of this triangle mesh, if they have been computed **and** this mesh was
    /// marked as [`TriMeshFlags::ORIENTED`].
    #[cfg(feature = "dim3")]
    pub fn pseudo_normals_if_oriented(&self) -> Option<&TriMeshPseudoNormals> {
        if self.flags.intersects(TriMeshFlags::ORIENTED) {
            self.pseudo_normals.as_ref()
        } else {
            None
        }
    }
}

#[cfg(feature = "dim3")]
impl From<crate::shape::HeightField> for TriMesh {
    fn from(heightfield: crate::shape::HeightField) -> Self {
        let (vtx, idx) = heightfield.to_trimesh();
        TriMesh::new(vtx, idx).unwrap()
    }
}

#[cfg(feature = "dim3")]
impl From<Cuboid> for TriMesh {
    fn from(cuboid: Cuboid) -> Self {
        let (vtx, idx) = cuboid.to_trimesh();
        TriMesh::new(vtx, idx).unwrap()
    }
}

impl CompositeShape for TriMesh {
    fn map_part_at(
        &self,
        i: u32,
        f: &mut dyn FnMut(Option<&Pose>, &dyn Shape, Option<&dyn NormalConstraints>),
    ) {
        let tri = self.triangle(i);
        let normals = self.triangle_normal_constraints(i);
        f(
            None,
            &tri,
            normals.as_ref().map(|n| n as &dyn NormalConstraints),
        )
    }

    fn bvh(&self) -> &Bvh {
        &self.bvh
    }
}

impl TypedCompositeShape for TriMesh {
    type PartShape = Triangle;
    type PartNormalConstraints = TrianglePseudoNormals;

    #[inline(always)]
    fn map_typed_part_at<T>(
        &self,
        i: u32,
        mut f: impl FnMut(Option<&Pose>, &Self::PartShape, Option<&Self::PartNormalConstraints>) -> T,
    ) -> Option<T> {
        let tri = self.triangle(i);
        let pseudo_normals = self.triangle_normal_constraints(i);
        Some(f(None, &tri, pseudo_normals.as_ref()))
    }

    #[inline(always)]
    fn map_untyped_part_at<T>(
        &self,
        i: u32,
        mut f: impl FnMut(Option<&Pose>, &dyn Shape, Option<&dyn NormalConstraints>) -> T,
    ) -> Option<T> {
        let tri = self.triangle(i);
        let pseudo_normals = self.triangle_normal_constraints(i);
        Some(f(
            None,
            &tri,
            pseudo_normals.as_ref().map(|n| n as &dyn NormalConstraints),
        ))
    }
}

#[cfg(test)]
mod test {
    use crate::math::{Real, Vector};
    use crate::shape::{Cuboid, TriMesh, TriMeshFlags};

    #[test]
    fn trimesh_error_empty_indices() {
        assert!(
            TriMesh::with_flags(vec![], vec![], TriMeshFlags::empty()).is_err(),
            "A triangle mesh with no triangles is invalid."
        );
    }

    #[test]
    fn connected_components() {
        let (vtx, idx) = Cuboid::new(Vector::splat(0.5)).to_trimesh();

        // Push 10 copy of the mesh, each time pushed with an offset.
        let mut mesh = TriMesh::new(vtx.clone(), idx.clone()).unwrap();

        for i in 1..10 {
            let cc_vtx = vtx
                .iter()
                .map(|pt| *pt + Vector::splat(2.0 * i as Real))
                .collect();

            let to_append = TriMesh::new(cc_vtx, idx.clone()).unwrap();
            mesh.append(&to_append);
        }

        mesh.set_flags(TriMeshFlags::CONNECTED_COMPONENTS).unwrap();
        let connected_components = mesh.connected_components().unwrap();
        assert_eq!(connected_components.num_connected_components(), 10);

        let cc_meshes = connected_components.to_meshes(&mesh, TriMeshFlags::empty());

        for cc in cc_meshes {
            let cc = cc.unwrap();
            assert_eq!(cc.vertices.len(), vtx.len());
            assert_eq!(cc.indices.len(), idx.len());
        }
    }
}