Python:根据 Scipy 的 3D Delaunay 三角剖分计算 Voronoi Tesselation

标签 python 3d scipy delaunay voronoi

我有大约 50,000 个 3D 数据点,我从新的 scipy(我使用的是 0.10)运行了 scipy.spatial.Delaunay,这给了我一个非常有用的三角测量。

基于:http://en.wikipedia.org/wiki/Delaunay_triangulation (“与 Voronoi 图的关系”部分)

...我想知道是否有一种简单的方法可以得到这个三角剖分的“对偶图”,即 Voronoi Tesselation。

有什么线索吗?我对此的搜索似乎没有显示任何预建的 scipy 函数,我觉得这很奇怪!

谢谢, 爱德华

最佳答案

邻接信息可以在 Delaunay 对象的 neighbors 属性中找到。遗憾的是,代码目前并未向用户公开外心,因此您必须自己重新计算。

另外,延伸到无穷远的Voronoi边也不是直接用这种方法得到的。这仍有可能,但需要更多思考。

import numpy as np
from scipy.spatial import Delaunay

points = np.random.rand(30, 2)
tri = Delaunay(points)

p = tri.points[tri.vertices]

# Triangle vertices
A = p[:,0,:].T
B = p[:,1,:].T
C = p[:,2,:].T

# See http://en.wikipedia.org/wiki/Circumscribed_circle#Circumscribed_circles_of_triangles
# The following is just a direct transcription of the formula there
a = A - C
b = B - C

def dot2(u, v):
    return u[0]*v[0] + u[1]*v[1]

def cross2(u, v, w):
    """u x (v x w)"""
    return dot2(u, w)*v - dot2(u, v)*w

def ncross2(u, v):
    """|| u x v ||^2"""
    return sq2(u)*sq2(v) - dot2(u, v)**2

def sq2(u):
    return dot2(u, u)

cc = cross2(sq2(a) * b - sq2(b) * a, a, b) / (2*ncross2(a, b)) + C

# Grab the Voronoi edges
vc = cc[:,tri.neighbors]
vc[:,tri.neighbors == -1] = np.nan # edges at infinity, plotting those would need more work...

lines = []
lines.extend(zip(cc.T, vc[:,:,0].T))
lines.extend(zip(cc.T, vc[:,:,1].T))
lines.extend(zip(cc.T, vc[:,:,2].T))

# Plot it
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

lines = LineCollection(lines, edgecolor='k')

plt.hold(1)
plt.plot(points[:,0], points[:,1], '.')
plt.plot(cc[0], cc[1], '*')
plt.gca().add_collection(lines)
plt.axis('equal')
plt.xlim(-0.1, 1.1)
plt.ylim(-0.1, 1.1)
plt.show()

关于Python:根据 Scipy 的 3D Delaunay 三角剖分计算 Voronoi Tesselation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650645/

相关文章:

Python argv 验证

带有 Alpha channel 的 WPF 3D 纹理

python - 根据特定的月份值和以另一列为条件过滤 Pandas 数据框

python - 如何进行无限线程评论

c++ - 3d 数组索引上的迭代器

javascript - 使用三个js让对象看着一个点

python - 如何从 scipy Delaunay 三角剖分的输出中选择仅在一定体积(或总线长度)下进行简化?

python - Python 3 中的 random.sample(jupyter 笔记本)

python - 使用 scipy.linalg.solve_triangular 求解 xA=b

python - 如何在 python 中使用 pyarrow 从 S3 读取分区 Parquet 文件