Miscellaneous functions

Matrix I/O

In addition to numpy.save and numpy.load, we also provide an alternative way of saving and loading numpy.ndarray to files.

pymesh.save_matrix(filename, matrix, in_ascii=False)

Save matrix into file in .dmat format.

Parameters:
  • filename (str) – Output file name.
  • matrix (numpy.ndarray) – The matrix to save.
  • in_ascii (boolean) – Whether to save matrix in ASCII. Default is false, which saves in binary format to save space.
pymesh.load_matrix(filename)

Load matrix from file (assuming .dmat format).

Mesh type conversion

pymesh.quad_to_tri(mesh, keep_symmetry=False)

Convert quad mesh into triangles.

Parameters:
  • mesh (Mesh) – Input quad mesh.
  • keep_symmetry (boolean) – (optional) Whether to split quad symmetrically into triangles. Default is False.
Returns:

The resulting triangle mesh.

pymesh.hex_to_tet(mesh, keep_symmetry=False, subdiv_order=0)

Convert hex mesh into tet mesh.

Parameters:
  • mesh (Mesh) – Input hex mesh.
  • keep_symmetry (boolean) – (optional) Whether to split hex symmetrically into tets. Default is False.
  • subdiv_order (int) – (optional) Number of times to subdiv the hex before splitting. Default is 0.
Returns:

The resulting tet mesh.

Attribute type conversion

pymesh.convert_to_vertex_attribute(mesh, attr)

Convert attribute attr from either per-face or per-voxel attribute into per-vertex attribute.

Parameters:
  • mesh (Mesh) – Input mesh.
  • attr (numpy.ndarray) – #vertices by k matrix of floats.
Returns:

Per-vertex attribute. The value at a vertex will be the average of the values at its neighboring faces or voxels.

pymesh.convert_to_vertex_attribute_from_name(mesh, name)

Same as convert_to_vertex_attribute() except looking up attribute values from the input mesh using name.

pymesh.convert_to_face_attribute(mesh, attr)

Convert attribute attr from either per-vertex or per-voxel attribute into per-face attribute.

Parameters:
  • mesh (Mesh) – Input mesh.
  • attr (numpy.ndarray) – #faces by k matrix of floats.
Returns:

Per-face attribute. The value at a face will be the average of the values at its neighboring vertices or its neighboring voxels.

pymesh.convert_to_face_attribute_from_name(mesh, name)

Same as convert_to_face_attribute() except looking up attribute values from the input mesh using name.

pymesh.convert_to_voxel_attribute(mesh, attr)

Convert attribute attr from either per-vertex or per-face attribute into per-voxel attribute.

Parameters:
  • mesh (Mesh) – Input mesh.
  • attr (numpy.ndarray) – #voxel by k matrix of floats.
Returns:

Per-voxel attribute. The value at a voxel will be the average of the values at its neighboring vertices or faces.

pymesh.convert_to_voxel_attribute_from_name(mesh, name)

Same as convert_to_voxel_attribute() except looking up attribute values from the input mesh using name.

Attribute mapping

pymesh.map_vertex_attribute(mesh1, mesh2, attr_name, bvh=None)

Map vertex attribute from mesh1 to mesh2 based on closest points.

Parameters:
  • mesh1 (Mesh) – Source mesh, where the attribute is defined.
  • mesh2 (Mesh) – Target mesh, where the attribute is mapped to.
  • attr_name (string) – Attribute name.
  • bvh (BVH) – Pre-computed Bounded volume hierarchy if available.

A new attribute with name attr_name is added to mesh2.

pymesh.map_face_attribute(mesh1, mesh2, attr_name, bvh=None)

Map face attribute from mesh1 to mesh2 based on closest points.

Parameters:
  • mesh1 (Mesh) – Source mesh, where the attribute is defined.
  • mesh2 (Mesh) – Target mesh, where the attribute is mapped to.
  • attr_name (string) – Attribute name.
  • bvh (BVH) – Pre-computed Bounded volume hierarchy if available.

A new attribute with name attr_name is added to mesh2.

pymesh.map_corner_attribute(mesh1, mesh2, attr_name, bvh=None)

Map per-vertex per-face attribute from mesh1 to mesh2 based on closest points.

Parameters:
  • mesh1 (Mesh) – Source mesh, where the attribute is defined.
  • mesh2 (Mesh) – Target mesh, where the attribute is mapped to.
  • attr_name (string) – Attribute name.
  • bvh (BVH) – Pre-computed Bounded volume hierarchy if available.

A new attribute with name attr_name is added to mesh2.

Quaternion

class pymesh.Quaternion(quat=[1, 0, 0, 0])

This class implements quaternion used for 3D rotations.

w

float – same as quaternion[0].

x

float – same as quaternion[1].

y

float – same as quaternion[2].

z

float – same as quaternion[3].

classmethod fromAxisAngle(axis, angle)

Crate quaternion from axis angle representation

Parameters:
  • angle (float) – Angle in radian.
  • axis (numpy.ndarray) – Rotational axis. Not necessarily normalized.
Returns:

The following values are returned.

  • quat (Quaternion): The corresponding quaternion object.

classmethod fromData(v1, v2)

Create the rotation to rotate v1 to v2

Parameters:
  • v1 (numpy.ndarray) – From vector. Normalization not necessary.
  • v2 (numpy.ndarray) – To vector. Normalization not necessary.
Returns:

The following values are returned.

  • quat (Quaternion): Corresponding quaternion that rotates v1 to v2.

norm()

Quaternion norm.

normalize()

Normalize quaterion to have length 1.

to_matrix()

Convert to rotational matrix.

Returns:The corresponding rotational matrix.
Return type:numpy.ndarray
conjugate()

returns the conjugate of this quaternion, does nothing to self.

rotate(v)

Rotate 3D vector v by this quaternion

Parameters:v (numpy.ndarray) – Must be 1D vector.
Returns:The rotated vector.

Exact number types

pymesh.Gmpz

alias of mock.

pymesh.Gmpq

alias of mock.

Exact predicates

pymesh.orient_2D(p1, p2, p3)

Determine the orientation 2D points p1, p2, p3

Parameters:p1,p2,p3 – 2D points.
Returns:positive if (p1, p2, p3) is in counterclockwise order. negative if (p1, p2, p3) is in clockwise order. 0.0 if they are collinear.
pymesh.orient_3D(p1, p2, p3, p4)

Determine the orientation 3D points p1, p2, p3, p4.

Parameters:p1,p2,p3,p4 – 3D points.
Returns:positive if p4 is below the plane formed by (p1, p2, p3). negative if p4 is above the plane formed by (p1, p2, p3). 0.0 if they are coplanar.
pymesh.in_circle(p1, p2, p3, p4)

Determine if p4 is in the circle formed by p1, p2, p3.

Parameters:p1,p2,p3,p4 – 2D points. orient_2D(p1, p2, p3) must be postive, otherwise the result will be flipped.
Returns:positive p4 is inside of the circle. negative p4 is outside of the circle. 0.0 if they are cocircular.
pymesh.in_sphere(p1, p2, p3, p4, p5)

Determine if p5 is in the sphere formed by p1, p2, p3, p4.

Parameters:p1,p2,p3,p4,p5 – 3D points. orient_3D(p1, p2, p3, p4) must be positive, otherwise the result will be flipped.
Returns:positive p5 is inside of the sphere. negative p5 is outside of the sphere. 0.0 if they are cospherical.

The following predicates are built on top of above fundamental predicates.

pymesh.is_colinear(v0, v1, v2)

Return true if v0, v1 and v2 are colinear. Colinear check is done using exact predicates.

Parameters:
  • v0 (numpy.ndarray) – vector of size 2 or 3.
  • v1 (numpy.ndarray) – vector of size 2 or 3.
  • v2 (numpy.ndarray) – vector of size 2 or 3.
Returns:

A boolean indicating whether v0, v1 and v2 are colinear.

pymesh.get_degenerated_faces(mesh)

A thin wrapper for get_degenerated_faces_raw().

pymesh.get_degenerated_faces_raw(vertices, faces)

Return indices of degenerated faces. A face is degenerated if all its 3 corners are colinear.

Parameters:
  • vertices (numpy.ndarray) – Vertex matrix.
  • faces (numpy.ndarray) – Face matrix.
Returns:

A numpy.ndarray of indices of degenerated faces.

pymesh.get_tet_orientations(mesh)

A thin wrapper of get_tet_orientations_raw.

pymesh.get_tet_orientations_raw(vertices, tets)

Compute orientation of each tet.

Parameters:
  • vertice (numpy.ndarray) – n by 3 matrix representing vertices.
  • tets (numpy.ndarray) – m by 4 matrix of vertex indices representing tets.
Returns:

A list of m floats, where
  • Positive number => tet is positively oriented.
  • 0 => tet is degenerate.
  • Negative number => tet is inverted.

Mesh compression

pymesh.compress(mesh, engine_name='draco')

Compress mesh data.

Parameters:
Returns:

A binary string representing the compressed mesh data.

A simple usage example:

>>> mesh = pymesh.generate_icosphere(1.0, [0, 0, 0])
>>> data = pymesh.compress(mesh)
>>> with open("out.drc", 'wb') as fout:
...     fout.write(data)
[1]Draco uses lossy compression. Both accuarcy and vertices/face order will be lost due to compression. Draco only works with triangular mesh or point cloud.
pymesh.decompress(data, engine_name='draco')

Decompress mesh data.

Parameters:
  • data (string) – Binary string representing compressed mesh.
  • engine_name (string) –

    Decompression engine name. Valid engines are:

Returns:

A mesh object encoded by the data.

A simple usage example:

>>> mesh = pymesh.generate_icosphere(1.0, [0, 0, 0])
>>> data = pymesh.compress(mesh)
>>> mesh2 = pymesh.decompress(data);

Mesh to graph

pymesh.mesh_to_graph(mesh)

Convert a mesh into a graph \((V, E)\), where \(V\) represents the mesh vertices, and \(E\) represent the edges.

Parameters:mesh (Mesh) – Input mesh.
Returns:The graph \((V, E)\).
pymesh.mesh_to_dual_graph(mesh)

Convert a mesh into a dual graph \((V, E)\), where \(V\) represents the mesh faces, and \(E\) represent the dual edges.

Parameters:mesh (Mesh) – Input mesh.
Returns:The graph \((V, E)\).