簡體   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