#ifndef __BVH_OPTIMIZER_H__
#define __BVH_OPTIMIZER_H__

#include "BVHBottom.h"
#include "BinTrees.h"

namespace VRUT
{

	class BVHOptimizer
	{

	public:

		struct BVHOptNode : public bTree::BinTreeNodeWithAABB<BVHOptNode>
		{
			int offset;
			unsigned int t;

			static void UpdateBVNodeStatic(BVHOptNode * node);

			BVHOptNode(BVHOptNode * parent, BVHOptNode * left, BVHOptNode * right);
			~BVHOptNode(void);
			
			void UpdateBVNode(void);

		};

	private:

		BVHOptNode * BVHToOpt(BVHBottom * bvh);
		void OptToBVH(BVHOptNode * optRoot, BVHBottom * bvh);

		float GetCost(BVHOptNode * node);
		unsigned int ComputeTrisCount(BVHOptNode * node);
		unsigned int ComputeNodesCount(BVHOptNode * node);
		void ReorderTriangles(BVHOptNode * root, BVHBottom * bvh);
		void CollapseLeaf(BVHOptNode * node);
		void CollapseLeaves(BVHOptNode * root, BVHBottom * bvh);

	public:

		void Optimize(BVHBottom * bvh, int ratio, int maxIterations, int threshold, int termThreshold);

	};

}

#endif /* __BVH_OPTIMIZER_H__ */
