-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbvhtree.cpp
184 lines (145 loc) · 4.87 KB
/
bvhtree.cpp
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
//
// bvhtree.cpp
// Oculus
//
// Created by Manuel Broncano Rodriguez on 6/12/13.
// Copyright (c) 2013 Manuel Broncano Rodriguez. All rights reserved.
//
#include "bvhtree.h"
#include <algorithm>
#define DEBUG_BVH
using std::cout;
using std::endl;
// functor to sort by axis
struct sortNodeAxis {
int i;
sortNodeAxis(int i) : i(i) {}
bool operator()(const BVHTreeNode* a, const BVHTreeNode* b) {
return a->bbox.center().s[i] < b->bbox.center().s[i];
}
};
struct partValueAxis {
int axis;
Vector value;
partValueAxis(int axis, Vector value) : axis(axis), value(value) {};
bool operator()(const BVHTreeNode* a) {
Vector center = a->bbox.center();
bool ret = center.s[axis] < value.s[axis];
return ret;
}
};
BVHTree::BVHTree(std::vector<Primitive>& primitives) {
for (int i = 0; i < primitives.size(); i ++) {
BVHTreeNode *node = new BVHTreeNode(primitives[i], i);
//node->bbox += 1.f;
primitiveVec.push_back(node);
}
rootNode = Build(primitiveVec, 0, primitiveVec.size());
// rootNode = BuildAlt(primitiveVec, 0, primitiveVec.size() - 1);
bvh_vec_t list;
cout << "bvh tree:" << endl;
DumpTree(rootNode, bvh_vec);
#ifdef DEBUG_BVH
cout << endl << "serialized tree:" << endl;
for (size_t i = 0; i<bvh_vec.size(); i ++) {
cout << "[" << std::right << std::setw(2) << i << "] skip: " << std::left << std::setw(2) << bvh_vec[i].skip;
if (bvh_vec[i].pid != P_NONE)
cout << " leaf: " << bvh_vec[i].pid;
cout << endl;
}
#endif
}
BVHTreeNode *BVHTree::BuildAlt(node_vec_t &list, size_t start, size_t end, int axis) const {
size_t d = end - start;
if (start > end)
throw "assert";
if (d == 0) {
return list[start];
}
// calculate the union bounding box
BVHTreeNode *node = new BVHTreeNode();
for (size_t i = start; i <= end; i ++) {
node->bbox += list[i]->bbox;
}
// sort the nodes over alternate axis by the center
std::sort(list.begin() + start, list.begin() + end, sortNodeAxis(axis));
axis = (axis + 1) % 3;
// recurse on the left and right sides
size_t split = start + d/2;
node->left = BuildAlt(list, start, split, axis);
if ((split+1) <= end)
node->right = BuildAlt(list, split+1, end, axis);
return node;
}
BVHTreeNode *BVHTree::Build(node_vec_t &list, size_t start, size_t end) const {
size_t d = end - start;
if (d < 1)
throw "assert";
if (d == 1) {
return list[start];
}
// calculate the union bounding box and the mean center
Vector mean = vec_zero;
BVHTreeNode *node = new BVHTreeNode();
for (size_t i = start; i < end; i ++) {
node->bbox += list[i]->bbox;
mean = mean + list[i]->bbox.center();
}
mean = mean / d;
// calculate the axis with more variance (where the bbs are more spread)
Vector variance = vec_zero;
for (size_t i = start; i < end; i ++) {
Vector sep = list[i]->bbox.center() - mean;
variance = variance + sep * sep; // note this is *not* the dot product
}
// chooses the axis with the biggest variance
int axis = 0;
if (variance.y > variance.x && variance.y > variance.z) {
axis = 1;
} else if (variance.z > variance.x) {
axis = 2;
}
// sort the nodes over axis by the center
// std::sort(list.begin() + start, list.begin() + end, sortNodeAxis(axis));
// node_vec_t::iterator middle = std::partition(list.begin() + start, list.begin() + end, partValueAxis(axis, mean));
// partitions the list over the predicate
node_vec_t::iterator middle = std::partition(list.begin() + start, list.begin() + end, partValueAxis(axis, mean));
size_t split = start + std::distance(list.begin() + start, middle);
if (split == end) {
split--;
}
if (split == start) {
split ++;
}
// recurse on the left and right sides
node->left = Build(list, start, split);
if (split != end)
node->right = Build(list, split, end);
return node;
}
void BVHTree::DumpTree(BVHTreeNode *node, bvh_vec_t& list, int depth) const {
if (node == nullptr)
return;
BVHNode n;
n.pid = (unsigned int)node->primitiveIndex;
n.min = node->bbox.min;
n.max = node->bbox.max;
size_t ofs = list.size();
list.push_back(n);
#ifdef DEBUG_BVH
cout << "[" << std::setw(2) << ofs << "]";
for(int i = depth; i; i--) cout << " |";
#endif
if (node->isLeaf()) {
#ifdef DEBUG_BVH
cout << " leaf: " << node->primitiveIndex << endl;
#endif
} else {
#ifdef DEBUG_BVH
cout << " node" << endl;
#endif
DumpTree(node->left, list, depth + 1);
DumpTree(node->right, list, depth + 1);
}
list[ofs].skip = (unsigned int)list.size();
}