inc*_*ent 5 python voronoi scipy tesselation
我正在使用一种算法,对于每次迭代,需要找到一组生态协调所属的Voronoi图的哪个区域.也就是说,每个坐标位于哪个区域内.(我们可以假设所有坐标都属于一个区域,如果这有任何区别的话.)
我还没有任何适用于Python的代码,但伪代码看起来像这样:
## we are in two dimensions and we have 0<x<1, 0<y<1.
for i in xrange(1000):
XY = get_random_points_in_domain()
XY_candidates = get_random_points_in_domain()
vor = Voronoi(XY) # for instance scipy.spatial.Voronoi
regions = get_regions_of_candidates(vor,XY_candidates) # this is the function i need
## use regions for something
Run Code Online (Sandbox Code Playgroud)
我知道scipy.Delaunay有一个名为find_simplex的函数,它会在Delaunay三角剖分中完成我想要的简单操作,但是我需要Voronoi图,并且我希望避免构建它们.
问题:
1.是否有某种类型的库可以让我轻松完成这项工作?
2.如果没有,是否有一个好的算法可以让我有效地做到这一点?
更新
Jamie的解决方案正是我想要的.我有点尴尬,虽然我自己也没想过......
您无需为此实际计算Voronoi区域.根据定义,集合中一个点周围的Voronoi区域由更接近该点的所有点组成,而不是集合中的任何其他点.所以你只需要计算距离并找到最近的邻居.使用scipy's cKDTree你可以这样做:
import numpy as np
from scipy.spatial import cKDTree
n_voronoi, n_test = 100, 1000
voronoi_points = np.random.rand(n_voronoi, 2)
test_points = np.random.rand(n_test, 2)
voronoi_kdtree = cKDTree(voronoi_points)
test_point_dist, test_point_regions = voronoi_kdtree.query(test_points, k=1)
Run Code Online (Sandbox Code Playgroud)
test_point_regions现在拥有一个形状数组,(n_test, 1)其中各点的索引voronoi_points最接近你的每个test_points.
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |