Mesh#

class voxel.Mesh(vertices, faces)#

A triangular mesh in 3D world space.

property vertices: Tensor#

Point positions represented by a (V, 3) tensor.

property num_vertices: int#

Total number of vertices in the mesh.

property faces: Tensor#

Triangle faces represented by a (F, 3) tensor.

property num_faces: int#

Total number of faces in the mesh.

new(vertices) → Mesh#

Construct a new mesh instance with the provided vertices, while preserving any unchanged properties of the original.

Parameters:

vertices (Tensor) – The new vertices tensor replacement.

property device: device#

The device of the mesh vertex and face tensors.

to(device) → Mesh#

Move the mesh (vertex and face tensors) to a device.

Parameters:

device (device) – The target device.

Returns:

Mesh – A new mesh instance.

cpu() → Mesh#

Move the mesh vertex and face tensors to the CPU.

Returns:

Mesh – A new mesh instance with the data on the CPU.

cuda() → Mesh#

Move the mesh vertex and face tensors to the GPU.

Returns:

Mesh – A new mesh instance with the data on the GPU.

type(dtype) → Mesh#

Cast the mesh vertices to a new data type.

Parameters:

dtype (torch.dtype) – The target vertex data type.

Returns:

Mesh – A new mesh instance with the casted vertices.

save(filename, fmt=None, **kwargs) → None#

Save the mesh to a file.

Parameters:
  • filename (PathLike) – The path to the file to save.

  • fmt (str, optional) – The format of the file. If None, the format is determined by the file extension.

  • **kwargs (Any) – Additional arguments passed to the file writing method.

property triangles: Tensor#

Triangle coordinate array with shape (F, 3, 3).

property triangles_cross: Tensor#

Vertex cross-product with shape (F, 3).

property edges: Tensor#

All directional edges in the mesh, with shape (E, 2). Note these are not unique.

property uniform_laplacian: Tensor#

The sparse uniform Laplacian matrix for the mesh connectivity. Note that it is generally faster to use the gather() or smooth_features() methods for diffusing features on the mesh graph.

property edge_face: Tensor#

Face indices corresponding to each directional edge in the mesh, with shape (E,).

property unique_edge_indices: tuple#

Indices that extract all unique edges from the directional edge list.

property unique_edges: Tensor#

Unique bi-directional edges in the mesh, with shape (U, 2).

property adjacent_faces: Tensor#

Adjacent face indices corresponding to each edge in unique_edges.

property face_normals: Tensor#

Face (unit) normals with shape (F, 3).

property face_areas: Tensor#

Face areas with shape (F,)

property face_angles: Tensor#

Face angles (in radians) with shape (F, 3).

property vertex_normals: Tensor#

Vertex (unit) normals, computed from face normals weighted by their angle.

property vertex_areas: Tensor#

The total face surface area contributed to each vertex.

flip_faces() → Mesh#

Flip triangular face directions.

gather(features, reduce='mean') → Tensor#

Gather and reduce neighboring vertex features across mesh edges, i.e. for each vertex, combine the features of its adjacent vertices.

Parameters:
  • features (Tensor) – Per-vertex features of shape \((V, C)\).

  • reduce (str, optional) – Reduction applied over each vertex’s neighbors, e.g. ‘mean’, ‘sum’, ‘amax’, or ‘amin’. Defaults to ‘mean’.

Returns:

Tensor – Reduced per-vertex features matching the input shape.

smooth_mesh(alpha=0.5, iterations=1) → Mesh#

Smooth the mesh vertex positions with the uniform Laplacian operator.

Parameters:
  • alpha (float, optional) – Smoothing factor between 0 and 1. Defaults to 0.5.

  • iterations (int, optional) – Number of smoothing iterations. Defaults to 1.

Returns:

Mesh – Smoothed mesh.

smooth_features(features, alpha=0.5, iterations=1) → Tensor#

Smooth vertex features with the uniform Laplacian operator. Note that this does not actually use the sparse Laplacian matrix, but rather the vertex gather method, which is roughly 2x faster.

Parameters:
  • features (Tensor) – Input features to smooth of the shape \((V,)\) or \((V, C)\).

  • alpha (float, optional) – Smoothing factor between 0 and 1. Defaults to 0.5.

  • iterations (int, optional) – Number of smoothing iterations. Defaults to 1.

Returns:

Tensor – Smoothed features matching the input shape.

transform(transform) → Mesh#

Transform mesh vertex coordinates with an affine matrix.

Parameters:

transform (AffineMatrix) – Affine transformation.

Returns:

Mesh – Transformed mesh.

bounds(margin=None, *components) → BoundingBox#

Compute the axis-aligned bounding box enclosing the mesh vertices.

Parameters:
  • margin (float or Tensor, optional) – Margin (in vertex units) to expand the bounds. Can be a positive or negative delta.

  • *components (float) – Additional components of margin, allowing values to be passed as separate positional arguments, e.g. bounds(1, 2, 3).

Returns:

BoundingBox – Bounding box enclosing the vertices.

extract_submesh(vertex_mask) → Mesh#

Extract a submesh containing only the vertices included in an input mask.

Parameters:

vertex_mask (Tensor) – A boolean vertex mask indicating which vertices to include in the submesh.

Returns:

Mesh – Extracted mesh with a subset of vertices and remapped face indices.

largest_connected_components(k=1) → Tensor#

Compute a mask indicating the top k largest connected components in the mesh graph, where ‘largest’ is defined by number of vertices. This method uses scipy under the hood and therefore runs on the CPU.

Parameters:

k (int, optional) – The top k largest components to include. Defaults to 1.

Returns:

Tensor – A boolean vertex mask representing vertices in the top k components.