kaolin.non_commercial

License

This submodule contains features under NSCL license restricted to non commercial usage for research and evaluation purposes.

API

class kaolin.non_commercial.FlexiCubes(device='cuda')

This class implements the FlexiCubes method for extracting meshes from scalar fields. It maintains a series of lookup tables and indices to support the mesh extraction process. FlexiCubes, a differentiable variant of the Dual Marching Cubes (DMC) scheme, enhances the geometric fidelity and mesh quality of reconstructed meshes by dynamically adjusting the surface representation through gradient-based optimization.

During instantiation, the class loads DMC tables from a file and transforms them into PyTorch tensors on the specified device.

flexicubes

Note

For more details and example usage in optimization, refer to the Flexible Isosurface Extraction for Gradient-Based Mesh Optimization SIGGRAPH 2023 paper, and the project page.

device

Specifies the computational device (default is “cuda”).

Type

str

dmc_table

Dual Marching Cubes (DMC) table that encodes the edges associated with each dual vertex in 256 Marching Cubes (MC) configurations.

Type

torch.Tensor

num_vd_table

Table holding the number of dual vertices in each of the 256 MC configurations.

Type

torch.Tensor

check_table

Table resolving ambiguity in cases C16 and C19 of the DMC configurations.

Type

torch.Tensor

tet_table

Lookup table used in tetrahedralizing the isosurface.

Type

torch.Tensor

quad_split_1

Indices for splitting a quad into two triangles along one diagonal.

Type

torch.Tensor

quad_split_2

Alternative indices for splitting a quad into two triangles along the other diagonal.

Type

torch.Tensor

quad_split_train

Indices for splitting a quad into four triangles during training by connecting all edges to their midpoints.

Type

torch.Tensor

cube_corners

Defines the positions of a standard unit cube’s eight corners in 3D space, ordered starting from the origin (0,0,0), moving along the x-axis, then y-axis, and finally z-axis. Used as a blueprint for generating a voxel grid.

Type

torch.Tensor

cube_corners_idx

Cube corners indexed as powers of 2, used to retrieve the case id.

Type

torch.Tensor

cube_edges

Edge connections in a cube, listed in pairs. Used to retrieve edge vertices in DMC.

Type

torch.Tensor

edge_dir_table

A mapping tensor that associates edge indices with their corresponding axis. For instance, edge_dir_table[0] = 0 indicates that the first edge is oriented along the x-axis.

Type

torch.Tensor

dir_faces_table

A tensor that maps the corresponding axis of shared edges across four adjacent cubes to the shared faces of these cubes. For instance, dir_faces_table[0] = [5, 4] implies that for four cubes sharing an edge along the x-axis, the first and second cubes share faces indexed as 5 and 4, respectively. This tensor is only utilized during isosurface tetrahedralization.

Type

torch.Tensor

adj_pairs

A tensor containing index pairs that correspond to neighboring cubes that share the same edge.

Type

torch.Tensor

__call__(voxelgrid_vertices, scalar_field, cube_idx, resolution, qef_reg_scale=0.001, weight_scale=0.99, beta=None, alpha=None, gamma_f=None, training=False, output_tetmesh=False, grad_func=None)

Main function for mesh extraction from scalar field using FlexiCubes. This function converts discrete signed distance fields, encoded on voxel grids and additional per-cube parameters, to triangle or tetrahedral meshes using a differentiable operation as described in Flexible Isosurface Extraction for Gradient-Based Mesh Optimization. FlexiCubes enhances mesh quality and geometric fidelity by adjusting the surface representation based on gradient optimization. The output surface is differentiable with respect to the input vertex positions, scalar field values, and weight parameters.

If you intend to extract a surface mesh from a fixed Signed Distance Field without the optimization of parameters, it is suggested to provide the “grad_func” which should return the surface gradient at any given 3D position. When grad_func is provided, the process to determine the dual vertex position adapts to solve a Quadratic Error Function (QEF), as described in the Manifold Dual Contouring paper, and employs an smart splitting strategy. Please note, this approach is non-differentiable.

For more details and example usage in optimization, refer to the Flexible Isosurface Extraction for Gradient-Based Mesh Optimization SIGGRAPH 2023 paper.

Parameters
  • voxelgrid_vertices (torch.Tensor) – Coordinates of the voxel grid vertices, can be deformed, of shape \((\text{num_vertices}, 3)\).

  • scalar_field (torch.Tensor) – Scalar field values at each vertex of the voxel grid. Negative values denote that the corresponding vertex resides inside the isosurface, of shape \((\text{num_vertices},)\). This affects the directions of the extracted triangle faces and volume to be tetrahedralized.

  • cube_idx (torch.Tensor) – Indices of 8 vertices for each cube in the voxel grid, of shape \((\text{num_cube}, 8)\).

  • resolution (int or list[int]) – The resolution of the voxel grid. If an integer is provided, it is used for all three dimensions. If a list or tuple of 3 integers is provided, they specify the resolution for the x, y, and z dimensions respectively.

  • qef_reg_scale (float, optional) – The scaling factor applied to the regularization loss to prevent issues with singularity when solving the QEF. This parameter is only used when a ‘grad_func’ is specified. Default: 1e-3.

  • weight_scale (float, optional) – The scale of weights in FlexiCubes. Should be between 0 and 1. Default: 0.99.

  • beta (torch.Tensor, optional) – Weight parameters for the cube edges to adjust dual vertices positioning, of shape \((\text{num_cube}, 12)\). Default: uniform value for all edges.

  • alpha (torch.Tensor, optional) – Weight parameters for the cube corners to adjust dual vertices positioning, of shape \((\text{num_cubes}, 8)\). Default: uniform value for all vertices.

  • gamma_f (torch.Tensor, optional) – Weight parameters to control the splitting of quadrilaterals into triangles, of shape \((\text{num_cubes},)\). Default: uniform value for all cubes.

  • training (bool, optional) – If set to True, applies differentiable quad splitting for training. Default: False.

  • output_tetmesh (bool, optional) – If set to True, outputs a tetrahedral mesh, otherwise, outputs a triangular mesh. Defaults: False.

  • grad_func (callable, optional) – A function to compute the surface gradient at specified 3D positions (input: Nx3 positions). The function should return gradients as an Nx3 tensor. If None, the original FlexiCubes algorithm is utilized. Defaults to None.

Returns

  • Vertices for the extracted triangular/tetrahedral mesh, of shape \((\text{num_vertices}, 3)\).

  • Faces for the extracted triangular/tetrahedral mesh, of shape \((\text{num_faces}, 3)\)/\((\text{num_faces}, 4)\).

  • Regularizer L_dev, computed per dual vertex, of shape \((\text{num_dual_vertices},)\).

Return type

(torch.Tensor, torch.LongTensor, torch.Tensor)

construct_voxel_grid(resolution)

Generates a voxel grid based on the specified resolution.

The voxelgrid is represented as a mesh, with the voxels corners as vertices.

Parameters

resolution (int or list[int]) – The resolution of the voxel grid. If an integer is provided, it is used for all three dimensions. If a list or tuple of 3 integers is provided, they define the resolution for the x, y, and z dimensions respectively.

Returns

  • The corners of the voxels of the constructed voxelgrid, centered and normalized (i.e: the vertices are in the range \([-0.5, 0.5]), of shape :math:\).

  • The indices into vertices for each voxel, of shape \((\text{res}, 8)\).

Return type

(torch.Tensor, torch.Tensor)