using MTV3D65;

namespace OctreeRendering
{
    class BoundingFrustum
    {
        public readonly TV_PLANE[] Planes;

        public BoundingFrustum(TV_PLANE[] planes)
        {
            Planes = planes;
        }
        public BoundingFrustum()
        {
            Planes = new TV_PLANE[6];
        }

        public ContainmentType Contains(BoundingBox bb) 
        {
            ContainmentType result = ContainmentType.Contains;
	        for (int i = 0; i < 6; i++) 
            {
                var plane = Planes[i];
                if (Dot(plane, bb.GetPVertex(plane.Normal)) < 0)
			        return ContainmentType.Disjoint;
		        if (Dot(plane, bb.GetNVertex(plane.Normal)) < 0)
			        result = ContainmentType.Intersects;
	        }
            return result;
        }

        static float Dot(TV_PLANE plane, Vector3f point)
        {
            var normal = plane.Normal;
            return normal.x * point.X + normal.y * point.Y + normal.z * point.Z + plane.Dist;
        }
    }
}
