/*
  MODULE : COL (Intersection & Collision Library)
  AUTHOR : Frederic PHILIPPE
           UBI R&D
  FILE : Inters.h (Low level intersection)
*/
 
#ifndef COL_INTERS_H
#define COL_INTERS_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"
 
/*
-----------------------------------------------------------------------------
CONSTANT DECLARATION
-----------------------------------------------------------------------------
*/
 
#define INT_C_xNoSolution   0
#define INT_C_xOneSolution  1
#define INT_C_xTwoSolutions 2
 
#define INT_C_xEpsInc ( MTH_M_xDoubleToReal ( 0.00001 ) )
 
/*
-----------------------------------------------------------------------------
TYPES DEFINITION
-----------------------------------------------------------------------------
*/
 
#if 0
typedef struct INT_tdstIntSegTri_
{
  MTH3D_tdstVector *p_stVertexA;
  MTH3D_tdstVector *p_stVectAB;
  MTH3D_tdstVector *p_stVertex1;
  MTH3D_tdstVector *p_stVertex2;
  MTH3D_tdstVector *p_stVertex3;
  MTH3D_tdstVector *p_stNormal;
  MTH_tdxReal xDPlan;
  MTH_tdxReal xDotProduct;
  MTH_tdxReal xT;
  MTH3D_tdstVector stHit;
}
  INT_tdstIntSegTri, *INT_tdpstIntSegTri;
 
typedef struct INT_tdstIntSegSph_
{
  MTH3D_tdstVector *p_stVertexA;
  MTH3D_tdstVector *p_stVectAB;
  MTH3D_tdstVector *p_stVertex1;
  MTH3D_tdstVector *p_stVertex2;
  MTH3D_tdstVector *p_stVertex3;
  MTH3D_tdstVector *p_stNormal;
  MTH_tdxReal xDPlan;
  MTH_tdxReal xDotProduct;
  MTH_tdxReal xT;
  MTH3D_tdstVector stHit;
}
  INT_tdstIntSegSph, *INT_tdpstIntSegSph;
 
typedef struct INT_tdstIntSegCyl_
{
  MTH3D_tdstVector *p_stVertex1;
  MTH3D_tdstVector *p_stVect12;
  MTH3D_tdstVector *p_stVertexA;
  MTH3D_tdstVector *p_stVectAB;
  MTH_tdxReal xCylinderRadius;
  MTH_tdxReal xTSegment;
  MTH3D_tdstVector stHitSegment;
  MTH3D_tdstVector stHitLine;
}
  INT_tdstIntSegCyl, *INT_tdpstIntSegCyl;
#endif
 
/*
-----------------------------------------------------------------------------
GLOBAL VARIABLE DECLARATION
-----------------------------------------------------------------------------
*/
 
/* statistiques */
#if 0
extern long INT_g_lIntTriangle;
extern long INT_g_lIntTriangleBound;
extern long INT_g_lIntTriangleBack;
extern long INT_g_lIntTriangleT;
#endif
 
/*
-----------------------------------------------------------------------------
FUNCTIONS DECLARATION
-----------------------------------------------------------------------------
*/
 
#define MTH_M_xMulAdd( A, T, B ) ( ((A)*(T)) + (B) )
 
extern CPA_EXPORT ACP_tdxIndex INT_fn_xTrinomeResolution ( MTH_tdxReal xA,
                                         MTH_tdxReal xB,
                                         MTH_tdxReal xC,
                                         MTH_tdxReal *p_xT1,
                                         MTH_tdxReal *p_xT2 );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectBoxWithBox ( MTH3D_tdstVector *p_stMinPoint1,
                                          MTH3D_tdstVector *p_stMaxPoint1,
                                          MTH3D_tdstVector *p_stMinPoint2,
                                          MTH3D_tdstVector *p_stMaxPoint2 );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSphereWithBox ( MTH3D_tdstVector *p_stCenter,
                                             MTH_tdxReal xRadius,
                                             MTH3D_tdstVector *p_stMinPoint,
                                             MTH3D_tdstVector *p_stMaxPoint );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSphereWithSphere ( MTH3D_tdstVector *p_stCenter1,
                                                MTH_tdxReal xRadius1,
                                                MTH3D_tdstVector *p_stCenter2,
                                                MTH_tdxReal xRadius2 );
 
extern CPA_EXPORT unsigned long INT_fn_ulGetPositionPointWithBox ( MTH3D_tdstVector *p_stPoint,
                                                 MTH3D_tdstVector *p_stMinPoint,
                                                 MTH3D_tdstVector *p_stMaxPoint );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bGetInclusionPointInBox ( MTH3D_tdstVector *p_stPoint,
                                      MTH3D_tdstVector *p_stMinPoint,
                                      MTH3D_tdstVector *p_stMaxPoint );
 
extern CPA_EXPORT unsigned long INT_fn_ulGetPositionPointWithEdgeBox ( MTH3D_tdstVector *p_stPoint,
                                                     MTH3D_tdstVector *p_stMinPoint,
                                                     MTH3D_tdstVector *p_stMaxPoint,
                                                     MTH3D_tdstVector *p_stDelta,
                                                     MTH3D_tdstVector *p_stNegDelta );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectEdgeWithBox ( MTH3D_tdstVector *p_stVertex1,
                                    MTH3D_tdstVector *p_stVertex2,
                                    MTH3D_tdstVector *p_stVect12,
                                    MTH3D_tdstVector *p_stMinPoint,
                                    MTH3D_tdstVector *p_stMaxPoint,
                                    unsigned long ulExploredPlan );
 
/*ANNECY TQ 06/03/98{*/
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectEdgeWithBoxWithHit ( MTH3D_tdstVector *p_stVertex1,
                                    MTH3D_tdstVector *p_stVertex2,
                                    MTH3D_tdstVector *p_stVect12,
                                    MTH3D_tdstVector *p_stMinPoint,
                                    MTH3D_tdstVector *p_stMaxPoint,
                                    unsigned long ulExploredPlan,
									MTH3D_tdstVector *p_stHit);
/*ENDANNECY TQ}*/
 
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithBox ( MTH3D_tdstVector *p_stVertex1,
                                       MTH3D_tdstVector *p_stVertex2,
                                       MTH3D_tdstVector *p_stVect12,
                                       MTH3D_tdstVector *p_stMinPoint,
                                       MTH3D_tdstVector *p_stMaxPoint,
                                       MTH_tdxReal *p_xT,
                                       MTH3D_tdstVector *p_stHit,
                                       MTH3D_tdstVector *p_stNormal );
 
#ifdef ACTIVE_EDITOR
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithBox ( MTH3D_tdstVector *p_stVertex1,
                                       MTH3D_tdstVector *p_stVertex2,
                                       MTH3D_tdstVector *p_stVect12,
                                       MTH3D_tdstVector *p_stMinPoint,
                                       MTH3D_tdstVector *p_stMaxPoint,
                                       ACP_tdxBool *bBack,
                                       MTH_tdxReal *p_xT,
                                       MTH3D_tdstVector *p_stHit,
                                       MTH3D_tdstVector *p_stNormal );
#endif
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithBoxForOctree ( MTH3D_tdstVector *p_stVertex1,
                                                       MTH3D_tdstVector *p_stVertex2,
                                                       MTH3D_tdstVector *p_stVect12,
                                                       MTH3D_tdstVector *p_stMinPoint,
                                                       MTH3D_tdstVector *p_stMaxPoint,
                                                       ACP_tdxBool *bBack,
                                                       MTH_tdxReal *p_xT );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectNearSemiAxeWithBoxForOctree ( MTH3D_tdstVector *p_stVertex1,
                                                           MTH3D_tdstVector *p_stVertex2,
                                                           MTH3D_tdstVector *p_stVect12,
                                                           MTH_tdxReal xNearDistance,
                                                           MTH3D_tdstVector *p_stMinPoint,
                                                           MTH3D_tdstVector *p_stMaxPoint,
                                                           MTH_tdxReal *p_xT );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSemiAxeWithBox ( MTH3D_tdstVector *p_stVertex1,
                                                    MTH3D_tdstVector *p_stVertex2,
                                                    MTH3D_tdstVector *p_stVect12,
                                                    MTH3D_tdstVector *p_stMinPoint,
                                                    MTH3D_tdstVector *p_stMaxPoint );
 
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSegmentWithBox ( MTH3D_tdstVector *p_stVertex1,
                                                    MTH3D_tdstVector *p_stVect12,
                                                    MTH3D_tdstVector *p_stMinPoint,
                                                    MTH3D_tdstVector *p_stMaxPoint, 
                                                    MTH_tdxReal *_p_xT);
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bGetPointInTriangle ( MTH3D_tdstVector *p_stPoint,
                                  MTH3D_tdstVector *p_stVertex0,
                                  MTH3D_tdstVector *p_stVertex1,
                                  MTH3D_tdstVector *p_stVertex2,
                                  MTH3D_tdstVector *p_stNormal );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithTriangle ( MTH3D_tdstVector *p_stVertexA,
                                            MTH3D_tdstVector *p_stVectAB,
                                            MTH3D_tdstVector *p_stVertex1,
                                            MTH3D_tdstVector *p_stVertex2,
                                            MTH3D_tdstVector *p_stVertex3,
                                            MTH3D_tdstVector *p_stNormal,
                                            MTH_tdxReal xDPlan,
                                            MTH_tdxReal *p_xT,
                                            MTH3D_tdstVector *p_stHit );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithTriangle2 ( MTH3D_tdstVector *p_stVertexA,
                                                   MTH3D_tdstVector *p_stVectAB,
                                                   MTH3D_tdstVector *p_stVertex1,
                                                   MTH3D_tdstVector *p_stVertex2,
                                                   MTH3D_tdstVector *p_stVertex3,
                                                   MTH3D_tdstVector *p_stNormal,
                                                   MTH_tdxReal xDPlan,
                                                   MTH_tdxReal xDotProduct,
                                                   MTH_tdxReal *p_xT,
                                                   MTH3D_tdstVector *p_stHit );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSegmentWithTriangle ( MTH3D_tdstVector *p_stVertexA,
                                                         MTH3D_tdstVector *p_stVectAB,
                                                         MTH3D_tdstVector *p_stVertex1,
                                                         MTH3D_tdstVector *p_stVertex2,
                                                         MTH3D_tdstVector *p_stVertex3,
                                                         MTH3D_tdstVector *p_stNormal,
                                                         MTH_tdxReal xDPlan );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithTriangle ( MTH3D_tdstVector *p_stVertexA,
                                            MTH3D_tdstVector *p_stVectAB,
                                            MTH3D_tdstVector *p_stVertex1,
                                            MTH3D_tdstVector *p_stVertex2,
                                            MTH3D_tdstVector *p_stVertex3,
                                            MTH3D_tdstVector *p_stNormal,
                                            MTH_tdxReal xDPlan,
                                            ACP_tdxBool *bBack,
                                            MTH_tdxReal *p_xT,
                                            MTH3D_tdstVector *p_stHit );
 
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectTriangleWithBox ( MTH3D_tdstVector *p_stVertex1,
                                        MTH3D_tdstVector *p_stVertex2,
                                        MTH3D_tdstVector *p_stVertex3,
                                        MTH3D_tdstVector *p_stNormal,
                                        MTH3D_tdstVector *p_stMinPoint,
                                        MTH3D_tdstVector *p_stMaxPoint );
 
/*ANNECY TQ 04/03/98{*/
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectTriangleWithBoxWithHit ( MTH3D_tdstVector *p_stVertex1,
                                        MTH3D_tdstVector *p_stVertex2,
                                        MTH3D_tdstVector *p_stVertex3,
                                        MTH3D_tdstVector *p_stNormal,
                                        MTH3D_tdstVector *p_stMinPoint,
                                        MTH3D_tdstVector *p_stMaxPoint,
										MTH3D_tdstVector *p_stHit);
/*ENDANNECY TQ}*/
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bGetInclusionPointInSphere ( MTH3D_tdstVector *p_stPoint,
                                                MTH3D_tdstVector *p_stSphereCenter,
                                                MTH_tdxReal xSphereRadius );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithSphere ( MTH3D_tdstVector *p_stVertex1,
                                          MTH3D_tdstVector *p_stVect12,
                                          MTH3D_tdstVector *p_stSphereCenter,
                                          MTH_tdxReal xSphereRadius,
                                          MTH_tdxReal *p_xT,
                                          MTH3D_tdstVector *p_stHit,
                                          MTH3D_tdstVector *p_stNormal );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSegmentWithSphere ( MTH3D_tdstVector *p_stVertex1,
                                                       MTH3D_tdstVector *p_stVect12,
                                                       MTH3D_tdstVector *p_stSphereCenter,
                                                       MTH_tdxReal xSphereRadius );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithSphere ( MTH3D_tdstVector *p_stVertex1,
                                          MTH3D_tdstVector *p_stVect12,
                                          MTH3D_tdstVector *p_stSphereCenter,
                                          MTH_tdxReal xSphereRadius,
                                          ACP_tdxBool *bBack,
                                          MTH_tdxReal *p_xT,
                                          MTH3D_tdstVector *p_stHit,
                                          MTH3D_tdstVector *p_stNormal );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bDetectIntersectSemiAxeWithSphere ( MTH3D_tdstVector *p_stVertex1,
                                                       MTH3D_tdstVector *p_stVect12,
                                                       MTH3D_tdstVector *p_stSphereCenter,
                                                       MTH_tdxReal xSphereRadius );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSegmentWithCylinder ( MTH3D_tdstVector *p_stVertex1,
                                            MTH3D_tdstVector *p_stVect12,
                                            MTH3D_tdstVector *p_stVertexA,
                                            MTH3D_tdstVector *p_stVectAB,
                                            MTH_tdxReal xCylinderRadius,
                                            MTH_tdxReal *p_xTSegment,
                                            MTH3D_tdstVector *p_stHitSegment,
                                            MTH3D_tdstVector *p_stHitLine );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bIntersectSemiAxeWithCone ( MTH3D_tdstVector *p_stVertex1,
                                               MTH3D_tdstVector *p_stVect12,
                                               MTH3D_tdstVector *p_stTopPoint,
                                               MTH3D_tdstVector *p_stBasePoint,
                                               MTH_tdxReal xBaseRadius,
                                               ACP_tdxBool *bBack,
                                               MTH_tdxReal *p_xT,
                                               MTH3D_tdstVector *p_stHit );
 
extern CPA_EXPORT ACP_tdxBool INT_fn_bGetInclusionPointInCone ( MTH3D_tdstVector *p_stPoint,
                                              MTH3D_tdstVector *p_stTopPoint,
                                              MTH3D_tdstVector *p_stBasePoint,
                                              MTH_tdxReal xBaseRadius );
 
extern CPA_EXPORT MTH_tdxReal INT_fn_xDistancePointToLine ( MTH3D_tdstVector *p_stPoint,
                                          MTH3D_tdstVector *p_stVertexA,
                                          MTH3D_tdstVector *p_stVectAB,
                                          MTH_tdxReal *p_xT,
                                          MTH3D_tdstVector *p_stHit );
 
extern CPA_EXPORT MTH_tdxReal INT_fn_xSqrDistancePointToSegment ( MTH3D_tdstVector *p_stPoint,
                                                MTH3D_tdstVector *p_stVertexA,
                                                MTH3D_tdstVector *p_stVectAB );
 
extern CPA_EXPORT MTH_tdxReal INT_fn_xDistanceSegmentToLine ( MTH3D_tdstVector *p_stVertex1, /* segment */
                                            MTH3D_tdstVector *p_stVect12,
                                            MTH3D_tdstVector *p_stVertexA, /* line */
                                            MTH3D_tdstVector *p_stVectAB,
                                            MTH_tdxReal *p_xTLine,
                                            MTH3D_tdstVector *p_stHitSegment,
                                            MTH3D_tdstVector *p_stHitLine );
 
/* ANNECY AV STATIC {*/
extern CPA_EXPORT ACP_tdxBool INT_fn_bIsOnTriangle ( MTH3D_tdstVector *_p_stPointI,
                                                     MTH3D_tdstVector *_p_stPointA,
                                                     MTH3D_tdstVector *_p_stPointB,
                                                     MTH3D_tdstVector *_p_stPointC );
/* END ANNECY AV STATIC }*/
 
#ifdef __cplusplus
};
#endif /* __cplusplus */
 
#endif /* COL_INTERS_H */
