mesh_face_matrix

compas.datastructures.mesh_face_matrix(mesh, rtype='array')[source]

Construct the face matrix from a Mesh datastructure.

Parameters
  • mesh (Mesh) – Instance of mesh.

  • rtype (Literal[‘array’, ‘csc’, ‘csr’, ‘coo’, ‘list’], optional) – 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.

\[\begin{split}F_{ij} = \begin{cases} 1 & \text{if vertex j is part of face i} \\ 0 & \text{otherwise} \end{cases}\end{split}\]

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')
>>> 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