查找 2 个三角形在 3D 中是否垂直 space

Find if 2 triangles are perpendicular in 3D space

我有 2 个 3D 三角形 space 由 3 个点组成。

我假设我需要使用点积,但我该如何排列矩阵?

我想我有这些作品,但需要帮助来安排它:)

谢谢。

下面包含当前代码,不相信它是正确的。

vx1 = self.vertices[f[1]].x-self.vertices[f[0]].x
vy1 = self.vertices[f[1]].y-self.vertices[f[0]].y
vz1 = self.vertices[f[1]].z-self.vertices[f[0]].z

vx2 = self.vertices[f[2]].x-self.vertices[f[0]].x
vy2 = self.vertices[f[2]].y-self.vertices[f[0]].y
vz2 = self.vertices[f[2]].z-self.vertices[f[0]].z

plane1 = np.cross([vx1,vy1,vz1],[vx2, vy2, vz2])

vx3 = self.vertices[ff[1]].x-self.vertices[ff[0]].x
vy3 = self.vertices[ff[1]].y-self.vertices[ff[0]].y
vz3 = self.vertices[ff[1]].z-self.vertices[ff[0]].z

vx4 = self.vertices[ff[2]].x-self.vertices[ff[0]].x
vy4 = self.vertices[ff[2]].y-self.vertices[ff[0]].y
vz4 = self.vertices[ff[2]].z-self.vertices[ff[0]].z

plane2 = np.cross([vx3,vy3,vz3],[vx4, vy4, vz4])

print("p1",plane1)
print("p2",plane2)
print("dot",np.dot(plane1,plane2))

if np.dot(plane1,plane2) == 0:
    print("perpendictular")

我在这里假设三角形不需要真正相交才能被考虑 "perpendicular"。

在那种情况下,三角形是垂直的当且仅当它们的法向量是垂直的。要找到三角形 1 的法向量,请对构成边的向量进行叉积。 IE。如果三角形 1 由点 t1p1t1p2t1p3 定义,则计算 t1p2-t1p1t1p3-t1p1 的叉积。 (使用 numpy.cross 来做到这一点。)对三角形 2 做同样的事情。

现在用点积看看那些法向量是相互垂直的。 (使用 numpy.dot 来做到这一点。)如果它们的点积为零,则这些向量是垂直的。

如果你的点有浮点坐标,你应该检查 "close to zero" 而不是 "equal to zero" 来处理轻微的计算错误。我会将实际的 Python/numpy 代码留给您。如果您需要更多帮助,请表现出更多努力。

@Rory Daulton 的回答很直观而且非常好。我只想补充一点,您可以使用 Binet-Cauchy formula 进行十倍加速:

import numpy as np

TR1, TR2 = np.random.random((2,3,3))

def xprod():
    return np.cross(*(TR1[:2]-TR1[2]))@np.cross(*(TR2[:2]-TR2[2]))

def bincau():
    aux = ((TR1[:2]-TR1[2])@(TR2[:2]-TR2[2]).T).ravel()
    return aux[0]*aux[3] - aux[1]*aux[2]

xprod()
# -0.04300263623056995
bincau()
# -0.043002636230569956

from timeit import timeit

timeit(xprod, number=100000)
# 7.751510428992333
timeit(bincau, number=100000)
# 0.620043026006897