I have an Nx3 numpy array listing the 3 vertices for N triangular faces of a surface, and an Nx1 array of values corresponding to each of these faces.
I want to convert (as best as possible) these "faces" values into "vertex" values, for example by finding the mean value for all the faces that a vertex is associated with.
My current solution is fine for small values of N, but scales as the no. of faces x no. of vertices, which gets impractical very quickly:
def face2vertVal(faces, facesVals, verts):
# INPUT:
# faces: Nx3 array of N vertex IDs for N faces
# facesVals: Nx1 array of some parameter for each face
# OUTPUT:
# vertsVals: Px1 array of the mean value in "facesVals" that corresponds
# to each vertex
import numpy as np
vertsVals = np.zeros(faces.max()+1)
for vertex in range(0, faces.max()+1):
tmpVals = np.zeros(1)
for row in range(0, faces.shape[0]):
if faces[row].__contains__(vertex):
tmpVals = np.append(tmpVals, facesVals[row])
vertsVals[vertex] = tmpVals[1:].mean()
del tmpVals
return vertsVals
Thanks in advance.
EDIT: Vectorised approach is very fast, but requires too much memory for 700k faces and 350k vertices.