向量与圆锥的交点 [英] Points of intersection of vector with cone

查看:151
本文介绍了向量与圆锥的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量 A 定义为:(Ao + t ∗ Ad)

I have a Vector A defined as : (Ao+t∗Ad)

我还有一个具有顶点(圆锥形尖端) V ,轴方向 D ,基本半径 R 和高度 H的圆锥体.

I also have a Cone with vertex (cone tip) V, axis direction D, base radius R and height H.

如何找到矢量和圆锥之间的交点?我正在使用glm进行数学运算.

How can I find the points of intersection between the vector and cone? I'm using glm for maths.

这是一个简单的例子:

Here's a simple illustration:

推荐答案

我无法处理所有射线与圆锥相交的情况,例如,射线位于圆锥上还是射线与圆锥相切,因为对于我而言这不是必需的,但这是我最终得到的解决方案:

I'm not handling all the cases where the ray intersects the cone, such as if the ray lies on the cone or if the ray is tangent to the cone, because it's not necessary in my case, but here's the solution I ended up with:

std::array<glm::vec3,2> getLine2ConeIntersection(const glm::vec3 &ap_,const glm::vec3 &ad_ , const glm::vec3 &coneBaseCntr_,const glm::vec3 &coneVertex_,float coneRadius_) const
{
    glm::vec3 axis = (coneBaseCntr_-coneVertex_);
    glm::vec3 theta = (axis/glm::length(axis));
    float m = pow(coneRadius_,2)/pow(glm::length(axis),2);
    glm::vec3 w = (ap_-coneVertex_);

    float a = glm::dot(ad_,ad_) - m*(pow(glm::dot(ad_,theta),2)) - pow(glm::dot(ad_,theta),2);
    float b = 2.f*( glm::dot(ad_,w) - m*glm::dot(ad_,theta)*glm::dot(w,theta) - glm::dot(ad_,theta)*glm::dot(w,theta) );
    float c = glm::dot(w,w) - m*pow(glm::dot(w,theta),2) - pow(glm::dot(w,theta),2);

    float Discriminant = pow(b,2) - (4.f*a*c);

    if (Discriminant >= 0)
        return std::array<glm::vec3,2>{{
                                        (ap_+static_cast<float>(((-b) - sqrt(Discriminant))/(2.f*a))*ad_),
                                        (ap_+static_cast<float>(((-b) + sqrt(Discriminant))/(2.f*a))*ad_)
                                      }};

    return glm::vec3(0,0,0);
}

其中ap_是向量上的点,而ad_是向量的方向.

Where ap_ is a point on the vector and ad_ is it's direction.

这篇关于向量与圆锥的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆