kaolin.ops.mesh¶
API¶
- kaolin.ops.mesh.adjacency_matrix(num_vertices, faces, sparse=True)¶
Calculates a adjacency matrix of a mesh.
- Parameters
- Returns
adjacency matrix
- Return type
(torch.FloatTensor or torch.sparse.FloatTensor)
Example
>>> faces = torch.tensor([[0, 1, 2]]) >>> adjacency_matrix(3, faces) tensor(indices=tensor([[0, 0, 1, 1, 2, 2], [1, 2, 0, 2, 0, 1]]), values=tensor([1., 1., 1., 1., 1., 1.]), size=(3, 3), nnz=6, layout=torch.sparse_coo)
- kaolin.ops.mesh.check_sign(verts, faces, points, hash_resolution=512)¶
Checks if a set of points is contained inside a mesh.
Each batch takes in v vertices, f faces of a watertight trimesh, and p points to check if they are inside the mesh. Shoots a ray from each point to be checked and calculates the number of intersections between the ray and triangles in the mesh. Uses the parity of the number of intersections to determine if the point is inside the mesh.
- Parameters
verts (torch.Tensor) – vertices of shape (batch_size, num_vertices, 3)
faces (torch.Tensor) – faces of shape (num_faces, 3)
points (torch.Tensor) – points of shape (batch_size, num_points, 3) to check
hash_resolution (int) – resolution used to check the points sign
- Returns
length p tensor indicating whether each point is inside the mesh
- Return type
(torch.BoolTensor)
Example
>>> device = 'cuda' if torch.cuda.is_available() else 'cpu' >>> verts = torch.tensor([[[0., 0., 0.], ... [1., 0.5, 1.], ... [0.5, 1., 1.], ... [1., 1., 0.5]]], device = device) >>> faces = torch.tensor([[0, 3, 1], ... [0, 1, 2], ... [0, 2, 3], ... [3, 2, 1]], device = device) >>> axis = torch.linspace(0.1, 0.9, 3, device = device) >>> p_x, p_y, p_z = torch.meshgrid(axis + 0.01, axis + 0.02, axis + 0.03) >>> points = torch.cat((p_x.unsqueeze(-1), p_y.unsqueeze(-1), p_z.unsqueeze(-1)), dim=3) >>> points = points.view(1, -1, 3) >>> check_sign(verts, faces, points) tensor([[ True, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, True, False, False, False, False, False, True, False, True, False]], device='cuda:0')
- kaolin.ops.mesh.face_areas(vertices, faces)¶
Compute the areas of each face of triangle meshes.
- Parameters
vertices (torch.Tensor) – The vertices of the meshes, of shape \((\text{batch_size}, \text{num_vertices}, 3)\).
faces (torch.LongTensor) – the faces of the meshes, of shape \((\text{num_faces}, 3)\).
- Returns
the face areas of same type as vertices and of shape \((\text{batch_size}, \text{num_faces})\).
- Return type
- kaolin.ops.mesh.face_normals(face_vertices, unit=False)¶
Calculate normals of triangle meshes.
- Parameters
face_vertices (torch.Tensor) – 3D points in camera coordinate, of shape \((\text{batch_size}, \text{num_faces}, 3, 3)\), 9 means 3 triangle vertices and each contains xyz.
unit (bool) – if true, return normals as unit vectors, default is False
- Returns
face normals, of shape \((\text{batch_size}, \text{num_faces}, 3)\)
- Return type
(torch.FloatTensor)
- kaolin.ops.mesh.index_vertices_by_faces(vertices_features, faces)¶
Index vertex features to convert per vertex tensor to per vertex per face tensor.
- Parameters
vertices_features (torch.FloatTensor) – vertices features, of shape \((\text{batch_size}, \text{num_points}, \text{knum})\), knum is feature dimension The features could be xyz position, rgb color, or even neural network features
faces (torch.LongTensor) – face index, of shape \((\text{num_faces}, \text{num_vertices})\)
- Returns
the face features, of shape \((\text{batch_size}, \text{num_faces}, \text{num_vertices}, \text{knum})\)
- Return type
(torch.FloatTensor)
- kaolin.ops.mesh.packed_face_areas(vertices, first_idx_vertices, faces, num_faces_per_mesh)¶
Compute the areas of each face of triangle meshes.
- Parameters
vertices (torch.Tensor) – The packed vertices of the meshes, of shape \((\text{num_vertices}, 3)\).
first_idx_vertices (torch.Tensor) – The first_idx associated to vertices, of shape \((\text{batch_size})\).
faces (torch.LongTensor) – The packed faces of the meshes, of shape \((\text{num_faces}, 3)\).
num_faces_per_mesh – The number of faces per mesh, of shape \((\text{batch_size})\).
- Returns
The face areas of same type as vertices and of shape \((\text{num_faces})\).
- Return type
- kaolin.ops.mesh.packed_sample_points(vertices, first_idx_vertices, faces, num_faces_per_mesh, num_samples, areas=None)¶
Uniformly sample points over the surface of triangle meshes.
First face on which the point is sampled is randomly selected, with the probability of selection being proportional to the area of the face. then the coordinate on the face is uniformly sampled.
The return pointclouds are with fixed batching.
- Parameters
vertices (torch.Tensor) – The vertices of the meshes, of shape
(num_vertices, 3)
faces (torch.LongTensor) – The faces of the mesh, of shape
(num_faces, 3)
- Returns
the pointclouds of shape
(batch_size, num_points, 3)
, and the indexes of the faces selected (as merged faces), of shape(batch_size, num_points)
- Return type
(torch.Tensor, torch.LongTensor)
- kaolin.ops.mesh.sample_points(vertices, faces, num_samples, areas=None)¶
Uniformly sample points over the surface of triangle meshes.
First face on which the point is sampled is randomly selected, with the probability of selection being proportional to the area of the face. then the coordinate on the face is uniformly sampled.
- Parameters
vertices (torch.Tensor) – The vertices of the meshes, of shape \((\text{batch_size}, \text{num_vertices}, 3)\).
faces (torch.LongTensor) – The faces of the mesh, of shape \((\text{num_faces}, 3)\).
num_samples (int) – The number of point sampled per mesh.
areas (torch.Tensor, optional) – The areas of each face, of shape \((\text{batch_size}, \text{num_faces})\), can be preprocessed, for fast on-the-fly sampling, will be computed if None (default).
- Returns
the pointclouds of shape \((\text{batch_size}, \text{num_points}, 3)\), and the indexes of the faces selected, of shape \((\text{batch_size}, \text{num_points})\).
- Return type
(torch.Tensor, torch.LongTensor)
- kaolin.ops.mesh.uniform_laplacian(num_vertices, faces)¶
Calculates the uniform laplacian of a mesh. \(L[i, j] = \frac{1}{(number of neighbouring vertices of i)}\) if i, j are neighbours. \(L[i, j] = -1\) if i == j. \(L[i, j] = 0\) otherwise.
- Parameters
num_vertices (int) – Number of vertices for the mesh.
faces (torch.LongTensor) – Faces of shape \((\text{num_faces}, \text{face_size})\) of the mesh.
- Returns
Uniform laplacian of the mesh of size \((\text{num_vertices}, \text{num_vertices})\)
- Return type
Example
>>> faces = torch.tensor([[0, 1, 2]]) >>> uniform_laplacian(3, faces) tensor([[-1.0000, 0.5000, 0.5000], [ 0.5000, -1.0000, 0.5000], [ 0.5000, 0.5000, -1.0000]])