Points of intersection of vector with cone

China☆狼群 提交于 2019-12-10 18:34:58

问题


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

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

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

Here's a simple illustration:


回答1:


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);
}

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



来源:https://stackoverflow.com/questions/34157690/points-of-intersection-of-vector-with-cone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!