/*=========================================================================
* PhObjs.c : Physical Objects functions
*
* Version              1.0
* Creation date        18/02/97
* Revision date        18/03/97
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1997
*=======================================================================*/

#define PO_FRIEND
#include "ACP_Base.h"

#include "GEO.h"
#include "GLI.h"      
#include "COL.h"      
#include "SPO.h"
#include "PCS.h"

#include "PO.h"


/* ***************************************************************************** */
/* **************** Creation and first init of a PO **************************** */
/* ***************************************************************************** */


/*-----------------------------------------------------------------------------
 *  Description :   Allocate a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/02/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
PO_tdxHandleToPhysicalObject PO_fn_hAllocPhysicalObject(void)
{
  PO_tdstPhysicalObject *p_stPhysicalObject;

  /*p_stPhysicalObject = (struct PO_tdstPhysicalObject_ *)Mmg_fn_p_vAlloc(sizeof(struct PO_tdstPhysicalObject_));
	if ((stCurrentModeInfo.ucModuleId != 7) || (stCurrentModeInfo.ucMode)){
		p_stPhysicalObject->_hVisualSet = NULL;// just for breack
	}*/
    MMG_fn_vAddMemoryInfo (MMG_C_lTypePO , MMG_C_lSubTypePOStructure , NULL);
	GEO_M_CPAMalloc(p_stPhysicalObject,struct PO_tdstPhysicalObject_ *, sizeof(struct PO_tdstPhysicalObject_), E_uwGEONotEnoughtMemory);
  //Set  (char *s_szBankName,char *s_szObjectName)
  //p_stPhysicalObject->p_stGeometricObject=GLI_p_stFindOrLoadGeometric(s_szBankName,s_szObjectName);

  p_stPhysicalObject->_hVisualSet = NULL;
	p_stPhysicalObject->_hCollideSet = NULL;
	p_stPhysicalObject->_hBoundingVolume = NULL;


  return (p_stPhysicalObject);
}


/*-----------------------------------------------------------------------------
 *  Description :   Free a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/02/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
#ifndef _FIRE_DEADCODE_U64_
void PO_fn_vFreePhysicalObject(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  Mmg_fn_vFree4Ch(_hPhysicalObject,C_ucMmgDefaultChannel);
}
#endif /* _FIRE_DEADCODE_U64_ */


/* ***************************************************************************** */
/* ******************** Access functions to Object in PO *********************** */
/* ***************************************************************************** */
/*-----------------------------------------------------------------------------
 *  Description :   Get the Object in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
/*
void * PO_fn_p_vGetObject(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  return(_hPhysicalObject->p_vObject);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   Set the Object in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
/*
void PO_fn_p_vSetObject(PO_tdxHandleToPhysicalObject _hPhysicalObject,void * p_vObject)
{
  _hPhysicalObject->p_vObject = p_vObject;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   Get the Visual Set Object in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
GEO_tdxHandleToVisualSet PO_fn_hGetVisualSet(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  return(_hPhysicalObject->_hVisualSet);
}

/*-----------------------------------------------------------------------------
 *  Description :   Set the Visual Set Object in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
#ifndef _FIRE_DEADCODE_U64_
void PO_fn_vSetVisualSet(PO_tdxHandleToPhysicalObject _hPhysicalObject
                        ,GEO_tdxHandleToVisualSet _hNewVisualSet)
{
  _hPhysicalObject->_hVisualSet = _hNewVisualSet;
}
#endif /* _FIRE_DEADCODE_U64_ */


/* ***************************************************************************** */
/* **************** Access functions to CollideSet in PO *********************** */
/* ***************************************************************************** */
/*-----------------------------------------------------------------------------
 *  Description :   Get the Collide Set in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 19/02/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
PCS_tdxHandleToPhysicalCollSet PO_fn_hGetCollideSet(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  return(_hPhysicalObject->_hCollideSet);
}

/*-----------------------------------------------------------------------------
 *  Description :   Set the Collide Set in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 19/02/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
#ifndef _FIRE_DEADCODE_U64_
void PO_fn_vSetCollideSet(PO_tdxHandleToPhysicalObject _hPhysicalObject
                         ,PCS_tdxHandleToPhysicalCollSet _hNewCollideSet)
{
  _hPhysicalObject->_hCollideSet = _hNewCollideSet;
}
#endif /* _FIRE_DEADCODE_U64_ */


/* ***************************************************************************** */
/* **************** Access functions to BoundingVolume in PO ******************* */
/* ***************************************************************************** */
/*-----------------------------------------------------------------------------
 *  Description :   Get the Bounding Volume in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
GEO_tdxHandleToBoundingSphere PO_fn_hGetBoundingVolume(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  return(_hPhysicalObject->_hBoundingVolume);
}

/*-----------------------------------------------------------------------------
 *  Description :   Set the Bounding Volume in a Physical Object
 *-----------------------------------------------------------------------------
 *  Creation date : 18/03/97            Author : Sebastien DAVID
 *-----------------------------------------------------------------------------*/
void PO_fn_vSetBoundingVolume(PO_tdxHandleToPhysicalObject _hPhysicalObject,
                              GEO_tdxHandleToBoundingSphere _hNewBoundingVolume)
{
  _hPhysicalObject->_hBoundingVolume = _hNewBoundingVolume;
}




// temporairement !!!!!!!!!!!!!!!

void HIE_fn_vComputeOnePoBoundingVolume(PO_tdxHandleToPhysicalObject _hPhysicalObject)
{
  MTH3D_tdstVector stLocalCenter;
  MTH_tdxReal xRadius;
  GEO_tdxHandleToBoundingSphere hBoundingSphere;
  ACP_tdxHandleOfObject hGeometricObject;
  MTH_tdxReal xReal;
  
  GEO_fn_vCreateBoundingSphere(&hBoundingSphere);	
  
  GLI_vGetVisualSetLOD(PO_fn_hGetVisualSet(_hPhysicalObject), 0, &xReal, &hGeometricObject);
  xRadius=0.0f;
  
#ifndef U64
#ifdef USE_ALTIMAPS
  if( hGeometricObject->d_xListOfElementsTypes[ 0 ] == GEO_C_xElementAltimap )
  {
	  //--- Altimap ---
	  GEO_vGetAltimapOrigin( hGeometricObject, 0, &stLocalCenter );
	  GEO_fn_vAddAltimapToSphere(hGeometricObject,&stLocalCenter,&xRadius);
  }
  else	
#endif //USE_ALTIMAPS
  {
	  //--- NOT Altimap ---
	  MTH3D_M_vCopyVector(&stLocalCenter, hGeometricObject->d_stListOfPoints);
	  GEO_fn_vAddObjectToSphere(hGeometricObject,&stLocalCenter,&xRadius);
  }
#else
  if( hGeometricObject->d_xListOfElementsTypes != NULL ) // Temporary antibug for sprites AR980327
  {
#ifdef USE_ALTIMAPS
	  if( hGeometricObject->d_xListOfElementsTypes[ 0 ] == GEO_C_xElementAltimap )
	  {
		  //--- Altimap ---
		  GEO_vGetAltimapOrigin( hGeometricObject, 0, &stLocalCenter );
		  GEO_fn_vAddAltimapToSphere(hGeometricObject,&stLocalCenter,&xRadius);
	  }
	  else
#endif //USE_ALTIMAPS
		  if(hGeometricObject->d_stListOfPoints!=NULL)	
		  {
			  //--- NOT Altimap ---
			  MTH3D_M_vCopyVector(&stLocalCenter, hGeometricObject->d_stListOfPoints);
			  GEO_fn_vAddObjectToSphere(hGeometricObject,&stLocalCenter,&xRadius);
		  }
		  else
		  {
			  MTH3D_M_vSetVectorElements(&stLocalCenter,
				  MTH_M_xFloatToReal(hGeometricObject->a3_fCenter[0]),
				  MTH_M_xFloatToReal(hGeometricObject->a3_fCenter[1]),
				  MTH_M_xFloatToReal(hGeometricObject->a3_fCenter[2]));
			  xRadius=MTH_M_xFloatToReal(hGeometricObject->fRadius);
		  }
  }
  else
  {
	  MTH3D_M_vSetVectorElements(&stLocalCenter,MTH_C_ZERO,MTH_C_ZERO,MTH_C_ZERO);
	  xRadius=0.0f;
  }
#endif
  
  GEO_fn_vSetBoundingSphere(hBoundingSphere, &stLocalCenter, xRadius);
  PO_fn_vSetBoundingVolume(_hPhysicalObject ,hBoundingSphere);
}

