diff --git a/inc/hgl/TypeFunc.h b/inc/hgl/TypeFunc.h index 37b5d2c..54528f2 100644 --- a/inc/hgl/TypeFunc.h +++ b/inc/hgl/TypeFunc.h @@ -179,234 +179,9 @@ namespace hgl template<> inline int32 HGL_INTEGER_MIN() { return HGL_S32_MIN; } template<> inline int64 HGL_INTEGER_MIN() { return HGL_S64_MIN; } - /** - * 星期每天枚举 - */ - enum class Weekday - { - Sunday=0, ///<星期天 - Monday, ///<星期一 - Tuesday, ///<星期二 - Wednesday, ///<星期三 - Thursday, ///<星期四 - Friday, ///<星期五 - Saturday, ///<星期六 - - BEGIN_RANGE =Sunday, - END_RANGE =Saturday, - RANGE_SIZE =END_RANGE-BEGIN_RANGE+1 - }; - - /** - * 月份枚举 - */ - enum class Month - { - January=1, ///<一月 - February, ///<二月 - March, ///<三月 - April, ///<四月 - May, ///<五月 - June, ///<六月 - July, ///<七月 - August, ///<八月 - September, ///<九月 - October, ///<十月 - November, ///<十一月 - December, ///<十二月 - - BEGIN_RANGE =January, - END_RANGE =December, - RANGE_SIZE =END_RANGE-BEGIN_RANGE+1 - };//enum class Month - - constexpr uint HGL_TIME_ONE_SECOND =1; - constexpr uint HGL_TIME_HALF_MINUTE =30; - constexpr uint HGL_TIME_ONE_MINUTE =60; - constexpr uint HGL_TIME_HALF_HOUR =30*HGL_TIME_ONE_MINUTE; - constexpr uint HGL_TIME_ONE_HOUR =60*HGL_TIME_ONE_MINUTE; - constexpr uint HGL_TIME_HALF_DAY =12*HGL_TIME_ONE_HOUR; - constexpr uint HGL_TIME_ONE_DAY =24*HGL_TIME_ONE_HOUR; - constexpr uint HGL_TIME_ONE_WEEK =7*HGL_TIME_ONE_DAY; - constexpr uint HGL_TIME_ONE_YEAR =365*HGL_TIME_ONE_DAY; - - constexpr uint HGL_HOUR_HALF_DAY =12; - constexpr uint HGL_HOUR_ONE_DAY =24; - - constexpr uint HGL_DAY_ONE_WEEK =7; - constexpr uint HGL_DAY_ONE_YEAR =365; - - constexpr uint HGL_MONTH_ONE_YEAR =12; - - constexpr uint HGL_MILLI_SEC_PRE_SEC =1000; //毫秒 - constexpr uint HGL_MICRO_SEC_PER_SEC =1000*1000; //微秒 - constexpr uint HGL_NANO_SEC_PER_SEC =1000*1000*1000; //纳秒 - constexpr char LowerHexChar[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; ///<小写16进制字符 constexpr char UpperHexChar[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; ///<大写16进制字符 - constexpr double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //欧拉数(自然对数的底数) - constexpr double HGL_LOG2E =1.44269504088896340736; - constexpr double HGL_LOG10E =0.434294481903251827651; - constexpr double HGL_LN2 =0.693147180559945309417; - constexpr double HGL_LN10 =2.30258509299404568402; - constexpr double HGL_PI =3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068; - constexpr double HGL_PI_2 =1.57079632679489661923; - constexpr double HGL_PI_4 =0.785398163397448309616; - constexpr double HGL_PI_3_4 =2.356194490192344928938; - constexpr double HGL_1_PI =0.318309886183790671538; - constexpr double HGL_2_PI =0.636619772367581343076; - constexpr double HGL_2_SQRTPI =1.12837916709551257390; - constexpr double HGL_SQRT2 =1.41421356237309504880168872420969807; - constexpr double HGL_SQRT1_2 =0.707106781186547524401; - - constexpr double HGL_COS_ANG_0 =1; - constexpr double HGL_COS_ANG_45 =0.707106781187; - constexpr double HGL_COS_ANG_90 =0; - constexpr double HGL_COS_ANG_135=-0.707106781187; - constexpr double HGL_COS_ANG_180=-1; - constexpr double HGL_COS_ANG_225=-0.707106781187; - constexpr double HGL_COS_ANG_270=0; - constexpr double HGL_COS_ANG_315=0.707106781187; - - #define HGL_DEF_ANG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); - - HGL_DEF_ANG2RAD(0) - HGL_DEF_ANG2RAD(45) - HGL_DEF_ANG2RAD(90) - HGL_DEF_ANG2RAD(135) - HGL_DEF_ANG2RAD(180) - HGL_DEF_ANG2RAD(225) - HGL_DEF_ANG2RAD(270) - HGL_DEF_ANG2RAD(315) - HGL_DEF_ANG2RAD(360) - - #undef HGL_DEF_ANG2RAD - - constexpr double HGL_GOLDEN_RATIO =0.61803398874989484820458683436563811772030917980576;//黄金比例 - constexpr double HGL_SILVER_RATIO =2.4142135623730950488; //白银比例 - - constexpr double HGL_SPEED_OF_SOUND =331.3f; //音速(米/秒) - constexpr double HGL_SPEED_OF_LIGHT =299792458; //光速(米/秒) - - constexpr double HGL_ABSOLUTE_ZERO =-273.15f; //绝对零度 - - constexpr double HGL_UNIVERSAL_GRAVITATION =6.67384e-11; //万有引力常数 - - constexpr double HGL_EARTH_GRAVITATIONAL_ACCELERATION =9.80665; //地球上的重力加速度(牛顿) - constexpr double HGL_EARTH_MASS =5.9722e+24; //地球质量 - constexpr double HGL_EARTH_RADIUS =6371000; //地球半径(米) - - /** - * 物体万有引力计算 - * @param m1 星球质量 - * @param m2 物体质量 - * @param length 到星球中心的距离 - */ - template - inline T UniversalGravitation(const T m1,const T m2,const T length) - { - return HGL_UNIVERSAL_GRAVITATION*((m1*m2)/(length*length)); - } - - /** - * 星球重力加速度计算
- * 理论上: (地球质量*万有引力常数)/(地球半径的平方)=地球上的重力加速度 - * @param m 星球质量 - * @param raidus 星球半径 - * @return 星球的重力加速度(牛顿) - */ - template - inline T UniversalGravitation(const T m,const T radius) - { - return (HGL_UNIVERSAL_GRAVITATION*m)/(radius*radius); - } - - /** - * 向心力计算 - * @param m 质量 - * @param v 速度 - * @param r 距离 - */ - template - inline T CentripetalForce(const T m,const T v,const T r) - { - return (m*v*v)/r; - } - - /** - * 加速度计算 - * @param power 推力 - * @param weight 质量 - */ - template - inline T AddSpeed(const T &power,const T &weight) - { - return power/weight; - } - - /** - * 物体运动质量计算(物体运动越快,质量越大) - * @param m0 静止质量 - * @param v 运动速度 - */ - template - inline T RunWeight(const T &m0,const T &v) - { - return m0/sqrt(1-(v*v)/(HGL_SPEED_OF_LIGHT*HGL_SPEED_OF_LIGHT)); - } - - inline float half_to_float(const uint16 &h) - { - return ((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13); - } - - inline uint16 float_to_half(const float &f) - { - const uint32 x = *((uint32 *)&f); - - return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); - } - - /** - * 正圆面积计算 - * @param radius 半径 - */ - inline double CircleArea(const double radius) - { - return(radius*radius*HGL_PI); - } - - /** - * 椭圆面积计算 - * @param l_radius 长半径 - * @param s_radius 短半径 - */ - inline double ElipseArea(const double l_radius,const double s_radius) - { - return(l_radius*s_radius*HGL_PI); - } - - /** - * 球体积计算 - * @param radius 球半径 - */ - inline double SphereVolume(const double radius) - { - return(radius*radius*radius*(HGL_PI*4.0f))/3.0f; - } - - /** - * 椭球体积计算 - * @param x_radius x半径 - * @param y_radius y半径 - * @param z_radius z半径 - */ - inline double EllipsoidVolume(const double x_radius,const double y_radius,const double z_radius) - { - return(x_radius*y_radius*z_radius*(HGL_PI*4.0f))/3.0f; - } - /** * 取适合正巧大于当前数的2次幂值 */ @@ -512,40 +287,6 @@ namespace hgl return GetMipLevel(hgl_max(hgl_max(width,height),depth)); } - /** - * 角度转弧度 - */ - inline double hgl_ang2rad(const double ang) - { - return ang*(HGL_PI/180.0f); - } - - /** - * 弧度转角度 - */ - inline double hgl_rad2ang(const double rad) - { - return rad*(180.0f/HGL_PI); - } - - /** - * 浮点数截取小数点后指定位度 - * @param value 要截取的浮点数 - * @param num 要截取的位数 - */ - template - inline T hgl_clip_float(const T value,const int num) - { - if(num<=0) - return int64(value); - - if(value==0)return(0); - - double per=pow(10,num); - - return double(floor(value*per))/per; - } - /** * 同类型复制 */ diff --git a/inc/hgl/math/FastTriangle.h b/inc/hgl/math/FastTriangle.h index a02165b..25c6705 100644 --- a/inc/hgl/math/FastTriangle.h +++ b/inc/hgl/math/FastTriangle.h @@ -1,16 +1,42 @@ #ifndef HGL_ALGORITHM_MATH_FAST_TRIANGLE_FUNCTION_INCLUDE #define HGL_ALGORITHM_MATH_FAST_TRIANGLE_FUNCTION_INCLUDE +#include +#include namespace hgl { double Lsin(int angle); ///<低精度sin计算,注意传入的参数为角度而非弧度 double Lcos(int angle); ///<低精度cos计算,注意传入的参数为角度而非弧度 void Lsincos(int angle, double &s, double &c); ///<低精度sin+cos计算,注意传入的参数为角度而非弧度 + /** + * Approximates acos(x) with a max absolute error of 9.0x10^-3. + * Valid in the range -1..1. + */ + inline float Lacos(float x) + { + // 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. + */ + inline float LacosPositive(float x) + { + float p = -0.1565827 * x + 1.570796; + return p * sqrt(1.0 - x); + } + /** * 低精度atan函数 */ - double inline Latan(double z) + inline double Latan(double z) { constexpr double n1 = 0.97239411f; constexpr double n2 = -0.19194795f; @@ -23,7 +49,7 @@ namespace hgl /** * 近似Pow2.2,使用此函数等于pow(x,2.2) */ - double inline LPow22(double x) + inline double LPow22(double x) { return x*(1.12*x - 0.12); } diff --git a/inc/hgl/math/MathConst.h b/inc/hgl/math/MathConst.h new file mode 100644 index 0000000..014ac7f --- /dev/null +++ b/inc/hgl/math/MathConst.h @@ -0,0 +1,131 @@ +#ifndef HGL_MATH_CONST_INCLUDE +#define HGL_MATH_CONST_INCLUDE + +#include + +namespace hgl +{ + constexpr double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //ŷ(Ȼĵ) + constexpr double HGL_LOG2E =1.44269504088896340736; + constexpr double HGL_LOG10E =0.434294481903251827651; + constexpr double HGL_LN2 =0.693147180559945309417; + constexpr double HGL_LN10 =2.30258509299404568402; + constexpr double HGL_PI =3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068; + constexpr double HGL_PI_2 =1.57079632679489661923; + constexpr double HGL_PI_4 =0.785398163397448309616; + constexpr double HGL_PI_3_4 =2.356194490192344928938; + constexpr double HGL_1_PI =0.318309886183790671538; + constexpr double HGL_2_PI =0.636619772367581343076; + constexpr double HGL_2_SQRTPI =1.12837916709551257390; + constexpr double HGL_SQRT2 =1.41421356237309504880168872420969807; + constexpr double HGL_SQRT1_2 =0.707106781186547524401; + + constexpr double HGL_COS_ANG_0 =1; + constexpr double HGL_COS_ANG_45 =0.707106781187; + constexpr double HGL_COS_ANG_90 =0; + constexpr double HGL_COS_ANG_135=-0.707106781187; + constexpr double HGL_COS_ANG_180=-1; + constexpr double HGL_COS_ANG_225=-0.707106781187; + constexpr double HGL_COS_ANG_270=0; + constexpr double HGL_COS_ANG_315=0.707106781187; + + #define HGL_DEF_ANG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); + + HGL_DEF_ANG2RAD(0) + HGL_DEF_ANG2RAD(45) + HGL_DEF_ANG2RAD(90) + HGL_DEF_ANG2RAD(135) + HGL_DEF_ANG2RAD(180) + HGL_DEF_ANG2RAD(225) + HGL_DEF_ANG2RAD(270) + HGL_DEF_ANG2RAD(315) + HGL_DEF_ANG2RAD(360) + + #undef HGL_DEF_ANG2RAD + + /** + * Ƕת + */ + inline double hgl_ang2rad(const double ang) + { + return ang*(HGL_PI/180.0f); + } + + /** + * תǶ + */ + inline double hgl_rad2ang(const double rad) + { + return rad*(180.0f/HGL_PI); + } + + /** + * ȡСָλ + * @param value Ҫȡĸ + * @param num Ҫȡλ + */ + template + inline T hgl_clip_float(const T value,const int num) + { + if(num<=0) + return int64(value); + + if(value==0)return(0); + + double per=pow(10,num); + + return double(floor(value*per))/per; + } + + inline float half_to_float(const uint16 &h) + { + return ((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13); + } + + inline uint16 float_to_half(const float &f) + { + const uint32 x = *((uint32 *)&f); + + return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); + } + + /** + * Բ + * @param radius 뾶 + */ + inline double CircleArea(const double radius) + { + return(radius*radius*HGL_PI); + } + + /** + * Բ + * @param l_radius 뾶 + * @param s_radius ̰뾶 + */ + inline double ElipseArea(const double l_radius,const double s_radius) + { + return(l_radius*s_radius*HGL_PI); + } + + /** + * + * @param radius 뾶 + */ + inline double SphereVolume(const double radius) + { + return(radius*radius*radius*(HGL_PI*4.0f))/3.0f; + } + + /** + * + * @param x_radius x뾶 + * @param y_radius y뾶 + * @param z_radius z뾶 + */ + inline double EllipsoidVolume(const double x_radius,const double y_radius,const double z_radius) + { + return(x_radius*y_radius*z_radius*(HGL_PI*4.0f))/3.0f; + } +}//namespace hgl +#endif//HGL_MATH_CONST_INCLUDE diff --git a/inc/hgl/math/PhysicsConst.h b/inc/hgl/math/PhysicsConst.h new file mode 100644 index 0000000..becb8d8 --- /dev/null +++ b/inc/hgl/math/PhysicsConst.h @@ -0,0 +1,79 @@ +#ifndef HGL_MATH_PHYSICS_CONST_INCLUDE +#define HGL_MATH_PHYSICS_CONST_INCLUDE + +namespace hgl +{ + constexpr double HGL_GOLDEN_RATIO =0.61803398874989484820458683436563811772030917980576;//ƽ + constexpr double HGL_SILVER_RATIO =2.4142135623730950488; // + + constexpr double HGL_SPEED_OF_SOUND =331.3f; //(/) + constexpr double HGL_SPEED_OF_LIGHT =299792458; //(/) + + constexpr double HGL_ABSOLUTE_ZERO =-273.15f; // + + constexpr double HGL_UNIVERSAL_GRAVITATION =6.67384e-11; // + + constexpr double HGL_EARTH_GRAVITATIONAL_ACCELERATION =9.80665; //ϵٶ(ţ) + constexpr double HGL_EARTH_MASS =5.9722e+24; // + constexpr double HGL_EARTH_RADIUS =6371000; //뾶() + + /** + * + * @param m1 + * @param m2 + * @param length ĵľ + */ + template + inline T UniversalGravitation(const T m1,const T m2,const T length) + { + return HGL_UNIVERSAL_GRAVITATION*((m1*m2)/(length*length)); + } + + /** + * ٶȼ
+ * : (*)/(뾶ƽ)=ϵٶ + * @param m + * @param raidus 뾶 + * @return ٶ(ţ) + */ + template + inline T UniversalGravitation(const T m,const T radius) + { + return (HGL_UNIVERSAL_GRAVITATION*m)/(radius*radius); + } + + /** + * + * @param m + * @param v ٶ + * @param r + */ + template + inline T CentripetalForce(const T m,const T v,const T r) + { + return (m*v*v)/r; + } + + /** + * ٶȼ + * @param power + * @param weight + */ + template + inline T AddSpeed(const T &power,const T &weight) + { + return power/weight; + } + + /** + * ˶(˶Խ죬Խ) + * @param m0 ֹ + * @param v ˶ٶ + */ + template + inline T RunWeight(const T &m0,const T &v) + { + return m0/sqrt(1-(v*v)/(HGL_SPEED_OF_LIGHT*HGL_SPEED_OF_LIGHT)); + } +}//namespace hgl +#endif//HGL_MATH_PHYSICS_CONST_INCLUDE diff --git a/inc/hgl/math/TimeConst.h b/inc/hgl/math/TimeConst.h new file mode 100644 index 0000000..643c2a2 --- /dev/null +++ b/inc/hgl/math/TimeConst.h @@ -0,0 +1,68 @@ +#ifndef HGL_MATH_TIME_CONST_INCLUDE +#define HGL_MATH_TIME_CONST_INCLUDE +namespace hgl +{ + /** + * ÿö + */ + enum class Weekday + { + Sunday=0, ///< + Monday, ///<һ + Tuesday, ///<ڶ + Wednesday, ///< + Thursday, ///< + Friday, ///< + Saturday, ///< + + BEGIN_RANGE =Sunday, + END_RANGE =Saturday, + RANGE_SIZE =END_RANGE-BEGIN_RANGE+1 + }; + + /** + * ·ö + */ + enum class Month + { + January=1, ///<һ + February, ///< + March, ///< + April, ///< + May, ///< + June, ///< + July, ///< + August, ///< + September, ///< + October, ///<ʮ + November, ///<ʮһ + December, ///<ʮ + + BEGIN_RANGE =January, + END_RANGE =December, + RANGE_SIZE =END_RANGE-BEGIN_RANGE+1 + };//enum class Month + + constexpr uint HGL_TIME_ONE_SECOND =1; + constexpr uint HGL_TIME_HALF_MINUTE =30; + constexpr uint HGL_TIME_ONE_MINUTE =60; + constexpr uint HGL_TIME_HALF_HOUR =30*HGL_TIME_ONE_MINUTE; + constexpr uint HGL_TIME_ONE_HOUR =60*HGL_TIME_ONE_MINUTE; + constexpr uint HGL_TIME_HALF_DAY =12*HGL_TIME_ONE_HOUR; + constexpr uint HGL_TIME_ONE_DAY =24*HGL_TIME_ONE_HOUR; + constexpr uint HGL_TIME_ONE_WEEK =7*HGL_TIME_ONE_DAY; + constexpr uint HGL_TIME_ONE_YEAR =365*HGL_TIME_ONE_DAY; + + constexpr uint HGL_HOUR_HALF_DAY =12; + constexpr uint HGL_HOUR_ONE_DAY =24; + + constexpr uint HGL_DAY_ONE_WEEK =7; + constexpr uint HGL_DAY_ONE_YEAR =365; + + constexpr uint HGL_MONTH_ONE_YEAR =12; + + constexpr uint HGL_MILLI_SEC_PRE_SEC =1000; // + constexpr uint HGL_MICRO_SEC_PER_SEC =1000*1000; //΢ + constexpr uint HGL_NANO_SEC_PER_SEC =1000*1000*1000; // +}//namespace hgl +#endif//HGL_MATH_TIME_CONST_INCLUDE diff --git a/inc/hgl/type/DataType.h b/inc/hgl/type/DataType.h index 2b5b23b..73c372d 100644 --- a/inc/hgl/type/DataType.h +++ b/inc/hgl/type/DataType.h @@ -6,6 +6,9 @@ #include // 智能指针 #include // 字符与简单字符处理 #include // 常用数值宏及内联函数 +#include // 数学常数 +#include // 时间常数 +#include // 物理常数 #include // 编码序定义 #include // 比较操作符实现定义 diff --git a/inc/hgl/type/DateTime.h b/inc/hgl/type/DateTime.h index b7299b2..60c7b00 100644 --- a/inc/hgl/type/DateTime.h +++ b/inc/hgl/type/DateTime.h @@ -2,7 +2,7 @@ #define HGL_DATETIME_INCLUDE #include -#include +#include namespace hgl { diff --git a/src/Math/LAtan.cpp b/src/Math/LAtan.cpp index 9c15ea2..9818a58 100644 --- a/src/Math/LAtan.cpp +++ b/src/Math/LAtan.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace hgl {