I am working with a mesh of triangles (in 3D, although I doubt it makes a difference). The mesh is given as list of lists, each list containing the indices of the three vertices of a triangle in said mesh.
0 2 6 0 12 15 2 66 10234
I want to find the neighbours of each point, i.e., to have an array where the first row contains the neighbours of point 0, the second the neighbours of point 1 and so on.
How do I make it more efficient?
points is a list of lists containing the point coordinates, while
triangles is a list of lists containing the triangles in the mesh:
from numpy import ma pointsNb = points.shape trianglesNb = triangles.shape neighbours = [ for i in range(pointsNb)] # for each point in points, make a list of its neighbours # find neighbours for i in range(0, trianglesNb): triangle = ma.array(triangles[i][:], mask=False) for j, point in enumerate(triangles[i]): triangle[j] = ma.masked for possible_neighbour in triangle[~triangle.mask]: if possible_neighbour not in neighbours[point]: neighbours[point].append(possible_neighbour) triangle[j] = point