/****************************************************************************************

		DataCompUtils.cpp

		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Author:	Charles
		Date:	05-07-99

		Revision History

		when		who			what & why
		==========	==========	======================================================
		05-07-99	Charles		Created

*****************************************************************************************/

#include "I3DShTreeElement.h"
#include "I3DShLightsource.h"
#include "IShComponent.h"
#include "I3DShRenderFeature.h"
#include "DataCompUtils.h"
#include "I3DShInstance.h"

void DataComponentUtils::GetLightDataComponent(I3DShLightsource *light,IDType compID,IShParameterComponent **component)
{
	TMCCountedPtr<I3DShTreeElement> lightTree;		// we get the dataComponent

	light->QueryInterface(IID_I3DShTreeElement,(void**)&lightTree);
	ThrowIfNil(lightTree);

	TMCCountedPtr<I3DShDataComponent> dataComponent;
	lightTree->GetDataComponentByID(&dataComponent,compID);
	ThrowIfNil(dataComponent);
	
	dataComponent->QueryInterface(IID_IShParameterComponent,(void**)component);
	ThrowIfNil(component);
}


void DataComponentUtils::GetInstanceDataComponent(I3DShInstance *instance,IDType compID,IShParameterComponent **component)
{
	TMCCountedPtr<I3DShTreeElement> tree;		// we get the dataComponent

	instance->QueryInterface(IID_I3DShTreeElement,(void**)&tree);
	ThrowIfNil(tree);

	TMCCountedPtr<I3DShDataComponent> dataComponent;
	tree->GetDataComponentByID(&dataComponent,compID);
	
	if (dataComponent)
	{
		dataComponent->QueryInterface(IID_IShParameterComponent,(void**)component);
		ThrowIfNil(component);
	}
}


boolean DataComponentUtils::HasLightCone(I3DShTreeElement *tree)
{
	TMCCountedPtr<I3DShLightsource> light;
	tree->QueryInterface(IID_I3DShLightsource,(void**)&light);
	MCAssertPointerCheck(light);

	boolean			hasLightCone=false;
	boolean			hasLightSphere=false;
	real			halfAngle;
	TTransform3D	transform;

	if (light)
		light->GetLightInfo(hasLightCone,hasLightSphere,halfAngle,transform);

	return hasLightCone;
}

boolean DataComponentUtils::HasLightSphere(I3DShTreeElement *tree)
{
	TMCCountedPtr<I3DShLightsource> light;
	tree->QueryInterface(IID_I3DShLightsource,(void**)&light);
	MCAssertPointerCheck(light);

	boolean			hasLightCone=false;
	boolean			hasLightSphere=false;
	real			halfAngle;
	TTransform3D	transform;

	if (light)
		light->GetLightInfo(hasLightCone,hasLightSphere,halfAngle,transform);

	return hasLightSphere;
}


