Voronoi细胞体积(python)

时间:2013-10-28 12:36:48

标签: python-2.7 scipy voronoi qhull

我在Python 2.7中使用Scipy 0.13.0来计算3d中的一组Voronoi单元格。我需要获得每个单元的体积以(de)加权专有模拟的输出。有没有简单的方法这样做 - 当然这是一个常见的问题或Voronoi细胞的常见用途,但我找不到任何东西。运行以下代码,并转储scipy.spatial.Voronoi manual知道的所有内容。

from scipy.spatial import Voronoi
x=[0,1,0,1,0,1,0,1,0,1]
y=[0,0,1,1,2,2,3,3.5,4,4.5]
z=[0,0,0,0,0,1,1,1,1,1]
points=zip(x,y,z)
print points
vor=Voronoi(points)
print vor.regions
print vor.vertices
print vor.ridge_points
print vor.ridge_vertices
print vor.points
print vor.point_region

2 个答案:

答案 0 :(得分:4)

我已经破解了它。我的方法是:

  • 对于Voronoi图的每个区域
  • 对区域的顶点执行Delaunay三角剖分
    • 这将返回一组填充区域的不规则四面体
  • 可以计算出四面体的体积easily (wikipedia)
    • 将这些卷相加以获得该地区的数量。

我确信会有错误和编码不好 - 我会寻找前者,对后者的评论表示欢迎 - 特别是因为我对Python很新。我还在检查几件事 - 有时给出的顶点索引为-1,根据scipy手册“指示Voronoi图外的顶点”,此外,顶点是用在原始数据之外很好地坐标(插入numpy.random.seed(42)并检查点7的区域坐标,它们到〜(7,-14,6),点49是相似的。所以我需要弄清楚为什么有时会发生这种情况,有时我会得到索引-1。

from scipy.spatial import Voronoi,Delaunay
import numpy as np
import matplotlib.pyplot as plt

def tetravol(a,b,c,d):
 '''Calculates the volume of a tetrahedron, given vertices a,b,c and d (triplets)'''
 tetravol=abs(np.dot((a-d),np.cross((b-d),(c-d))))/6
 return tetravol

def vol(vor,p):
 '''Calculate volume of 3d Voronoi cell based on point p. Voronoi diagram is passed in v.'''
 dpoints=[]
 vol=0
 for v in vor.regions[vor.point_region[p]]:
  dpoints.append(list(vor.vertices[v]))
 tri=Delaunay(np.array(dpoints))
 for simplex in tri.simplices:
  vol+=tetravol(np.array(dpoints[simplex[0]]),np.array(dpoints[simplex[1]]),np.array(dpoints[simplex[2]]),np.array(dpoints[simplex[3]]))
 return vol

x= [np.random.random() for i in xrange(50)]
y= [np.random.random() for i in xrange(50)]
z= [np.random.random() for i in xrange(50)]
dpoints=[]
points=zip(x,y,z)
vor=Voronoi(points)
vtot=0


for i,p in enumerate(vor.points):
 out=False
 for v in vor.regions[vor.point_region[i]]:
  if v<=-1: #a point index of -1 is returned if the vertex is outside the Vornoi diagram, in this application these should be ignorable edge-cases
   out=True
  else:
 if not out:
  pvol=vol(vor,i)
  vtot+=pvol
  print "point "+str(i)+" with coordinates "+str(p)+" has volume "+str(pvol)

print "total volume= "+str(vtot)

#oddly, some vertices outside the boundary of the original data are returned, meaning that the total volume can be greater than the volume of the original.

答案 1 :(得分:1)

正如评论中提到的,您可以计算每个Voronoi单元的ConvexHull。由于Voronoi细胞是凸形的,因此您将获得适当的体积。

def voronoi_volumes(points):
    v = Voronoi(points)
    vol = np.zeros(v.npoints)
    for i, reg_num in enumerate(v.point_region):
        indices = v.regions[reg_num]
        if -1 in indices: # some regions can be opened
            vol[i] = np.inf
        else:
            vol[i] = ConvexHull(v.vertices[indices]).volume
    return vol

此方法适用于任何尺寸