Skip to content

Commit 9ed664e

Browse files
committed
demo: Add triangle filtering to demo and tests
For simplicity we only use non-stream version in the demo, but tests check both versions as well as winding preservation of otherwise duplicate triangles.
1 parent f24ec94 commit 9ed664e

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

demo/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,21 @@ void spatialClusterPoints(const Mesh& mesh, size_t cluster_size)
14841484
(end - start) * 1000);
14851485
}
14861486

1487+
void filterTriangles(const Mesh& mesh)
1488+
{
1489+
double start = timestamp();
1490+
1491+
std::vector<unsigned int> newindices(mesh.indices.size());
1492+
newindices.resize(meshopt_filterIndexBuffer(&newindices[0], &mesh.indices[0], mesh.indices.size(), &mesh.vertices[0], mesh.vertices.size(), sizeof(float) * 3, sizeof(Vertex)));
1493+
1494+
double end = timestamp();
1495+
1496+
printf("FilterIB : %d triangles -> %d (%.2f%% redundant) in %.2f msec\n",
1497+
int(mesh.indices.size() / 3), int(newindices.size() / 3),
1498+
double((mesh.indices.size() - newindices.size()) / 3) / double(mesh.indices.size() / 3) * 100.0,
1499+
(end - start) * 1000);
1500+
}
1501+
14871502
void tessellationAdjacency(const Mesh& mesh)
14881503
{
14891504
double start = timestamp();
@@ -1814,6 +1829,7 @@ void process(const char* path)
18141829
meshlets(copy, /* scan= */ false, /* uniform= */ true, /* flex= */ false, /* spatial= */ true);
18151830

18161831
shadow(copy);
1832+
filterTriangles(copy);
18171833
tessellationAdjacency(copy);
18181834
provoking(copy);
18191835

demo/tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,6 +2555,44 @@ static void simplifyUpdateLocked(unsigned int options)
25552555
assert(vb[3][3] == 0.2f);
25562556
}
25572557

2558+
static void filterTriangles()
2559+
{
2560+
// v0/v3 match fully; v0/v4 match on prefix only
2561+
const unsigned int vb[] = {
2562+
1, 0, 10,
2563+
2, 0, 20,
2564+
3, 0, 30,
2565+
1, 1, 10,
2566+
1, 1, 99 //
2567+
};
2568+
2569+
const unsigned int ib[] = {
2570+
0, 1, 2, //
2571+
3, 1, 2, // dup of (0,1,2)
2572+
4, 1, 2, // dup prefix
2573+
2, 1, 0, // opposite winding of (0,1,2)
2574+
0, 4, 1 // degen prefix
2575+
};
2576+
2577+
// prefix only: vertex key is first 4 bytes
2578+
unsigned int sib[15];
2579+
size_t slen = meshopt_filterIndexBuffer(sib, ib, 15, vb, 5, 4, 12);
2580+
2581+
unsigned int expecteds[] = {0, 1, 2, 2, 1, 0};
2582+
assert(slen == sizeof(expecteds) / sizeof(expecteds[0]));
2583+
assert(memcmp(sib, expecteds, sizeof(expecteds)) == 0);
2584+
2585+
// prefix+suffix: vertex key is first 4 and last 4 bytes
2586+
const meshopt_Stream streams[] = {{vb + 0, 4, 12}, {vb + 2, 4, 12}};
2587+
2588+
unsigned int mib[15];
2589+
size_t mlen = meshopt_filterIndexBufferMulti(mib, ib, 15, 5, streams, 2);
2590+
unsigned int expectedm[] = {0, 1, 2, 4, 1, 2, 2, 1, 0, 0, 4, 1};
2591+
2592+
assert(mlen == sizeof(expectedm) / sizeof(expectedm[0]));
2593+
assert(memcmp(mib, expectedm, sizeof(expectedm)) == 0);
2594+
}
2595+
25582596
static void adjacency()
25592597
{
25602598
// 0 1/4
@@ -3399,6 +3437,7 @@ void runTests()
33993437
simplifyUpdateLocked(0);
34003438
simplifyUpdateLocked(meshopt_SimplifySparse);
34013439

3440+
filterTriangles();
34023441
adjacency();
34033442
tessellation();
34043443
provoking();

0 commit comments

Comments
 (0)