namespace OctreeRendering
{
    struct BoundingBox
    {
        public Vector3f Min;
        public Vector3f Max;

        public BoundingBox(Vector3f min, Vector3f max)
        {
            Min = min;
            Max = max;
        }

        public void GetCorners(Vector3f[] corners)
        {
            corners[0].X = Min.X; corners[0].Y = Max.Y; corners[0].Z = Max.Z;
            corners[1].X = Max.X; corners[1].Y = Max.Y; corners[1].Z = Max.Z;
            corners[2].X = Max.X; corners[2].Y = Min.Y; corners[2].Z = Max.Z;
            corners[3].X = Min.X; corners[3].Y = Min.Y; corners[3].Z = Max.Z;
            corners[4].X = Min.X; corners[4].Y = Max.Y; corners[4].Z = Min.Z;
            corners[5].X = Max.X; corners[5].Y = Max.Y; corners[5].Z = Min.Z;
            corners[6].X = Max.X; corners[6].Y = Min.Y; corners[6].Z = Min.Z;
            corners[7].X = Min.X; corners[7].Y = Min.Y; corners[7].Z = Min.Z;
        }

        public BoundingBox Grow(Vector3f growth)
        {
            return new BoundingBox(Min - growth, Max + growth);
        }

        public Vector3f GetPVertex(Vector3f normal)
        {
	        var p = Min;
	        if (normal.X >= 0)  p.X = Max.X;
	        if (normal.Y >= 0)  p.Y = Max.Y;
	        if (normal.Z >= 0)  p.Z = Max.Z;
            return p;
        }

        public Vector3f GetNVertex(Vector3f normal)
        {
	        var n = Max;
	        if (normal.X >= 0)  n.X = Min.X;
	        if (normal.Y >= 0)  n.Y = Min.Y;
            if (normal.Z >= 0)  n.Z = Min.Z;
            return n;
        }

        public bool Contains(Vector3f point)
        {
            return point.X >= Min.X && point.X <= Max.X &&
                   point.Y >= Min.Y && point.Y <= Max.Y &&
                   point.Z >= Min.Z && point.Z <= Max.Z;
        }

        public override string ToString()
        {
            return string.Format("{{Min:{0} Max:{1}}}", Min, Max);
        }
    }
}
