/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/include/sgx/core/math/trigonometry.inl

*/

#ifndef SGX_CORE_MATH_TRIGONOMETRY_INLINE
#define SGX_CORE_MATH_TRIGONOMETRY_INLINE	1


SGX_INLINE tREAL32 sgxSin(tREAL32 theta)
{
	extern tREAL32			sgxSineTable[];
    const tREAL32 mul = 162.9746617261f;
    return sgxSineTable[((tUINT32)(theta * mul)) & 1023];
}

SGX_INLINE tREAL32 sgxCos(tREAL32 theta)
{
	extern tREAL32			sgxSineTable[];
    const tREAL32 mul = 162.9746617261f;
    return sgxSineTable[(((tUINT32)(theta * mul))+256) & 1023];
}

// This function exists so that platforms with more optimal
// handling of sin/cos can save time, without affecting the rest.
SGX_INLINE void sgxSinCos(tREAL32 &sine, tREAL32 &cosine, const tREAL32 theta)
{
	sine = sgxSin(theta);
	cosine = sgxCos(theta);
}

SGX_INLINE tREAL32 sgxASin(tREAL32 theta)
{
extern float asinf(float);
	return asinf(theta);
}

SGX_INLINE tREAL32 sgxACos(tREAL32 theta)
{
extern float acosf(float);
	return acosf(theta);
}

SGX_INLINE tREAL32 sgxAtan2(tREAL32 dx, tREAL32 dy)
{
	return std::atan2f(dx, dy);
}

SGX_INLINE tREAL32 sgxCosf(tREAL32 theta)
{
extern float cosf(float);
	return cosf(theta);
}

SGX_INLINE tREAL32 sgxSinf(tREAL32 theta)
{
extern float sinf(float);
	return sinf(theta);
}

SGX_INLINE tREAL32 sgxToDegrees(tREAL32 radians)
{
	return radians * 57.295779513082320876798154817014f;
}

SGX_INLINE tREAL32 sgxToRadians(tREAL32 degrees)
{
	return degrees * 0.017453292519943295769236907684886f;
}


/**
Taking the shortest way around, returning +ve or -ve to indicate direction
*/
SGX_INLINE tREAL32 sgxGetDeltaAngle(const tREAL32 fromTheta, const tREAL32 toTheta) {
   if (fromTheta < SGX_PI2 && toTheta > SGX_3PI2) {
		return toTheta - (fromTheta+SGX_2PI);

	} else if (fromTheta > SGX_3PI2 && toTheta < SGX_PI2) {
		return (toTheta+SGX_2PI) - fromTheta;
	}

	return toTheta - fromTheta;
}

#endif /* SGX_CORE_MATH_TRIGONOMETRY_INLINE */
