繁体   English   中英

如果我有细胞的坐标,如何找到 voronoi 细胞的种子?

[英]How can I find the seeds of the voronoi cells if I have the coordinates of the cells?

我想计算 voronoi 单元的顶点与该特定单元的种子之间的距离。 但是,我无法弄清楚如何获得该特定单元格的种子坐标。 我在这里给出了我的代码,它生成了一个单元格坐标数组。 谁能告诉我如何找到特定细胞的相应种子?

import numpy as np
import freud
import matplotlib
import matplotlib.pyplot as plt
import freud.box
import matplotlib.pyplot as plt

points = np.array([
    [-2, 2, 0],
    [2, 4, 0],
    [-2, 3, 0],
    [3, 5, 0]])

box = freud.box.Box(10,12, is2D= True )
voro = freud.locality.Voronoi()
cells= voro.compute((box, points)).polytopes
print(cells)
plt.figure()
ax = plt.gca()
ax.scatter(points[:, 0], points[:, 1], c= 'k')
voro.plot(ax=ax)
plt.show()

此代码的 output 是下面的 plot 并且 print(cells) 打印单元格顶点的坐标,如下所示。

在此处输入图像描述

[array([[-6.46875   , -1.40625   ,  0.        ],
       [-2.7       , -3.5       ,  0.        ],
       [-1.3       , -3.5       ,  0.        ],
       [ 2.26086957, -1.52173913,  0.        ],
       [ 0.25      ,  2.5       ,  0.        ],
       [-5.1       ,  2.5       ,  0.        ],
       [-5.25      ,  2.25      ,  0.        ]]), array([[ 0.25      ,  2.5       ,  0.        ],
       [ 2.26086957, -1.52173913,  0.        ],
       [ 3.53125   , -1.40625   ,  0.        ],
       [ 4.75      ,  2.25      ,  0.        ],
       [-1.16666667,  8.16666667,  0.        ]]), array([[-5.1       ,  2.5       ,  0.        ],
       [ 0.25      ,  2.5       ,  0.        ],
       [-1.16666667,  8.16666667,  0.        ],
       [-1.3       ,  8.5       ,  0.        ],
       [-2.7       ,  8.5       ,  0.        ]]), array([[ 4.75      ,  2.25      ,  0.        ],
       [ 4.9       ,  2.5       ,  0.        ],
       [ 7.3       ,  8.5       ,  0.        ],
       [ 3.53125   , 10.59375   ,  0.        ],
       [ 2.26086957, 10.47826087,  0.        ],
       [-1.3       ,  8.5       ,  0.        ],
       [-1.16666667,  8.16666667,  0.        ]])]


我想计算每个单元格的顶点到种子的距离,如图中的线条所示。

在此处输入图像描述

这是使用 scipy.spatial.Voronoi 的示例:

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

points = np.array([
    [-2, 2],
    [2, 4],
    [1, 3],
    [-2, 3],
    [3, 5],
    [-1, 5],
    [2, 1]]
    )

vor = Voronoi(points)

print("Coordinates of point 2:")
print(vor.points[2])

print("Region for point 2:")
print(vor.point_region[2])

print("Vertices for region:")
print(vor.regions[vor.point_region[2]])

print("Coordinates of Voronoi vertices:")
print(vor.vertices[vor.regions[vor.point_region[2]]])

for vert in vor.vertices[vor.regions[vor.point_region[2]]]:
    distance = np.linalg.norm(vor.points[2]-vert)
    print("Distance between " + str(vor.points[2]) + " and " + str(vert) + " is " + str(distance))

fig = voronoi_plot_2d(vor)

plt.gca().set_aspect('equal')

plt.show()

Output 运行代码为:

Coordinates of point 2:
[1. 3.]
Region for point 2:
4
Vertices for region:
[6, 4, 2, 0, 5]
Coordinates of Voronoi vertices:
[[-0.5         3.5       ]
 [ 0.5         4.5       ]
 [ 2.5         2.5       ]
 [-0.07142857  1.21428571]
 [-0.5         2.5       ]]
Distance between [1. 3.] and [-0.5  3.5] is 1.5811388300841898
Distance between [1. 3.] and [0.5 4.5] is 1.5811388300841898
Distance between [1. 3.] and [2.5 2.5] is 1.5811388300841898
Distance between [1. 3.] and [-0.07142857  1.21428571] is 2.0824828195876073
Distance between [1. 3.] and [-0.5  2.5] is 1.5811388300841898

生成的 Voronoi 图包含一些要遍历的数据结构,以将种子点与单元的 Voronoi 顶点相关联。 具体来说:

  1. point_region将点(种子)索引转换为区域数组中的关联索引
  2. regions包含给定单元格的 Voronoi 顶点的索引列表
  3. vertices包含 Voronoi 单元的顶点坐标

在上面的代码中,计算了下面显示的 Voronoi 图,并为图中心的 Voronoi 单元计算了种子点和顶点之间的距离。

沃罗诺伊图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM