This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kdtree.cc
127 lines (118 loc) · 3.78 KB
/
kdtree.cc
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
// Copyright (c) 2016 Kai Luo. All rights reserved.
#include <queue>
#include "boundbox.h"
#include "core_math.h"
#include "geometry.h"
#include "kdtree.h"
namespace zLi {
KdTree::Node::Node() : child{nullptr, nullptr}, split_axis(NonAxis) {}
void KdTree::Node::Insert(int axis, const Geometry &g) {
if (!geometry) {
geometry = std::make_unique<Geometry>(g);
return;
}
if (split_axis == NonAxis) {
split_axis = axis;
split_plane = ++axis & 1 ? geometry->Bounds().min_point[split_axis]
: geometry->Bounds().max_point[split_axis];
child[0] = new Node();
child[1] = new Node();
}
BoundBox b = g.Bounds();
if (b.min_point[split_axis] > split_plane) {
child[1]->Insert(++axis % 3, g);
} else if (b.max_point[split_axis] <= split_plane) {
child[0]->Insert(++axis % 3, g);
} else {
child[0]->Insert(++axis % 3, g);
child[1]->Insert(++axis % 3, g);
}
}
KdTree KdTree::BuildKdTree(std::vector<Geometry> &&gs) {
KdTree kd;
for (auto &g : gs) {
kd.world_ = Union(g.Bounds(), kd.world_);
}
kd.root_ = new Node();
// int k = 0;
for (auto &g : gs) {
kd.root_->Insert(UniformInt(0, 1023) % 3, g);
}
return kd;
}
std::optional<RaySurfaceIntersection> KdTree::Intersect(const Ray &r) {
auto test = world_.Intersect(r);
if (!test) {
return {};
}
std::queue<std::tuple<Node *, Float, Float>> q;
q.push(std::make_tuple(root_, std::get<0>(*test), std::get<1>(*test)));
std::unique_ptr<RaySurfaceIntersection> ans;
while (!q.empty()) {
auto task = q.front();
q.pop();
Node *current = std::get<0>(task);
Float tmin = std::get<1>(task), tmax = std::get<2>(task);
assert(tmin >= 0 && tmin <= tmax);
Ray ray(r.o, r.d, tmin, tmax);
if (current->geometry) {
auto test = current->geometry->Intersect(ray);
if (test && (!ans || ans->t > (*test).t)) {
ans = std::make_unique<RaySurfaceIntersection>(std::move(*test));
}
}
if (current->split_axis == NonAxis) {
continue;
}
if (ray.d[current->split_axis] == 0) {
if (ray.o[current->split_axis] <= current->split_plane &&
(!ans || ans->t > tmin)) {
q.push(std::make_tuple(current->child[0], tmin, tmax));
} else if (ray.o[current->split_axis] > current->split_plane &&
(!ans || ans->t > tmin)) {
q.push(std::make_tuple(current->child[1], tmin, tmax));
} // else if (!ans || ans->t > tmin) {
// q.push(std::make_tuple(current->child[0], tmin, tmax));
// q.push(std::make_tuple(current->child[1], tmin, tmax));
//}
continue;
}
Float tsplit = (current->split_plane - ray.o[current->split_axis]) /
ray.d[current->split_axis];
assert(!std::isnan(tsplit));
if (tmin <= tsplit && tsplit <= tmax) {
if (ray(tmin)[current->split_axis] <= current->split_plane) {
if (!ans || ans->t > tmin) {
q.push(std::make_tuple(current->child[0], tmin, tsplit));
}
if (!ans || ans->t > tsplit) {
q.push(std::make_tuple(current->child[1], tsplit, tmax));
}
} else {
if (!ans || ans->t > tmin) {
q.push(std::make_tuple(current->child[1], tmin, tsplit));
}
if (!ans || ans->t > tsplit) {
q.push(std::make_tuple(current->child[0], tsplit, tmax));
}
}
} else {
if (ray(tmin)[current->split_axis] < current->split_plane) {
if (!ans || ans->t > tmin) {
q.push(std::make_tuple(current->child[0], tmin, tmax));
}
} else {
if (!ans || ans->t > tmin) {
q.push(std::make_tuple(current->child[1], tmin, tmax));
}
}
}
}
if (ans) {
return *ans;
}
return {};
}
BoundBox KdTree::World() { return world_; }
KdTree::~KdTree() {}
} // namespace zLi