三角形内的点

point inside a triangle

我想问你是否有人知道如何在 space 的参考系统上检查一个点是否在给定三角形内。 我知道说到 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 e a + 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")

如果有人对我有什么建议,我一定会珍藏的。谢谢

描述的 2D 方法本质上是通过基础向量 p=v1-v0q=v2-v0.

分解向量 r = v-v0

在 3D 中,您可以将向量 r 分解为向量 pqn = 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 中的方法相同)