查找2D三角形在3D空间中是否垂直 [英] Find if 2 triangles are perpendicular in 3D space
本文介绍了查找2D三角形在3D空间中是否垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在3D空间中有2个由3个点组成的三角形.
I have 2 triangles in 3D space made of 3 points.
我假设我需要使用点积,但是如何排列矩阵?
I assume I need to use the dot product but how do I arrange the matrix?
我想我有作品,但需要帮助来安排:)
I think I have the pieces but need help to arrange it :)
谢谢.
下面包含当前代码,不确信它是正确的.
Current code included below, not convinced it is correct.
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")
推荐答案
@Rory Daulton的回答很直观,也很完美.我要补充一下,您可以使用 Binet-Cauchy公式十倍的加速:
@Rory Daulton's answer is intuitive and perfectly fine. I'd just add that you can use Binet-Cauchy formula for a ten-fold speedup:
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
这篇关于查找2D三角形在3D空间中是否垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文