求直线与三角形的交点

//求直线 s1 s2 与三角形v0,v1,v2的交点
e1 = v1 - v0;
e2 = v2 - v0;
dir = s2 - s1;
p = cross_product(dir, e2);
tmp = (num_type)1/(p*e1);
s = s1 - v0;
u = tmp * s * p;
if(u < 0 || u > 1)
{
    //the intersection is not in the triangle
    return;
}
q = cross_product(s, e1);
v = tmp * dir * q;
if(v < 0 || v > 1)
{
    //the intersection is not in the triangle
    return;
}
if(u + v > 1)
{
    //the intersection is not in the triangle
    return;
}

//the result is stored in inter->pt
pt = s1+(tmp*e2*q)*dir;

 

posted on 2014-03-06 16:42  索罗格  阅读(767)  评论(0)    收藏  举报

导航