我想问你是否有人知道如何在空间参考系统上检查一个点是否在给定的三角形内。我知道说到 2d 系统,我可以通过以下程序获得这一点:要确定给定的点 v 是否位于给定的三角形内,请考虑一个顶点,表示为 v0、v1 和 v2 是来自其他两个顶点的向量v0。用 v1 和 v2 表示从 v0 到 v 的向量然后给出
v = v0 + av1 + bv2
其中 a, b 是常数。求解 a, b
a = (det (v v2) - det (v0 v2)) / (det (v1 v2))
b = - (det (v v1) - det (v0 v1)) / (det (v1 v2))
所以如果 a, b> 0 ea + b <1,点 v 在三角形内。我想知道是否有类似的程序。
根据 MBo 的建议,我编写了以下代码:
def pointLocatedOnTheTriangularFace(self, point):
vertList = list(self.vertices)
system = []
for i in range(4):
p = vertList[1].coords-vertList[0].coords
q = vertList[2].coords-vertList[0].coords
n = np.cross(p,q)
for i in range(3):
system.append([p[i],q[i],n[i]])
try:
solution = list(np.linalg.solve(system,np.array(point-vertList[0].coords)))
if solution[0] > 0 and solution[1] > 0 and solution[0]+solution[1]<1 and solution[2]==0:
return True
except np.linalg.LinAlgError:
print("The system having the floor {0} {1} {2} with the point {3} does not admit solutions.\n".format(vertList[0].coords,vertList[1].coords,vertList[2].coords,point))
v = vertList.pop(0)
vertList.append(v)
return False
我正在研究四面体,我感兴趣的三角形是四面体的面。我声明它不起作用,但我不明白为什么。谁能告诉我我做错了什么?我按以下方式更改了代码,它似乎有效。
def pointLocatedOnTheTriangularFace(self, point):
vertList = list(self.vertices)
system = []
for i in range(4):
r = point-vertList[0].coords
p = vertList[1].coords-vertList[0].coords
q = vertList[2].coords-vertList[0].coords
n = np.cross(p,q)
if np.inner(r,n) == 0:
system = [[p[0],q[0]],[p[1],q[1]]]
try:
solution = list(np.linalg.solve(system,np.array([r[0],r[1]])))
if solution[0] > 0 and solution[1] > 0 and solution[0]+solution[1]<1:
return True
except np.linalg.LinAlgError:
print("The system having the floor {0} {1} {2} with the point {3} does not admit solutions.\n".format(vertList[0].coords,vertList[1].coords,vertList[2].coords,point))
v = vertList.pop(0)
vertList.append(v)
return False
通过以下测试:
def test_PointPlacedOnTheTriangularFace(self):
tr1Vertices = [vertex([0,4.32978e-17,0.5],1),vertex([-0.433013,0.25,-0.5],2),vertex([-4.32978e-17,-0.5,-0.5],3),vertex([0.433013,0.25,-0.5],4)]
tr1= tetrahedron(tr1Vertices)
point = vertex([0,0,-0.5],1)
self.assertTrue(tr1.pointLocatedOnTheTriangularFace(point.coords), msg="In this test the point is inside the face")
def test_PointNotPlacedLocatedOnTheTriangularFace(self):
tr1Vertices = [vertex([0,4.32978e-17,0.5],1),vertex([-0.433013,0.25,-0.5],2),vertex([-4.32978e-17,-0.5,-0.5],3),vertex([0.433013,0.25,-0.5],4)]
tr1= tetrahedron(tr1Vertices)
point = vertex([1,0,-0.5],1)
self.assertFalse(tr1.pointLocatedOnTheTriangularFace(point.coords), msg="In this test the point is outside the face")
如果有人对我有任何建议,我一定会珍惜。谢谢。
所描述的二维方法本质上是r = v-v0
通过基向量p=v1-v0
和对向量进行分解q=v2-v0
。
在 3D 中,您可以r
通过向量分解向量p
,q
和n = p x q
(其中x
表示向量乘积运算)
如果所得系数a,b,c
满足限制a, b > 0, a + b < 1, c=0
,则点v
位于其内部的三角形平面中。
对于分解,求解此线性系统的未知数a,b,c
:
rx = a * px + b * qx + c * nx
ry = a * py + b * qy + c * ny
rz = a * pz + b * qz + c * nz
替代方法 - 检查点积r.dot.n
是否为零 - 在这种情况下,点位于平面上,系数с
为零,您可以求解简化系统a
并b
选择一对方程并排除第三个被加数(与 2D 中的方法相同)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句