Search code examples
pythonnumpymeshtrimesh

Plot facet normals in trimesh from the facet center


For this sample torus mesh in trimesh, I've managed to plot the normals to facets:

import numpy as np
import trimesh
mesh = trimesh.creation.torus(9, 3)
# adapted from https://github.com/mikedh/trimesh/issues/549
vec = np.column_stack((mesh.facets_origin, mesh.facets_origin + (mesh.facets_normal * mesh.scale * .05)))
path = trimesh.load_path(vec.reshape((-1, 2, 3)))
trimesh.Scene([mesh, path]).show(smooth=False)

below is the resultant plot: trimesh view

but as you can see, the origin of the facets is simply one of the vertices. How can the center of the facet be used as an origin for normals instead? I see mesh.triangles_center but there's not such luxury defined for facets it seems.


Solution

  • Had to calculate the centroids myself, trimesh doesn't do that for us.

    # calculate the center points
    facets_boundaries_xyz = [np.array(mesh.vertices.view(np.ndarray)[f_b]) for f_b in mesh.facets_boundary]
    mesh_facets_center = np.array([bound.mean(axis=(0, 1)) for bound in facets_boundaries_xyz])
    
    # plot
    vec = np.column_stack((mesh_facets_center, mesh_facets_center + (mesh.facets_normal * mesh.scale * .02)))
    path = trimesh.load_path(vec.reshape((-1, 2, 3)))
    trimesh.Scene([mesh, path]).show(smooth=False)
    
    

    fixed normals