Delaunay三角剖分后如何生成边索引?

How to generate edge index after Delaunay triangulation?

我正在使用 Python 3.7。 有一组点。 我通过这些点生成 Delaunay 三角剖分。

import numpy as np
points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1], [1.5, 0.6], [1.2, 0.5], [1.7, 0.9], [1.1, 0.1]])
from scipy.spatial import Delaunay
tri = Delaunay(points)

如何删除超过边长阈值的边? 如何绘制新的 Delaunay 三角剖分(删除边缘后)? 我的想法是通过点生成边缘索引。喜欢,

[e1, (0,0),(1,0),e1_length], [e2, (0,0),(1,1),e2_length], ...

我们需要进行三个操作:将三角形从 Delaunay 对象转换为边集(删除重复项),计算每条边的长度和满足条件的 select 边。

创建边集并计算长度:

def less_first(a, b):
    return [a,b] if a < b else [b,a]

def delaunay2edges(tri):

    list_of_edges = []

    for triangle in tri.simplices:
        for e1, e2 in [[0,1],[1,2],[2,0]]: # for all edges of triangle
            list_of_edges.append(less_first(triangle[e1],triangle[e2])) # always lesser index first

    array_of_edges = np.unique(list_of_edges, axis=0) # remove duplicates

    list_of_lengths = []

    for p1,p2 in array_of_edges:
        x1, y1 = tri.points[p1]
        x2, y2 = tri.points[p2]
        list_of_lengths.append((x1-x2)**2 + (y1-y2)**2)

    array_of_lengths = np.sqrt(np.array(list_of_lengths))

    return array_of_edges, array_of_lengths

edges, lengths = delaunay2edges(tri)

按标准选择边(例如长度 > 0.5):

criterion = np.argwhere(lengths > 0.5).flatten()

selected_edges = edges[criterion]

print('Removed', len(edges) - len(selected_edges), 'edges')

绘图:

import matplotlib.pyplot as plt
    
plt.triplot(tri.points[:,0], tri.points[:,1], tri.simplices, color='red')

x_lines = []
y_lines = []

for p1,p2 in selected_edges:
    x1,y1 = points[p1]
    x2,y2 = points[p2]
    plt.plot([x1,x2],[y1,y2], color='blue')

plt.scatter(points[:,0],points[:,1])

plt.show()