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
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
|
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
#include "meshoptimizer.h"
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>
#ifndef TRACE
#define TRACE 0
#endif
#if TRACE
#include <stdio.h>
#endif
#if TRACE
#define TRACESTATS(i) stats[i]++;
#else
#define TRACESTATS(i) (void)0
#endif
// This work is based on:
// Michael Garland and Paul S. Heckbert. Surface simplification using quadric error metrics. 1997
// Michael Garland. Quadric-based polygonal surface simplification. 1999
// Peter Lindstrom. Out-of-Core Simplification of Large Polygonal Models. 2000
// Matthias Teschner, Bruno Heidelberger, Matthias Mueller, Danat Pomeranets, Markus Gross. Optimized Spatial Hashing for Collision Detection of Deformable Objects. 2003
// Peter Van Sandt, Yannis Chronis, Jignesh M. Patel. Efficiently Searching In-Memory Sorted Arrays: Revenge of the Interpolation Search? 2019
// Hugues Hoppe. New Quadric Metric for Simplifying Meshes with Appearance Attributes. 1999
namespace meshopt
{
struct EdgeAdjacency
{
struct Edge
{
unsigned int next;
unsigned int prev;
};
unsigned int* offsets;
Edge* data;
};
static void prepareEdgeAdjacency(EdgeAdjacency& adjacency, size_t index_count, size_t vertex_count, meshopt_Allocator& allocator)
{
adjacency.offsets = allocator.allocate<unsigned int>(vertex_count + 1);
adjacency.data = allocator.allocate<EdgeAdjacency::Edge>(index_count);
}
static void updateEdgeAdjacency(EdgeAdjacency& adjacency, const unsigned int* indices, size_t index_count, size_t vertex_count, const unsigned int* remap)
{
size_t face_count = index_count / 3;
unsigned int* offsets = adjacency.offsets + 1;
EdgeAdjacency::Edge* data = adjacency.data;
// fill edge counts
memset(offsets, 0, vertex_count * sizeof(unsigned int));
for (size_t i = 0; i < index_count; ++i)
{
unsigned int v = remap ? remap[indices[i]] : indices[i];
assert(v < vertex_count);
offsets[v]++;
}
// fill offset table
unsigned int offset = 0;
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int count = offsets[i];
offsets[i] = offset;
offset += count;
}
assert(offset == index_count);
// fill edge data
for (size_t i = 0; i < face_count; ++i)
{
unsigned int a = indices[i * 3 + 0], b = indices[i * 3 + 1], c = indices[i * 3 + 2];
if (remap)
{
a = remap[a];
b = remap[b];
c = remap[c];
}
data[offsets[a]].next = b;
data[offsets[a]].prev = c;
offsets[a]++;
data[offsets[b]].next = c;
data[offsets[b]].prev = a;
offsets[b]++;
data[offsets[c]].next = a;
data[offsets[c]].prev = b;
offsets[c]++;
}
// finalize offsets
adjacency.offsets[0] = 0;
assert(adjacency.offsets[vertex_count] == index_count);
}
struct PositionHasher
{
const float* vertex_positions;
size_t vertex_stride_float;
const unsigned int* sparse_remap;
size_t hash(unsigned int index) const
{
unsigned int ri = sparse_remap ? sparse_remap[index] : index;
const unsigned int* key = reinterpret_cast<const unsigned int*>(vertex_positions + ri * vertex_stride_float);
// scramble bits to make sure that integer coordinates have entropy in lower bits
unsigned int x = key[0] ^ (key[0] >> 17);
unsigned int y = key[1] ^ (key[1] >> 17);
unsigned int z = key[2] ^ (key[2] >> 17);
// Optimized Spatial Hashing for Collision Detection of Deformable Objects
return (x * 73856093) ^ (y * 19349663) ^ (z * 83492791);
}
bool equal(unsigned int lhs, unsigned int rhs) const
{
unsigned int li = sparse_remap ? sparse_remap[lhs] : lhs;
unsigned int ri = sparse_remap ? sparse_remap[rhs] : rhs;
return memcmp(vertex_positions + li * vertex_stride_float, vertex_positions + ri * vertex_stride_float, sizeof(float) * 3) == 0;
}
};
struct RemapHasher
{
unsigned int* remap;
size_t hash(unsigned int id) const
{
return id * 0x5bd1e995;
}
bool equal(unsigned int lhs, unsigned int rhs) const
{
return remap[lhs] == rhs;
}
};
static size_t hashBuckets2(size_t count)
{
size_t buckets = 1;
while (buckets < count + count / 4)
buckets *= 2;
return buckets;
}
template <typename T, typename Hash>
static T* hashLookup2(T* table, size_t buckets, const Hash& hash, const T& key, const T& empty)
{
assert(buckets > 0);
assert((buckets & (buckets - 1)) == 0);
size_t hashmod = buckets - 1;
size_t bucket = hash.hash(key) & hashmod;
for (size_t probe = 0; probe <= hashmod; ++probe)
{
T& item = table[bucket];
if (item == empty)
return &item;
if (hash.equal(item, key))
return &item;
// hash collision, quadratic probing
bucket = (bucket + probe + 1) & hashmod;
}
assert(false && "Hash table is full"); // unreachable
return NULL;
}
static void buildPositionRemap(unsigned int* remap, unsigned int* wedge, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned int* sparse_remap, meshopt_Allocator& allocator)
{
PositionHasher hasher = {vertex_positions_data, vertex_positions_stride / sizeof(float), sparse_remap};
size_t table_size = hashBuckets2(vertex_count);
unsigned int* table = allocator.allocate<unsigned int>(table_size);
memset(table, -1, table_size * sizeof(unsigned int));
// build forward remap: for each vertex, which other (canonical) vertex does it map to?
// we use position equivalence for this, and remap vertices to other existing vertices
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int index = unsigned(i);
unsigned int* entry = hashLookup2(table, table_size, hasher, index, ~0u);
if (*entry == ~0u)
*entry = index;
remap[index] = *entry;
}
// build wedge table: for each vertex, which other vertex is the next wedge that also maps to the same vertex?
// entries in table form a (cyclic) wedge loop per vertex; for manifold vertices, wedge[i] == remap[i] == i
for (size_t i = 0; i < vertex_count; ++i)
wedge[i] = unsigned(i);
for (size_t i = 0; i < vertex_count; ++i)
if (remap[i] != i)
{
unsigned int r = remap[i];
wedge[i] = wedge[r];
wedge[r] = unsigned(i);
}
allocator.deallocate(table);
}
static unsigned int* buildSparseRemap(unsigned int* indices, size_t index_count, size_t vertex_count, size_t* out_vertex_count, meshopt_Allocator& allocator)
{
// use a bit set to compute the precise number of unique vertices
unsigned char* filter = allocator.allocate<unsigned char>((vertex_count + 7) / 8);
memset(filter, 0, (vertex_count + 7) / 8);
size_t unique = 0;
for (size_t i = 0; i < index_count; ++i)
{
unsigned int index = indices[i];
assert(index < vertex_count);
unique += (filter[index / 8] & (1 << (index % 8))) == 0;
filter[index / 8] |= 1 << (index % 8);
}
unsigned int* remap = allocator.allocate<unsigned int>(unique);
size_t offset = 0;
// temporary map dense => sparse; we allocate it last so that we can deallocate it
size_t revremap_size = hashBuckets2(unique);
unsigned int* revremap = allocator.allocate<unsigned int>(revremap_size);
memset(revremap, -1, revremap_size * sizeof(unsigned int));
// fill remap, using revremap as a helper, and rewrite indices in the same pass
RemapHasher hasher = {remap};
for (size_t i = 0; i < index_count; ++i)
{
unsigned int index = indices[i];
unsigned int* entry = hashLookup2(revremap, revremap_size, hasher, index, ~0u);
if (*entry == ~0u)
{
remap[offset] = index;
*entry = unsigned(offset);
offset++;
}
indices[i] = *entry;
}
allocator.deallocate(revremap);
assert(offset == unique);
*out_vertex_count = unique;
return remap;
}
enum VertexKind
{
Kind_Manifold, // not on an attribute seam, not on any boundary
Kind_Border, // not on an attribute seam, has exactly two open edges
Kind_Seam, // on an attribute seam with exactly two attribute seam edges
Kind_Complex, // none of the above; these vertices can move as long as all wedges move to the target vertex
Kind_Locked, // none of the above; these vertices can't move
Kind_Count
};
// manifold vertices can collapse onto anything
// border/seam vertices can collapse onto border/seam respectively, or locked
// complex vertices can collapse onto complex/locked
// a rule of thumb is that collapsing kind A into kind B preserves the kind B in the target vertex
// for example, while we could collapse Complex into Manifold, this would mean the target vertex isn't Manifold anymore
const unsigned char kCanCollapse[Kind_Count][Kind_Count] = {
{1, 1, 1, 1, 1},
{0, 1, 0, 0, 1},
{0, 0, 1, 0, 1},
{0, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
};
// if a vertex is manifold or seam, adjoining edges are guaranteed to have an opposite edge
// note that for seam edges, the opposite edge isn't present in the attribute-based topology
// but is present if you consider a position-only mesh variant
const unsigned char kHasOpposite[Kind_Count][Kind_Count] = {
{1, 1, 1, 0, 1},
{1, 0, 1, 0, 0},
{1, 1, 1, 0, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 0},
};
static bool hasEdge(const EdgeAdjacency& adjacency, unsigned int a, unsigned int b)
{
unsigned int count = adjacency.offsets[a + 1] - adjacency.offsets[a];
const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[a];
for (size_t i = 0; i < count; ++i)
if (edges[i].next == b)
return true;
return false;
}
static void classifyVertices(unsigned char* result, unsigned int* loop, unsigned int* loopback, size_t vertex_count, const EdgeAdjacency& adjacency, const unsigned int* remap, const unsigned int* wedge, const unsigned char* vertex_lock, const unsigned int* sparse_remap, unsigned int options)
{
memset(loop, -1, vertex_count * sizeof(unsigned int));
memset(loopback, -1, vertex_count * sizeof(unsigned int));
// incoming & outgoing open edges: ~0u if no open edges, i if there are more than 1
// note that this is the same data as required in loop[] arrays; loop[] data is only valid for border/seam
// but here it's okay to fill the data out for other types of vertices as well
unsigned int* openinc = loopback;
unsigned int* openout = loop;
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int vertex = unsigned(i);
unsigned int count = adjacency.offsets[vertex + 1] - adjacency.offsets[vertex];
const EdgeAdjacency::Edge* edges = adjacency.data + adjacency.offsets[vertex];
for (size_t j = 0; j < count; ++j)
{
unsigned int target = edges[j].next;
if (target == vertex)
{
// degenerate triangles have two distinct edges instead of three, and the self edge
// is bi-directional by definition; this can break border/seam classification by "closing"
// the open edge from another triangle and falsely marking the vertex as manifold
// instead we mark the vertex as having >1 open edges which turns it into locked/complex
openinc[vertex] = openout[vertex] = vertex;
}
else if (!hasEdge(adjacency, target, vertex))
{
openinc[target] = (openinc[target] == ~0u) ? vertex : target;
openout[vertex] = (openout[vertex] == ~0u) ? target : vertex;
}
}
}
#if TRACE
size_t stats[4] = {};
#endif
for (size_t i = 0; i < vertex_count; ++i)
{
if (remap[i] == i)
{
if (wedge[i] == i)
{
// no attribute seam, need to check if it's manifold
unsigned int openi = openinc[i], openo = openout[i];
// note: we classify any vertices with no open edges as manifold
// this is technically incorrect - if 4 triangles share an edge, we'll classify vertices as manifold
// it's unclear if this is a problem in practice
if (openi == ~0u && openo == ~0u)
{
result[i] = Kind_Manifold;
}
else if (openi != i && openo != i)
{
result[i] = Kind_Border;
}
else
{
result[i] = Kind_Locked;
TRACESTATS(0);
}
}
else if (wedge[wedge[i]] == i)
{
// attribute seam; need to distinguish between Seam and Locked
unsigned int w = wedge[i];
unsigned int openiv = openinc[i], openov = openout[i];
unsigned int openiw = openinc[w], openow = openout[w];
// seam should have one open half-edge for each vertex, and the edges need to "connect" - point to the same vertex post-remap
if (openiv != ~0u && openiv != i && openov != ~0u && openov != i &&
openiw != ~0u && openiw != w && openow != ~0u && openow != w)
{
if (remap[openiv] == remap[openow] && remap[openov] == remap[openiw] && remap[openiv] != remap[openov])
{
result[i] = Kind_Seam;
}
else
{
result[i] = Kind_Locked;
TRACESTATS(1);
}
}
else
{
result[i] = Kind_Locked;
TRACESTATS(2);
}
}
else
{
// more than one vertex maps to this one; we don't have classification available
result[i] = Kind_Locked;
TRACESTATS(3);
}
}
else
{
assert(remap[i] < i);
result[i] = result[remap[i]];
}
}
if (vertex_lock)
{
// vertex_lock may lock any wedge, not just the primary vertex, so we need to lock the primary vertex and relock any wedges
for (size_t i = 0; i < vertex_count; ++i)
if (vertex_lock[sparse_remap ? sparse_remap[i] : i])
result[remap[i]] = Kind_Locked;
for (size_t i = 0; i < vertex_count; ++i)
if (result[remap[i]] == Kind_Locked)
result[i] = Kind_Locked;
}
if (options & meshopt_SimplifyLockBorder)
for (size_t i = 0; i < vertex_count; ++i)
if (result[i] == Kind_Border)
result[i] = Kind_Locked;
#if TRACE
printf("locked: many open edges %d, disconnected seam %d, many seam edges %d, many wedges %d\n",
int(stats[0]), int(stats[1]), int(stats[2]), int(stats[3]));
#endif
}
struct Vector3
{
float x, y, z;
};
static float rescalePositions(Vector3* result, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const unsigned int* sparse_remap = NULL)
{
size_t vertex_stride_float = vertex_positions_stride / sizeof(float);
float minv[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
float maxv[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
const float* v = vertex_positions_data + ri * vertex_stride_float;
if (result)
{
result[i].x = v[0];
result[i].y = v[1];
result[i].z = v[2];
}
for (int j = 0; j < 3; ++j)
{
float vj = v[j];
minv[j] = minv[j] > vj ? vj : minv[j];
maxv[j] = maxv[j] < vj ? vj : maxv[j];
}
}
float extent = 0.f;
extent = (maxv[0] - minv[0]) < extent ? extent : (maxv[0] - minv[0]);
extent = (maxv[1] - minv[1]) < extent ? extent : (maxv[1] - minv[1]);
extent = (maxv[2] - minv[2]) < extent ? extent : (maxv[2] - minv[2]);
if (result)
{
float scale = extent == 0 ? 0.f : 1.f / extent;
for (size_t i = 0; i < vertex_count; ++i)
{
result[i].x = (result[i].x - minv[0]) * scale;
result[i].y = (result[i].y - minv[1]) * scale;
result[i].z = (result[i].z - minv[2]) * scale;
}
}
return extent;
}
static void rescaleAttributes(float* result, const float* vertex_attributes_data, size_t vertex_count, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned int* attribute_remap, const unsigned int* sparse_remap)
{
size_t vertex_attributes_stride_float = vertex_attributes_stride / sizeof(float);
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int ri = sparse_remap ? sparse_remap[i] : unsigned(i);
for (size_t k = 0; k < attribute_count; ++k)
{
unsigned int rk = attribute_remap[k];
float a = vertex_attributes_data[ri * vertex_attributes_stride_float + rk];
result[i * attribute_count + k] = a * attribute_weights[rk];
}
}
}
static const size_t kMaxAttributes = 32;
struct Quadric
{
// a00*x^2 + a11*y^2 + a22*z^2 + 2*(a10*xy + a20*xz + a21*yz) + b0*x + b1*y + b2*z + c
float a00, a11, a22;
float a10, a20, a21;
float b0, b1, b2, c;
float w;
};
struct QuadricGrad
{
// gx*x + gy*y + gz*z + gw
float gx, gy, gz, gw;
};
struct Reservoir
{
float x, y, z;
float r, g, b;
float w;
};
struct Collapse
{
unsigned int v0;
unsigned int v1;
union
{
unsigned int bidi;
float error;
unsigned int errorui;
};
};
static float normalize(Vector3& v)
{
float length = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
if (length > 0)
{
v.x /= length;
v.y /= length;
v.z /= length;
}
return length;
}
static void quadricAdd(Quadric& Q, const Quadric& R)
{
Q.a00 += R.a00;
Q.a11 += R.a11;
Q.a22 += R.a22;
Q.a10 += R.a10;
Q.a20 += R.a20;
Q.a21 += R.a21;
Q.b0 += R.b0;
Q.b1 += R.b1;
Q.b2 += R.b2;
Q.c += R.c;
Q.w += R.w;
}
static void quadricAdd(QuadricGrad* G, const QuadricGrad* R, size_t attribute_count)
{
for (size_t k = 0; k < attribute_count; ++k)
{
G[k].gx += R[k].gx;
G[k].gy += R[k].gy;
G[k].gz += R[k].gz;
G[k].gw += R[k].gw;
}
}
static float quadricEval(const Quadric& Q, const Vector3& v)
{
float rx = Q.b0;
float ry = Q.b1;
float rz = Q.b2;
rx += Q.a10 * v.y;
ry += Q.a21 * v.z;
rz += Q.a20 * v.x;
rx *= 2;
ry *= 2;
rz *= 2;
rx += Q.a00 * v.x;
ry += Q.a11 * v.y;
rz += Q.a22 * v.z;
float r = Q.c;
r += rx * v.x;
r += ry * v.y;
r += rz * v.z;
return r;
}
static float quadricError(const Quadric& Q, const Vector3& v)
{
float r = quadricEval(Q, v);
float s = Q.w == 0.f ? 0.f : 1.f / Q.w;
return fabsf(r) * s;
}
static float quadricError(const Quadric& Q, const QuadricGrad* G, size_t attribute_count, const Vector3& v, const float* va)
{
float r = quadricEval(Q, v);
// see quadricFromAttributes for general derivation; here we need to add the parts of (eval(pos) - attr)^2 that depend on attr
for (size_t k = 0; k < attribute_count; ++k)
{
float a = va[k];
float g = v.x * G[k].gx + v.y * G[k].gy + v.z * G[k].gz + G[k].gw;
r += a * (a * Q.w - 2 * g);
}
// note: unlike position error, we do not normalize by Q.w to retain edge scaling as described in quadricFromAttributes
return fabsf(r);
}
static void quadricFromPlane(Quadric& Q, float a, float b, float c, float d, float w)
{
float aw = a * w;
float bw = b * w;
float cw = c * w;
float dw = d * w;
Q.a00 = a * aw;
Q.a11 = b * bw;
Q.a22 = c * cw;
Q.a10 = a * bw;
Q.a20 = a * cw;
Q.a21 = b * cw;
Q.b0 = a * dw;
Q.b1 = b * dw;
Q.b2 = c * dw;
Q.c = d * dw;
Q.w = w;
}
static void quadricFromTriangle(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
{
Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
// normal = cross(p1 - p0, p2 - p0)
Vector3 normal = {p10.y * p20.z - p10.z * p20.y, p10.z * p20.x - p10.x * p20.z, p10.x * p20.y - p10.y * p20.x};
float area = normalize(normal);
float distance = normal.x * p0.x + normal.y * p0.y + normal.z * p0.z;
// we use sqrtf(area) so that the error is scaled linearly; this tends to improve silhouettes
quadricFromPlane(Q, normal.x, normal.y, normal.z, -distance, sqrtf(area) * weight);
}
static void quadricFromTriangleEdge(Quadric& Q, const Vector3& p0, const Vector3& p1, const Vector3& p2, float weight)
{
Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
// edge length; keep squared length around for projection correction
float lengthsq = p10.x * p10.x + p10.y * p10.y + p10.z * p10.z;
float length = sqrtf(lengthsq);
// p20p = length of projection of p2-p0 onto p1-p0; note that p10 is unnormalized so we need to correct it later
Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
float p20p = p20.x * p10.x + p20.y * p10.y + p20.z * p10.z;
// perp = perpendicular vector from p2 to line segment p1-p0
// note: since p10 is unnormalized we need to correct the projection; we scale p20 instead to take advantage of normalize below
Vector3 perp = {p20.x * lengthsq - p10.x * p20p, p20.y * lengthsq - p10.y * p20p, p20.z * lengthsq - p10.z * p20p};
normalize(perp);
float distance = perp.x * p0.x + perp.y * p0.y + perp.z * p0.z;
// note: the weight is scaled linearly with edge length; this has to match the triangle weight
quadricFromPlane(Q, perp.x, perp.y, perp.z, -distance, length * weight);
}
static void quadricFromAttributes(Quadric& Q, QuadricGrad* G, const Vector3& p0, const Vector3& p1, const Vector3& p2, const float* va0, const float* va1, const float* va2, size_t attribute_count)
{
// for each attribute we want to encode the following function into the quadric:
// (eval(pos) - attr)^2
// where eval(pos) interpolates attribute across the triangle like so:
// eval(pos) = pos.x * gx + pos.y * gy + pos.z * gz + gw
// where gx/gy/gz/gw are gradients
Vector3 p10 = {p1.x - p0.x, p1.y - p0.y, p1.z - p0.z};
Vector3 p20 = {p2.x - p0.x, p2.y - p0.y, p2.z - p0.z};
// normal = cross(p1 - p0, p2 - p0)
Vector3 normal = {p10.y * p20.z - p10.z * p20.y, p10.z * p20.x - p10.x * p20.z, p10.x * p20.y - p10.y * p20.x};
float area = sqrtf(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z) * 0.5f;
// quadric is weighted with the square of edge length (= area)
// this equalizes the units with the positional error (which, after normalization, is a square of distance)
// as a result, a change in weighted attribute of 1 along distance d is approximately equivalent to a change in position of d
float w = area;
// we compute gradients using barycentric coordinates; barycentric coordinates can be computed as follows:
// v = (d11 * d20 - d01 * d21) / denom
// w = (d00 * d21 - d01 * d20) / denom
// u = 1 - v - w
// here v0, v1 are triangle edge vectors, v2 is a vector from point to triangle corner, and dij = dot(vi, vj)
// note: v2 and d20/d21 can not be evaluated here as v2 is effectively an unknown variable; we need these only as variables for derivation of gradients
const Vector3& v0 = p10;
const Vector3& v1 = p20;
float d00 = v0.x * v0.x + v0.y * v0.y + v0.z * v0.z;
float d01 = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
float d11 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
float denom = d00 * d11 - d01 * d01;
float denomr = denom == 0 ? 0.f : 1.f / denom;
// precompute gradient factors
// these are derived by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w and factoring out expressions that are shared between attributes
float gx1 = (d11 * v0.x - d01 * v1.x) * denomr;
float gx2 = (d00 * v1.x - d01 * v0.x) * denomr;
float gy1 = (d11 * v0.y - d01 * v1.y) * denomr;
float gy2 = (d00 * v1.y - d01 * v0.y) * denomr;
float gz1 = (d11 * v0.z - d01 * v1.z) * denomr;
float gz2 = (d00 * v1.z - d01 * v0.z) * denomr;
memset(&Q, 0, sizeof(Quadric));
Q.w = w;
for (size_t k = 0; k < attribute_count; ++k)
{
float a0 = va0[k], a1 = va1[k], a2 = va2[k];
// compute gradient of eval(pos) for x/y/z/w
// the formulas below are obtained by directly computing derivative of eval(pos) = a0 * u + a1 * v + a2 * w
float gx = gx1 * (a1 - a0) + gx2 * (a2 - a0);
float gy = gy1 * (a1 - a0) + gy2 * (a2 - a0);
float gz = gz1 * (a1 - a0) + gz2 * (a2 - a0);
float gw = a0 - p0.x * gx - p0.y * gy - p0.z * gz;
// quadric encodes (eval(pos)-attr)^2; this means that the resulting expansion needs to compute, for example, pos.x * pos.y * K
// since quadrics already encode factors for pos.x * pos.y, we can accumulate almost everything in basic quadric fields
// note: for simplicity we scale all factors by weight here instead of outside the loop
Q.a00 += w * (gx * gx);
Q.a11 += w * (gy * gy);
Q.a22 += w * (gz * gz);
Q.a10 += w * (gy * gx);
Q.a20 += w * (gz * gx);
Q.a21 += w * (gz * gy);
Q.b0 += w * (gx * gw);
Q.b1 += w * (gy * gw);
Q.b2 += w * (gz * gw);
Q.c += w * (gw * gw);
// the only remaining sum components are ones that depend on attr; these will be addded during error evaluation, see quadricError
G[k].gx = w * gx;
G[k].gy = w * gy;
G[k].gz = w * gz;
G[k].gw = w * gw;
}
}
static void fillFaceQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap)
{
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int i0 = indices[i + 0];
unsigned int i1 = indices[i + 1];
unsigned int i2 = indices[i + 2];
Quadric Q;
quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], 1.f);
quadricAdd(vertex_quadrics[remap[i0]], Q);
quadricAdd(vertex_quadrics[remap[i1]], Q);
quadricAdd(vertex_quadrics[remap[i2]], Q);
}
}
static void fillEdgeQuadrics(Quadric* vertex_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* remap, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback)
{
for (size_t i = 0; i < index_count; i += 3)
{
static const int next[4] = {1, 2, 0, 1};
for (int e = 0; e < 3; ++e)
{
unsigned int i0 = indices[i + e];
unsigned int i1 = indices[i + next[e]];
unsigned char k0 = vertex_kind[i0];
unsigned char k1 = vertex_kind[i1];
// check that either i0 or i1 are border/seam and are on the same edge loop
// note that we need to add the error even for edged that connect e.g. border & locked
// if we don't do that, the adjacent border->border edge won't have correct errors for corners
if (k0 != Kind_Border && k0 != Kind_Seam && k1 != Kind_Border && k1 != Kind_Seam)
continue;
if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
continue;
if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
continue;
// seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
continue;
unsigned int i2 = indices[i + next[e + 1]];
// we try hard to maintain border edge geometry; seam edges can move more freely
// due to topological restrictions on collapses, seam quadrics slightly improves collapse structure but aren't critical
const float kEdgeWeightSeam = 1.f;
const float kEdgeWeightBorder = 10.f;
float edgeWeight = (k0 == Kind_Border || k1 == Kind_Border) ? kEdgeWeightBorder : kEdgeWeightSeam;
Quadric Q;
quadricFromTriangleEdge(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], edgeWeight);
quadricAdd(vertex_quadrics[remap[i0]], Q);
quadricAdd(vertex_quadrics[remap[i1]], Q);
}
}
}
static void fillAttributeQuadrics(Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const float* vertex_attributes, size_t attribute_count)
{
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int i0 = indices[i + 0];
unsigned int i1 = indices[i + 1];
unsigned int i2 = indices[i + 2];
Quadric QA;
QuadricGrad G[kMaxAttributes];
quadricFromAttributes(QA, G, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], &vertex_attributes[i0 * attribute_count], &vertex_attributes[i1 * attribute_count], &vertex_attributes[i2 * attribute_count], attribute_count);
quadricAdd(attribute_quadrics[i0], QA);
quadricAdd(attribute_quadrics[i1], QA);
quadricAdd(attribute_quadrics[i2], QA);
quadricAdd(&attribute_gradients[i0 * attribute_count], G, attribute_count);
quadricAdd(&attribute_gradients[i1 * attribute_count], G, attribute_count);
quadricAdd(&attribute_gradients[i2 * attribute_count], G, attribute_count);
}
}
// does triangle ABC flip when C is replaced with D?
static bool hasTriangleFlip(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d)
{
Vector3 eb = {b.x - a.x, b.y - a.y, b.z - a.z};
Vector3 ec = {c.x - a.x, c.y - a.y, c.z - a.z};
Vector3 ed = {d.x - a.x, d.y - a.y, d.z - a.z};
Vector3 nbc = {eb.y * ec.z - eb.z * ec.y, eb.z * ec.x - eb.x * ec.z, eb.x * ec.y - eb.y * ec.x};
Vector3 nbd = {eb.y * ed.z - eb.z * ed.y, eb.z * ed.x - eb.x * ed.z, eb.x * ed.y - eb.y * ed.x};
float ndp = nbc.x * nbd.x + nbc.y * nbd.y + nbc.z * nbd.z;
float abc = nbc.x * nbc.x + nbc.y * nbc.y + nbc.z * nbc.z;
float abd = nbd.x * nbd.x + nbd.y * nbd.y + nbd.z * nbd.z;
// scale is cos(angle); somewhat arbitrarily set to ~75 degrees
// note that the "pure" check is ndp <= 0 (90 degree cutoff) but that allows flipping through a series of close-to-90 collapses
return ndp <= 0.25f * sqrtf(abc * abd);
}
static bool hasTriangleFlips(const EdgeAdjacency& adjacency, const Vector3* vertex_positions, const unsigned int* collapse_remap, unsigned int i0, unsigned int i1)
{
assert(collapse_remap[i0] == i0);
assert(collapse_remap[i1] == i1);
const Vector3& v0 = vertex_positions[i0];
const Vector3& v1 = vertex_positions[i1];
const EdgeAdjacency::Edge* edges = &adjacency.data[adjacency.offsets[i0]];
size_t count = adjacency.offsets[i0 + 1] - adjacency.offsets[i0];
for (size_t i = 0; i < count; ++i)
{
unsigned int a = collapse_remap[edges[i].next];
unsigned int b = collapse_remap[edges[i].prev];
// skip triangles that will get collapsed by i0->i1 collapse or already got collapsed previously
if (a == i1 || b == i1 || a == b)
continue;
// early-out when at least one triangle flips due to a collapse
if (hasTriangleFlip(vertex_positions[a], vertex_positions[b], v0, v1))
{
#if TRACE >= 2
printf("edge block %d -> %d: flip welded %d %d %d\n", i0, i1, a, i0, b);
#endif
return true;
}
}
return false;
}
static size_t boundEdgeCollapses(const EdgeAdjacency& adjacency, size_t vertex_count, size_t index_count, unsigned char* vertex_kind)
{
size_t dual_count = 0;
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned char k = vertex_kind[i];
unsigned int e = adjacency.offsets[i + 1] - adjacency.offsets[i];
dual_count += (k == Kind_Manifold || k == Kind_Seam) ? e : 0;
}
assert(dual_count <= index_count);
// pad capacity by 3 so that we can check for overflow once per triangle instead of once per edge
return (index_count - dual_count / 2) + 3;
}
static size_t pickEdgeCollapses(Collapse* collapses, size_t collapse_capacity, const unsigned int* indices, size_t index_count, const unsigned int* remap, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback)
{
size_t collapse_count = 0;
for (size_t i = 0; i < index_count; i += 3)
{
static const int next[3] = {1, 2, 0};
// this should never happen as boundEdgeCollapses should give an upper bound for the collapse count, but in an unlikely event it does we can just drop extra collapses
if (collapse_count + 3 > collapse_capacity)
break;
for (int e = 0; e < 3; ++e)
{
unsigned int i0 = indices[i + e];
unsigned int i1 = indices[i + next[e]];
// this can happen either when input has a zero-length edge, or when we perform collapses for complex
// topology w/seams and collapse a manifold vertex that connects to both wedges onto one of them
// we leave edges like this alone since they may be important for preserving mesh integrity
if (remap[i0] == remap[i1])
continue;
unsigned char k0 = vertex_kind[i0];
unsigned char k1 = vertex_kind[i1];
// the edge has to be collapsible in at least one direction
if (!(kCanCollapse[k0][k1] | kCanCollapse[k1][k0]))
continue;
// manifold and seam edges should occur twice (i0->i1 and i1->i0) - skip redundant edges
if (kHasOpposite[k0][k1] && remap[i1] > remap[i0])
continue;
// two vertices are on a border or a seam, but there's no direct edge between them
// this indicates that they belong to two different edge loops and we should not collapse this edge
// loop[] tracks half edges so we only need to check i0->i1
if (k0 == k1 && (k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
continue;
if (k0 == Kind_Locked || k1 == Kind_Locked)
{
// the same check as above, but for border/seam -> locked collapses
// loop[] and loopback[] track half edges so we only need to check one of them
if ((k0 == Kind_Border || k0 == Kind_Seam) && loop[i0] != i1)
continue;
if ((k1 == Kind_Border || k1 == Kind_Seam) && loopback[i1] != i0)
continue;
}
// edge can be collapsed in either direction - we will pick the one with minimum error
// note: we evaluate error later during collapse ranking, here we just tag the edge as bidirectional
if (kCanCollapse[k0][k1] & kCanCollapse[k1][k0])
{
Collapse c = {i0, i1, {/* bidi= */ 1}};
collapses[collapse_count++] = c;
}
else
{
// edge can only be collapsed in one direction
unsigned int e0 = kCanCollapse[k0][k1] ? i0 : i1;
unsigned int e1 = kCanCollapse[k0][k1] ? i1 : i0;
Collapse c = {e0, e1, {/* bidi= */ 0}};
collapses[collapse_count++] = c;
}
}
}
return collapse_count;
}
static void rankEdgeCollapses(Collapse* collapses, size_t collapse_count, const Vector3* vertex_positions, const float* vertex_attributes, const Quadric* vertex_quadrics, const Quadric* attribute_quadrics, const QuadricGrad* attribute_gradients, size_t attribute_count, const unsigned int* remap)
{
for (size_t i = 0; i < collapse_count; ++i)
{
Collapse& c = collapses[i];
unsigned int i0 = c.v0;
unsigned int i1 = c.v1;
// most edges are bidirectional which means we need to evaluate errors for two collapses
// to keep this code branchless we just use the same edge for unidirectional edges
unsigned int j0 = c.bidi ? i1 : i0;
unsigned int j1 = c.bidi ? i0 : i1;
float ei = quadricError(vertex_quadrics[remap[i0]], vertex_positions[i1]);
float ej = quadricError(vertex_quadrics[remap[j0]], vertex_positions[j1]);
#if TRACE >= 3
float di = ei, dj = ej;
#endif
if (attribute_count)
{
// note: ideally we would evaluate max/avg of attribute errors for seam edges, but it's not clear if it's worth the extra cost
ei += quadricError(attribute_quadrics[i0], &attribute_gradients[i0 * attribute_count], attribute_count, vertex_positions[i1], &vertex_attributes[i1 * attribute_count]);
ej += quadricError(attribute_quadrics[j0], &attribute_gradients[j0 * attribute_count], attribute_count, vertex_positions[j1], &vertex_attributes[j1 * attribute_count]);
}
// pick edge direction with minimal error
c.v0 = ei <= ej ? i0 : j0;
c.v1 = ei <= ej ? i1 : j1;
c.error = ei <= ej ? ei : ej;
#if TRACE >= 3
if (i0 == j0) // c.bidi has been overwritten
printf("edge eval %d -> %d: error %f (pos %f, attr %f)\n", c.v0, c.v1,
sqrtf(c.error), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj));
else
printf("edge eval %d -> %d: error %f (pos %f, attr %f); reverse %f (pos %f, attr %f)\n", c.v0, c.v1,
sqrtf(ei <= ej ? ei : ej), sqrtf(ei <= ej ? di : dj), sqrtf(ei <= ej ? ei - di : ej - dj),
sqrtf(ei <= ej ? ej : ei), sqrtf(ei <= ej ? dj : di), sqrtf(ei <= ej ? ej - dj : ei - di));
#endif
}
}
static void sortEdgeCollapses(unsigned int* sort_order, const Collapse* collapses, size_t collapse_count)
{
// we use counting sort to order collapses by error; since the exact sort order is not as critical,
// only top 12 bits of exponent+mantissa (8 bits of exponent and 4 bits of mantissa) are used.
// to avoid excessive stack usage, we clamp the exponent range as collapses with errors much higher than 1 are not useful.
const unsigned int sort_bits = 12;
const unsigned int sort_bins = 2048 + 512; // exponent range [-127, 32)
// fill histogram for counting sort
unsigned int histogram[sort_bins];
memset(histogram, 0, sizeof(histogram));
for (size_t i = 0; i < collapse_count; ++i)
{
// skip sign bit since error is non-negative
unsigned int error = collapses[i].errorui;
unsigned int key = (error << 1) >> (32 - sort_bits);
key = key < sort_bins ? key : sort_bins - 1;
histogram[key]++;
}
// compute offsets based on histogram data
size_t histogram_sum = 0;
for (size_t i = 0; i < sort_bins; ++i)
{
size_t count = histogram[i];
histogram[i] = unsigned(histogram_sum);
histogram_sum += count;
}
assert(histogram_sum == collapse_count);
// compute sort order based on offsets
for (size_t i = 0; i < collapse_count; ++i)
{
// skip sign bit since error is non-negative
unsigned int error = collapses[i].errorui;
unsigned int key = (error << 1) >> (32 - sort_bits);
key = key < sort_bins ? key : sort_bins - 1;
sort_order[histogram[key]++] = unsigned(i);
}
}
static size_t performEdgeCollapses(unsigned int* collapse_remap, unsigned char* collapse_locked, const Collapse* collapses, size_t collapse_count, const unsigned int* collapse_order, const unsigned int* remap, const unsigned int* wedge, const unsigned char* vertex_kind, const unsigned int* loop, const unsigned int* loopback, const Vector3* vertex_positions, const EdgeAdjacency& adjacency, size_t triangle_collapse_goal, float error_limit, float& result_error)
{
size_t edge_collapses = 0;
size_t triangle_collapses = 0;
// most collapses remove 2 triangles; use this to establish a bound on the pass in terms of error limit
// note that edge_collapse_goal is an estimate; triangle_collapse_goal will be used to actually limit collapses
size_t edge_collapse_goal = triangle_collapse_goal / 2;
#if TRACE
size_t stats[7] = {};
#endif
for (size_t i = 0; i < collapse_count; ++i)
{
const Collapse& c = collapses[collapse_order[i]];
TRACESTATS(0);
if (c.error > error_limit)
{
TRACESTATS(4);
break;
}
if (triangle_collapses >= triangle_collapse_goal)
{
TRACESTATS(5);
break;
}
// we limit the error in each pass based on the error of optimal last collapse; since many collapses will be locked
// as they will share vertices with other successfull collapses, we need to increase the acceptable error by some factor
float error_goal = edge_collapse_goal < collapse_count ? 1.5f * collapses[collapse_order[edge_collapse_goal]].error : FLT_MAX;
// on average, each collapse is expected to lock 6 other collapses; to avoid degenerate passes on meshes with odd
// topology, we only abort if we got over 1/6 collapses accordingly.
if (c.error > error_goal && c.error > result_error && triangle_collapses > triangle_collapse_goal / 6)
{
TRACESTATS(6);
break;
}
unsigned int i0 = c.v0;
unsigned int i1 = c.v1;
unsigned int r0 = remap[i0];
unsigned int r1 = remap[i1];
unsigned char kind = vertex_kind[i0];
// we don't collapse vertices that had source or target vertex involved in a collapse
// it's important to not move the vertices twice since it complicates the tracking/remapping logic
// it's important to not move other vertices towards a moved vertex to preserve error since we don't re-rank collapses mid-pass
if (collapse_locked[r0] | collapse_locked[r1])
{
TRACESTATS(1);
continue;
}
if (hasTriangleFlips(adjacency, vertex_positions, collapse_remap, r0, r1))
{
// adjust collapse goal since this collapse is invalid and shouldn't factor into error goal
edge_collapse_goal++;
TRACESTATS(2);
continue;
}
#if TRACE >= 2
printf("edge commit %d -> %d: kind %d->%d, error %f\n", i0, i1, vertex_kind[i0], vertex_kind[i1], sqrtf(c.error));
#endif
assert(collapse_remap[r0] == r0);
assert(collapse_remap[r1] == r1);
if (kind == Kind_Complex)
{
// remap all vertices in the complex to the target vertex
unsigned int v = i0;
do
{
collapse_remap[v] = i1;
v = wedge[v];
} while (v != i0);
}
else if (kind == Kind_Seam)
{
// for seam collapses we need to move the seam pair together; this is a bit tricky to compute since we need to rely on edge loops as target vertex may be locked (and thus have more than two wedges)
unsigned int s0 = wedge[i0];
unsigned int s1 = loop[i0] == i1 ? loopback[s0] : loop[s0];
assert(s0 != i0 && wedge[s0] == i0);
assert(s1 != ~0u && remap[s1] == r1);
// additional asserts to verify that the seam pair is consistent
assert(kind != vertex_kind[i1] || s1 == wedge[i1]);
assert(loop[i0] == i1 || loopback[i0] == i1);
assert(loop[s0] == s1 || loopback[s0] == s1);
// note: this should never happen due to the assertion above, but when disabled if we ever hit this case we'll get a memory safety issue; for now play it safe
s1 = (s1 != ~0u) ? s1 : wedge[i1];
collapse_remap[i0] = i1;
collapse_remap[s0] = s1;
}
else
{
assert(wedge[i0] == i0);
collapse_remap[i0] = i1;
}
// note: we technically don't need to lock r1 if it's a locked vertex, as it can't move and its quadric won't be used
// however, this results in slightly worse error on some meshes because the locked collapses get an unfair advantage wrt scheduling
collapse_locked[r0] = 1;
collapse_locked[r1] = 1;
// border edges collapse 1 triangle, other edges collapse 2 or more
triangle_collapses += (kind == Kind_Border) ? 1 : 2;
edge_collapses++;
result_error = result_error < c.error ? c.error : result_error;
}
#if TRACE
float error_goal_last = edge_collapse_goal < collapse_count ? 1.5f * collapses[collapse_order[edge_collapse_goal]].error : FLT_MAX;
float error_goal_limit = error_goal_last < error_limit ? error_goal_last : error_limit;
printf("removed %d triangles, error %e (goal %e); evaluated %d/%d collapses (done %d, skipped %d, invalid %d); %s\n",
int(triangle_collapses), sqrtf(result_error), sqrtf(error_goal_limit),
int(stats[0]), int(collapse_count), int(edge_collapses), int(stats[1]), int(stats[2]),
stats[4] ? "error limit" : (stats[5] ? "count limit" : (stats[6] ? "error goal" : "out of collapses")));
#endif
return edge_collapses;
}
static void updateQuadrics(const unsigned int* collapse_remap, size_t vertex_count, Quadric* vertex_quadrics, Quadric* attribute_quadrics, QuadricGrad* attribute_gradients, size_t attribute_count, const Vector3* vertex_positions, const unsigned int* remap, float& vertex_error)
{
for (size_t i = 0; i < vertex_count; ++i)
{
if (collapse_remap[i] == i)
continue;
unsigned int i0 = unsigned(i);
unsigned int i1 = collapse_remap[i];
unsigned int r0 = remap[i0];
unsigned int r1 = remap[i1];
// ensure we only update vertex_quadrics once: primary vertex must be moved if any wedge is moved
if (i0 == r0)
quadricAdd(vertex_quadrics[r1], vertex_quadrics[r0]);
if (attribute_count)
{
quadricAdd(attribute_quadrics[i1], attribute_quadrics[i0]);
quadricAdd(&attribute_gradients[i1 * attribute_count], &attribute_gradients[i0 * attribute_count], attribute_count);
if (i0 == r0)
{
// when attributes are used, distance error needs to be recomputed as collapses don't track it; it is safe to do this after the quadric adjustment
float derr = quadricError(vertex_quadrics[r0], vertex_positions[r1]);
vertex_error = vertex_error < derr ? derr : vertex_error;
}
}
}
}
static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap)
{
size_t write = 0;
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int v0 = collapse_remap[indices[i + 0]];
unsigned int v1 = collapse_remap[indices[i + 1]];
unsigned int v2 = collapse_remap[indices[i + 2]];
// we never move the vertex twice during a single pass
assert(collapse_remap[v0] == v0);
assert(collapse_remap[v1] == v1);
assert(collapse_remap[v2] == v2);
if (v0 != v1 && v0 != v2 && v1 != v2)
{
indices[write + 0] = v0;
indices[write + 1] = v1;
indices[write + 2] = v2;
write += 3;
}
}
return write;
}
static void remapEdgeLoops(unsigned int* loop, size_t vertex_count, const unsigned int* collapse_remap)
{
for (size_t i = 0; i < vertex_count; ++i)
{
// note: this is a no-op for vertices that were remapped
// ideally we would clear the loop entries for those for consistency, even though they aren't going to be used
// however, the remapping process needs loop information for remapped vertices, so this would require a separate pass
if (loop[i] != ~0u)
{
unsigned int l = loop[i];
unsigned int r = collapse_remap[l];
// i == r is a special case when the seam edge is collapsed in a direction opposite to where loop goes
if (i == r)
loop[i] = (loop[l] != ~0u) ? collapse_remap[loop[l]] : ~0u;
else
loop[i] = r;
}
}
}
static unsigned int follow(unsigned int* parents, unsigned int index)
{
while (index != parents[index])
{
unsigned int parent = parents[index];
parents[index] = parents[parent];
index = parent;
}
return index;
}
static size_t buildComponents(unsigned int* components, size_t vertex_count, const unsigned int* indices, size_t index_count, const unsigned int* remap)
{
for (size_t i = 0; i < vertex_count; ++i)
components[i] = unsigned(i);
// compute a unique (but not sequential!) index for each component via union-find
for (size_t i = 0; i < index_count; i += 3)
{
static const int next[4] = {1, 2, 0, 1};
for (int e = 0; e < 3; ++e)
{
unsigned int i0 = indices[i + e];
unsigned int i1 = indices[i + next[e]];
unsigned int r0 = remap[i0];
unsigned int r1 = remap[i1];
r0 = follow(components, r0);
r1 = follow(components, r1);
// merge components with larger indices into components with smaller indices
// this guarantees that the root of the component is always the one with the smallest index
if (r0 != r1)
components[r0 < r1 ? r1 : r0] = r0 < r1 ? r0 : r1;
}
}
// make sure each element points to the component root *before* we renumber the components
for (size_t i = 0; i < vertex_count; ++i)
if (remap[i] == i)
components[i] = follow(components, unsigned(i));
unsigned int next_component = 0;
// renumber components using sequential indices
// a sequential pass is sufficient because component root always has the smallest index
// note: it is unsafe to use follow() in this pass because we're replacing component links with sequential indices inplace
for (size_t i = 0; i < vertex_count; ++i)
{
if (remap[i] == i)
{
unsigned int root = components[i];
assert(root <= i); // make sure we already computed the component for non-roots
components[i] = (root == i) ? next_component++ : components[root];
}
else
{
assert(remap[i] < i); // make sure we already computed the component
components[i] = components[remap[i]];
}
}
return next_component;
}
static void measureComponents(float* component_errors, size_t component_count, const unsigned int* components, const Vector3* vertex_positions, size_t vertex_count)
{
memset(component_errors, 0, component_count * 4 * sizeof(float));
// compute approximate sphere center for each component as an average
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int c = components[i];
assert(components[i] < component_count);
Vector3 v = vertex_positions[i]; // copy avoids aliasing issues
component_errors[c * 4 + 0] += v.x;
component_errors[c * 4 + 1] += v.y;
component_errors[c * 4 + 2] += v.z;
component_errors[c * 4 + 3] += 1; // weight
}
// complete the center computation, and reinitialize [3] as a radius
for (size_t i = 0; i < component_count; ++i)
{
float w = component_errors[i * 4 + 3];
float iw = w == 0.f ? 0.f : 1.f / w;
component_errors[i * 4 + 0] *= iw;
component_errors[i * 4 + 1] *= iw;
component_errors[i * 4 + 2] *= iw;
component_errors[i * 4 + 3] = 0; // radius
}
// compute squared radius for each component
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int c = components[i];
float dx = vertex_positions[i].x - component_errors[c * 4 + 0];
float dy = vertex_positions[i].y - component_errors[c * 4 + 1];
float dz = vertex_positions[i].z - component_errors[c * 4 + 2];
float r = dx * dx + dy * dy + dz * dz;
component_errors[c * 4 + 3] = component_errors[c * 4 + 3] < r ? r : component_errors[c * 4 + 3];
}
// we've used the output buffer as scratch space, so we need to move the results to proper indices
for (size_t i = 0; i < component_count; ++i)
{
#if TRACE >= 2
printf("component %d: center %f %f %f, error %e\n", int(i),
component_errors[i * 4 + 0], component_errors[i * 4 + 1], component_errors[i * 4 + 2], sqrtf(component_errors[i * 4 + 3]));
#endif
// note: we keep the squared error to make it match quadric error metric
component_errors[i] = component_errors[i * 4 + 3];
}
}
static size_t pruneComponents(unsigned int* indices, size_t index_count, const unsigned int* components, const float* component_errors, size_t component_count, float error_cutoff, float& nexterror)
{
size_t write = 0;
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int c = components[indices[i]];
assert(c == components[indices[i + 1]] && c == components[indices[i + 2]]);
if (component_errors[c] > error_cutoff)
{
indices[write + 0] = indices[i + 0];
indices[write + 1] = indices[i + 1];
indices[write + 2] = indices[i + 2];
write += 3;
}
}
#if TRACE
size_t pruned_components = 0;
for (size_t i = 0; i < component_count; ++i)
pruned_components += (component_errors[i] >= nexterror && component_errors[i] <= error_cutoff);
printf("pruned %d triangles in %d components (goal %e)\n", int((index_count - write) / 3), int(pruned_components), sqrtf(error_cutoff));
#endif
// update next error with the smallest error of the remaining components for future pruning
nexterror = FLT_MAX;
for (size_t i = 0; i < component_count; ++i)
if (component_errors[i] > error_cutoff)
nexterror = nexterror > component_errors[i] ? component_errors[i] : nexterror;
return write;
}
struct CellHasher
{
const unsigned int* vertex_ids;
size_t hash(unsigned int i) const
{
unsigned int h = vertex_ids[i];
// MurmurHash2 finalizer
h ^= h >> 13;
h *= 0x5bd1e995;
h ^= h >> 15;
return h;
}
bool equal(unsigned int lhs, unsigned int rhs) const
{
return vertex_ids[lhs] == vertex_ids[rhs];
}
};
struct IdHasher
{
size_t hash(unsigned int id) const
{
unsigned int h = id;
// MurmurHash2 finalizer
h ^= h >> 13;
h *= 0x5bd1e995;
h ^= h >> 15;
return h;
}
bool equal(unsigned int lhs, unsigned int rhs) const
{
return lhs == rhs;
}
};
struct TriangleHasher
{
const unsigned int* indices;
size_t hash(unsigned int i) const
{
const unsigned int* tri = indices + i * 3;
// Optimized Spatial Hashing for Collision Detection of Deformable Objects
return (tri[0] * 73856093) ^ (tri[1] * 19349663) ^ (tri[2] * 83492791);
}
bool equal(unsigned int lhs, unsigned int rhs) const
{
const unsigned int* lt = indices + lhs * 3;
const unsigned int* rt = indices + rhs * 3;
return lt[0] == rt[0] && lt[1] == rt[1] && lt[2] == rt[2];
}
};
static void computeVertexIds(unsigned int* vertex_ids, const Vector3* vertex_positions, size_t vertex_count, int grid_size)
{
assert(grid_size >= 1 && grid_size <= 1024);
float cell_scale = float(grid_size - 1);
for (size_t i = 0; i < vertex_count; ++i)
{
const Vector3& v = vertex_positions[i];
int xi = int(v.x * cell_scale + 0.5f);
int yi = int(v.y * cell_scale + 0.5f);
int zi = int(v.z * cell_scale + 0.5f);
vertex_ids[i] = (xi << 20) | (yi << 10) | zi;
}
}
static size_t countTriangles(const unsigned int* vertex_ids, const unsigned int* indices, size_t index_count)
{
size_t result = 0;
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int id0 = vertex_ids[indices[i + 0]];
unsigned int id1 = vertex_ids[indices[i + 1]];
unsigned int id2 = vertex_ids[indices[i + 2]];
result += (id0 != id1) & (id0 != id2) & (id1 != id2);
}
return result;
}
static size_t fillVertexCells(unsigned int* table, size_t table_size, unsigned int* vertex_cells, const unsigned int* vertex_ids, size_t vertex_count)
{
CellHasher hasher = {vertex_ids};
memset(table, -1, table_size * sizeof(unsigned int));
size_t result = 0;
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int* entry = hashLookup2(table, table_size, hasher, unsigned(i), ~0u);
if (*entry == ~0u)
{
*entry = unsigned(i);
vertex_cells[i] = unsigned(result++);
}
else
{
vertex_cells[i] = vertex_cells[*entry];
}
}
return result;
}
static size_t countVertexCells(unsigned int* table, size_t table_size, const unsigned int* vertex_ids, size_t vertex_count)
{
IdHasher hasher;
memset(table, -1, table_size * sizeof(unsigned int));
size_t result = 0;
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int id = vertex_ids[i];
unsigned int* entry = hashLookup2(table, table_size, hasher, id, ~0u);
result += (*entry == ~0u);
*entry = id;
}
return result;
}
static void fillCellQuadrics(Quadric* cell_quadrics, const unsigned int* indices, size_t index_count, const Vector3* vertex_positions, const unsigned int* vertex_cells)
{
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int i0 = indices[i + 0];
unsigned int i1 = indices[i + 1];
unsigned int i2 = indices[i + 2];
unsigned int c0 = vertex_cells[i0];
unsigned int c1 = vertex_cells[i1];
unsigned int c2 = vertex_cells[i2];
int single_cell = (c0 == c1) & (c0 == c2);
Quadric Q;
quadricFromTriangle(Q, vertex_positions[i0], vertex_positions[i1], vertex_positions[i2], single_cell ? 3.f : 1.f);
if (single_cell)
{
quadricAdd(cell_quadrics[c0], Q);
}
else
{
quadricAdd(cell_quadrics[c0], Q);
quadricAdd(cell_quadrics[c1], Q);
quadricAdd(cell_quadrics[c2], Q);
}
}
}
static void fillCellReservoirs(Reservoir* cell_reservoirs, size_t cell_count, const Vector3* vertex_positions, const float* vertex_colors, size_t vertex_colors_stride, size_t vertex_count, const unsigned int* vertex_cells)
{
static const float dummy_color[] = {0.f, 0.f, 0.f};
size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int cell = vertex_cells[i];
const Vector3& v = vertex_positions[i];
Reservoir& r = cell_reservoirs[cell];
const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
r.x += v.x;
r.y += v.y;
r.z += v.z;
r.r += color[0];
r.g += color[1];
r.b += color[2];
r.w += 1.f;
}
for (size_t i = 0; i < cell_count; ++i)
{
Reservoir& r = cell_reservoirs[i];
float iw = r.w == 0.f ? 0.f : 1.f / r.w;
r.x *= iw;
r.y *= iw;
r.z *= iw;
r.r *= iw;
r.g *= iw;
r.b *= iw;
}
}
static void fillCellRemap(unsigned int* cell_remap, float* cell_errors, size_t cell_count, const unsigned int* vertex_cells, const Quadric* cell_quadrics, const Vector3* vertex_positions, size_t vertex_count)
{
memset(cell_remap, -1, cell_count * sizeof(unsigned int));
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int cell = vertex_cells[i];
float error = quadricError(cell_quadrics[cell], vertex_positions[i]);
if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
{
cell_remap[cell] = unsigned(i);
cell_errors[cell] = error;
}
}
}
static void fillCellRemap(unsigned int* cell_remap, float* cell_errors, size_t cell_count, const unsigned int* vertex_cells, const Reservoir* cell_reservoirs, const Vector3* vertex_positions, const float* vertex_colors, size_t vertex_colors_stride, float color_weight, size_t vertex_count)
{
static const float dummy_color[] = {0.f, 0.f, 0.f};
size_t vertex_colors_stride_float = vertex_colors_stride / sizeof(float);
memset(cell_remap, -1, cell_count * sizeof(unsigned int));
for (size_t i = 0; i < vertex_count; ++i)
{
unsigned int cell = vertex_cells[i];
const Vector3& v = vertex_positions[i];
const Reservoir& r = cell_reservoirs[cell];
const float* color = vertex_colors ? &vertex_colors[i * vertex_colors_stride_float] : dummy_color;
float pos_error = (v.x - r.x) * (v.x - r.x) + (v.y - r.y) * (v.y - r.y) + (v.z - r.z) * (v.z - r.z);
float col_error = (color[0] - r.r) * (color[0] - r.r) + (color[1] - r.g) * (color[1] - r.g) + (color[2] - r.b) * (color[2] - r.b);
float error = pos_error + color_weight * col_error;
if (cell_remap[cell] == ~0u || cell_errors[cell] > error)
{
cell_remap[cell] = unsigned(i);
cell_errors[cell] = error;
}
}
}
static size_t filterTriangles(unsigned int* destination, unsigned int* tritable, size_t tritable_size, const unsigned int* indices, size_t index_count, const unsigned int* vertex_cells, const unsigned int* cell_remap)
{
TriangleHasher hasher = {destination};
memset(tritable, -1, tritable_size * sizeof(unsigned int));
size_t result = 0;
for (size_t i = 0; i < index_count; i += 3)
{
unsigned int c0 = vertex_cells[indices[i + 0]];
unsigned int c1 = vertex_cells[indices[i + 1]];
unsigned int c2 = vertex_cells[indices[i + 2]];
if (c0 != c1 && c0 != c2 && c1 != c2)
{
unsigned int a = cell_remap[c0];
unsigned int b = cell_remap[c1];
unsigned int c = cell_remap[c2];
if (b < a && b < c)
{
unsigned int t = a;
a = b, b = c, c = t;
}
else if (c < a && c < b)
{
unsigned int t = c;
c = b, b = a, a = t;
}
destination[result * 3 + 0] = a;
destination[result * 3 + 1] = b;
destination[result * 3 + 2] = c;
unsigned int* entry = hashLookup2(tritable, tritable_size, hasher, unsigned(result), ~0u);
if (*entry == ~0u)
*entry = unsigned(result++);
}
}
return result * 3;
}
static float interpolate(float y, float x0, float y0, float x1, float y1, float x2, float y2)
{
// three point interpolation from "revenge of interpolation search" paper
float num = (y1 - y) * (x1 - x2) * (x1 - x0) * (y2 - y0);
float den = (y2 - y) * (x1 - x2) * (y0 - y1) + (y0 - y) * (x1 - x0) * (y1 - y2);
return x1 + num / den;
}
} // namespace meshopt
// Note: this is only exposed for debug visualization purposes; do *not* use
enum
{
meshopt_SimplifyInternalDebug = 1 << 30
};
size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
using namespace meshopt;
assert(index_count % 3 == 0);
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
assert(vertex_positions_stride % sizeof(float) == 0);
assert(target_index_count <= index_count);
assert(target_error >= 0);
assert((options & ~(meshopt_SimplifyLockBorder | meshopt_SimplifySparse | meshopt_SimplifyErrorAbsolute | meshopt_SimplifyPrune | meshopt_SimplifyInternalDebug)) == 0);
assert(vertex_attributes_stride >= attribute_count * sizeof(float) && vertex_attributes_stride <= 256);
assert(vertex_attributes_stride % sizeof(float) == 0);
assert(attribute_count <= kMaxAttributes);
for (size_t i = 0; i < attribute_count; ++i)
assert(attribute_weights[i] >= 0);
meshopt_Allocator allocator;
unsigned int* result = destination;
if (result != indices)
memcpy(result, indices, index_count * sizeof(unsigned int));
// build an index remap and update indices/vertex_count to minimize the subsequent work
// note: as a consequence, errors will be computed relative to the subset extent
unsigned int* sparse_remap = NULL;
if (options & meshopt_SimplifySparse)
sparse_remap = buildSparseRemap(result, index_count, vertex_count, &vertex_count, allocator);
// build adjacency information
EdgeAdjacency adjacency = {};
prepareEdgeAdjacency(adjacency, index_count, vertex_count, allocator);
updateEdgeAdjacency(adjacency, result, index_count, vertex_count, NULL);
// build position remap that maps each vertex to the one with identical position
unsigned int* remap = allocator.allocate<unsigned int>(vertex_count);
unsigned int* wedge = allocator.allocate<unsigned int>(vertex_count);
buildPositionRemap(remap, wedge, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap, allocator);
// classify vertices; vertex kind determines collapse rules, see kCanCollapse
unsigned char* vertex_kind = allocator.allocate<unsigned char>(vertex_count);
unsigned int* loop = allocator.allocate<unsigned int>(vertex_count);
unsigned int* loopback = allocator.allocate<unsigned int>(vertex_count);
classifyVertices(vertex_kind, loop, loopback, vertex_count, adjacency, remap, wedge, vertex_lock, sparse_remap, options);
#if TRACE
size_t unique_positions = 0;
for (size_t i = 0; i < vertex_count; ++i)
unique_positions += remap[i] == i;
printf("position remap: %d vertices => %d positions\n", int(vertex_count), int(unique_positions));
size_t kinds[Kind_Count] = {};
for (size_t i = 0; i < vertex_count; ++i)
kinds[vertex_kind[i]] += remap[i] == i;
printf("kinds: manifold %d, border %d, seam %d, complex %d, locked %d\n",
int(kinds[Kind_Manifold]), int(kinds[Kind_Border]), int(kinds[Kind_Seam]), int(kinds[Kind_Complex]), int(kinds[Kind_Locked]));
#endif
Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
float vertex_scale = rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride, sparse_remap);
float* vertex_attributes = NULL;
if (attribute_count)
{
unsigned int attribute_remap[kMaxAttributes];
// remap attributes to only include ones with weight > 0 to minimize memory/compute overhead for quadrics
size_t attributes_used = 0;
for (size_t i = 0; i < attribute_count; ++i)
if (attribute_weights[i] > 0)
attribute_remap[attributes_used++] = unsigned(i);
attribute_count = attributes_used;
vertex_attributes = allocator.allocate<float>(vertex_count * attribute_count);
rescaleAttributes(vertex_attributes, vertex_attributes_data, vertex_count, vertex_attributes_stride, attribute_weights, attribute_count, attribute_remap, sparse_remap);
}
Quadric* vertex_quadrics = allocator.allocate<Quadric>(vertex_count);
memset(vertex_quadrics, 0, vertex_count * sizeof(Quadric));
Quadric* attribute_quadrics = NULL;
QuadricGrad* attribute_gradients = NULL;
if (attribute_count)
{
attribute_quadrics = allocator.allocate<Quadric>(vertex_count);
memset(attribute_quadrics, 0, vertex_count * sizeof(Quadric));
attribute_gradients = allocator.allocate<QuadricGrad>(vertex_count * attribute_count);
memset(attribute_gradients, 0, vertex_count * attribute_count * sizeof(QuadricGrad));
}
fillFaceQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap);
fillEdgeQuadrics(vertex_quadrics, result, index_count, vertex_positions, remap, vertex_kind, loop, loopback);
if (attribute_count)
fillAttributeQuadrics(attribute_quadrics, attribute_gradients, result, index_count, vertex_positions, vertex_attributes, attribute_count);
unsigned int* components = NULL;
float* component_errors = NULL;
size_t component_count = 0;
float component_nexterror = 0;
if (options & meshopt_SimplifyPrune)
{
components = allocator.allocate<unsigned int>(vertex_count);
component_count = buildComponents(components, vertex_count, result, index_count, remap);
component_errors = allocator.allocate<float>(component_count * 4); // overallocate for temporary use inside measureComponents
measureComponents(component_errors, component_count, components, vertex_positions, vertex_count);
component_nexterror = FLT_MAX;
for (size_t i = 0; i < component_count; ++i)
component_nexterror = component_nexterror > component_errors[i] ? component_errors[i] : component_nexterror;
#if TRACE
printf("components: %d (min error %e)\n", int(component_count), sqrtf(component_nexterror));
#endif
}
#if TRACE
size_t pass_count = 0;
#endif
size_t collapse_capacity = boundEdgeCollapses(adjacency, vertex_count, index_count, vertex_kind);
Collapse* edge_collapses = allocator.allocate<Collapse>(collapse_capacity);
unsigned int* collapse_order = allocator.allocate<unsigned int>(collapse_capacity);
unsigned int* collapse_remap = allocator.allocate<unsigned int>(vertex_count);
unsigned char* collapse_locked = allocator.allocate<unsigned char>(vertex_count);
size_t result_count = index_count;
float result_error = 0;
float vertex_error = 0;
// target_error input is linear; we need to adjust it to match quadricError units
float error_scale = (options & meshopt_SimplifyErrorAbsolute) ? vertex_scale : 1.f;
float error_limit = (target_error * target_error) / (error_scale * error_scale);
while (result_count > target_index_count)
{
// note: throughout the simplification process adjacency structure reflects welded topology for result-in-progress
updateEdgeAdjacency(adjacency, result, result_count, vertex_count, remap);
size_t edge_collapse_count = pickEdgeCollapses(edge_collapses, collapse_capacity, result, result_count, remap, vertex_kind, loop, loopback);
assert(edge_collapse_count <= collapse_capacity);
// no edges can be collapsed any more due to topology restrictions
if (edge_collapse_count == 0)
break;
#if TRACE
printf("pass %d:%c", int(pass_count++), TRACE >= 2 ? '\n' : ' ');
#endif
rankEdgeCollapses(edge_collapses, edge_collapse_count, vertex_positions, vertex_attributes, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, remap);
sortEdgeCollapses(collapse_order, edge_collapses, edge_collapse_count);
size_t triangle_collapse_goal = (result_count - target_index_count) / 3;
for (size_t i = 0; i < vertex_count; ++i)
collapse_remap[i] = unsigned(i);
memset(collapse_locked, 0, vertex_count);
size_t collapses = performEdgeCollapses(collapse_remap, collapse_locked, edge_collapses, edge_collapse_count, collapse_order, remap, wedge, vertex_kind, loop, loopback, vertex_positions, adjacency, triangle_collapse_goal, error_limit, result_error);
// no edges can be collapsed any more due to hitting the error limit or triangle collapse limit
if (collapses == 0)
break;
updateQuadrics(collapse_remap, vertex_count, vertex_quadrics, attribute_quadrics, attribute_gradients, attribute_count, vertex_positions, remap, vertex_error);
// updateQuadrics will update vertex error if we use attributes, but if we don't then result_error and vertex_error are equivalent
vertex_error = attribute_count == 0 ? result_error : vertex_error;
remapEdgeLoops(loop, vertex_count, collapse_remap);
remapEdgeLoops(loopback, vertex_count, collapse_remap);
size_t new_count = remapIndexBuffer(result, result_count, collapse_remap);
assert(new_count < result_count);
result_count = new_count;
if ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= vertex_error)
result_count = pruneComponents(result, result_count, components, component_errors, component_count, vertex_error, component_nexterror);
}
// we're done with the regular simplification but we're still short of the target; try pruning more aggressively towards error_limit
while ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= error_limit)
{
#if TRACE
printf("pass %d: cleanup; ", int(pass_count++));
#endif
float component_cutoff = component_nexterror * 1.5f < error_limit ? component_nexterror * 1.5f : error_limit;
// track maximum error in eligible components as we are increasing resulting error
float component_maxerror = 0;
for (size_t i = 0; i < component_count; ++i)
if (component_errors[i] > component_maxerror && component_errors[i] <= component_cutoff)
component_maxerror = component_errors[i];
size_t new_count = pruneComponents(result, result_count, components, component_errors, component_count, component_cutoff, component_nexterror);
if (new_count == result_count)
break;
result_count = new_count;
result_error = result_error < component_maxerror ? component_maxerror : result_error;
vertex_error = vertex_error < component_maxerror ? component_maxerror : vertex_error;
}
#if TRACE
printf("result: %d triangles, error: %e; total %d passes\n", int(result_count / 3), sqrtf(result_error), int(pass_count));
#endif
// if debug visualization data is requested, fill it instead of index data; for simplicity, this doesn't work with sparsity
if ((options & meshopt_SimplifyInternalDebug) && !sparse_remap)
{
assert(Kind_Count <= 8 && vertex_count < (1 << 28)); // 3 bit kind, 1 bit loop
for (size_t i = 0; i < result_count; i += 3)
{
unsigned int a = result[i + 0], b = result[i + 1], c = result[i + 2];
result[i + 0] |= (vertex_kind[a] << 28) | (unsigned(loop[a] == b || loopback[b] == a) << 31);
result[i + 1] |= (vertex_kind[b] << 28) | (unsigned(loop[b] == c || loopback[c] == b) << 31);
result[i + 2] |= (vertex_kind[c] << 28) | (unsigned(loop[c] == a || loopback[a] == c) << 31);
}
}
// convert resulting indices back into the dense space of the larger mesh
if (sparse_remap)
for (size_t i = 0; i < result_count; ++i)
result[i] = sparse_remap[result[i]];
// result_error is quadratic; we need to remap it back to linear
if (out_result_error)
*out_result_error = sqrtf(vertex_error) * error_scale;
return result_count;
}
size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
return meshopt_simplifyEdge(destination, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, NULL, 0, NULL, 0, NULL, target_index_count, target_error, options, out_result_error);
}
size_t meshopt_simplifyWithAttributes(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
return meshopt_simplifyEdge(destination, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, vertex_attributes_data, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, out_result_error);
}
size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* out_result_error)
{
using namespace meshopt;
assert(index_count % 3 == 0);
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
assert(vertex_positions_stride % sizeof(float) == 0);
assert(target_index_count <= index_count);
// we expect to get ~2 triangles/vertex in the output
size_t target_cell_count = target_index_count / 6;
meshopt_Allocator allocator;
Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
// find the optimal grid size using guided binary search
#if TRACE
printf("source: %d vertices, %d triangles\n", int(vertex_count), int(index_count / 3));
printf("target: %d cells, %d triangles\n", int(target_cell_count), int(target_index_count / 3));
#endif
unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
const int kInterpolationPasses = 5;
// invariant: # of triangles in min_grid <= target_count
int min_grid = int(1.f / (target_error < 1e-3f ? 1e-3f : target_error));
int max_grid = 1025;
size_t min_triangles = 0;
size_t max_triangles = index_count / 3;
// when we're error-limited, we compute the triangle count for the min. size; this accelerates convergence and provides the correct answer when we can't use a larger grid
if (min_grid > 1)
{
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
min_triangles = countTriangles(vertex_ids, indices, index_count);
}
// instead of starting in the middle, let's guess as to what the answer might be! triangle count usually grows as a square of grid size...
int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
{
if (min_triangles >= target_index_count / 3 || max_grid - min_grid <= 1)
break;
// we clamp the prediction of the grid size to make sure that the search converges
int grid_size = next_grid_size;
grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
size_t triangles = countTriangles(vertex_ids, indices, index_count);
#if TRACE
printf("pass %d (%s): grid size %d, triangles %d, %s\n",
pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses ? "lerp" : "binary"),
grid_size, int(triangles),
(triangles <= target_index_count / 3) ? "under" : "over");
#endif
float tip = interpolate(float(size_t(target_index_count / 3)), float(min_grid), float(min_triangles), float(grid_size), float(triangles), float(max_grid), float(max_triangles));
if (triangles <= target_index_count / 3)
{
min_grid = grid_size;
min_triangles = triangles;
}
else
{
max_grid = grid_size;
max_triangles = triangles;
}
// we start by using interpolation search - it usually converges faster
// however, interpolation search has a worst case of O(N) so we switch to binary search after a few iterations which converges in O(logN)
next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
}
if (min_triangles == 0)
{
if (out_result_error)
*out_result_error = 1.f;
return 0;
}
// build vertex->cell association by mapping all vertices with the same quantized position to the same cell
size_t table_size = hashBuckets2(vertex_count);
unsigned int* table = allocator.allocate<unsigned int>(table_size);
unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
// build a quadric for each target cell
Quadric* cell_quadrics = allocator.allocate<Quadric>(cell_count);
memset(cell_quadrics, 0, cell_count * sizeof(Quadric));
fillCellQuadrics(cell_quadrics, indices, index_count, vertex_positions, vertex_cells);
// for each target cell, find the vertex with the minimal error
unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
float* cell_errors = allocator.allocate<float>(cell_count);
fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_quadrics, vertex_positions, vertex_count);
// compute error
float result_error = 0.f;
for (size_t i = 0; i < cell_count; ++i)
result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
// collapse triangles!
// note that we need to filter out triangles that we've already output because we very frequently generate redundant triangles between cells :(
size_t tritable_size = hashBuckets2(min_triangles);
unsigned int* tritable = allocator.allocate<unsigned int>(tritable_size);
size_t write = filterTriangles(destination, tritable, tritable_size, indices, index_count, vertex_cells, cell_remap);
#if TRACE
printf("result: %d cells, %d triangles (%d unfiltered), error %e\n", int(cell_count), int(write / 3), int(min_triangles), sqrtf(result_error));
#endif
if (out_result_error)
*out_result_error = sqrtf(result_error);
return write;
}
size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_colors, size_t vertex_colors_stride, float color_weight, size_t target_vertex_count)
{
using namespace meshopt;
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
assert(vertex_positions_stride % sizeof(float) == 0);
assert(vertex_colors_stride == 0 || (vertex_colors_stride >= 12 && vertex_colors_stride <= 256));
assert(vertex_colors_stride % sizeof(float) == 0);
assert(vertex_colors == NULL || vertex_colors_stride != 0);
assert(target_vertex_count <= vertex_count);
size_t target_cell_count = target_vertex_count;
if (target_cell_count == 0)
return 0;
meshopt_Allocator allocator;
Vector3* vertex_positions = allocator.allocate<Vector3>(vertex_count);
rescalePositions(vertex_positions, vertex_positions_data, vertex_count, vertex_positions_stride);
// find the optimal grid size using guided binary search
#if TRACE
printf("source: %d vertices\n", int(vertex_count));
printf("target: %d cells\n", int(target_cell_count));
#endif
unsigned int* vertex_ids = allocator.allocate<unsigned int>(vertex_count);
size_t table_size = hashBuckets2(vertex_count);
unsigned int* table = allocator.allocate<unsigned int>(table_size);
const int kInterpolationPasses = 5;
// invariant: # of vertices in min_grid <= target_count
int min_grid = 0;
int max_grid = 1025;
size_t min_vertices = 0;
size_t max_vertices = vertex_count;
// instead of starting in the middle, let's guess as to what the answer might be! triangle count usually grows as a square of grid size...
int next_grid_size = int(sqrtf(float(target_cell_count)) + 0.5f);
for (int pass = 0; pass < 10 + kInterpolationPasses; ++pass)
{
assert(min_vertices < target_vertex_count);
assert(max_grid - min_grid > 1);
// we clamp the prediction of the grid size to make sure that the search converges
int grid_size = next_grid_size;
grid_size = (grid_size <= min_grid) ? min_grid + 1 : (grid_size >= max_grid ? max_grid - 1 : grid_size);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, grid_size);
size_t vertices = countVertexCells(table, table_size, vertex_ids, vertex_count);
#if TRACE
printf("pass %d (%s): grid size %d, vertices %d, %s\n",
pass, (pass == 0) ? "guess" : (pass <= kInterpolationPasses ? "lerp" : "binary"),
grid_size, int(vertices),
(vertices <= target_vertex_count) ? "under" : "over");
#endif
float tip = interpolate(float(target_vertex_count), float(min_grid), float(min_vertices), float(grid_size), float(vertices), float(max_grid), float(max_vertices));
if (vertices <= target_vertex_count)
{
min_grid = grid_size;
min_vertices = vertices;
}
else
{
max_grid = grid_size;
max_vertices = vertices;
}
if (vertices == target_vertex_count || max_grid - min_grid <= 1)
break;
// we start by using interpolation search - it usually converges faster
// however, interpolation search has a worst case of O(N) so we switch to binary search after a few iterations which converges in O(logN)
next_grid_size = (pass < kInterpolationPasses) ? int(tip + 0.5f) : (min_grid + max_grid) / 2;
}
if (min_vertices == 0)
return 0;
// build vertex->cell association by mapping all vertices with the same quantized position to the same cell
unsigned int* vertex_cells = allocator.allocate<unsigned int>(vertex_count);
computeVertexIds(vertex_ids, vertex_positions, vertex_count, min_grid);
size_t cell_count = fillVertexCells(table, table_size, vertex_cells, vertex_ids, vertex_count);
// accumulate points into a reservoir for each target cell
Reservoir* cell_reservoirs = allocator.allocate<Reservoir>(cell_count);
memset(cell_reservoirs, 0, cell_count * sizeof(Reservoir));
fillCellReservoirs(cell_reservoirs, cell_count, vertex_positions, vertex_colors, vertex_colors_stride, vertex_count, vertex_cells);
// for each target cell, find the vertex with the minimal error
unsigned int* cell_remap = allocator.allocate<unsigned int>(cell_count);
float* cell_errors = allocator.allocate<float>(cell_count);
// we scale the color weight to bring it to the same scale as position so that error addition makes sense
float color_weight_scaled = color_weight * (min_grid == 1 ? 1.f : 1.f / (min_grid - 1));
fillCellRemap(cell_remap, cell_errors, cell_count, vertex_cells, cell_reservoirs, vertex_positions, vertex_colors, vertex_colors_stride, color_weight_scaled * color_weight_scaled, vertex_count);
// copy results to the output
assert(cell_count <= target_vertex_count);
memcpy(destination, cell_remap, sizeof(unsigned int) * cell_count);
#if TRACE
// compute error
float result_error = 0.f;
for (size_t i = 0; i < cell_count; ++i)
result_error = result_error < cell_errors[i] ? cell_errors[i] : result_error;
printf("result: %d cells, %e error\n", int(cell_count), sqrtf(result_error));
#endif
return cell_count;
}
float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
{
using namespace meshopt;
assert(vertex_positions_stride >= 12 && vertex_positions_stride <= 256);
assert(vertex_positions_stride % sizeof(float) == 0);
float extent = rescalePositions(NULL, vertex_positions, vertex_count, vertex_positions_stride);
return extent;
}
|