[英]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))
則臉部不平行。
但是如何檢測它們是否相交呢?
糟糕,我無視我的評論:想到了另一種方法。
對於面F1
和F2
,將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
}
P0
和P1
形成F1
的邊之一,而V0
, V1
和V2
形成F2
的三角形之一。
這部分用於多邊形是否共面(即平行且在同一平面上)。 這次,取F1
的所有邊緣和F2
的所有邊緣; 對於F1
的每個邊緣,檢查它是否與F2
的任何一條邊緣相交,如果一對相交,則立即返回true。
要進行這樣的邊緣相交:(改編自https://gist.github.com/hanigamal/6556506 )
A0
, A1
形成F1
的邊緣, B0
, B1
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.