三维空间判断线与三角形相交
原文:https://members.loria.fr/SLazard/ARC-Visi3D/Pant-project/files/Line_Triangle.html
给出直线上两点坐标以及三角形三个顶点坐标(v0,v1,v2)
可能的情况为:相交或共面,相交中分为:穿过三角形,穿过三角形边缘,穿过三角形顶点
解决这个问题之前先理解两个定义:Plucker坐标, Side Operator
Plucker坐标用来表示由a =(px,py,pz)和b =(qx,qy,qz)组成的直线
L =(L [0],L [1],L [2],L [3],L [4],L [5])
l0 = a[0] * b[1] - b[0] * a[1]
l1 = a[0] * b[2] - b[0] * a[2]
l2 = a[0] - b[0]
l3 = a[1] * b[2] - b[1] * a[2]
l4 = a[2] - b[2]
l5 = b[1] - a[1]
Side Operator是一个对Plucker坐标和的运算:(a,b都是Plucker坐标)
side(a,b) = a[0]*b[4] + a[1]*b[5] + a[2]*b[3] + a[3]*b[2] + a[4]*b[0] + a[5]*b[1]
举一个Python的例子
1 def plucker(a, b): 2 l0 = a[0] * b[1] - b[0] * a[1] 3 l1 = a[0] * b[2] - b[0] * a[2] 4 l2 = a[0] - b[0] 5 l3 = a[1] * b[2] - b[1] * a[2] 6 l4 = a[2] - b[2] 7 l5 = b[1] - a[1] 8 return [l0, l1, l2, l3, l4, l5] 9 10 def sideOp(a, b): 11 res = a[0] * b[4] + a[1] * b[5] + a[2] * b[3] + a[3] * b[2] + a[4 ] * b[0] + a[5] * b[1] 12 return res 13 #定义点 14 a = (0, 0, 0) 15 b = (1, 1, 1) 16 17 v0 = (0,0,1) 18 v1 = (0,1,0) 19 v2 = (1,0,0) 20 21 #计算 22 e1 = plucker(v1, v0) 23 e2 = plucker(v2, v1) 24 e3 = plucker(v0, v2) 25 L = plucker(a, b) 26 27 s1 = sideOp(L, e1) 28 s2 = sideOp(L, e2) 29 s3 = sideOp(L, e3) 30 31 if s1 == 0 and s2 == 0 and s3 == 0: 32 print("线和三角形共面") 33 elif (s1>0 and s2>0 and s3>0) or (s1<0 and s2<0 and s3<0): 34 print("线穿过三角形") 35 elif (s1 == 0 and s2*s3>0) or (s2 == 0 and s1*s3>0) or (s3 == 0 and s1*s2>0): 36 print("线穿过三角形边缘") 37 elif (s1 == 0 and (s2 == 0)) or (s1 == 0 and (s3 == 0)) or (s2 == 0 and (s3 == 0)): 38 print("线穿过三角形顶点")
原文中有具体算出相交点的算法,需要的可以去看看。
如果需要判断线段与三角形相交情况,看这篇文章:https://members.loria.fr/SLazard/ARC-Visi3D/Pant-project/files/Line_Segment_Triangle.html