def face_matrix(self): face_vertices = [None] * self.mesh.number_of_faces() for fkey in self.mesh.faces(): face_vertices[self.fkey_index[fkey]] = [ self.key_index[key] for key in self.mesh.face_vertices(fkey) ] return face_matrix(face_vertices, rtype='csr', normalize=True)
def mesh_face_matrix(mesh, rtype='array'): r"""Construct the face matrix from a Mesh datastructure. Parameters ---------- mesh : compas.datastructures.Mesh Instance of mesh. rtype : {'array', 'csc', 'csr', 'coo', 'list'} Format of the result. Returns ------- array-like Constructed mesh face matrix. Notes ----- The face matrix represents the relationship between faces and vertices. Each row of the matrix represents a face. Each column represents a vertex. The matrix is filled with zeros except where a relationship between a vertex and a face exist. .. math:: F_{ij} = \begin{cases} 1 & \text{if vertex j is part of face i} \\ 0 & \text{otherwise} \end{cases} The face matrix can for example be used to compute the centroids of all faces of a mesh. Examples -------- >>> from compas.datastructures import Mesh >>> mesh = Mesh.from_polyhedron(6) >>> F = mesh_face_matrix(mesh) >>> type(F) <class 'numpy.ndarray'> >>> F = mesh_face_matrix(mesh, rtype='csr') >>> type(F) <class 'scipy.sparse.csr.csr_matrix'> >>> from numpy import allclose >>> xyz = asarray(mesh.vertices_attributes('xyz')) >>> F = mesh_face_matrix(mesh, rtype='csr') >>> c1 = F.dot(xyz) / F.sum(axis=1) >>> c2 = [mesh.face_centroid(fkey) for fkey in mesh.faces()] >>> allclose(c1, c2) True """ key_index = {key: index for index, key in enumerate(mesh.vertices())} face_vertices = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in mesh.faces()] return face_matrix(face_vertices, rtype=rtype)
def mesh_face_matrix(mesh, rtype='csr'): r"""Construct the face matrix from a Mesh datastructure. Parameters ---------- mesh : obj Mesh datastructure object to get data from. rtype : {'array', 'csc', 'csr', 'coo', 'list'} Format of the result. Returns ------- array-like Constructed mesh face matrix. Notes ----- The face matrix represents the relationship between faces and vertices. Each row of the matrix represents a face. Each column represents a vertex. The matrix is filled with zeros except where a relationship between a vertex and a face exist. .. math:: F_{ij} = \begin{cases} 1 & \text{if vertex j is part of face i} \\ 0 & \text{otherwise} \end{cases} The face matrix can for example be used to compute the centroids of all faces of a mesh. Examples -------- .. code-block:: python import compas from compas.datastructures import Mesh from compas.datastructures import mesh_face_matrix mesh = Mesh.from_obj(compas.get('faces.obj')) F = mesh_face_matrix(mesh) xyz = array([mesh.vertex_coordinates(key) for key in mesh.vertices()]) c = F.dot(xyz) / normrow(F) """ key_index = {key: index for index, key in enumerate(mesh.vertices())} face_vertices = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in mesh.faces()] return face_matrix(face_vertices, rtype=rtype)