I have two numpy matrices with with different shapes:
vertices = np.array([[ 100, 101, 102, 103, -1],
[ 200, 201, 202, 203, 204],
[ 300, 301, 302, 303, 104],
[ 505, 506, 507, -1, -1]])
faces = np.array([[ 104, 102, 100],
[1202, 203, 2000],
[ 303, 505, 104],
[ 101, 102, 104]])
I want to link the index of each row in faces to rows in vertices in order to compute the correspdoning area for each vertex. The area computation is not in this post because it is irrelevant.
Each vertex can correspond to any number of faces. Each face can only be attached to one vertex.
Link rule: For each row in vertices, if 2 or more elements exist in a faces row, those rows are linked.
Expected result is a dictionary with keys that are indices into vertices, and values that are the count of linked faces for the corresponding vertex.
Expected result:
{0: 2.0, 2: 1.0}
I wrote a working algorithm but I am looking for a more performant implementation:
def area(faces, corresponding_vertices_id):
skeleton_node_corresponding_area = defaultdict(lambda: 0.)
for face in faces:
for skeleton_node, vectex_id in enumerate(corresponding_vertices_id):
if np.count_nonzero(np.in1d(face, vectice_id)) >=2 :
area = 1 # Mock action. real area will be compute later
skeleton_node_corresponding_area[skeleton_node] = skeleton_node_corresponding_area[skeleton_node] + area
break
return skeleton_node_corresponding_area
area(faces, vertices)
defaultdict(<function __main__.area.<locals>.<lambda>()>, {0: 2.0, 2: 1.0})