How to find all neighbors of a given point in a delaunay triangulation using scipy.spatial.Delaunay?

前端 未结 7 2016
一个人的身影
一个人的身影 2020-12-28 19:40

I have been searching for an answer to this question but cannot find anything useful.

I am working with the python scientific computing stack (scipy,numpy,matplotlib

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 19:44

    The methods described above cycle through all the simplices, which could take very long, in case there's a large number of points. A better way might be to use Delaunay.vertex_neighbor_vertices, which already contains all the information about the neighbors. Unfortunately, extracting the information

    def find_neighbors(pindex, triang):
    
        return triang.vertex_neighbor_vertices[1][triang.vertex_neighbor_vertices[0][pindex]:triang.vertex_neighbor_vertices[0][pindex+1]]
    

    The following code demonstrates how to get the indices of some vertex (number 17, in this example):

    import scipy.spatial
    import numpy
    import pylab
    
    x_list = numpy.random.random(200)
    y_list = numpy.random.random(200)
    
    tri = scipy.spatial.Delaunay(numpy.array([[x,y] for x,y in zip(x_list, y_list)]))
    
    pindex = 17
    
    neighbor_indices = find_neighbors(pindex,tri)
    
    pylab.plot(x_list, y_list, 'b.')
    pylab.plot(x_list[pindex], y_list[pindex], 'dg')
    pylab.plot([x_list[i] for i in neighbor_indices],
               [y_list[i] for i in neighbor_indices], 'ro')    
    
    pylab.show()
    

提交回复
热议问题