diff --git a/inc/hgl/math/Area.h b/inc/hgl/math/Area.h new file mode 100644 index 0000000..bfa8308 --- /dev/null +++ b/inc/hgl/math/Area.h @@ -0,0 +1,70 @@ +#pragma once + +#include + +namespace hgl +{ + /* + * 根据三角形的三边长计算三角形的面积 + */ + inline const float TriangleArea(const float a,const float b,const float c) + { + const float s=(a+b+c)*0.5f; + + return sqrt(s*(s-a)*(s-b)*(s-c)); + } + + inline const float TriangleArea(const Vector2f &a,const Vector2f &b,const Vector2f &c) + { + return fabs((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x))*0.5f; + } + + inline const float TriangleArea(const Vector3f &a,const Vector3f &b,const Vector3f &c) + { + return glm::length(glm::cross(b-a,c-a))*0.5f; + } + + inline const float RectangleArea(const float width,const float height) + { + return width*height; + } + + inline const float RectangleArea(const Vector2f &size) + { + return size.x*size.y; + } + + /** + * 正圆面积计算 + * @param radius 半径 + */ + inline const float CircleArea(const float radius) + { + return HGL_PI*radius*radius; + } + + /** + * 椭圆面积计算 + * @param l_radius 长半径 + * @param s_radius 短半径 + */ + inline const float EllipseArea(const float horizontal_radius,const float vertical_radius) + { + return HGL_PI*horizontal_radius*vertical_radius; + } + + inline const float EllipseArea(const Vector2f &radius) + { + return HGL_PI*radius.x*radius.y; + } + + inline const float SectorArea(const float radius,const float angle) + { + return HGL_PI*radius*radius*angle/360.0f; + } + + inline const float QuadArea(const Vector3f &a,const Vector3f &b,const Vector3f &c,const Vector3f &d) + { + return TriangleArea(a,b,c)+TriangleArea(a,c,d); + } +}//namespace hgl diff --git a/inc/hgl/math/Bezier.h b/inc/hgl/math/Bezier.h new file mode 100644 index 0000000..77566f6 --- /dev/null +++ b/inc/hgl/math/Bezier.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace hgl +{ + +}//namespace hgl diff --git a/inc/hgl/math/Math.h b/inc/hgl/math/Math.h index 30fb8b4..f664e93 100644 --- a/inc/hgl/math/Math.h +++ b/inc/hgl/math/Math.h @@ -3,6 +3,7 @@ #include #include +#include #include // Game Math and Geometry Library #include diff --git a/inc/hgl/math/MathConst.h b/inc/hgl/math/MathConst.h index b9980f6..4eec878 100644 --- a/inc/hgl/math/MathConst.h +++ b/inc/hgl/math/MathConst.h @@ -55,7 +55,7 @@ namespace hgl constexpr const double HGL_SIN_270=-1; constexpr const double HGL_SIN_315=-0.707106781187; - #define HGL_DEF_DEG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); + #define HGL_DEF_DEG2RAD(ang) constexpr const double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); HGL_DEF_DEG2RAD(0) HGL_DEF_DEG2RAD(45) @@ -104,25 +104,6 @@ namespace hgl return double(floor(value*per))/per; } - - /** - * 正圆面积计算 - * @param radius 半径 - */ - inline constexpr double CircleArea(const double radius) - { - return(radius*radius*HGL_PI); - } - - /** - * 椭圆面积计算 - * @param l_radius 长半径 - * @param s_radius 短半径 - */ - inline constexpr double ElipseArea(const double l_radius,const double s_radius) - { - return(l_radius*s_radius*HGL_PI); - } /** * 球体积计算 diff --git a/inc/hgl/math/Vector.h b/inc/hgl/math/Vector.h index d534c29..e445057 100644 --- a/inc/hgl/math/Vector.h +++ b/inc/hgl/math/Vector.h @@ -261,19 +261,19 @@ namespace hgl return from + (to - from) * alpha; } - inline bool is_nearly_equal(const float v1,const float v2,const float deviation=HGL_FLOAT_SMALL) + inline bool is_nearly_equal(const float v1,const float v2,const float deviation=HGL_FLOAT_KINDA_SMALL) { - return fabsf(v1-v2)<=HGL_FLOAT_SMALL; + return fabsf(v1-v2)<=deviation; } - inline bool is_nearly_equal(const Vector2f &v1,const Vector2f &v2,const float deviation=HGL_FLOAT_SMALL) + inline bool is_nearly_equal(const Vector2f &v1,const Vector2f &v2,const float deviation=HGL_FLOAT_KINDA_SMALL) { - return length_squared_2d(v1,v2)<=HGL_FLOAT_SMALL; + return length_squared_2d(v1,v2)<=deviation; } - inline bool is_nearly_equal(const Vector3f &v1,const Vector3f &v2,const float deviation=HGL_FLOAT_SMALL) + inline bool is_nearly_equal(const Vector3f &v1,const Vector3f &v2,const float deviation=HGL_FLOAT_KINDA_SMALL) { - return length_squared(v1,v2)<=HGL_FLOAT_SMALL; + return length_squared(v1,v2)<=deviation; } /** @@ -413,5 +413,15 @@ namespace hgl &&IsNearlyEqual(a.y,b.y) &&IsNearlyEqual(a.z,b.z); } + + inline const Vector3f LerpDirection(const Vector3f ¢er,const Vector3f &from_direction,const Vector3f &to_direction,const float alpha) + { + return glm::normalize(center + (from_direction - center) * alpha + (to_direction - center) * alpha); + } + + inline const Vector3f SLerpDirection(const Vector3f ¢er,const Vector3f &from,const Vector3f &to,const float alpha) + { + return glm::normalize(glm::slerp(from - center, to - center, alpha) + center); + } }//namespace hgl #endif//HGL_ALGORITHM_MATH_VECTOR_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eeca449..f625404 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,6 +51,8 @@ SET(MATH_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/math) ##Math-------------------------------------------------------- SET(MATH_HEADER_FILES ${MATH_INCLUDE_PATH}/bvec.h ${MATH_INCLUDE_PATH}/FastTriangle.h + ${MATH_INCLUDE_PATH}/Bezier.h + ${MATH_INCLUDE_PATH}/Area.h ${MATH_INCLUDE_PATH}/Math.h ${MATH_INCLUDE_PATH}/MathConst.h ${MATH_INCLUDE_PATH}/Matrix.h