/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/domains/graphics/primitives/polygon.cpp

*/

#include "sgx/core/core.hpp"
#include "domains/graphics/color/color.hpp"
#include "domains/graphics/primitives/primitives.hpp"


void	sgxPrimitivePolygon::getBoundingSphere(sgxVector3 &pos, tREAL32 &radius) const {
	sgxVector3 minpos, maxpos;
	
	getBoundingBox(minpos, maxpos);

	pos = sgxLerp(minpos, maxpos, 0.5f);
	
	radius = minpos.getDistance(pos);
	radius = sgxMax(radius, maxpos.getDistance(pos));
}

void	sgxPrimitivePolygon::getBoundingBox(sgxVector3 &minpos, sgxVector3 &maxpos) const {
	minpos.set(SGX_FLOAT_MAX_VALUE, SGX_FLOAT_MAX_VALUE, SGX_FLOAT_MAX_VALUE);
	maxpos.set(SGX_FLOAT_MIN_VALUE, SGX_FLOAT_MIN_VALUE, SGX_FLOAT_MIN_VALUE);
	
	sgxVector<sgxPolyVtx>::const_iterator cit = vtx.begin();
	for(;cit!=vtx.end();++cit) {
		const sgxVector3 &v = (*cit).vect;

		minpos.x = sgxMin(v.x, minpos.x);
		minpos.y = sgxMin(v.y, minpos.y);
		minpos.z = sgxMin(v.z, minpos.z);

		maxpos.x = sgxMax(v.x, maxpos.x);
		maxpos.y = sgxMax(v.y, maxpos.y);
		maxpos.z = sgxMax(v.z, maxpos.z);
	}
}

