簡體   English   中英

如何在3D中檢測兩張臉的交集

[英]How to detect intersection of two faces in 3D

可以說

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

};

face1和face2是myFace類型的面孔。

double ac = face1.pNormal * face2.pNormal;

如果(!(ac <1.00000001 && ac> 0.99999999)&&!(ac> -1.00000001 && ac <-0.99999999))

則臉部不平行。

但是如何檢測它們是否相交呢?

糟糕,我無視我的評論:想到了另一種方法。


  • 第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形成F2的三角形之一。

  • 如果其中一項檢查(應為8)返回1,則它們肯定相交( 立即返回true)。
  • 如果它們全部返回0,則它們不相交。
  • 如果其中一個檢查返回2(第一個檢查可能會執行此操作),那么我們需要一個不同的方法。 停止這些檢查,然后立即轉到步驟2。

  • 第2步:

這部分用於多邊形是否共面(即平行且在同一平面上)。 這次,取F1的所有邊緣和F2的所有邊緣; 對於F1的每個邊緣,檢查它是否與F2的任何一條邊緣相交,如果一對相交,則立即返回true。

要進行這樣的邊緣相交:(改編自https://gist.github.com/hanigamal/6556506

A0A1形成F1的邊緣, B0B1 F2的邊緣。

int intersection(Vector A0, Vector A1, Vector B0, Vector B1)
{
   Vector dA = A1 - A0;
   Vector dB = B1 - B0;
   Vector dC = B0 - A0;

   double s = dot(cross(dC, dB), cross(dA, dB)) / norm2(cross(dA, dB));
    return (s >= 0.0 && s <= 1.0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM