Skip to content

Commit

Permalink
Custom MinHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-van-bergen committed Oct 8, 2021
1 parent 479cffd commit 9cd7928
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions Pathtracer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
<ClInclude Include="Src\Util\Parser.h" />
<ClInclude Include="Src\Util\PerfTest.h" />
<ClInclude Include="Src\Util\PMJ.h" />
<ClInclude Include="Src\Util\MinHeap.h" />
<ClInclude Include="Src\Util\ScopeTimer.h" />
<ClInclude Include="Src\Util\Shader.h" />
<ClInclude Include="Src\Util\String.h" />
Expand Down
3 changes: 3 additions & 0 deletions Pathtracer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,8 @@
<ClInclude Include="Src\Util\Mutex.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="Src\Util\MinHeap.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
</Project>
18 changes: 10 additions & 8 deletions Src/BVH/BVHOptimizer.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "BVHOptimizer.h"

#include <queue>
#include <random>

#include "Config.h"

#include "Util/Util.h"
#include "Util/Array.h"
#include "Util/MinHeap.h"
#include "Util/ScopeTimer.h"

static std::default_random_engine random_engine;
Expand Down Expand Up @@ -105,18 +105,20 @@ static void select_nodes_measure(const BVH & bvh, const int parent_indices[], in
static void find_reinsertion(const BVH & bvh, const BVHNode2 & node_reinsert, float & min_cost, int & min_index) {
float node_reinsert_area = node_reinsert.aabb.surface_area();

// Compare based on induced cost
auto cmp = [](const std::pair<int, float> & a, const std::pair<int, float> & b) {
return a.second < b.second;
};
struct Pair {
int node_index;
float induced_cost;

std::priority_queue<std::pair<int, float>, Array<std::pair<int, float>>, decltype(cmp)> priority_queue(cmp);
bool operator<(Pair other) const {
return induced_cost < other.induced_cost; // Compare based on induced cost
}
};

MinHeap<Pair> priority_queue;
priority_queue.emplace(0, 0.0f); // Push BVH root with 0 induced cost

while (priority_queue.size() > 0) {
auto [node_index, induced_cost] = priority_queue.top();
priority_queue.pop();
auto [node_index, induced_cost] = priority_queue.pop();

const BVHNode2 & node = bvh.nodes_2[node_index];

Expand Down
6 changes: 3 additions & 3 deletions Src/Util/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Array {
size_t count = 0;
size_t capacity = 0;

constexpr Array(int initial_count = 0) {
constexpr Array(size_t initial_count = 0) {
resize(initial_count);
}

Expand Down Expand Up @@ -167,12 +167,12 @@ struct Array {
return reinterpret_cast<const T *>(buffer);
}

constexpr T & operator[](int index) {
constexpr T & operator[](size_t index) {
assert(0 <= index && index < count);
return data()[index];
}

constexpr const T & operator[](int index) const {
constexpr const T & operator[](size_t index) const {
assert(0 <= index && index < count);
return data()[index];
}
Expand Down

0 comments on commit 9cd7928

Please sign in to comment.