-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctTree.cpp
360 lines (291 loc) · 7.05 KB
/
OctTree.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <iostream>
#include "OctTree.h"
void OctNode::add(Nbody* par) {
//string pref = "";
//for (int i = 0; i < depth; i++) {
// pref += "\t";
//}
if (depth > max_depth) {
cout << "DepthProblem!" << endl;
return; }
//cout << pref << "Inserting Particle" << endl;
//cout << pref << min << " " << max << endl;
//if (!checkBoundary(par->position)) {
// std::cout << pref <<"Tried to add particle that doesnt fit in Node!" << endl;
//}
int childnum;
if(!particle) {
//cout << pref<<"Node was empty....";
if (numChildParticles == 0) {
//cout << "Just inserting!" << endl;
particle = par;
}
else {
childnum = getCorrespondingChild(par->position);
//cout << "Inserting in child " << childnum << endl;
if (children[childnum]) {
(*children[childnum]).add(par);
}
else {
pair<Vec3f, Vec3f> min_max = computeBoundingBox(childnum);
if (depth + 1 > max_depth) {
//cout << "Could not create child. Depth Prob" << endl;
return;
}
children[childnum] = new OctNode(depth+1, max_depth, min_max.first, min_max.second);
(*children[childnum]).add(par);
}
}
}
else {
//cout << pref << "Leaf already has particle. ";
// Have to resolve that
// First just add the particle
childnum = getCorrespondingChild(particle->position);
//cout << particle->position << endl;
//cout << "Adding into child " << childnum << " ";
if (children[childnum]) {
//cout << "that is already there" << endl;
(*children[childnum]).add(particle);
}
else {
//cout << "that we have to create!" << endl;
if (depth + 1 > max_depth) {
//cout << "Could not create child. Depth Prob" << endl;
return;
}
pair<Vec3f, Vec3f> min_max = computeBoundingBox(childnum);
//cout << min_max.first << " " << min_max.second;
children[childnum] = new OctNode(depth+1 ,max_depth ,min_max.first, min_max.second);
children[childnum]->add(particle);
}
//cout << pref << "Now inserting new value ";
//Now add your own particle in the subtree
childnum = getCorrespondingChild(par->position);
//cout << par->position << endl;
//cout << "into " << childnum << " ";
if (children[childnum]) {
//cout << "that is already there!" << endl;
children[childnum]->add(par);
}
else {
//cout << "that we have to create!" << endl;
pair<Vec3f, Vec3f> min_max = computeBoundingBox(childnum);
children[childnum] = new OctNode(depth + 1, max_depth, min_max.first, min_max.second);
children[childnum]->add(par);
}
//hasParticle = false;
particle = NULL;
}
numChildParticles++;
}
int OctNode::getCorrespondingChild(Vec3f v) {
int childNum = 0;
Vec3f center = Vec3f((max + min) / 2);
bool tx = center.x > v.x;
bool ty = center.y > v.y;
bool tz = center.z > v.z;
childNum |= 1 * (int)tz;
childNum |= 2 * (int)ty;
childNum |= 4 * (int)tx;
return childNum;
if (tx) {
if (ty) {
if (tz) {
return 0;
}
else {
return 1;
}
}
else {
if (tz) {
return 2;
}
else {
return 3;
}
}
}
else {
if (ty) {
if (tz) {
return 4;
}
else {
return 5;
}
}
else {
if (tz) {
return 6;
}
else {
return 7;
}
}
}
}
bool OctNode::checkBoundary(Vec3f p) {
if (p.x < min.x || p.x >= max.x) {
return false;
}
if (p.y < min.y || p.y >= max.y) {
return false;
}
if (p.z < min.z || p.z >= max.z) {
return false;
}
return true;
}
pair<Vec3f, Vec3f> OctNode::computeBoundingBox(int childnum) {
Vec3f new_max = Vec3f();
Vec3f new_min = Vec3f();
Vec3f center = Vec3f((max+min)/2);
bool tz = childnum & 1;
bool ty = childnum & 2;
bool tx = childnum & 4;
if (tz) {
new_min.z = min.z;
new_max.z = center.z;
}
else {
new_min.z = center.z;
new_max.z = max.z;
}
if (ty) {
new_min.y = min.y;
new_max.y = center.y;
}
else {
new_min.y = center.y;
new_max.y = max.y;
}
if (tx) {
new_min.x = min.x;
new_max.x = center.x;
}
else {
new_min.x = center.x;
new_max.x = max.x;
}
return pair<Vec3f, Vec3f>(new_min, new_max);
if (childnum / 4 == 0) {
new_max.x -= (max.x - min.x) / 2;
}
else {
childnum %= 4;
new_min.x += (max.x - min.x) / 2;
}
childnum %= 4;
if (childnum / 2 == 0) {
new_max.y -= (max.y - min.y) / 2;
}
else {
new_min.y += (max.y - min.y) / 2;
}
childnum %= 2;
if (childnum / 1 == 0) {
new_max.z -= (max.z - min.z) / 2;
}
else {
new_min.z += (max.z - min.z) / 2;
}
return pair<Vec3f, Vec3f>(new_min, new_max);
}
//Update the center of mass and mass of the cell as well as the bounding box.
void OctNode::updateTree() {
numChildParticles = 0;
if (particle) {
mass = particle->mass;
centerOfMass = particle->position;
numChildParticles = 1;
return;
}
int tmp_num_children = 0;
for (int i = 0; i < 8; i++) {
if (children[i]) {
tmp_num_children++;
(*children[i]).updateTree();
mass += (*children[i]).mass;
centerOfMass += children[i]->mass * children[i]->centerOfMass;
numChildParticles += children[i]->numChildParticles;
}
}
//Leere Raumregion. Blatt des Baums.
if (tmp_num_children == 0) {
return;
}
centerOfMass /= mass;
}
int OctNode::updateParticle(int index, vector<Nbody*>* particles, vector<Vec3f>* acc, vector<int>* coll, float threshold){
float cell_length = (max - min).length();
Vec3f delta_dir = (*particles)[index]->position - centerOfMass;
float dist = delta_dir.length();
if (particle) {
if (particle == (*particles)[index] || dist <= (particle->radius+(*particles)[index]->radius) ) {
return 0;
}
(*acc)[index] -= (float)(mass / pow(dist, 3)) * delta_dir;
return 0;
}
if (cell_length / dist < threshold) {
(*acc)[index] -= (float)(mass / pow(dist, 3)) * delta_dir;
return 0;
}
for (int i = 0; i < 8; i++) {
if (children[i]) {
children[i]->updateParticle(index, particles, acc, coll, threshold);
}
}
}
void OctNode::clear() {
particle = NULL;
numChildParticles = 0;
mass = 0.f; centerOfMass = Vec3f(0.f, 0.f, 0.f);
for (int i = 0; i < 8; i++) {
delete children[i];
children[i] = NULL;
}
}
void OctNode::write(ofstream* file, int child_id) {
if (depth == 0) {
*file << "<!--XML Document-->\n";
*file << "<?xml version='1.0' encoding='us-ascii'>\n";
}
string pref = "";
for (int i = 0; i < depth; i++) {
pref += "\t";
}
if (numChildParticles == 1) {
*file << pref << "< Node " << "depth=\"" << depth << "\" id=\"" << child_id << "\" numPart=\"" << numChildParticles << "\" mass=\"" << mass << "\" center=\"(" << centerOfMass << ")\"/>\n";
}
else {
*file << pref <<"< Node " << "depth=\"" << depth << "\" id=\"" << child_id << "\" numPart=\"" << numChildParticles << "\" mass=\"" << mass << "\" center=\"(" << centerOfMass << ")\">\n";
for (int i = 0; i < 8; i++) {
if (children[i]) {
children[i]->write(file, i);
}
}
*file << pref << "</Node>\n";
}
}
void OctNode::print() {
string pref = "";
for (int i = 0; i < depth; i++) {
pref += "\t";
}
if (particle) {
cout << pref << (*particle).position << endl;
}
else {
cout << " ---- " << endl;
}
for (int i = 0; i < 8; i++) {
if (children[i]) {
children[i]->print();
} {
cout << pref << "----" << endl;
}
}
}