-
Notifications
You must be signed in to change notification settings - Fork 3
/
sphere_tree.h
255 lines (219 loc) · 6.8 KB
/
sphere_tree.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#pragma once
#include "gl/glew.h"
#include "glm/vec3.hpp"
#include "glm/glm.hpp"
#include <vector>
#include <iostream>
#include <stack>
// ********************************
// 空间Sphere Tree,用于加速求交
// 现在还没实现完GPU版本
// 之后可以用在黑洞周围的粒子显示上
// ********************************
using namespace std;
using namespace glm;
float length2(vec3 v) {
return dot(v, v);
}
struct LeafSphereInfo {
int particle_type;
int __no_use;
};
static_assert(sizeof(LeafSphereInfo) == 8, "size");
struct Sphere {
vec3 position;
float radius = -1;
bool contains(vec3 pos) {
return radius * radius >= length2(pos - position);
}
Sphere() = default;
Sphere(vec3 p, float r) : position(p), radius(r) {};
};
struct SphereTreeNode {
Sphere sphere;
vector<SphereTreeNode*> children;
LeafSphereInfo info;
int leaf_index = -1;
//SphereTreeNode* left = nullptr;
//SphereTreeNode* right = nullptr;
bool leaf() { return children.size() == 0; }
int height() {
int h = -1;
for (SphereTreeNode* child : children) {
h = max(h, child->height());
}
return h + 1;
}
SphereTreeNode(Sphere sp) : sphere(sp) {};
};
struct GPUSphereTreeNode {
vec3 position;
float radius;
int on_hit; // next. -1 = particle
int on_miss; // miss. -1 = end
};
static_assert(sizeof(GPUSphereTreeNode) == 24, "size");
class GPUSphereTree {
int _n_nodes;
GPUSphereTreeNode* _nodes;
int _n_info;
LeafSphereInfo* _info;
public:
GPUSphereTree(int n_nodes, GPUSphereTreeNode* nodes, int n_info, LeafSphereInfo* info) : _n_nodes(n_nodes), _nodes(nodes), _n_info(n_info), _info(info) {
}
~GPUSphereTree() {
if (_nodes) delete _nodes;
if (_info) delete _info;
}
};
class SphereTree {
vector<Sphere> _spheres;
SphereTreeNode* _root = nullptr;
float find_coverage_radius(vec3 center, const vector<Sphere>& spheres) {
int i_max_dist;
float max_dist = -1;
int i = 0;
for (auto& sp : spheres) {
float dist = length(sp.position - center) + sp.radius;
if (dist > max_dist) {
i_max_dist = i;
max_dist = dist;
}
i++;
}
return max_dist;
}
int pick_farthest(vec3 center, const vector<Sphere>& spheres) {
int i_max_dist;
float max_dist2 = -1;
int i = 0;
for (auto& sp : spheres) {
float dist2 = length2(sp.position - center);
if (dist2 > max_dist2) {
i_max_dist = i;
max_dist2 = dist2;
}
i++;
}
return i_max_dist;
}
int count_nodes(SphereTreeNode* node) {
int c = 1;
for (auto& child : node->children) {
c += count_nodes(child);
}
return c;
}
SphereTreeNode* construct_node(vector<Sphere> spheres) {
if (spheres.size() == 0) return nullptr;
if (spheres.size() == 1) return new SphereTreeNode(spheres[0]);
// binary partition the spheres
vector<Sphere> left;
vector<Sphere> right;
left.reserve(spheres.size() / 2);
right.reserve(spheres.size() / 2);
// pick a random sphere
int i1 = rand() % spheres.size();
vec3 center1 = spheres[i1].position;
// pick a farthest sphere
int i2 = pick_farthest(center1, spheres);
vec3 center2 = spheres[i2].position;
// classify the spheres to left or right
for (auto& sp : spheres) {
float l1 = length(sp.position - center1);
float l2 = length(sp.position - center2);
if (l1 < l2) {
left.push_back(sp);
}
else {
right.push_back(sp);
}
}
// find center position
vec3 sum_pos = vec3();
for (auto& sp : spheres) {
sum_pos += sp.position;
}
vec3 avg_pos = sum_pos / float(spheres.size());
float cover = find_coverage_radius(avg_pos, spheres);
auto node = new SphereTreeNode(Sphere(avg_pos, cover));
if (left.size() == spheres.size() || right.size() == spheres.size()) {
for (auto& sp : spheres) {
node->children.push_back(new SphereTreeNode(sp));
}
}
else {
node->children.push_back(construct_node(move(left)));
node->children.push_back(construct_node(move(right)));
}
return node;
}
public:
SphereTree(vector<Sphere> spheres) {
_spheres = move(spheres);
_root = construct_node(_spheres);
};
SphereTree(const SphereTree&) = delete;
SphereTree(SphereTree&&) = delete;
int height() {
return _root->height();
}
SphereTreeNode* query_first(vec3 position, int& amt) {
amt = 0;
stack<SphereTreeNode*> stk;
stk.push(_root);
while (!stk.empty()) {
auto current = stk.top();
stk.pop();
if (current->sphere.contains(position)) {
amt++;
if (current->leaf()) return current;
for (int i = current->children.size() - 1; i >= 0; i--) {
stk.push(current->children[i]);
}
}
}
return nullptr;
}
GPUSphereTree generate_gpu_tree() {
auto* leaf_info = new LeafSphereInfo[_spheres.size()];
auto* nodes = new GPUSphereTreeNode[count_nodes(_root)];
stack<SphereTreeNode*> stk;
stk.push(_root);
int i_node = 0;
int i_leaf = 0;
while (!stk.empty()) {
auto current = stk.top();
stk.pop();
i_node++;
}
GPUSphereTree gt(i_node, nodes, _spheres.size(), leaf_info);
return gt;
};
};
// 测试程序
int main2() {
// vec3 v = vec3();
vector<Sphere> spheres;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
spheres.push_back(Sphere(vec3(i, j, 0), 1));
}
}
//spheres.push_back(Sphere(vec3(0, 0, 0), 1));
//spheres.push_back(Sphere(vec3(0, 0, 0), .5));
//spheres.push_back(Sphere(vec3(2, 0, 0), .5));
//spheres.push_back(Sphere(vec3(3, 0, 0), .5));
//spheres.push_back(Sphere(vec3(4, 0, 0), .5));
//spheres.push_back(Sphere(vec3(4, 2, 0), .5));
//spheres.push_back(Sphere(vec3(4, 1, 0), .5));
//spheres.push_back(Sphere(vec3(8, 0, 0), .5));
//spheres.push_back(Sphere(vec3(9, 0, 0), .5));
//spheres.push_back(Sphere(vec3(9, 1, 0), .5));
//spheres.push_back(Sphere(vec3(8, 2, 0), .5));
SphereTree tree(move(spheres));
int h = tree.height();
int amt = 0;
auto result = tree.query_first(vec3(21.3, 28, 0), amt);
return 1;
}