点在三角形内 [英] point inside a triangle

查看:25
本文介绍了点在三角形内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你是否有人知道如何在空间参考系统上检查一个点是否在给定三角形内.我知道说到 2d 系统,我可以通过以下程序获得这一点:要确定给定点 v 是否位于给定三角形内,请考虑单个顶点,表示为 v0,v1 和 v2 是来自其他两个顶点 v0 的向量.用 v1 和 v2 表示从 v0 到 v 的向量然后给出

I wanted to ask you if anyone knew how to check if a point was inside a given triangle, on a reference system in space. I am aware that speaking of 2d systems I can obtain this point with the following procedures: To determine if a given point v lies within a given triangle, consider a single vertex, denoted v0, v1 and v2 are the vectors from the other two vertices v0. Expressing the vector from v0 to v in terms of v1 and v2 then gives

v = v0 + av1 + bv2

其中 a, b 是常数.求解 a, b

where a, b are constant. Solve for a, b

a= (det (v v2) - det (v0 v2))/(det (v1 v2))
b= - (det (v v1) - det (v0 v1))/(det (v1 v2))

所以如果 a, b> 点 v 在三角形内0 e a + b <1.我想知道是否有类似的程序.

So the point v is inside the triangle if a, b> 0 e a + b <1. I was wondering if there was a similar procedure or not.

根据 MBo 的建议,我编写了以下代码:

After MBo's advice I wrote the following code:

    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.
".format(vertList[0].coords,vertList[1].coords,vertList[2].coords,point))
        v = vertList.pop(0)
        vertList.append(v)
    return False

我正在研究四面体,我感兴趣的三角形是四面体的面.我声明它不起作用,但我不明白为什么.谁能告诉我我做错了什么?我按照以下方式更改了代码,它似乎可以工作.

I am working with tetrahedra, and the triangles that interest me are the faces of the tetrahedron. I state that it does not work but I can not understand why. Can anyone tell me what am I doing wrong? I changed the code in the following way and it seems to work.

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.
".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")

如果有人对我有什么建议,我一定会好好珍惜.谢谢.

If anyone has any advice for me I will surely treasure it. Thanks.

推荐答案

描述的 2D 方法本质上是将向量 r = v-v0 分解为基向量 p=v1-v0q=v2-v0.

Described 2D approach is essentially decomposition of vector r = v-v0 by basis vectors p=v1-v0 and q=v2-v0.

在 3D 中,您可以通过向量 pqn = pxq(其中 x 表示向量乘积运算)

In 3D you can decompose vector r by vectors p, q and n = p x q (where x denotes vector product operation)

如果结果系数 a,b,c 满足限制 a, b >0,a+b<1、c=0,则v点在三角形平面内.

If resulting coefficients a,b,c fulfill limits a, b > 0, a + b < 1, c=0, then point v lies in triangle plane inside it.

对于分解解这个线性系统的未知数a,b,c:

For decomposition solve this linear system for unknowns 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 是否为零 - 在这种情况下,点位于平面上,系数 с 为零,您可以求解ab 选择一对方程并排除第三个被加数的简化系统(与二维中的方法相同)

Alternative approach - check that dot product r.dot.n is zero - in this case point lies in the plane, coefficient с is zero, and you can solve simplified system for a and b choosing a pair of equations and excluding the third summand (same method as in 2D)

这篇关于点在三角形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆