如何在3D中检测两个面的交集

时间:2016-03-18 21:35:17

标签: c++ 3d intersection plane face

让我们说

struct myFace
{
    3DPoint p0;
    3DPoint p1;
    3DPoint p2;
    3DPoint p3;
    3DPoint pNormal;

};

face1和face2是myFace类型的面孔。

double ac = face1.pNormal * face2.pNormal;

if(!(ac <1.00000001&amp;&amp; ac&gt; 0.99999999)&amp;&amp;!(ac> -1.00000001&amp;&amp; ac&lt; -0.99999999))

然后面孔不平行。

但是如何检测它们是否相交?

1 个答案:

答案 0 :(得分:1)

哎呀忽略我的评论:想到另一种方法来做到这一点。

  • 第1步:

对于面孔F1F2,将F2个点作为两个三角形,例如分别为(p0, p1, p2)(p1, p2, p3)。然后选择F1的边缘,即(p0, p1)(p1, p2)(p2, p3)(p3, p0),然后将它们与两个三角形相交。

我发现了一些代码:(改编自http://geomalgorithms.com/a06-_intersect-2.html

#define SMALL_NUM   0.00000001

/* 
   returns: 0 if no intersection 
            1 if parallel but disjoint
            2 if coplanar
*/
int intersect3D_RayTriangle(Vector P0, Vector P1, Vector V0, Vector V1, Vector V2)
{
    Vector    u, v, n;              // triangle vectors
    Vector    dir, w0, w;           // ray vectors
    float     r, a, b;              // params to calc ray-plane intersect

    // get triangle edge vectors and plane normal
    u = V1 - V0;
    v = V2 - V0;
    n = cross(u, v);

    dir = P1 - P0;             // ray direction vector
    w0 = P0 - V0;
    a = -dot(n, w0);
    b = dot(n, dir);
    if (fabs(b) < SMALL_NUM)   // ray is parallel to triangle plane
        return (fabs(a) < SMALL_NUM ? 2 : 0);

    // get intersect point of ray with triangle plane
    r = a / b;
    if (r < 0.0 || r > 1.0)
        return 0;                   // => no intersect
    Vector I = R.P0 + r * dir;      // intersect point of ray and plane

    // is I inside T?
    float uu, uv, vv, wu, wv, D;
    uu = dot(u, u);
    uv = dot(u, v);
    vv = dot(v, v);
    w = I - V0;
    wu = dot(w, u);
    wv = dot(w, v);
    D = uv * uv - uu * vv;

    // get and test parametric coords
    float s, t;
    s = (uv * wv - vv * wu) / D;
    if (s < 0.0 || s > 1.0)         // I is outside T
        return 0;
    t = (uv * wu - uu * wv) / D;
    if (t < 0.0 || (s + t) > 1.0)  // I is outside T
        return 0;

    return 1;                       // I is in T
}

P0P1构成F1的一条边,而V0V1V2形成{{1}三角形。

  • 如果其中一个检查(应该是8)返回1,那么它们肯定相交(立即返回 )。
  • 如果所有都返回0,那么它们就不会相交。
  • 如果其中一个检查返回2(第一次检查可能会这样做),那么我们需要一个不同的方法。停止这些检查,立即转到第2步。
  • 第2步:

这部分适用于多边形是否共面(即平行且在同一平面内)。这一次,取所有F2的边缘和F1的所有边缘;对于每个F2的边缘,检查它是否与任何F1的边相交,如果一对相交,则立即返回true

做这样的边缘交叉:(改编自https://gist.github.com/hanigamal/6556506

F2A0形成A1的边缘,F1B0来自B1

F2