Meshes¶
Mesh3D¶
-
class
meshpy.Mesh3D(vertices, triangles, normals=None, density=1.0, center_of_mass=None, uniform_com=False)¶ Bases:
objectA triangular mesh for a three-dimensional shape representation.
-
vertices¶ numpy.ndarrayof float – A #verts by 3 array, where each row contains an ordered [x,y,z] set that describes one vertex.
-
triangles¶ numpy.ndarrayof int – A #tris by 3 array, where each row contains indices of vertices in the vertices array that are part of the triangle.
-
normals¶ numpy.ndarrayof float – A #normals by 3 array, where each row contains a normalized vector. This list should contain one norm per vertex.
-
density¶ float – The density of the mesh.
-
center_of_mass¶ numpy.ndarrayof float – The 3D location of the mesh’s center of mass.
-
mass¶ float – The mass of the mesh (read-only).
-
inertia¶ numpy.ndarrayof float – The 3x3 inertial matrix of the mesh (read-only).
-
bb_center¶ numpy.ndarrayof float – The 3D location of the center of the mesh’s minimal bounding box (read-only).
-
centroid¶ numpy.ndarrayof float – The 3D location of the mesh’s vertex mean (read-only).
-
__init__(vertices, triangles, normals=None, density=1.0, center_of_mass=None, uniform_com=False)¶ Construct a 3D triangular mesh.
Parameters: - vertices (
numpy.ndarrayof float) – A #verts by 3 array, where each row contains an ordered [x,y,z] set that describes one vertex. - triangles (
numpy.ndarrayof int) – A #tris by 3 array, where each row contains indices of vertices in the vertices array that are part of the triangle. - normals (
numpy.ndarrayof float) – A #normals by 3 array, where each row contains a normalized vector. This list should contain one norm per vertex. - density (float) – The density of the mesh.
- center_of_mass (
numpy.ndarrayof float) – The 3D location of the mesh’s center of mass. - uniform_com (bool) – Whether or not to assume a uniform mass density for center of mass comp
- vertices (
-
vertices¶ numpy.ndarrayof float – A #verts by 3 array, where each row contains an ordered [x,y,z] set that describes one vertex.
-
triangles¶ numpy.ndarrayof int – A #tris by 3 array, where each row contains indices of vertices in the vertices array that are part of the triangle.
-
normals¶ numpy.ndarrayof float – A #normals by 3 array, where each row contains a normalized vector. This list should contain one norm per vertex.
-
density¶ float – The density of the mesh.
-
center_of_mass¶ numpy.ndarrayof float – The 3D location of the mesh’s center of mass.
-
num_vertices¶ int– The number of total vertices
-
num_triangles¶ int– The number of total triangles
-
mass¶ float – The mass of the mesh (read-only).
-
inertia¶ numpy.ndarrayof float – The 3x3 inertial matrix of the mesh (read-only).
-
bb_center¶ numpy.ndarrayof float – The 3D location of the center of the mesh’s minimal bounding box (read-only).
-
centroid¶ numpy.ndarrayof float – The 3D location of the mesh’s vertex mean (read-only).
-
min_coords()¶ Returns the minimum coordinates of the mesh.
Returns: A 3-ndarray of floats that represents the minimal x, y, and z coordinates represented in the mesh. Return type: numpy.ndarrayof float
-
max_coords()¶ Returns the maximum coordinates of the mesh.
Returns: A 3-ndarray of floats that represents the minimal x, y, and z coordinates represented in the mesh. Return type: numpy.ndarrayof float
-
bounding_box()¶ Returns the mesh’s bounding box corners.
Returns: A 2-tuple of 3-ndarrays of floats. The first 3-array contains the vertex of the smallest corner of the bounding box, and the second 3-array contains the largest corner of the bounding box. Return type: tupleofnumpy.ndarrayof float
-
bounding_box_mesh()¶ Returns the mesh bounding box as a mesh.
Returns: A Mesh3D representation of the mesh’s bounding box. Return type: Mesh3D
-
principal_dims()¶ Returns the maximal span of the mesh’s coordinates.
The maximal span is the maximum coordinate value minus the minimal coordinate value in each principal axis.
Returns: A 3-ndarray of floats that represents the maximal x, y, and z spans of the mesh. Return type: numpy.ndarrayof float
-
support(direction)¶ Returns the support function in the given direction
Parameters: direction ( numpy.ndarrayof float) – A 3-ndarray of floats that is a unit vector in the direction of the desired support.Returns: A 3-ndarray of floats that represents the support. Return type: numpy.ndarrayof float
-
tri_centers()¶ Returns an array of the triangle centers as 3D points.
Returns: An ndarray of 3-ndarrays of floats, where each 3-ndarray represents the 3D point at the center of the corresponding mesh triangle. Return type: numpy.ndarrayofnumpy.ndarrayof float
-
tri_normals(align_to_hull=False)¶ Returns a list of the triangle normals.
Parameters: align_to_hull (bool) – If true, we re-orient the normals to point outward from the mesh by using the convex hull. Returns: A #triangles by 3 array of floats, where each 3-ndarray represents the 3D normal vector of the corresponding triangle. Return type: numpy.ndarrayof float
-
surface_area()¶ Return the surface area of the mesh.
Returns: The surface area of the mesh. Return type: float
-
total_volume()¶ Return the total volume of the mesh.
Returns: The total volume of the mesh. Return type: float
-
covariance()¶ Return the total covariance of the mesh’s triangles.
Returns: The total covariance of the mesh’s triangles. Return type: float
-
remove_bad_tris()¶ Remove triangles with out-of-bounds vertices from the mesh.
-
remove_unreferenced_vertices()¶ Remove any vertices that are not part of a triangular face.
Note
This method will fail if any bad triangles are present, so run remove_bad_tris() first if you’re unsure if bad triangles are present.
Returns: Returns True if vertices were removed, False otherwise. Return type: bool
-
center_vertices_avg()¶ Center the mesh’s vertices at the centroid.
This shifts the mesh without rotating it so that the centroid (mean) of all vertices is at the origin.
-
center_vertices_bb()¶ Center the mesh’s vertices at the center of its bounding box.
This shifts the mesh without rotating it so that the center of its bounding box is at the origin.
-
normalize_vertices()¶ Normalize the mesh’s orientation along its principal axes.
Transforms the vertices and normals of the mesh such that the origin of the resulting mesh’s coordinate frame is at the center of the bounding box and the principal axes (as determined from PCA) are aligned with the vertical Z, Y, and X axes in that order.
-
compute_vertex_normals()¶ Get normals from triangles
-
flip_normals()¶ Flips the mesh normals.
-
copy()¶ Return a copy of the mesh.
Note
This method only copies the vertices and triangles of the mesh.
-
subdivide(min_tri_length=inf)¶ Return a copy of the mesh that has been subdivided by one iteration.
Note
This method only copies the vertices and triangles of the mesh.
-
transform(T)¶ Return a copy of the mesh that has been transformed by T.
Parameters: T ( RigidTransform) – The RigidTransform by which the mesh is transformed.Note
This method only copies the vertices and triangles of the mesh.
-
random_points(n_points)¶ Generate uniformly random points on the surface of the mesh.
Parameters: n_points (int) – The number of random points to generate. Returns: A n_points by 3 ndarray that contains the sampled 3D points. Return type: numpy.ndarrayof float
-
ray_intersections(ray, point, distance)¶ Returns a list containing the indices of the triangles that are intersected by the given ray emanating from the given point within some distance.
-
get_T_surface_obj(T_obj_surface, delta=0.0)¶ Gets the transformation that puts the object resting exactly on the z=delta plane
Parameters: - T_obj_surface (
RigidTransform) – The RigidTransform by which the mesh is transformed. - delta (float) – Z-coordinate to rest the mesh on
Note
This method copies the vertices and triangles of the mesh.
- T_obj_surface (
-
rescale_dimension(scale, scaling_type=0)¶ Rescales the vertex coordinates to scale using the given scaling_type.
Parameters: - scale (float) – The desired scaling factor of the selected dimension, if scaling_type is ScalingTypeMin, ScalingTypeMed, ScalingTypeMax, or ScalingTypeDiag. Otherwise, the overall scaling factor.
- scaling_type (int) – One of ScalingTypeMin, ScalingTypeMed, ScalingTypeMax, ScalingTypeRelative, or ScalingTypeDiag. ScalingTypeMin scales the smallest vertex extent (X, Y, or Z) by scale, ScalingTypeMed scales the median vertex extent, and ScalingTypeMax scales the maximum vertex extent. ScalingTypeDiag scales the bounding box diagonal (divided by three), and ScalingTypeRelative provides absolute scaling.
-
rescale(scale_factor)¶ Rescales the vertex coordinates by scale_factor.
Parameters: scale_factor (float) – The desired scale factor for the mesh’s vertices.
-
convex_hull()¶ Return a 3D mesh that represents the convex hull of the mesh.
-
stable_poses(min_prob=0.0)¶ Computes all valid StablePose objects for the mesh.
Parameters: min_prob (float) – stable poses that are less likely than this threshold will be discarded Returns: A list of StablePose objects for the mesh. Return type: listofStablePose
-
resting_pose(T_obj_world, eps=1e-10)¶ Returns the stable pose that the mesh will rest on if it lands on an infinite planar worksurface quasi-statically in the given transformation (only the rotation is used).
Parameters: - T_obj_world (
autolab_core.RigidTransform) – transformation from object to table basis (z-axis upward) specifying the orientation of the mesh - eps (float) – numeric tolerance in cone projection solver
Returns: stable pose specifying the face that the mesh will land on
Return type: StablePose- T_obj_world (
-
merge(other_mesh)¶ Combines this mesh with another mesh.
Parameters: other_mesh ( Mesh3D) – the mesh to combine withReturns: merged mesh Return type: Mesh3D
-
flip_tri_orientation()¶ Flips the orientation of all triangles.
-
visualize(color=(0.5, 0.5, 0.5), style=’surface’, opacity=1.0)¶ Plots visualization of mesh using MayaVI.
Parameters: - color (
tupleof float) – 3-tuple of floats in [0,1] to give the mesh’s color - style (
str) – Either ‘surface’, which produces an opaque surface, or ‘wireframe’, which produces a wireframe. - opacity (float) – A value in [0,1] indicating the opacity of the mesh. Zero is transparent, one is opaque.
Returns: The displayed surface.
Return type: mayavi.modules.surface.Surface- color (
-
static
load(filename, cache_dir, preproc_script=None)¶ Load a mesh from a file.
Note
If the mesh is not already in .obj format, this requires the installation of meshlab. Meshlab has a command called meshlabserver that is used to convert the file into a .obj format.
Parameters: - filename (
str) – Path to mesh file. - cache_dir (
str) – A directory to store a converted .obj file in, if the file isn’t already in .obj format. - preproc_script (
str) – The path to an optional script to run before converting the mesh file to .obj if necessary.
Returns: A 3D mesh object read from the file.
Return type: Mesh3D- filename (
-