网格中的块状独特三角形

时间:2018-08-24 15:06:38

标签: python numpy unique mesh

我有一个存储在numpy中的三角形网格。一些三角形是重复的,我想将其删除。 我的numpy数组的示例:

# Some points
a=(-2,-2,0)
b=(1,-2,0)
c=(1, 1,0)
d=(-2,1,0)
e=(-2,-2,1)
f=(1,-2,1)
g=(1,1,1)
h=(-2,1,1)

# Some triangles
t1=(a,b,c)
t2=(c,d,a)
t3=(e,f,g)
t4=(g,h,e)

# The numpy array with duplicated t1 and t3
points=numpy.array([t1,t2,t3,t4,t1,t3])

我尝试使用intersect1d和unique,但是找不到消除所有不止一次出现的三角形的方法。我想念什么?

3 个答案:

答案 0 :(得分:0)

一种解决方案是构建一个set的三角形,并且必须首先对每个三角形的点进行排序:

# Some points
a=(-2,-2,0)
b=(1,-2,0)
c=(1, 1,0)
d=(-2,1,0)
e=(-2,-2,1)
f=(1,-2,1)
g=(1,1,1)
h=(-2,1,1)

# Some triangles
t1=(a,b,c)
t2=(c,d,a)
t3=(e,f,g)
t4=(g,h,e)

# The numpy array with duplicated t1 and t3
triangles = [t1,t2,t3,t4,t1,t3]

set( tuple(sorted(points)) for points in triangles )

给予:

{((-2, -2, 0), (-2, 1, 0), (1, 1, 0)),
 ((-2, -2, 0), (1, -2, 0), (1, 1, 0)),
 ((-2, -2, 1), (-2, 1, 1), (1, 1, 1)),
 ((-2, -2, 1), (1, -2, 1), (1, 1, 1))}

答案 1 :(得分:0)

假设顶点的顺序无关紧要,您可以先对顶点进行排序,然后使用类似于此处的类似技术来删除重复的三角形:Removing duplicate columns and rows from a NumPy 2D array

def unique_triangles(points):
    points = np.sort(points,axis=1)
    unique_points = np.unique(points.flatten().view([('',points.dtype)]*points.shape[1]*points.shape[2]))
    return unique_points.view(points.dtype).reshape((-1,points.shape[1],points.shape[2]))

示例:

>>> unique_triangles(points)
array([[[-2, -2,  0],
        [ 1, -2,  0],
        [ 1,  1,  0]],

       [[-2, -2,  1],
        [ 1, -2,  1],
        [ 1,  1,  1]],

       [[ 1,  1,  0],
        [-2,  1,  0],
        [-2, -2,  0]],

       [[ 1,  1,  1],
        [-2,  1,  1],
        [-2, -2,  1]]])

答案 2 :(得分:0)

这个确切的问题是促使我创建numpy_indexed软件包的第一个动机:

import numpy_indexed as npi
npi.unique(triangles)

从那时起,它已经涵盖了更多的内容。而且,从那时起numpy便将轴参数添加到unique

np.unique(triangles, axis=0)

应该完成相同的事情,并且执行基本相同的基础操作。 npi.unique也有一个轴参数,但是默认为0。