2019-08-19 19:19:58 +08:00
|
|
|
|
#ifndef HGL_ALGORITHM_MATH_FAST_TRIANGLE_FUNCTION_INCLUDE
|
|
|
|
|
#define HGL_ALGORITHM_MATH_FAST_TRIANGLE_FUNCTION_INCLUDE
|
|
|
|
|
|
2021-05-10 11:25:21 +08:00
|
|
|
|
#include<hgl/math/MathConst.h>
|
|
|
|
|
#include<math.h>
|
2019-08-19 19:19:58 +08:00
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
double Lsin(int angle); ///<低精度sin计算,注意传入的参数为角度而非弧度
|
|
|
|
|
double Lcos(int angle); ///<低精度cos计算,注意传入的参数为角度而非弧度
|
|
|
|
|
void Lsincos(int angle, double &s, double &c); ///<低精度sin+cos计算,注意传入的参数为角度而非弧度
|
|
|
|
|
|
2021-05-10 11:25:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* Approximates acos(x) with a max absolute error of 9.0x10^-3.
|
|
|
|
|
* Valid in the range -1..1.
|
|
|
|
|
*/
|
2023-02-07 22:42:42 +08:00
|
|
|
|
inline const float Lacos(float x)
|
2021-05-10 11:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
// Lagarde 2014, "Inverse trigonometric functions GPU optimization for AMD GCN architecture"
|
|
|
|
|
// This is the approximation of degree 1, with a max absolute error of 9.0x10^-3
|
|
|
|
|
float y = fabs(x);
|
|
|
|
|
float p = -0.1565827 * y + 1.570796;
|
|
|
|
|
p *= sqrt(1.0 - y);
|
|
|
|
|
return x >= 0.0 ? p : HGL_PI - p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Approximates acos(x) with a max absolute error of 9.0x10^-3.
|
|
|
|
|
* Valid only in the range 0..1.
|
|
|
|
|
*/
|
2023-02-07 22:42:42 +08:00
|
|
|
|
inline const float LacosPositive(float x)
|
2021-05-10 11:25:21 +08:00
|
|
|
|
{
|
2023-02-07 22:42:42 +08:00
|
|
|
|
return (-0.1565827 * x + 1.570796) * sqrt(1.0 - x);
|
2021-05-10 11:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 22:42:42 +08:00
|
|
|
|
inline constexpr double ApproxSin(double angle)
|
|
|
|
|
{
|
|
|
|
|
return angle*(1.27323954f-angle*angle*(0.405284735f-angle*angle*(0.075133f-angle*angle*0.0078f)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline constexpr double ApproxCos(double angle)
|
|
|
|
|
{
|
|
|
|
|
return 1.27323954f-angle*angle*(0.405284735f-angle*angle*(0.075133f-angle*angle*0.0078f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline constexpr double ApproxAtan2(double x,double y)
|
|
|
|
|
{
|
|
|
|
|
return x/(1.27323954f+y*y*(0.405284735f+y*y*(0.075133f+y*y*0.0078f)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline constexpr double ApproxAtan(double z)
|
|
|
|
|
{
|
|
|
|
|
return z*(1.27323954f-z*z*(0.405284735f-z*z*(0.075133f-z*z*0.0078f)));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 19:19:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* 低精度atan函数
|
|
|
|
|
*/
|
2023-02-07 22:42:42 +08:00
|
|
|
|
inline constexpr double Latan(double z)
|
2019-08-19 19:19:58 +08:00
|
|
|
|
{
|
|
|
|
|
constexpr double n1 = 0.97239411f;
|
|
|
|
|
constexpr double n2 = -0.19194795f;
|
|
|
|
|
|
|
|
|
|
return (n1 + n2 * z * z) * z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double Latan2(double y, double x); ///<低精度atan2函数
|
2020-01-20 16:57:33 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 近似Pow2.2,使用此函数等于pow(x,2.2)
|
|
|
|
|
*/
|
2023-02-07 22:42:42 +08:00
|
|
|
|
inline constexpr double LPow22(double x)
|
2020-01-20 16:57:33 +08:00
|
|
|
|
{
|
2020-01-20 17:05:13 +08:00
|
|
|
|
return x*(1.12*x - 0.12);
|
2020-01-20 16:57:33 +08:00
|
|
|
|
}
|
2019-08-19 19:19:58 +08:00
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_ALGORITHM_MATH_FAST_TRIANGLE_FUNCTION_INCLUDE
|