#include "ProxyHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	ChildProxy::OptimizeFlatShading(OptimizeFlatShadingProcess *process)
{
	Check_Object(this);
	Check_Object(process);
	process->OptimizeCallback(this);
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	PolygonMeshProxy::OptimizeFlatShading(OptimizeFlatShadingProcess *process)
{
	Check_Object(this);
	Check_Object(process);

	//
	//--------------------------
	// Make sure we can continue
	//--------------------------
	//
	process->OptimizeCallback(this);
	if (!process->continueProcess)
		return true;

	STOP(("Not implemented"));
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	GroupProxy::OptimizeFlatShading(OptimizeFlatShadingProcess *process)
{
	Check_Object(this);
	Check_Object(process);

	//
	//--------------------------
	// Make sure we can continue
	//--------------------------
	//
	process->OptimizeCallback(this);
	if (!process->continueProcess)
		return true;

	//
	//-------------------
	// Split the children
	//-------------------
	//
	unsigned child_count = GetChildCount();
	ChildProxy *child = UseFirstChildProxy();
	for (unsigned i=0; i<child_count; ++i)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (child->OptimizeFlatShading(process))
			child->DetachReference();
		child = next;
		if (!process->continueProcess)
		{
			if (child)
				child->DetachReference();
			break;
		}
	}

	//
	//-------------------------------------------
	// Make sure to discard any remaining proxies
	//-------------------------------------------
	//
	if (child)
		child->DetachReference();
	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	SceneProxy::OptimizeFlatShading(OptimizeFlatShadingProcess *process)
{
	Check_Object(this);
	Check_Object(process);

	//
	//--------------------------
	// Make sure we can continue
	//--------------------------
	//
	process->OptimizeCallback(this);
	if (!process->continueProcess)
		return;

	//
	//-------------------
	// Split the children
	//-------------------
	//
	unsigned child_count = GetChildCount();
	ChildProxy *child = UseFirstChildProxy();
	unsigned i;
	for (i=0; i<child_count; ++i)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (child->OptimizeFlatShading(process))
			child->DetachReference();
		child = next;
		if (!process->continueProcess)
		{
			if (child)
				child->DetachReference();
			break;
		}
	}

	//
	//------------------------------------------------------------------------
	// Make sure to discard any remaining proxies, and bypass the second phase
	// of the sort if the process has been aborted
	//------------------------------------------------------------------------
	//
	if (child)
		child->DetachReference();
}
