[英]How to set a maximum distance of a Delaunay triangle side in Python
[英]How to calculate the neighbors of a list of Delaunay triangles know the vertex of each triangle in Python
我已經為此工作了一段時間。 我有一個知道所有頂點的 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 中有這樣一個模塊,可以通過知道每個三角形的頂點來獲得每個三角形的鄰居。
這樣的函數是matplotlib.tri.Triangulation(x, y,triangles=None, mask=None)這個類有兩個屬性: edges和neighbors ,可以用來計算每個三角形的鄰居。
我的解決方案:查看單形的所有組合,然后在字典列表中構建一個圖形。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.