Skip to content
Snippets Groups Projects
Commit 80e146f5 authored by Paul Heinzlreiter's avatar Paul Heinzlreiter
Browse files

* fixing bh tree build

parent c4980efd
No related merge requests found
......@@ -37,7 +37,6 @@ namespace nbody {
void BarnesHutTree::build(vector<Body> bodies) {
TreeNode* current;
unsigned long nodes = 0;
this->clean();
if (bodies.empty()) return;
......@@ -59,24 +58,25 @@ namespace nbody {
}
if (current->isSplitable()) {
vector<Box> subboxes = this->splitBB(current);
vector<Body> localBodies = current->getBodies();
TreeNode* after = current->next;
current->leaf = false;
for (vector<Box>::iterator bit = subboxes.begin(); bit != subboxes.end(); bit++) {
for (vector<Box>::iterator it = subboxes.begin(); it != subboxes.end(); it++) {
TreeNode* child = new TreeNode(this);
nodes++;
child->bb = *bit;
child->bodies = bit->containedBodies(localBodies);
current->next->insertBefore(child);
child->afterSubtree = (bit == subboxes.begin()) ? current->afterSubtree : child->next;
child->bb = *it;
//TODO: move bodies
child->bodies = it->containedBodies(current->bodies);
current->insertAfter(child);
if (it != subboxes.begin()) {
child->afterSubtree = child->next;
} else {
child->afterSubtree = current->afterSubtree;
}
}
current->bodies.clear();
}
current = current->next;
}
cout << "nodes " << nodes << endl;
}
}
......@@ -37,14 +37,9 @@ namespace nbody {
if (!current->isCorrect()) {
return false;
}
if (current->next != this->nodes && current->next != current->afterSubtree) {
cout << "inner node: " << current->bodies.size() << endl;
} else {
cout << "leaf: " << current->bodies.size() << endl;
}
//TODO: fix
/*
if (current->next != this->nodes && current->next != current->afterSubtree) {
if (!current->leaf) {
//inner node
int numChildren = 0;
......@@ -67,6 +62,15 @@ namespace nbody {
return true;
}
unsigned long Tree::numberOfNodes() {
unsigned long nodes = 0;
for (TreeNode* node = this->nodes->next; node != this->nodes; node = node->next) {
nodes++;
}
return nodes;
}
vector<Body> Tree::dubinskiParse(string filename) {
vector<Body> result;
string line;
......
......@@ -24,6 +24,7 @@ namespace nbody {
virtual void clean();
virtual void build(vector<Body> bodies) = 0;
virtual int numberOfChildren() = 0;
virtual unsigned long numberOfNodes();
virtual bool isCorrect();
};
......
......@@ -70,7 +70,7 @@ namespace nbody {
}
void TreeNode::insertAfter(TreeNode* node) {
this->insertBefore(node->next);
this->next->insertBefore(node);
}
void TreeNode::unlink() {
......@@ -97,28 +97,37 @@ namespace nbody {
}
for (vector<Body>::iterator it = this->bodies.begin(); it != this->bodies.end(); it++) {
if (!this->bb.isContained(*it)) {
cout << "bb out of bounds" << endl;
return false;
}
}
if (this->next == this->afterSubtree && !this->leaf) {
cout << "inner node inconsistency" << endl;
return false;
}
if (!this->leaf) {
TreeNode* current = this->next;
int children = 0;
while (current != NULL && current != this->afterSubtree) {
current = current->afterSubtree;
children++;
}
if (current == NULL) {
cout << "afterSubtree null" << endl;
return false;
}
if (children != this->tree->numberOfChildren()) {
cout << "wrong number of children " << children << endl;
return false;
}
current = this->next;
for (int i = 0; i < this->tree->numberOfChildren(); i++) {
current = current->afterSubtree;
}
if (current->afterSubtree != this->afterSubtree) {
cout << "inner node afterSubtree inconsistent" << endl;
if (current != this->afterSubtree) {
cout << "last sibling afterSubtree inconsistent" << endl;
return false;
}
}
if (!this->leaf && this->bodies.size() > 0) {
cout << "non-empty leaf" << endl;
cout << "non-empty inner node" << endl;
return false;
}
return true;
......
......@@ -15,6 +15,7 @@ namespace nbody {
friend void Tree::clean();
friend void BarnesHutTree::build(vector<Body> bodies);
friend bool Tree::isCorrect();
friend unsigned long Tree::numberOfNodes();
protected:
Box bb;
vector<Body> bodies;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment