[qfvis] Make cluster reconstruction O(N)

For most (if not all) maps. The heapsort is needed only if the clustered
leafs are not contiguous, but most bsp compilers output contiguous leaf
clusters, so is just a bit of protection. The difference isn't really
noticeable on a fast machine, but no point in doing more work than
necessary.
This commit is contained in:
Bill Currie 2021-08-06 13:47:56 +09:00
parent b320c3352f
commit 9a93bf8d4a

View file

@ -260,15 +260,24 @@ static void
reconstruct_clusters (void) reconstruct_clusters (void)
{ {
leafvis = malloc (num_leafs * sizeof (leafvis_t)); leafvis = malloc (num_leafs * sizeof (leafvis_t));
int sorted = 1;
num_clusters = 1;
for (unsigned i = 0; i < num_leafs; i++) { for (unsigned i = 0; i < num_leafs; i++) {
leafvis[i].visoffs = bsp->leafs[i + 1].visofs; leafvis[i].visoffs = bsp->leafs[i + 1].visofs;
leafvis[i].leafnum = i; leafvis[i].leafnum = i;
if (i > 0) {
num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs;
if (leafvis[i].visoffs < leafvis[i - 1].visoffs) {
sorted = 0;
}
}
} }
heapsort (leafvis, num_leafs, sizeof (leafvis_t), leaf_compare); if (!sorted) {
heapsort (leafvis, num_leafs, sizeof (leafvis_t), leaf_compare);
num_clusters = 1; num_clusters = 1;
for (unsigned i = 1; i < num_leafs; i++) { for (unsigned i = 1; i < num_leafs; i++) {
num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs; num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs;
}
} }
leafcluster = malloc (num_leafs * sizeof (uint32_t)); leafcluster = malloc (num_leafs * sizeof (uint32_t));