From a75acfabd16070fa112bf2966ecaf4449d0ec1ab Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Mon, 4 Sep 2023 21:56:26 +0800 Subject: [PATCH] improved "ray_intersection_angle" functions. --- inc/hgl/math/Vector.h | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/inc/hgl/math/Vector.h b/inc/hgl/math/Vector.h index c4d13ed..763b90e 100644 --- a/inc/hgl/math/Vector.h +++ b/inc/hgl/math/Vector.h @@ -262,18 +262,47 @@ namespace hgl return length_squared(v1,v2)<=(gap*gap); } - inline float ray_angle_cos(const Vector3f &ray_dir, const Vector3f &ray_pos, const Vector3f &pos) + /** + * 计算射线与点的夹角(cos) + */ + inline float ray_intersection_angle_cos(const Vector3f &ray_dir, const Vector3f &ray_pos, const Vector3f &pos) { return dot(ray_dir, normalized(pos - ray_pos)); } /** - * 做一个2D旋转计算 - * @param result 结果 - * @param source 原始点坐标 - * @param center 圆心坐标 - * @param ang 旋转角度 - */ + * 计算射线与点的夹角(弧度) + */ + inline float ray_intersection_angle_radian(const Vector3f &ray_dir, const Vector3f &ray_pos, const Vector3f &pos) + { + double val=dot(ray_dir, normalized(pos - ray_pos)); + + if(val>1)return 0; + if(val<-1)return HGL_PI; + + return acos(val); + } + + /** + * 计算射线与点的夹角(角度) + */ + inline float ray_intersection_angle_degree(const Vector3f &ray_dir, const Vector3f &ray_pos, const Vector3f &pos) + { + double val=dot(ray_dir, normalized(pos - ray_pos)); + + if(val>1)return 0; + if(val<-1)return 180; + + return rad2deg(acos(val)); + } + + /** + * 做一个2D旋转计算 + * @param result 结果 + * @param source 原始点坐标 + * @param center 圆心坐标 + * @param ang 旋转角度 + */ template inline void rotate2d(T1 &result, const T2 &source, const T3 ¢er, const double ang) {