【问题标题】:How to calculate the neighbors of a list of Delaunay triangles know the vertex of each triangle in Python如何计算Delaunay三角形列表的邻居知道Python中每个三角形的顶点
【发布时间】:2015-11-14 15:56:56
【问题描述】:

我已经为此工作了很长时间。我有一个知道所有顶点的 Delaunay 三角形列表,现在我需要计算每个三角形的邻居。

我知道 python 在 scipy.spatial 中有模块 Delaunay,它可以用来计算单形和知道点列表的邻居。但是给定所有单纯形,我如何计算邻居。

三角形列表如下所示:

[[[634706.612442, 3086432.2967], [635268.645733, 3086636.61233],[634830.249107, 3087157.20293]]
[[634706.612442, 3086432.2967], [634830.249107, 3087157.20293], [634401.962216, 3086874.97886]]
[[656237.10083, 3061518.637755], [656776.863279, 3061883.38021], [656330.134218, 3062431.49804]]
[[656237.10083, 3061518.637755], [656330.134218, 3062431.49804], [655787.935768, 3061995.043438]]
[[656541.118122, 3060981.747767], [657223.592341, 3061335.26239], [656776.863279, 3061883.38021]]
[[656541.118122, 3060981.747767], [656776.863279, 3061883.38021], [656237.10083, 3061518.637755]]] 

给定每个顶点的 x,y 坐标。

【问题讨论】:

  • 我不确定你在问什么不在空间库中。如果你有单纯形,那么你有没有顶点?
  • 是的,我有顶点,然后我正在寻找每个三角形的邻居。

标签: python scipy delaunay


【解决方案1】:

您可以遍历三角形的所有边并测试它是否共享相同的边。

【讨论】:

  • 感谢您的回复!这是一个不错的选择。但是我有太多的这些三角形,超过 40,000 个。这些三角形是网格生成器的结果。在我看来,这将非常耗时。
【解决方案2】:

我已经解决了这个问题。实际上,Python中有这样一个模块,可以通过知道每个三角形的顶点来获得每个三角形的邻居。

这样的函数是ma​​tplotlib.tri.Triangulation(x, y, triangles=None, mask=None) 这个类有两个属性:edgesneighbors,可以用来计算每个三角形的邻居。

【讨论】:

    【解决方案3】:

    我的解决方案:查看所有单纯形的组合,然后在字典列表中构建一个图形。

    from collections import defaultdict
    from itertools import permutations
    import matplotlib.pyplot as plt
    
    points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])
    tri = Delaunay(points)
    
    # Visualize Delaunay Graph
    plt.triplot(points[:, 0], points[:, 1], tri.simplices)
    plt.plot(points[:, 0], points[:, 1], 'o')
    for j, p in enumerate(points):
        plt.text(p[0] - 0.03, p[1] + 0.03, j, ha='right')  # label the points
    
    graph = defaultdict(list)
    for simplex in tri.simplices:
        sc = permutations(simplex, 2)
        for cc in sc:
            if cc[1] not in graph[cc[0]]:
                graph[cc[0]].append(cc[1])
    
    print(graph)
    

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多