I have a .vtk mesh with N points and F polygon (triangle) faces, and i'd like to build an N x N adjacency matrix to represent the connectivity between the points.
I've tried mesh.GetLines().GetData()
however, this returns an empty array. I've also tried mesh.GetPolys().GetData()
and this gives an flat array of 4 x F elements.
From inspecting the .vtk file, I know that each face is given as 3, point1, point2, point3
where I assume 3 indicates the faces are triangular. From here it is possible to create the adjacency matrix by iterating through the list, however I'd like to know there if there is any inbuilt VTK functions that can do the job for me.
I also have the mesh in .stl format, if that helps.
Thanks
It is possible to create the adj matrix by iterating over the vtk polys faces and adding a 1 in an empty matrix for every edge connection like so:
polygons = vtk_to_numpy(mesh.GetPolys().GetData())
adj = np.zeros((len(coords), len(coords)))
print('ADJACENCY MATRIX SHAPE: ',adj.shape)
for i in range(0, int(len(polygons)), 4):
line = polygons[i:i+4] # 3, point1, point2, point3
face = line[1:] # point1, point2, point3
n1, n2, n3 = face[0], face[1], face[2]
adj[n1,n2] = 1
adj[n2,n1] = 1
adj[n1,n3] = 1
adj[n3,n1] = 1
adj[n2,n3] = 1
adj[n3,n2] = 1
Altenatively, if using .stl
files, this can be done with the trimesh and networkx packages, like so:
mesh = trimesh.load_mesh('mesh.stl')
adj = networkx.adjacency_matrix(trimesh.graph.vertex_adjacency_graph(mesh))