geometry > Voronoi を試してみた (2D, 3D)


動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0

実施してみた。

link

2D

Jupyterコード。

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

'''
v0.1 Oct. 7, 2017
  - show 4 Voronoi vertices generated for 9 points
'''

points = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2],
                  [2, 0], [2, 1] , [2, 2]])
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()

for idx, pts in enumerate(vor.points):
    print("point " + str(idx) + "(" + str(pts) + ")")

for idx, vts in enumerate(vor.vertices):
    print("vertices " + str(idx) + "(" + str(vts) + ")")

run
point 0([ 0.  0.])
point 1([ 0.  1.])
point 2([ 0.  2.])
point 3([ 1.  0.])
point 4([ 1.  1.])
point 5([ 1.  2.])
point 6([ 2.  0.])
point 7([ 2.  1.])
point 8([ 2.  2.])
vertices 0([ 0.5  0.5])
vertices 1([ 1.5  0.5])
vertices 2([ 0.5  1.5])
vertices 3([ 1.5  1.5])

与えられたpointsに対してverticesを計算できるようだ。

3D

VoronoiクラスはN次元対応のようだ。
3D描画は用意されていないようではある。

Jupyterコード。

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

'''
v0.1 Oct. 7, 2017
  - calc Voronoi (3D)
'''

inx = np.linspace(0, 2, 3, endpoint=True)
iny = np.linspace(0, 2, 3, endpoint=True)
inz = np.linspace(0, 2, 3, endpoint=True)
mx, my, mz = np.meshgrid(inx, iny, inz)

arr = []
for ax in range(len(mx)):
    for ay in range(len(my)):
        for az in range(len(mz)):
            arr += [[mx[ax][ay][az], my[ax][ay][az], mz[ax][ay][az]]]

points = np.array(arr)
vor = Voronoi(points)

#voronoi_plot_2d(vor)
#plt.show()

#for idx, pts in enumerate(vor.points):
#    print("point " + str(idx) + "(" + str(pts) + ")")

for idx, vts in enumerate(vor.vertices):
    print("vertices " + str(idx) + "(" + str(vts) + ")")
run
vertices 0([ 1.5  1.5  1.5])
vertices 1([ 1.5  0.5  1.5])
vertices 2([ 1.5  0.5  0.5])
vertices 3([ 1.5  1.5  0.5])
vertices 4([ 0.5  0.5  0.5])
vertices 5([ 0.5  0.5  1.5])
vertices 6([ 0.5  1.5  0.5])
vertices 7([ 0.5  1.5  1.5])