#include "Box.hpp"

namespace nbody {
	Box::Box() {
	}

	Box::~Box() {
	}

	double Box::getMin(int index) {
		return this->min[index];
	}

	double Box::getMax(int index) {
		return this->max[index];
	}

	void Box::setMin(int index, double value) {
		this->min[index] = value;
	}

	void Box::setMax(int index, double value) {
		this->max[index] = value;
	}

	void Box::extendToCube() {
		int longestSide = -1;
		double sidelength = 0.0;

		for (int i = 0; i < 3; i++) {
			if (this->max[i] - this->min[i] >= sidelength) {
				longestSide = i;
				sidelength = this->max[i] - this->min[i];
			}
		}
		if (longestSide == -1) {
			return;
		}
		for (int i = 0; i < 3; i++) {
			if (i != longestSide) {
				double extend = (sidelength - (this->max[i] - this->min[i])) / 2.0;

				this->min[i] -= extend;
				this->max[i] += extend;
			}
		}
	}

	vector<Body> Box::containedBodies(vector<Body> bodies) {
		vector<Body> result;

		for (vector<Body>::iterator it = bodies.begin(); it != bodies.end(); it++) {
			if (it->getPosition(0) >= this->min[0] && it->getPosition(0) <= this->max[0] &&
					it->getPosition(1) >= this->min[1] && it->getPosition(1) <= this->max[1] &&
					it->getPosition(2) >= this->min[2] && it->getPosition(2) <= this->max[2]) {
				result.push_back(*it);
			}
		}
		return result;
	}
}
