
#include "GroundZone.h"

#include "MemoryManager/debugNew.h"


using namespace ground;

GroundZone::GroundZone()
{
	this->id = 0;
	this->cases.clear();
	this->maxValue = 0.0f;
	this->minValue = 0.0f;
	this->medValue = 0.0f;
	this->colCircleCenter.setCoord(0.0f,0.0f);
	this->colCircleRadius = 0.0f;
	this->colAABBmaxX = 0.0f;
	this->colAABBmaxY = 0.0f;
	this->colAABBminX = 0.0f;
	this->colAABBminY = 0.0f;
}

GroundZone::~GroundZone()
{
	this->cases.clear();
}

int GroundZone::getID()
{
	return this->id;
}

const std::vector<Case*>* GroundZone::getCases()
{
	return &this->cases;
}

float GroundZone::getMaxCaseValue()
{
	return this->maxValue;
}

float GroundZone::getMinCaseValue()
{
	return this->minValue;
}

float GroundZone::getMedCaseValue()
{
	return this->medValue;
}

void GroundZone::computeCollisionArea()
{
	colCircleCenter.setCoord(0.0f, 0.0f);
	colCircleRadius = 0.0f;
	this->colAABBmaxX = -9999.0f;
	this->colAABBmaxY = -9999.0f;
	this->colAABBminX = 9999.0f;
	this->colAABBminY = 9999.0f;
	float tmp,x,y;

	std::vector<Case*>::iterator itc;
	//centre de gravité
	for(itc=cases.begin();itc!=cases.end();itc++) {
		//AABB2D
		if(this->colAABBmaxX<(*itc)->getCoord()[0]) this->colAABBmaxX = (float)((*itc)->getCoord()[0]);
		if(this->colAABBmaxY<(*itc)->getCoord()[1]) this->colAABBmaxY = (float)((*itc)->getCoord()[1]);
		if(this->colAABBminX>(*itc)->getCoord()[0]) this->colAABBminX = (float)((*itc)->getCoord()[0]);
		if(this->colAABBminY>(*itc)->getCoord()[1]) this->colAABBminY = (float)((*itc)->getCoord()[1]);
	}
	colCircleCenter.setX( (colAABBmaxX+colAABBminX)/2.0f );
	colCircleCenter.setY( (colAABBmaxY+colAABBminY)/2.0f );
	//rayon au carré
	for(itc=cases.begin();itc!=cases.end();itc++) {
		x = (*itc)->getCoord()[0] - colCircleCenter.getX();
		y = (*itc)->getCoord()[1] - colCircleCenter.getY();
		tmp =	x*x + y*y;
		if(tmp>colCircleRadius)
			colCircleRadius = tmp;
	}
}

void GroundZone::renderDebug()
{
	float x,y,xsuiv,ysuiv;
	glColor3f(1.0f,1.0f,1.0f);
	glPointSize(10.0f);
	glBegin(GL_POINTS);
	glVertex3f( colCircleCenter.getX()*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f, 30.0f , colCircleCenter.getY()*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f );
	glEnd();
	glPointSize(1.0f);

	//aabb2D
	glBegin(GL_QUADS);
	//
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 0.0f,		colAABBminY*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 0.0f,		colAABBminY*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 30.0f,	colAABBminY*CASE_SIDE_SIZE);
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 30.0f,	colAABBminY*CASE_SIDE_SIZE);
	//
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 0.0f,		(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 0.0f,		(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 30.0f,	(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 30.0f,	(colAABBmaxY+1)*CASE_SIDE_SIZE);
	//
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 0.0f,		colAABBminY*CASE_SIDE_SIZE);
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 0.0f,		(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 30.0f,	(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( colAABBminX*CASE_SIDE_SIZE		, 30.0f,	colAABBminY*CASE_SIDE_SIZE);
	//
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 0.0f,		colAABBminY*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 0.0f,		(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 30.0f,	(colAABBmaxY+1)*CASE_SIDE_SIZE);
	glVertex3f( (colAABBmaxX+1)*CASE_SIDE_SIZE	, 30.0f,	colAABBminY*CASE_SIDE_SIZE);
	//
	glEnd();

	//cercle
	glBegin(GL_QUADS);
	//
	const float pi2 = 2*3.141592654f;
	const float step = pi2/16.0f;
	float radius = sqrt(this->colCircleRadius);
	glColor3f(0.5f,0.5f,0.5f);
	for(float alpha=0;alpha<pi2;alpha+=step) {
		x = colCircleCenter.getX()		+ radius*cos(alpha);
		y = colCircleCenter.getY()		+ radius*sin(alpha);
		xsuiv = colCircleCenter.getX()	+ radius*cos(alpha+step);
		ysuiv = colCircleCenter.getY()	+ radius*sin(alpha+step);

		glVertex3f( x*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f		, 0.0f,		y*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f);
		glVertex3f( x*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f		, 30.0f,	y*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f);
		glVertex3f( xsuiv*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f	, 30.0f,	ysuiv*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f);
		glVertex3f( xsuiv*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f	, 0.0f,		ysuiv*CASE_SIDE_SIZE+CASE_SIDE_SIZE/2.0f);
	}
	//
	glEnd();
}

