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.
- filename (
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.
- 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.
- 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.
- mesh (
-
pymesh.
convert_to_vertex_attribute_from_name
(mesh, name)¶ Same as
convert_to_vertex_attribute()
except looking up attribute values from the inputmesh
usingname
.
-
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.
- mesh (
-
pymesh.
convert_to_face_attribute_from_name
(mesh, name)¶ Same as
convert_to_face_attribute()
except looking up attribute values from the inputmesh
usingname
.
-
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.
- mesh (
-
pymesh.
convert_to_voxel_attribute_from_name
(mesh, name)¶ Same as
convert_to_voxel_attribute()
except looking up attribute values from the inputmesh
usingname
.
Quaternion¶
-
class
pymesh.
Quaternion
(quat=[1, 0, 0, 0])¶ This class implements quaternion used for 3D rotations.
-
w
¶ float
– same asquaternion[0]
.
-
x
¶ float
– same asquaternion[1]
.
-
y
¶ float
– same asquaternion[1]
.
-
z
¶ float
– same asquaternion[2]
.
-
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.
- angle (
-
classmethod
fromData
(v1, v2)¶ Create the rotation to rotate v1 to v2
Parameters: - v1 (
numpy.ndarray
) – Vrom vector. Normalization not necessary. - v2 (
numpy.ndarray
) – To vector. Normalization not necessary.
Returns: The following values are returned.
quat
(Quaternion
): Corresponding quaternion that rotatesv1
tov2
.
- v1 (
-
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 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
andv2
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
andv2
are colinear.- v0 (
-
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.- vertices (
-
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.
- vertice (
Mesh compression¶
-
pymesh.
compress
(mesh, engine_name='draco')¶ Compress mesh data.
Parameters: - mesh (
Mesh
) – Input mesh. - engine_name (
string
) –Valid engines are:
draco
: Google’s Draco engine [1]
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. - mesh (
-
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:
draco
: Google’s Draco engine
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);
- data (