Newer
Older
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
#include <cfloat>
#include "TreeNode.hpp"
namespace nbody {
using namespace std;
TreeNode::TreeNode() {
for (int i = 0; i < 3; i++) {
this->bb.min[i] = 1.0;
this->bb.max[i] = -1.0;
}
this->afterMySubtree = NULL;
}
TreeNode::~TreeNode() {
while (!this->children.empty()) {
delete this->children.back();
this->children.pop_back();
}
}
bool TreeNode::isSplitable() {
for (int i = 0; i < 3; i++) {
if (this->bb.max[i] - this->bb.min[i] < FLT_MIN) {
return false;
}
}
return true;
}
void TreeNode::extendBBforBodies() {
if (this->bodies.empty()) return;
for (int i = 0; i < 3; i++) {
this->bb.min[i] = this->bodies.front().getPosition(i);
this->bb.max[i] = this->bodies.front().getPosition(i);
}
for (vector<Body>::iterator it = this->bodies.begin(); it != this->bodies.end(); it++) {
for (int i = 0; i < 3; i++) {
if (it->getPosition(i) < this->bb.min[i]) {
this->bb.min[i] = it->getPosition(i);
}
if (it->getPosition(i) > this->bb.max[i]) {
this->bb.max[i] = it->getPosition(i);
}
}
}
}
void TreeNode::extendBBtoCube() {
int longestSide = -1;
double sidelength = 0.0;
for (int i = 0; i < 3; i++) {
if (this->bb.max[i] - this->bb.min[i] >= sidelength) {
longestSide = i;
sidelength = this->bb.max[i] - this->bb.min[i];
}
}
if (longestSide == -1) {
return;
}
for (int i = 0; i < 3; i++) {
if (i != longestSide) {
double extend = (sidelength - (this->bb.max[i] - this->bb.min[i])) / 2.0;
this->bb.min[i] -= extend;
this->bb.max[i] += extend;
}
}
}
}