[英]Finding voronoi regions that contain a list of arbitrary coordinates
我正在使用一种算法,该算法对于每次迭代,需要找到一组arbirary协调所属的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
我知道scipy.Delaunay有一个名为find_simplex的函数,它会在Delaunay三角剖分中完成我想要的简单操作,但是我需要Voronoi图,并且我希望避免构建它们。
问题 :
1.是否有某种类型的库可以让我轻松完成这项工作?
2.如果没有,是否有一个好的算法可以让我有效地做到这一点?
更新
Jamie的解决方案正是我想要的。 我有点尴尬,虽然我自己也没想过......
您无需为此实际计算Voronoi区域。 根据定义,集合中一个点周围的Voronoi区域由更接近该点的所有点组成,而不是集合中的任何其他点。 所以你只需要计算距离并找到最近的邻居。 使用scipy的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)
test_point_regions
现在保存一个shape (n_test, 1)
数组,其中(n_test, 1)
中的点voronoi_points
最接近每个test_points
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.