Python:3D中从Scipy的Delaunay三角剖分计算Voronoi镶嵌

2024-04-16 23:53:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大约50000个三维数据点,我已经从新的scipy(我使用的是0.10)运行scipy.space.Delaunay,这给了我一个非常有用的三角剖分。

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

……我想知道是否有一种简单的方法可以得到这种三角测量的“对偶图”,即Voronoi镶嵌。

有线索吗?我在这上面的搜索似乎没有显示任何预建的scipy函数,我发现这几乎是奇怪的!

谢谢, 爱德华


Tags: 数据方法orghttp关系wikispacescipy
3条回答

相邻信息可以在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()

由于我花了大量的时间在这个问题上,我想分享我的解决方案,如何得到Voronoi多边形而不仅仅是边。

代码位于https://gist.github.com/letmaik/8803860,并扩展到tauran的解。

首先,我修改了代码,分别给出了顶点和(对)索引(=边),因为在处理索引而不是点坐标时,许多计算都可以简化。

然后,在voronoi_cell_lines方法中,我确定哪些边属于哪个单元格。为此,我使用一个相关问题的Alink的建议解。也就是说,对于每条边,找到两个最近的输入点(=单元格),并从中创建映射。

最后一步是创建实际的多边形(请参见voronoi_polygons方法)。首先,需要关闭具有悬挂边缘的外部单元。这就像查看所有边并检查哪些边只有一个相邻边一样简单。这样的边可以是零,也可以是两条。在两个例子中,我通过引入一个额外的边来连接它们。

最后,每个单元中的无序边需要按正确的顺序排列,以便从中派生多边形。

用法是:

P = np.random.random((100,2))

fig = plt.figure(figsize=(4.5,4.5))
axes = plt.subplot(1,1,1)

plt.axis([-0.05,1.05,-0.05,1.05])

vertices, lineIndices = voronoi(P)        
cells = voronoi_cell_lines(P, vertices, lineIndices)
polys = voronoi_polygons(cells)

for pIdx, polyIndices in polys.items():
    poly = vertices[np.asarray(polyIndices)]
    p = matplotlib.patches.Polygon(poly, facecolor=np.random.rand(3,1))
    axes.add_patch(p)

X,Y = P[:,0],P[:,1]
plt.scatter(X, Y, marker='.', zorder=2)

plt.axis([-0.05,1.05,-0.05,1.05])
plt.show()

哪些输出:

Voronoi polygons

该代码可能不适合大量的输入点,在某些方面可以改进。不过,这可能对其他有类似问题的人有所帮助。

我遇到了同样的问题,并根据pv的答案和我在网上找到的其他代码片段构建了一个解决方案。该解返回一个完整的Voronoi图,包括不存在三角形邻域的外线。

#!/usr/bin/env python
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay

def voronoi(P):
    delauny = Delaunay(P)
    triangles = delauny.points[delauny.vertices]

    lines = []

    # Triangle vertices
    A = triangles[:, 0]
    B = triangles[:, 1]
    C = triangles[:, 2]
    lines.extend(zip(A, B))
    lines.extend(zip(B, C))
    lines.extend(zip(C, A))
    lines = matplotlib.collections.LineCollection(lines, color='r')
    plt.gca().add_collection(lines)

    circum_centers = np.array([triangle_csc(tri) for tri in triangles])

    segments = []
    for i, triangle in enumerate(triangles):
        circum_center = circum_centers[i]
        for j, neighbor in enumerate(delauny.neighbors[i]):
            if neighbor != -1:
                segments.append((circum_center, circum_centers[neighbor]))
            else:
                ps = triangle[(j+1)%3] - triangle[(j-1)%3]
                ps = np.array((ps[1], -ps[0]))

                middle = (triangle[(j+1)%3] + triangle[(j-1)%3]) * 0.5
                di = middle - triangle[j]

                ps /= np.linalg.norm(ps)
                di /= np.linalg.norm(di)

                if np.dot(di, ps) < 0.0:
                    ps *= -1000.0
                else:
                    ps *= 1000.0
                segments.append((circum_center, circum_center + ps))
    return segments

def triangle_csc(pts):
    rows, cols = pts.shape

    A = np.bmat([[2 * np.dot(pts, pts.T), np.ones((rows, 1))],
                 [np.ones((1, rows)), np.zeros((1, 1))]])

    b = np.hstack((np.sum(pts * pts, axis=1), np.ones((1))))
    x = np.linalg.solve(A,b)
    bary_coords = x[:-1]
    return np.sum(pts * np.tile(bary_coords.reshape((pts.shape[0], 1)), (1, pts.shape[1])), axis=0)

if __name__ == '__main__':
    P = np.random.random((300,2))

    X,Y = P[:,0],P[:,1]

    fig = plt.figure(figsize=(4.5,4.5))
    axes = plt.subplot(1,1,1)

    plt.scatter(X, Y, marker='.')
    plt.axis([-0.05,1.05,-0.05,1.05])

    segments = voronoi(P)
    lines = matplotlib.collections.LineCollection(segments, color='k')
    axes.add_collection(lines)
    plt.axis([-0.05,1.05,-0.05,1.05])
    plt.show()

黑线=Voronoi图,红线=Delauny三角形 Black lines = Voronoi diagram, Red lines = Delauny triangles

相关问题 更多 >