矢量与圆锥的交点

Points of intersection of vector with cone

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

我还有一个圆锥,顶点(圆锥尖)V,轴向D,底半径R 和身高 H.

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

这是一个简单的例子:

我没有处理光线与圆锥相交的所有情况,例如光线是否位于圆锥上或光线是否与圆锥相切,因为在我的情况下这不是必需的,但这是我最终得到的解决方案:

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
{
    std::array<glm::vec3,2> pois;
    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)
    {
        pois[0] = (ap_+static_cast<float>(((-b) - sqrt(discriminant))/(2.f*a))*ad_);
        pois[1] = (ap_+static_cast<float>(((-b) + sqrt(discriminant))/(2.f*a))*ad_);
    }

    return pois;
}

其中 ap_ 是矢量上的一个点,ad_ 是它的方向。