-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdtree.hpp
183 lines (167 loc) · 5.48 KB
/
kdtree.hpp
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
#ifndef KDTREE_H
#define KDTREE_H
#include "primitive.hpp"
#include "util.hpp"
#include "ray.hpp"
#include "bbox.hpp"
#include <vector>
#include <algorithm>
#include <queue>
#include <thread>
#include <condition_variable>
#include <mutex>
struct Node;
struct Task {
Node* node;
BBox bbox;
};
std::queue<Task> q;
int count = 1;
struct Intersection {
double t;
const Primitive* object;
Intersection() : t(INFINITY), object(nullptr) {}
Intersection(double t, const Primitive* p) : t(t), object(p) {}
};
struct Node {
short axis;
double coord;
Node* left;
Node* right;
std::vector<Primitive*> objects;
inline bool is_leaf() const {
return !left;
}
Intersection intersect_contents(const Ray& ray) const {
const Primitive* closest = nullptr;
double min_dist = INFINITY;
for (auto obj = objects.begin(); obj != objects.end(); ++obj) {
double dist = (*obj)->intersect(ray);
if (!std::isinf(dist) && dist > EPS && dist < min_dist) {
min_dist = dist;
closest = *obj;
}
}
return {min_dist, closest};
}
Intersection intersect(const Ray& ray, double t_enter, double t_leave) const {
if (is_leaf()) {
return intersect_contents(ray);
}
double t_split = (coord - ray.start.coord[axis]) / ray.direction.coord[axis];
Node* lower = left;
Node* upper = right;
Intersection nearest;
if (ray.direction.coord[axis] < 0) {
std::swap(lower, upper);
}
if (t_split < t_enter) {
nearest = upper->intersect(ray, t_enter, t_leave);
} else if (t_split > t_leave) {
nearest = lower->intersect(ray, t_enter, t_leave);
} else {
nearest = lower->intersect(ray, t_enter, t_split);
if (std::isinf(nearest.t) || nearest.t > t_split) {
nearest = upper->intersect(ray, t_split, t_leave);
}
}
return nearest;
}
void split(BBox box) {
double best_value = INFINITY;
BBox best_left_box;
BBox best_right_box;
std::vector<Primitive*> back_list(objects);
for (int ax = 0; ax < 3; ++ax) {
std::sort(objects.begin(), objects.end(),
[ax](Primitive* a, Primitive* b) { return a->box.lower.coord[ax] < b->box.lower.coord[ax]; });
std::sort(back_list.begin(), back_list.end(),
[ax](Primitive* a, Primitive* b) { return a->box.upper.coord[ax] < b->box.upper.coord[ax]; });
size_t left_count = 0;
size_t right_count = objects.size();
for (size_t i = 0; i < objects.size(); i += objects.size() / 100 + 1) {
double splitter = back_list[i]->box.upper.coord[ax];
while (left_count < objects.size() && objects[left_count]->box.lower.coord[ax] < splitter + EPS) {
++left_count;
}
while (right_count > 0 && back_list[objects.size() - right_count]->box.upper.coord[ax] + EPS < splitter) {
--right_count;
}
if (splitter == box.lower.coord[ax]) {
continue;
}
BBox left_box = box;
BBox right_box = box;
left_box.upper.coord[ax] = splitter;
right_box.lower.coord[ax] = splitter;
double value = left_count * left_box.surface() + right_count * right_box.surface();
if (value < best_value) {
axis = ax;
coord = splitter;
best_value = value;
best_left_box = left_box;
best_right_box = right_box;
}
}
}
if (best_value*1.5 + 1000 < objects.size() * box.surface()) {
left = new Node();
right = new Node();
for (size_t i = 0; i < objects.size(); ++i) {
if (objects[i]->box.upper.coord[axis] > coord - EPS) {
right->objects.push_back(objects[i]);
}
if (objects[i]->box.lower.coord[axis] < coord + EPS) {
left->objects.push_back(objects[i]);
}
}
objects.clear();
q.push(Task{left, best_left_box});
q.push(Task{right, best_right_box});
count += 2;
}
count -= 1;
}
~Node() {
if (left) {
delete left;
}
if (right) {
delete right;
}
}
};
void run() {
}
class KDTree {
BBox outer;
Node* root;
public:
void build(std::vector<Primitive*> objects) {
log("Begin building kd-tree");
root = new Node();
root->objects = objects;
outer = objects[0]->bbox();
for (size_t i = 1; i < objects.size(); ++i) {
outer += objects[i]->bbox();
}
q.push(Task{root, outer});
while (!q.empty()) {
Task t = q.front();
q.pop();
t.node->split(t.bbox);
}
log("End building kd-tree");
}
Intersection intersect(Ray ray) const {
std::pair<double, double> inter = outer.intersect(ray);
if (inter.second < 0 || std::isinf(inter.second)) {
return Intersection();
}
return root->intersect(ray, inter.first, inter.second);
}
const BBox& get_bbox() {
return outer;
}
};
#endif