trimesh.geometry#

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

Find the rotation matrix that transforms one 3D vector to another.

Parameters:
  • a ((3,) float) – Unit vector

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

  • return_angle (bool) – Return the angle between vectors or not

Returns:

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

  • angle (float) – If return_angle angle in radians between a and b

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, dtype=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

columnsint

Number of columns, usually number of vertices

indices(m, d) int

Usually mesh.faces

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)

co

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, dtype=<class 'numpy.int64'>) ndarray[Any, dtype[_ScalarType_co]]#

Given an array of quad faces return them as triangle faces, also handles pure triangles and mixed triangles and quads.

Parameters:

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

Returns:

faces – Vertex indices of triangular faces.c

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, faces_sparse)#

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

  • faces_sparse (scipy.sparse.COO) – Sparse matrix

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 contribution 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