kaolin.metrics.trianglemesh

API

class kaolin.metrics.trianglemesh.UnbatchedTriangleDistanceCuda

Bases: torch.autograd.function.Function

static backward(ctx, grad_dist, grad_face_idx, grad_dist_type)

Defines a formula for differentiating the operation.

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by as many outputs did forward() return, and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input.

The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output.

static forward(ctx, points, face_vertices)

Performs the operation.

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

The context can be used to store tensors that can be then retrieved during the backward pass.

kaolin.metrics.trianglemesh.average_edge_length(vertices, faces)

Returns the average length of each faces in a mesh.

Parameters
  • vertices (torch.Tensor) – Batched vertices, of shape \((\text{batch_size}, \text{num_vertices}, 3)\).

  • faces (torch.LongTensor) – Faces, of shape \((\text{num_faces}, 3)\).

Returns

average length of each edges in a face, of shape \((\text{batch_size}, \text{num_faces})\).

Return type

(torch.Tensor)

Example

>>> vertices = torch.tensor([[[1, 0, 0],
...                           [0, 1, 0],
...                           [0, 0, 1]]], dtype=torch.float)
>>> faces = torch.tensor([[0, 1, 2]])
>>> average_edge_length(vertices, faces)
tensor([[1.4142]])
kaolin.metrics.trianglemesh.point_to_mesh_distance(pointclouds, face_vertices)

Computes the distances from pointclouds to meshes (represented by vertices and faces.) For each point in the pointcloud, it finds the nearest triangle in the mesh, and calculated its distance to that triangle.

Type 0 indicates the distance is from a point on the surface of the triangle.

Type 1 to 3 indicates the distance is from a point to a vertices.

Type 4 to 6 indicates the distance is from a point to an edge.

Parameters
  • pointclouds (torch.Tensor) – pointclouds, of shape \((\text{batch_size}, \text{num_points}, 3)\).

  • face_vertices (torch.Tensor) – vertices of each face of meshes, of shape \((\text{batch_size}, \text{num_faces}, 3, 3})\).

Returns

  • Distances between pointclouds and meshes, of shape \((\text{batch_size}, \text{num_points})\).

  • face indices selected, of shape \((\text{batch_size}, \text{num_points})\).

  • Types of distance of shape \((\text{batch_size}, \text{num_points})\).

Return type

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

Example

>>> from kaolin.ops.mesh import index_vertices_by_faces
>>> point = torch.tensor([[[0.5, 0.5, 0.5],
...                        [3., 4., 5.]]], device='cuda')
>>> vertices = torch.tensor([[[0., 0., 0.],
...                           [0., 1., 0.],
...                           [0., 0., 1.]]], device='cuda')
>>> faces = torch.tensor([[0, 1, 2]], dtype=torch.long, device='cuda')
>>> face_vertices = index_vertices_by_faces(vertices, faces)
>>> distance, index, dist_type = point_to_mesh_distance(point, face_vertices)
>>> distance
tensor([[ 0.2500, 41.0000]], device='cuda:0')
>>> index
tensor([[0, 0]], device='cuda:0')
>>> dist_type
tensor([[5, 5]], device='cuda:0', dtype=torch.int32)
kaolin.metrics.trianglemesh.uniform_laplacian_smoothing(vertices, faces)

Calculates the uniform laplacian smoothing of meshes. The position of updated vertices is defined as \(V_i = \frac{1}{N} * \sum^{N}_{j=1}V_j\), where \(N\) is the number of neighbours of \(V_i\), \(V_j\) is the position of the j-th adjacent vertex.

Parameters
  • vertices (torch.Tensor) – Vertices of the meshes, of shape \((\text{batch_size}, \text{num_vertices}, 3)\).

  • faces (torch.LongTensor) – Faces of the meshes, of shape \((\text{num_faces}, \text{face_size})\).

Returns

smoothed vertices, of shape \((\text{batch_size}, \text{num_vertices}, 3)\).

Return type

(torch.FloatTensor)

Example

>>> vertices = torch.tensor([[[1, 0, 0],
...                           [0, 1, 0],
...                           [0, 0, 1]]], dtype=torch.float)
>>> faces = torch.tensor([[0, 1, 2]])
>>> uniform_laplacian_smoothing(vertices, faces)
tensor([[[0.0000, 0.5000, 0.5000],
         [0.5000, 0.0000, 0.5000],
         [0.5000, 0.5000, 0.0000]]])