【问题标题】:How to generate edge index after Delaunay triangulation?Delaunay三角剖分后如何生成边缘索引?
【发布时间】:2021-11-29 10:09:25
【问题描述】:

我正在使用 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], ...

【问题讨论】:

    标签: python triangulation delaunay


    【解决方案1】:

    我们需要进行三个操作:将三角形从Delaunay对象转换为边集(去除重复项),计算每条边的长度并选择符合条件的边。

    创建边集并计算长度:

    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()
    

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 2013-11-19
      • 2015-01-07
      • 1970-01-01
      • 2012-04-06
      • 2019-04-18
      • 2011-07-15
      • 1970-01-01
      • 2012-09-03
      相关资源
      最近更新 更多