首页 > 解决方案 > 如何从点数组和边数组生成 scipy.spatial.Delaunay 对象

问题描述

我想从一组点创建一个scipy.spatial.Delaunay对象:P = [[P1x,P1y],[P2x,P2y],...,[PNx,PNy]]以及相应的三角剖分:T = [[T1a,T1b,T1c],[T2a,T2b,T2c],...,[TMa,TMb,TMc]] 这里提出了一个类似的问题: How to add simplices to a scipy Delaunay triangulation object。不幸的是,正如 xdze2 指出的那样,就我而言,我确实需要它是一个有效的 Delaunay 对象,以便我可以使用find_simplex().

在 matlab 中,我可以执行以下操作:

TR = triangulation(double(T),double(P)); %generating the triangulation object
[ID,B] = pointLocation(TR,X(:),Y(:)); %X and Y the coordinates of the points for which I want 
                                      %to find the simplex (ID) they lie in as well the 
                                      % barycentric coordinates in that simplex

我可以用 scipy.spatial.Delaunay 做类似的事情吗?

目前我已经编写了一个可行的暴力破解版本,但是暴力破解需要一些时间才能完成:对于相同数量的查询,我的算法需要大约 180 秒,而 matlab 内置函数 ( pointLocation) 需要 18 秒。我想看看我是否可以通过使用该find_simplex()方法获得加速。

我使用的蛮力方法利用了Delaunay 文档中描述的重心变换。对于每个单纯形,我计算每个点的重心坐标,并通过找到所有坐标为正的单纯形来找到正确的单纯形。

for num_simplex in range(T.shape[0]):
    #compute barycentric coordinates for all points in P
    b = transform_container[num_simplex,:2].dot(np.transpose(P-transform_container[num_simplex,2]))
    coords = np.c_[np.transpose(b), 1 - b.sum(axis=0)]
    # find negative coordinates
    neg_vect = coords < 0
    #find positions where ALL coords are positive
    pos_vect = np.sum(neg_vect,axis=1) == 0
   

transform_container 是每个单纯形的 Delaunay 对象变换矩阵的串联。该矩阵是通过对点的子集调用 Delaunay 生成的。这些子集构成了整个点集 P 的一个分区。

我的问题是我无法在我的整个点集上调用 Delaunay,因为存在未连接的特定节点。这就是为什么我使用点的子集来人为地防止某些边缘。

预先感谢您的帮助

标签: pythonscipydelaunay

解决方案


推荐阅读