/*
  MODULE : COL (Intersection & Collision Library)
  AUTHOR : Frederic PHILIPPE
           UBI R&D
  FILE : IntersGO.h (Geometric object intersection)
*/
 
#ifndef COL_INTERSGO_H
#define COL_INTERSGO_H
 
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
 
#include "cpa_expt.h"
 
/* For Global declaration in C files : */
#ifdef COL_GLOBALS
 #define COL_EXTERN extern
#else /* !COL_GLOBALS */
 #define COL_EXTERN
#endif /* !COL_GLOBALS */
 
/*
-----------------------------------------------------------------------------
INCLUDES FILES
-----------------------------------------------------------------------------
*/
 
#include "Futil.h"
#include "Box.h"
#include "Inters.h"
#include "OctreeGO.h"
 
/*
-----------------------------------------------------------------------------
CONSTANT DECLARATION
-----------------------------------------------------------------------------
*/
 
#define INT_C_lMaxProjectingEdges 100
 
/*
-----------------------------------------------------------------------------
TYPES DEFINITION
-----------------------------------------------------------------------------
*/
 
typedef struct COL_tdstShadowElement_
{
  ACP_tdxIndex xElementIndex;
  ACP_tdxIndex xDataElementIndex;
}
  COL_tdstShadowElement;
 
/*
-----------------------------------------------------------------------------
GLOBAL VARIABLE DECLARATION
-----------------------------------------------------------------------------
*/
 
/*
-----------------------------------------------------------------------------
MACROS DEFINITIONS
-----------------------------------------------------------------------------
*/
 
#ifdef USE_ALTIMAPS
/***********************************************************************************************/
/*  Name        : COL_M_vSortSquareSelection*/
/*  Description : Macro that sorts the given coords*/
/*  Author      : Marc FASCIA*/
/***********************************************************************************************/
#define COL_M_vSortSquareSelection( xMin, xMax )                                      \
  if( (xMin) > (xMax) )                                                               \
  {                                                                                   \
    ACP_tdxIndex  xSwap;                                                              \
    xSwap = xMax;                                                                     \
    xMax = xMin;                                                                      \
    xMin = xSwap;                                                                     \
  }
 
/***********************************************************************************************/
/*  Name        : COL_M_vScanSelectedSquares*/
/*  Description : Macro that scans all the squares selected */
/*  Author      : Marc FASCIA*/
/***********************************************************************************************/
#define COL_M_vScanSelectedSquares( xI, xJ, xV, xDx, xSMinX, xSMinY, xSMaxX, xSMaxY )          \
  COL_M_vSortSquareSelection( xSMinX, xSMaxX );                                                \
  COL_M_vSortSquareSelection( xSMinY, xSMaxY );                                                \
  for( (xV)=(xDx)*(xSMinY), (xJ)=(xSMinY) ; (xJ)<((xSMaxY) + 1) ; (xJ)++, (xV)=(xDx)*(xJ) )    \
    for( (xV)+=(xSMinX), (xI)=(xSMinX) ; (xI)<((xSMaxX) + 1) ; (xI)++, (xV)++ )
 
/***********************************************************************************************/
/*  Name        : COL_M_vClipSquareSelection*/
/*  Description : Clips the selection is bigger than the altimap*/
/*  Author      : Marc FASCIA*/
/***********************************************************************************************/
#define COL_M_vClipSquareSelection( p_Altimap, xX, xY, ClipMode )                     \
  if( ClipMode )                                                                      \
  {                                                                                   \
    if( ClipMode & 1 )                                                                \
      xX = 0;                                                                         \
    else                                                                              \
      if( ClipMode & 2 )                                                              \
        xX = p_Altimap->xWidth - 1;                                                   \
                                                                                      \
    if( ClipMode & 4 )                                                                \
      xY = 0;                                                                         \
    else                                                                              \
      if( ClipMode & 8 )                                                              \
        xY = p_Altimap->xDepth - 1;                                                   \
  }
 
/*
-----------------------------------------------------------------------------
FUNCTIONS DECLARATION
-----------------------------------------------------------------------------
*/
 
/***********************************************************************************************/
/*  Name        : COL_bSelectAltimapSquare*/
/*  Description : Sets the square coords of the square placed below or above the point*/
/*  Return val. : True if the point is above or below the altimap. False otherwise*/
/*                      0 : On the map*/
/*                      1 : Left*/
/*                      2 : Right*/
/*                      4 : Below*/
/*                      8 : Above*/
/*  Author      : Marc FASCIA*/
/***********************************************************************************************/
unsigned char COL_ucSelectAltimapSquare( GEO_tdstElementAltimap       * p_Altimap,
                                         MTH3D_tdstVector             * p_TestPoint,
                                         ACP_tdxIndex                 * p_xCoordX,
                                         ACP_tdxIndex                 * p_xCoordY );
 
/***********************************************************************************************/
/*  Name        : COL_ucBuildAltimapSquareTriangle*/
/*  Description : */
/*  Return val. : The type of the next triangle in this square*/
/*  Author      : Marc FASCIA*/
/***********************************************************************************************/
unsigned char COL_ucBuildAltimapSquareTriangle( GEO_tdstElementAltimap       * p_Altimap,      /* The altimap*/
                                                ACP_tdxIndex                 xSx,              /* Square coord X*/
                                                ACP_tdxIndex                 xSy,              /* Square coord Y*/
                                                unsigned char                ucTriangle,       /* Which triangle of this square*/
                                                MTH3D_tdstVector             * p_stV1,         /* Triplet of points*/
                                                MTH3D_tdstVector             * p_stV2,         /*         ''*/
                                                MTH3D_tdstVector             * p_stV3,         /*         ''*/
                                                MTH3D_tdstVector             * p_stNormal,     /* Normal vector*/
                                                ACP_tdxIndex                 * p_xFaceIndex ); /* Index of built face*/
#endif /*USE_ALTIMAPS*/
 
/*----------------------------------------------------------------------------------*/
extern CPA_EXPORT void INT_fn_vViewAndAddFaceElementInList ( ACP_tdxIndex xNbMaxElements,
                                           ACP_tdxIndex *p_xNbElements,
                                           struct GLI_tdstDataOfElement_ *d_stDataOfElement,
                                           MTH3D_tdstVector *p_stVertexA,
                                           ACP_tdxBool bBack,
                                           MTH3D_tdstVector *p_stHit,
                                           ACP_tdxIndex xElements,
                                           ACP_tdxIndex xDataElementIndex );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                         MTH3D_tdstVector *p_stVertexB,
                                                         MTH3D_tdstVector *p_stVectAB,
                                                         GEO_tdstGeometricObject *p_stGeomObj,
                                                         ACP_tdxIndex xNbMaxElements,
                                                         ACP_tdxIndex *p_xNbElements,
                                                         struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithFirstFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                                     MTH3D_tdstVector *p_stVertexB,
                                                                     MTH3D_tdstVector *p_stVectAB,
                                                                     GEO_tdstGeometricObject *p_stGeomObj,
                                                                     MTH3D_tdstVector *p_stHitPoint,
                                                                     ACP_tdxIndex *p_xElement,
                                                                     ACP_tdxIndex *p_xDataElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSegmentWithFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                                                  MTH3D_tdstVector *p_stVectAB,
                                                                                  GEO_tdstGeometricObject *p_stGeomObj );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                         MTH3D_tdstVector *p_stVertexB,
                                                         MTH3D_tdstVector *p_stVectAB,
                                                         GEO_tdstGeometricObject *p_stGeomObj,
                                                         ACP_tdxIndex xNbMaxElements,
                                                         ACP_tdxIndex *p_xNbElements,
                                                         struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bSemiAxeNearPointOfTriangle ( MTH3D_tdstVector *p_stVertexA,
                                                 MTH3D_tdstVector *p_stVectAB,
                                                 MTH_tdxReal xNearDistance,
                                                 MTH3D_tdstVector *p_stVertex1,
                                                 MTH3D_tdstVector *p_stVertex2,
                                                 MTH3D_tdstVector *p_stVertex3,
                                                 MTH_tdxReal *p_xT );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithShadowFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                                      MTH3D_tdstVector *p_stVertexB,
                                                                      MTH3D_tdstVector *p_stVectAB,
                                                                      GEO_tdstGeometricObject *p_stGeomObj,
                                                                      MTH3D_tdstVector *p_stHit,
                                                                      ACP_tdxIndex *p_xElement,
                                                                      ACP_tdxIndex *p_xDataElements
																	  );
 
ACP_tdxBool INT_fn_bIntersectSegmentWithShadowFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                                      MTH3D_tdstVector *p_stVertexB,
                                                                      MTH3D_tdstVector *p_stVectAB,
                                                                      GEO_tdstGeometricObject *p_stGeomObj,
                                                                      MTH3D_tdstVector *p_stHit,
                                                                      ACP_tdxIndex *p_xElement,
                                                                      ACP_tdxIndex *p_xDataElements
																	  );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSphereWithShadowFaceOfGeometricObject ( MTH3D_tdstVector *p_stCenter,
                                                                     MTH_tdxReal xRadius,
                                                                     GEO_tdstGeometricObject *p_stGeomObj,
                                                                     ACP_tdxIndex xNbMaxElements,
                                                                     ACP_tdxIndex *p_xNbElements,
                                                                     COL_tdstShadowElement *d_stShadowElement,
																	 ACP_tdxBool	*p_bBadMaterial);
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSemiAxeWithFaceOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                                MTH3D_tdstVector *p_stVertexB,
                                                                MTH3D_tdstVector *p_stVectAB,
                                                                GEO_tdstGeometricObject *p_stGeomObj );
 
extern CPA_EXPORT void INT_fn_vSearchAndReplacePointInElement ( GEO_tdstGeometricObject *p_stGeomObj,
                                              ACP_tdxIndex xNbElements,
                                              struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithPointOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                          MTH3D_tdstVector *p_stVectAB,
                                                          GEO_tdstGeometricObject *p_stGeomObj,
                                                          ACP_tdxIndex xNbMaxElements,
                                                          ACP_tdxIndex *p_xNbElements,
                                                          struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bEdgePartOfTriangle ( ACP_tdxIndex xEdge1,
                                  ACP_tdxIndex xEdge2,
                                  ACP_tdxIndex xPoint1,
                                  ACP_tdxIndex xPoint2,
                                  ACP_tdxIndex xPoint3 );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bEdgePartOfEdge ( ACP_tdxIndex xEdge1,
                              ACP_tdxIndex xEdge2,
                              ACP_tdxIndex xPoint1,
                              ACP_tdxIndex xPoint2 );
 
extern CPA_EXPORT void INT_fn_vSearchAndReplaceEdgeInElement ( GEO_tdstGeometricObject *p_stGeomObj,
                                              ACP_tdxIndex xNbElements,
                                              struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithEdgeOfGeometricObject ( MTH3D_tdstVector *p_stVertexA,
                                                         MTH3D_tdstVector *p_stVectAB,
                                                         GEO_tdstGeometricObject *p_stGeomObj,
                                                         ACP_tdxIndex xNbMaxElements,
                                                         ACP_tdxIndex *p_xNbElements,
                                                         struct GLI_tdstDataOfElement_ *d_stDataOfElement );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bGetInclusionPointInGeometricObject ( MTH3D_tdstVector *p_stPoint,
                                                         GEO_tdstGeometricObject *p_stGeomObj );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectGeomObjWithGeomObj ( GEO_tdstGeometricObject *p_stGeomObj1,
                                                        POS_tdstCompletePosition *_p_stMatrix1,
                                                        GEO_tdstGeometricObject *p_stGeomObj2,
                                                        POS_tdstCompletePosition *_p_stMatrix2,
                                                        GMT_tdxHandleToGameMaterial *_p_hMat1,
                                                        GMT_tdxHandleToGameMaterial *_p_hMat2 );
 
#ifdef __cplusplus
};
#endif /* __cplusplus */
 
#endif /* COL_INTERSGO_H */
