#include "ACP_base.h"
#include "GMT.h"
 
/*memory management of geometry.*/
/*
#include "ErrGEO.h"
#include "MemGEO.h"
*/
/* 007 #include "incGEO.h"*/
/*#include "MmgGEO.h"*/
/* 007 #include "incGLI.h"*/
/* 007*/
#include "geo.h"
#include "gli.h"
 
 
 
#include "SPO/HieMtStk.h"
 
extern POS_tdstCompletePosition *g_p_stCurrentMatrix ;/*provisoire*/
/*utiliser des EXTERN à l'occasion ...*/
GEO_tdxHandleToMatrix HIE_g_a_hMatrixStack[HIE_C_lMaxMatrixInStack];
GEO_tdxHandleToMatrix HIE_g_hCurrentMatrix ;
long HIE_g_lNbMatrixInStack = 0;
 
#define HIE_M_vUpdateStack()\
{\
	g_p_stCurrentMatrix=HIE_g_hCurrentMatrix=HIE_g_a_hMatrixStack[HIE_g_lNbMatrixInStack-1];\
}
 
 
GEO_tdxHandleToMatrix GEO_fn_hCreateMatrix(void)
{
	GEO_tdxHandleToMatrix hReturn;
 
	MMG_fn_vAddMemoryInfo( MMG_C_lTypePosition , MMG_C_lSubTypePosition , 0 );
	GEO_M_CPAMalloc(hReturn, GEO_tdxHandleToMatrix, sizeof(POS_tdstCompletePosition), E_uwGEONotEnoughtMemory);
		POS_fn_vSetIdentityMatrix(hReturn);
	return hReturn;
}
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void GEO_fn_vDestroyMatrix(GEO_tdxHandleToMatrix _hMatrix)
{
	GEO_M_CPAFree(_hMatrix);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
void HIE_fn_vCreateMatrixStack(void)
{
	HIE_g_a_hMatrixStack[0] = GEO_fn_hCreateMatrix();
	HIE_g_lNbMatrixInStack = 1;
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void HIE_fn_vDestroyMatrixStack(void)
{
	GEO_fn_vDestroyMatrix( HIE_g_a_hMatrixStack[0] );
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*erase all the stack and then put the identity on the top*/
void HIE_fn_vInitMatrixStack(void)
{
	HIE_g_lNbMatrixInStack=0;
	POS_fn_vSetIdentityMatrix(HIE_g_a_hMatrixStack[HIE_g_lNbMatrixInStack]);
	HIE_g_lNbMatrixInStack++;
	HIE_M_vUpdateStack();
 
}
 
ACP_tdxBool HIE_fn_bLoadIdentity(void)
{
  if( HIE_g_lNbMatrixInStack == HIE_C_lMaxMatrixInStack )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
 
	HIE_g_a_hMatrixStack[HIE_g_lNbMatrixInStack] = HIE_g_a_hMatrixStack[0];
	HIE_g_lNbMatrixInStack++;
	HIE_M_vUpdateStack();
	return OK;
}
 
/* remove the top of the stack*/
ACP_tdxBool HIE_fn_bPopMatrix(void)
{
	if( HIE_g_lNbMatrixInStack == 0 )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
 
	HIE_g_lNbMatrixInStack--;
	HIE_M_vUpdateStack();
	return OK;
}
 
/*put the parameter on the top of the stack*/
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool HIE_fn_bLoadMatrix(GEO_tdxHandleToMatrix _hMatrix)
{
	return HIE_fn_bStoreMatrix(_hMatrix);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
/*empty the stack*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void HIE_fn_vResetMatrixStack(void)
{
	HIE_g_lNbMatrixInStack=0;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
ACP_tdxBool HIE_fn_bStoreMatrix( GEO_tdxHandleToMatrix _hMatrix )
{
	if( HIE_g_lNbMatrixInStack == HIE_C_lMaxMatrixInStack )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
	
	HIE_g_a_hMatrixStack[HIE_g_lNbMatrixInStack] = _hMatrix;
 
	HIE_g_lNbMatrixInStack++;
	HIE_M_vUpdateStack();
	return OK;
}
 
/*//////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////////////////////////*/
/* New stack used to push the camera matrix first*/
/*//////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////////////////////////*/
 
extern POS_tdstCompletePosition  *g_p_stCurrentInCamCoordsMatrix;
POS_tdstCompletePosition		 HIE_g_a_stCameraMatrixStack[HIE_C_lMaxMatrixInStack+1];
unsigned long					 HIE_g_lNbMatrixInCameraStack;
 
 
#define HIE_M_vUpdateCameraMatrixStack()	\
{\
	g_p_stCurrentInCamCoordsMatrix = &HIE_g_a_stCameraMatrixStack[HIE_g_lNbMatrixInCameraStack-1];	\
}
 
/* empty the stack and push the camera mtrix of the given viewport*/
ACP_tdxBool HIE_fn_bInitCameraMatrixStack(GLD_tdxHandleToViewportAttributes _hVpt)
{
	HIE_g_lNbMatrixInCameraStack=0;
#if defined(ACTIVE_EDITOR)
	if( ((( GLI_tdstSpecificAttributesFor3D *)_hVpt->p_vSpecificToXD)->p_stCam) == NULL )
		return NOK;
#endif
	POS_fn_vCopyMatrix(&HIE_g_a_stCameraMatrixStack[HIE_g_lNbMatrixInCameraStack] , &((( GLI_tdstSpecificAttributesFor3D *)_hVpt->p_vSpecificToXD)->p_stCam)->stMatrix);
	HIE_g_lNbMatrixInCameraStack++;
	HIE_M_vUpdateCameraMatrixStack();
	return OK;
}
 
/* update the stak with global matrix of the given SuperObject*/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 07/06/99 */
ACP_tdxBool HIE_fn_bStoreOnCameraMatrixStack( GEO_tdxHandleToMatrix _hMatrix )
{
	if( !HIE_g_lNbMatrixInCameraStack || HIE_g_lNbMatrixInCameraStack == HIE_C_lMaxMatrixInStack )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
 
	POS_fn_vMulMatrixMatrix( &HIE_g_a_stCameraMatrixStack[HIE_g_lNbMatrixInCameraStack] , &HIE_g_a_stCameraMatrixStack[0]
																					    ,  _hMatrix );
	HIE_g_lNbMatrixInCameraStack++;
	HIE_M_vUpdateCameraMatrixStack();
 
	return OK;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 07/06/99 */

/* update the stak with local matrix of the given SuperObject*/
ACP_tdxBool HIE_fn_bPushOnCameraMatrixStack( GEO_tdxHandleToMatrix _hMatrix )
{
	if( !HIE_g_lNbMatrixInCameraStack || HIE_g_lNbMatrixInCameraStack == HIE_C_lMaxMatrixInStack )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
 
	POS_fn_vMulMatrixMatrix( &HIE_g_a_stCameraMatrixStack[HIE_g_lNbMatrixInCameraStack] , &HIE_g_a_stCameraMatrixStack[HIE_g_lNbMatrixInCameraStack-1]
																					    ,  _hMatrix );
	HIE_g_lNbMatrixInCameraStack++;
	HIE_M_vUpdateCameraMatrixStack();
 
	return OK;
}
 
/* */
ACP_tdxBool HIE_fn_bPopOnCameraMatrixStack()
{
	if( HIE_g_lNbMatrixInCameraStack == 0 )
#if !defined(U64)
		return NOK;
#else
	ASM_BREAK;
#endif /* U64 */
 
	HIE_g_lNbMatrixInCameraStack--;
	assert(HIE_g_lNbMatrixInCameraStack>0);
	HIE_M_vUpdateCameraMatrixStack();
 
	return OK;
}
 
/**/
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void HIE_fn_vSetCurrentInCamCoordsMatrix (POS_tdstCompletePosition  *p_stMatrix)
{
	g_p_stCurrentInCamCoordsMatrix = p_stMatrix;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
POS_tdstCompletePosition  * HIE_fn_pGetCurrentInCamCoordsMatrix (void)
{
	return g_p_stCurrentInCamCoordsMatrix;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
