I am trying to create a Voronoi diagram using a 3d array representing voxels where the coordinates are represented by the indices of the array. For example:
points = np.random.random([10,3])
vox_len = 0.1
lx = ly = lz = 11
hull_space=np.zeros([lx,ly,lz],dtype=np.int16)
hull_space.fill(-1)
for i in range(lx):
for j in range(ly):
for k in range(lz):
coord = np.array([i,j,k]).astype(float)*vox_len
diff = points - coord
dist = np.sqrt(diff[:,0]**2 + diff[:,1]**2 + diff[:,2]**2)
closest = np.argmin(dist)
hull_space[i][j][k]=closest
The problem is that with larger arrays the code is very slow. I've also tried:
grid = np.indices((lx,ly,lz)).astype(float)*vox_len
hull_space = np.ones([lx,ly,lz],dtype=np.int16)
hull_space.fill(-1)
closest_dist = np.ones(hull_space.shape)
closest_dist.fill(999)
for index,point in enumerate(points):
dist2 = (grid[0]-point[0])**2 + (grid[1]-point[1])**2 + (grid[2]-point[2])**2
hull_space[dist2 < closest_dist]=index
closest_dist[dist2 < closest_dist]=dist2[dist2 < closest_dist]
But results were even worse. I am aware of scipy's voronoi diagram methods but I need to build a voxel image in order to do distance transforms of the edges of the voronoi regions. My goal is to create an image of an entangled fibrous structure to use in a pore network model and the voxel approach is useful for getting pore volumes.
Any help making the algorithm run faster would be appreciated. For a 300x300x300 diagram with 500 points it takes about 50 minutes on my machine