向量与圆锥的交点

3
我有一个向量A,定义为:(Ao+t∗Ad) 我还有一个圆锥,其中顶点为V,轴方向为D,基底半径为R,高度为H
如何找到向量和圆锥之间的交点?我使用glm进行数学计算。
这是一个简单的示例: enter image description here
1个回答

4

我并没有处理光线与锥体相交的所有情况,例如光线位于锥体上或光线与锥体相切等情况,因为在我的情况下这些情况并不必要,但是这是我最终采用的解决方案:

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_则表示该向量的方向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接