Skip to content
Snippets Groups Projects
Tree.cpp 3.63 KiB
Newer Older
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include "Tree.hpp"
#include "Node.hpp"
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
	using namespace std;

Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		//insert dummy root node
		this->nodes = new Node(this);
		this->maxLeafBodies = 16;
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		delete this->nodes;
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		//remove all nodes; refresh root node
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		while (this->nodes->next != this->nodes) {
			Node* node = this->nodes->next;
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed

			node->unlink();
			delete node;
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		delete this->nodes;
		this->nodes = new Node(this);
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
	}

	bool Tree::isCorrect() {
		Node* current = this->nodes->next;
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed

		while (current != this->nodes) {
			if (!current->isCorrect()) {
				return false;
			}
			current = current->next;
		}
		return true;
	unsigned long Tree::numberOfNodes() {
		unsigned long nodes = 0;

		for (Node* node = this->nodes->next; node != this->nodes; node = node->next) {
			nodes++;
		}
		return nodes;
	}

	void Tree::accumulateForceOnto(Body& body) {
		Node* n = this->nodes->next;

Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		body.resetAcceleration();
		while (n != this->nodes) {
			if (n->sufficientForBody(body)) {
				n->representative.accumulateForceOnto(body);
			} else if (n->leaf) {
				for (vector<Body>::iterator it = n->bodies.begin(); it != n->bodies.end(); it++) {
					it->accumulateForceOnto(body);
				}
			}
			n = n->afterSubtree;
		}
	}

	void Tree::computeForces() {
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		for (Node* n = this->nodes->next; n != this->nodes; n = n->next) {
			if (n->leaf) {
				for (vector<Body>::iterator it = n->bodies.begin(); it != n->bodies.end(); it++) {
					this->accumulateForceOnto(*it);
	vector<Body> Tree::dubinskiParse(string filename) {
		vector<Body> result;
		string line;
		ifstream infile(filename.c_str(), ifstream::in);
		double mass, px, py, pz;
		while (infile >> mass >> px >> py >> pz) {
			Body b(px, py, pz, mass);

			result.push_back(b);
		}
		infile.close();
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
	vector<Body> Tree::extractLocalBodies() {
		vector<Body> result;

		while (this->nodes->next != this->nodes) {
			if (this->nodes->next->leaf) {
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
				for (vector<Body>::iterator it = this->nodes->next->bodies.begin(); it != this->nodes->next->bodies.end(); it++) {
					if (!it->refinement) {
						result.push_back(*it);
					}
				}
			}
			Node* h = this->nodes->next;
			this->nodes->next->unlink();
			delete(h);
		}
		this->clean();
		return result;
	}

	vector<Body> Tree::copyRefinements(Box domain) {
Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
		vector<Body> result;
		Node* current = this->nodes->next;

		while (current != this->nodes) {
			bool sufficient = current->sufficientForBox(domain);

			if (sufficient) {
				result.push_back(current->representative);
				current = current->afterSubtree;
			} else if (current->leaf) {
				result.insert(result.end(), current->bodies.begin(), current->bodies.end());
				current = current->next;
			} else {
				current = current->next;
			}
		}
		return result;
	}

	Box Tree::getRootBB() {
		return this->nodes->next->bb;
	}

Paul Heinzlreiter's avatar
Paul Heinzlreiter committed
	void Tree::rebuild(Box domain) {
		this->build(this->extractLocalBodies(), domain);
	void Tree::advance() {
		for (Node* n = this->nodes->next; n != this->nodes; n = n->next) {
			if (n->leaf) {
				for (vector<Body>::iterator it = n->bodies.begin(); it != n->bodies.end(); it++) {
					if (!it->refinement) {
						it->integrate();
					}
				}
			}
		}
	}

	Box Tree::getLocalBB() {
		Box result;

		for (Node* n = this->nodes->next; n != this->nodes; n = n->next) {
			if (n->leaf) {
				for (vector<Body>::iterator it = n->bodies.begin(); it != n->bodies.end(); it++) {
					if (!it->refinement) {
						result.extend(*it);
					}
				}
			}
		}
		return result;
	}

	void Tree::print() {
		this->nodes->next->bb.print();
	}