/******************************************************************************
	enmath.h
******************************************************************************/

#ifndef ENMATH_H
#define ENMATH_H

/******************************************************************************
	define flags
******************************************************************************/
#define USE_SIN_TABLE

/******************************************************************************
	define constants
******************************************************************************/

/******************************************************************************
	define functions
******************************************************************************/

/*---------------------------------------------------------------------------*/
#define	ABS(x)		((x) >= 0 ? (x) : -(x))
#define	SGN(x)		((x) == 0 ? 0 : ((x) > 0 ? 1 : -1))
#define	RAD(d)		((float)(d) / 180.F * M_PI)
#define	ROUND(x)	((x) >= 0.F ? (int)((x) + .5F) : (int)((x) - .5F))
#define	LENGTH(x,y,z)	sqrtf((x) * (x) + (y) * (y) + (z) * (z))
#define LENGTH2(x,y)	sqrtf((x) * (x) + (y) * (y))

#define	ABSA(x)		((x) %= 360, (x) >= 0 ? (x) : 360 + (x)) 

/*---------------------------------------------------------------------------*/
#ifdef USE_SIN_TABLE
#define	sint(x)		sin_t[(int)((x) / 360.F * SIN_RANGE) & SIN_RANGE - 1]
#define	cost(x)		sin_t[((int)((x) / 360.F * SIN_RANGE) + SIN_RANGE / 4) & SIN_RANGE - 1]
#else
#define sint(x)		sinf(RAD(x))
#define cost(x)		cosf(RAD(x))
#endif

/******************************************************************************
	external variables
******************************************************************************/

/******************************************************************************
	external functions
******************************************************************************/

extern void	Normalize(float ax, float ay, float az,
	float *bx, float *by, float *bz);
extern float DotProductCos(float ax, float ay, float az,
	float bx, float by, float bz);
extern void	CrossProduct(float ax, float ay, float az,
	float bx, float by, float bz, 
	float *cx, float *cy, float *cz);
extern void	CrossProductNorm(float ax, float ay, float az,
	float bx, float by, float bz, 
	float *cx, float *cy, float *cz);
	
extern float	acos(float x);
extern float	atan2(float y, float x);

extern int	DEFA(int x, int y);
extern int	DEFA360(int x, int y);
extern int	SGNA(int x, int y);
extern int	ADJT(int x);
extern int	DEFAT(int x, int y);
extern int	SGNAT(int x, int y);
extern float	FABSA(float x);
extern float	FDEFA(float x, float y);
extern float	FDEFA360(float x, float y);
extern int	FSGNA(float x, float y);
extern float	FADJT(float x);
extern float	FDEFAT(float x, float y);
extern int	FSGNAT(float x, float y);

extern void egApplyMtx(Mtxf mf, float x, float y, float z,
	float *ax, float *ay, float *az);
extern void egApplyMtxR(Mtxf mf, float x, float y, float z,
	float *ax, float *ay, float *az);
extern void	egMtxF2L(Mtxf mf, Mtx *m);
extern void	egTranslateF(Mtxf mf, float x, float y, float z);
extern void	egTranslate(Mtx *m, float x, float y, float z);
extern void	egRotateF(Mtxf mf, float a, float x, float y, float z);
extern void	egRotate(Mtx *m, float a, float x, float y, float z);
extern void	egRotateTransF(Mtxf mf, float a, float x, float y, float z,
	float tx, float ty, float tz);
extern void	egRotateTrans(Mtx *m, float a, float x, float y, float z,
	float tx, float ty, float tz);
extern void	egRotXYZTransF(Mtxf mf, float rx, float ry, float rz,
	float tx, float ty, float tz);
extern void	egRotXYZTrans(Mtx *m, float rx, float ry, float rz,
	float tx, float ty, float tz);
extern void	egRotZXYTransF(Mtxf mf, float rx, float ry, float rz,
	float tx, float ty, float tz);
extern void	egRotZXYTrans(Mtx *m, float rx, float ry, float rz,
	float tx, float ty, float tz);
extern void	egScaleF(Mtxf mf, float x, float y, float z);
extern void	egScale(Mtx *m, float x, float y, float z);

extern void	InitRnd(void);
extern int	GetRnd(int n);
extern float	GetRndF(float n);

#endif
