// Box.cpp

#include "Types.h"
#include "Math.h"
#include "List.h"
#include "Misc.h"
#include "Object.h"
#include "Box.h"


bool32 CBox::ContainsPoint( Vector3 *v )
{
	Plane
		*plane;
	s32
		p;
	float
		d;

	for (p=0; p<Planes.Count; p++)
	{
		plane = Planes[p];

		d = DotProduct( v, &plane->Normal );

		if (d < plane->Dist)
			return( FALSE );
	}

	return( TRUE );
}


void CBox::Load( CObject *obj )
{
	Plane
		*plane;
	SFace
		*face;
	Vector3
		v0,
		v1,
		v2;
	s32
		i;

	for (i=0; i<obj->NumFaces; i++)
	{
		plane = new Plane;

		face = obj->GetFace( i );
		v0 = *obj->GetVert( 0, face->vtx[0] );
		v1 = *obj->GetVert( 0, face->vtx[1] );
		v2 = *obj->GetVert( 0, face->vtx[2] );

		plane->Normal = CrossProduct( v1 - v0, v2 - v0 );
		plane->Normal.Normalize();
		plane->Dist = -(plane->Normal * v0);
		plane->Normal.Negate();

		Planes.Add( plane );
	}
}
