我在3D空间中有2个由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")
答案 0 :(得分:1)
在这里,我假设三角形不需要实际相交即可视为“垂直”。
在这种情况下,当且仅当法线向量垂直时,三角形才垂直。要找到三角形1的法线向量,请取构成边的向量的叉积。即如果三角形1由点t1p1
,t1p2
和t1p3
定义,则计算t1p2-t1p1
和t1p3-t1p1
的叉积。 (使用numpy.cross
。)对Triangle 2做同样的事情。
现在使用点积来查看那些法线向量彼此垂直。 (使用numpy.dot
。)如果这些向量的点积为零,则它们是垂直的。
如果点具有浮点坐标,则应检查“接近零”而不是“等于零”,以处理轻微的计算错误。我将实际的Python / numpy代码留给您。如果您需要更多帮助,请多下功夫。
答案 1 :(得分:0)
@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