#ifndef MATHLIB_H
#define MATHLIB_H
/*******************************************************************
 Copyright © 1998 Virtual Paradigm 

 $Release  : 1.0.0                 	    	                       
 $Revision : 												       
	r1.00	27-05-98											   
                                                                 
 $Functions:

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

#define PI 3.145926;
//#define DEGREES_TO_RADIANS PI/(float)180;
//#define RADIANS_TO_DEGREES (float)180/PI;


//***** Defines & Macros ****
#define CROSS(dest,v1,v2)                      \
              dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
              dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
              dest[2]=v1[0]*v2[1]-v1[1]*v2[0];

#define DOT(v1,v2)   ( (v1[0]*v2[0]) + (v1[1]*v2[1]) + (v1[2]*v2[2]) )
#define DOT2D(v1,v2) ( (v1[0]*v2[0]) + (v1[1]*v2[1]) )

#define SUB(dest,v1,v2)          \
            dest[0]=v1[0]-v2[0]; \
            dest[1]=v1[1]-v2[1]; \
            dest[2]=v1[2]-v2[2]; 

#define SUB2D(dest,v1,v2)          \
            dest[0]=v1[0]-v2[0]; \
            dest[1]=v1[1]-v2[1]; 

#define ADD(dest,v1,v2)          \
            dest[0]=v1[0]+v2[0]; \
            dest[1]=v1[1]+v2[1]; \
            dest[2]=v1[2]+v2[2]; 

#define ADD2D(dest,v1,v2)          \
            dest[0]=v1[0]+v2[0]; \
            dest[1]=v1[1]+v2[1]; 

#define NORMALIZE(norm) \
	{double length = (float)sqrt(norm[0]*norm[0] + \
					  	         norm[1]*norm[1] + \
								 norm[2]*norm[2]); \
    if(length==0)\
		length=1;\
				 \
    norm[0]/=length;\
    norm[1]/=length;\
    norm[2]/=length;\
	}

#define MAGNITUDE(length, vector)	\
    length = sqrt( vector[0]*vector[0]+vector[1]*vector[1]+vector[2]*vector[2]);	

#define MAGNITUDE2D(length, vector)	\
    {double x,y;			\
							\
    x=(double)vector[0]; y=(double)vector[1]; \
    length = ( (float)sqrt( x*x+y*y) );	\
	}	


/********************************************************************
	Volume Check
	
	returns
		 true   - Penetrating
		 false  - Non-penetrating
********************************************************************/

#define VOLUMECHECK(result,globDist,centroid1,centroid2) \
	{float deltaDist,dx,dy,dz;				\
	dx = centroid1[0] - centroid2[0];		\
	dy = centroid1[1] - centroid2[1];		\
	dz = centroid1[2] - centroid2[2];		\
	deltaDist = (dx*dx + dy*dy + dz*dz);	\
	result = (deltaDist<globDist)?1:0;}

//***** End Defines & Macros ****

// Functions 
void makeVector(T3F *init, T3F *term, T3F *vector);
extern int visFace(T3F n, T3F v);
extern int lineFacet(T3F p1,T3F p2,T3F pa,
					 T3F pb,T3F pc,float *p);
extern int triTriIntersect(float V0[3],float V1[3],float V2[3],
	  	                   float U0[3],float U1[3],float U2[3]);

#ifdef __cplusplus
extern "C" {
#endif

void buildTables(void);

extern float COS;
extern float SIN;

#ifdef __cplusplus
}
#endif

float pointPlane(T3F point, T3F pa, T3F pb, T3F pc);
float dotProduct(T3F u, T3F v);

int polyTriIntersect(struct TAGBSPSURF *poly, struct TAGMDLSURF *tri);

#endif

