python - Numpy method to generate an array of neighbour triangles from a list of neighbors - Stack Overflow

admin2025-04-25  2

I need to compute Voronoi vertices of N moving points. Since N>>1, I will be using scipy.spatial.Delaunay only at the first time step and do a bond updation (equiangulation) at other time steps. Here is the problem (illustrated for 3 points only).

At some time t in my simulation, I have

neighbours of 0: 12, 2, 39, 40 (4)

neighbours of 1: 56, 2, 9, 10 (4)

neighbours of 2: 0, 1, 5, 21, 19 (5)

I would like to build an array of simplexes of size (4+4+5) x 3, as

[[0, 12, 2],

[0, 2, 39],

[0, 39, 40],

[0, 40, 12],

[1, 56, 2],

[1, 2, 9],

[1, 9, 10],

[1, 10, 56],

[2, 0, 1],

[2, 1, 5],

[2, 5, 21],

[2, 21, 19],

[2, 19, 0]]

My approach so far:

numnbrs = np.array([4,4,5])

nbrs = array([12,  2, 39, 40,  56,  2,  9, 10,  0,  1,  5, 21, 19])
 
idx = np.cumsum(num_nbrs)


nbrs_splitted = np.split(nbrs, idx[0:len(idx)-1])

gives three subarrays of neighbours

[array([12,  2, 39, 40]), array([56,  2,  9, 10]), array([ 0,  1,  5, 21, 19])]

matrix = np.array(list(itertools.zip_longest(*nbrs_splitted))).T

final = sliding_window_view(matrix, 2, axis=1)

gives

array([[[np.int64(12), np.int64(2)],
        [np.int64(2), np.int64(39)],
        [np.int64(39), np.int64(40)],
        [np.int64(40), None]],

       [[np.int64(56), np.int64(2)],
        [np.int64(2), np.int64(9)],
        [np.int64(9), np.int64(10)],
        [np.int64(10), None]],

       [[np.int64(0), np.int64(1)],
        [np.int64(1), np.int64(5)],
        [np.int64(5), np.int64(21)],
        [np.int64(21), np.int64(19)]]], dtype=object)

I would very much appreciate any help here.

转载请注明原文地址:http://anycun.com/QandA/1745595077a90938.html