trimesh.geometry module

Functions

align_vectors(a, b[, return_angle])

Find a transform between two 3D vectors.

faces_to_edges(faces[, return_index])

Given a list of faces (n,3), return a list of edges (n*3,2)

index_sparse(columns, indices[, data])

Return a sparse matrix for which vertices are contained in which faces.

mean_vertex_normals(vertex_count, faces, …)

Find vertex normals from the mean of the faces that contain that vertex.

plane_transform(origin, normal)

Given the origin and normal of a plane find the transform that will move that plane to be coplanar with the XY plane.

triangulate_quads(quads)

Given a set of quad faces, return them as triangle faces.

vector_angle(pairs)

Find the angles between pairs of unit vectors.

vertex_face_indices(vertex_count, faces[, …])

Find vertex face indices from the faces array of vertices

weighted_vertex_normals(vertex_count, faces, …)

Compute vertex normals from the faces that contain that vertex.

trimesh.geometry.align_vectors(a, b, return_angle=False)

Find a transform between two 3D vectors.

Implements the method described here: http://ethaneade.com/rot_between_vectors.pdf

Parameters
  • a ((3,) float) – Source vector

  • b ((3,) float) – Target vector

  • return_angle (bool) – If True return the angle between the two vectors

Returns

  • transform ((4, 4) float) – Homogeneous transform from a to b

  • angle (float) – Angle between vectors in radians Only returned if return_angle

trimesh.geometry.faces_to_edges(faces, return_index=False)

Given a list of faces (n,3), return a list of edges (n*3,2)

Parameters

faces ((n, 3) int) – Vertex indices representing faces

Returns

edges – Vertex indices representing edges

Return type

(n*3, 2) int

trimesh.geometry.index_sparse(columns, indices, data=None)

Return a sparse matrix for which vertices are contained in which faces. A data vector can be passed which is then used instead of booleans

Parameters
  • columns (int) – Number of columns, usually number of vertices

  • indices ((m, d) int) – Usually mesh.faces

Returns

  • sparse (scipy.sparse.coo_matrix of shape (columns, len(faces))) – dtype is boolean

  • Examples – ———-

  • In [1] (sparse = faces_sparse(len(mesh.vertices), mesh.faces))

  • In [2] (sparse.shape)

  • Out[2] ((12, 20))

  • In [3] (mesh.faces.shape)

  • Out[3] ((20, 3))

  • In [4] (mesh.vertices.shape)

  • Out[4] ((12, 3))

  • In [5] (dense = sparse.toarray().astype(int))

  • In [6] (dense)

  • Out[6]

  • array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], – [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1]])

  • In [7] (dense.sum(axis=0))

  • Out[7] (array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]))

trimesh.geometry.mean_vertex_normals(vertex_count, faces, face_normals, sparse=None, **kwargs)

Find vertex normals from the mean of the faces that contain that vertex.

Parameters
  • vertex_count (int) – The number of vertices faces refer to

  • faces ((n, 3) int) – List of vertex indices

  • face_normals ((n, 3) float) – Normal vector for each face

Returns

vertex_normals – Normals for every vertex Vertices unreferenced by faces will be zero.

Return type

(vertex_count, 3) float

trimesh.geometry.plane_transform(origin, normal)

Given the origin and normal of a plane find the transform that will move that plane to be coplanar with the XY plane.

Parameters
  • origin ((3,) float) – Point that lies on the plane

  • normal ((3,) float) – Vector that points along normal of plane

Returns

transform – Transformation matrix to move points onto XY plane

Return type

(4,4) float

trimesh.geometry.triangulate_quads(quads)

Given a set of quad faces, return them as triangle faces.

Parameters

quads ((n, 4) int) – Vertex indices of quad faces

Returns

faces – Vertex indices of triangular faces

Return type

(m, 3) int

trimesh.geometry.vector_angle(pairs)

Find the angles between pairs of unit vectors.

Parameters

pairs ((n, 2, 3) float) – Unit vector pairs

Returns

angles – Angles between vectors in radians

Return type

(n,) float

trimesh.geometry.vertex_face_indices(vertex_count, faces, sparse=None)

Find vertex face indices from the faces array of vertices

Parameters
  • vertex_count (int) – The number of vertices faces refer to

  • faces ((n, 3) int) – List of vertex indices

Returns

vertex_faces – Face indices for every vertex Array padded with -1 in each row for all vertices with fewer face indices than the max number of face indices.

Return type

(vertex_count, ) int

trimesh.geometry.weighted_vertex_normals(vertex_count, faces, face_normals, face_angles, use_loop=False)

Compute vertex normals from the faces that contain that vertex. The contibution of a face’s normal to a vertex normal is the ratio of the corner-angle in which the vertex is, with respect to the sum of all corner-angles surrounding the vertex.

Grit Thuerrner & Charles A. Wuethrich (1998) Computing Vertex Normals from Polygonal Facets, Journal of Graphics Tools, 3:1, 43-46

Parameters
  • vertex_count (int) – The number of vertices faces refer to

  • faces ((n, 3) int) – List of vertex indices

  • face_normals ((n, 3) float) – Normal vector for each face

  • face_angles ((n, 3) float) – Angles at each vertex in the face

Returns

vertex_normals – Normals for every vertex Vertices unreferenced by faces will be zero.

Return type

(vertex_count, 3) float