kaolin.rep.Mesh

class Mesh(vertices: torch.Tensor, faces: torch.Tensor, uvs: torch.Tensor, face_textures: torch.Tensor, textures: torch.Tensor, edges: torch.Tensor, edge2key: dict, vv: torch.Tensor, vv_count: torch.Tensor, vf: torch.Tensor, vf_count: torch.Tensor, ve: torch.Tensor, ve_count: torch.Tensor, ff: torch.Tensor, ff_count: torch.Tensor, ef: torch.Tensor, ef_count: torch.Tensor, ee: torch.Tensor, ee_count: torch.Tensor)[source]

Abstract class to represent 3D polygon meshes.

static compute_adjacency_info(vertices: torch.Tensor, faces: torch.Tensor)[source]

Build data structures to help speed up connectivity queries. Assumes a homogeneous mesh, i.e., each face has the same number of vertices.

The outputs have the following format: AA, AA_count AA_count: [count_0, …, count_n] with AA: [[aa_{0,0}, …, aa_{0,count_0} (, -1, …, -1)],

[aa_{1,0}, …, aa_{1,count_1} (, -1, …, -1)],

[aa_{n,0}, …, aa_{n,count_n} (, -1, …, -1)]]

compute_laplacian()[source]
Calcualtes the laplcaian of the graph, meaning the average

difference between a vertex and its neighbors.

Returns

laplacian of the mesh.

Return type

(FloatTensor)

Example

>>> mesh = Mesh.from_obj('model.obj')
>>> lap = mesh.compute_laplacian()
cpu()[source]

“Maps all tensors of the current class to CPU.

cuda()[source]

“Maps all tensors of the current class to CUDA.

abstract classmethod from_obj(filename: str, with_vt: bool = False, enable_adjacency: bool = False, texture_res=4)[source]

Loads object in .obj wavefront format.

Parameters
  • filename (str) – location of file.

  • with_vt (bool) – objects loaded with textures specified by vertex textures.

  • enable_adjacency (bool) – adjacency information is computed.

  • texture_res (int) – resolution of loaded face colors.

Note: the with_vt parameter requires cuda.

Example

>>> mesh = Mesh.from_obj('model.obj')
>>> mesh.vertices.shape
torch.Size([482, 3])
>>> mesh.faces.shape
torch.Size([960, 3])
classmethod from_tensors(vertices: torch.Tensor, faces: torch.Tensor, uvs: torch.Tensor = None, face_textures: torch.Tensor = None, textures: torch.Tensor = None, enable_adjacency=False)[source]

Returns mesh with supplied tensor information.

Parameters
static get_common_vertex(e1: torch.Tensor, e2: torch.Tensor)[source]

Returns the common vertex in edges e1 and e2 (if any).

Parameters
Returns

Index of common vertex

(shape: \(1\)).

first_nbr (torch.LongTensor): Index of one neighbouring

vertex of the common vertex (shape: \(1\)).

second_nbr (torch.LongTensor): Index of the other neighbouring

vertex of the common vertex (shape: \(1\)).

Return type

common_vertex (torch.LongTensor)

static get_edge_order(a: int, b: int)[source]

Returns (a, b) or (b, a), depending on which is smaller. (Smaller element first, for unique keys)

Parameters
  • a (int) – Index of first vertex in edge.

  • b (int) – Index of second vertex in edge.

static get_edges_from_face(f: torch.Tensor)[source]

Returns a list of edges forming the current face.

Parameters
  • f – Face (quadruplet of indices into ‘vertices’).

  • vertices (torch.Tensor) – Vertices (3D points).

Returns

List of tuples (a, b) for each edge (a, b) in

faces.

Return type

edge_inds (list)

static has_common_vertex(e1: torch.Tensor, e2: torch.Tensor)[source]

Returns True if the vertices e1, e2 share a common vertex, False otherwise.

Parameters
Returns

Whether or not e1 and e2 share a common vertex.

Return type

(bool)

laplacian_smoothing(iterations: int = 1)[source]

Applies laplacian smoothing to the mesh.

Parameters

iterations (int) – number of iterations to run the algorithm for.

Example

>>> mesh = Mesh.from_obj('model.obj')
>>> mesh.compute_laplacian().abs().mean()
tensor(0.0010)
>>> mesh.laplacian_smoothing(iterations=3)
>>> mesh.compute_laplacian().abs().mean()
tensor(9.9956e-05)
static list_of_lists_to_matrix(list_of_lists: list, sublist_lengths: torch.Tensor, matrix: torch.Tensor)[source]

Takes a list of lists (each sub-list of variable size), and maps it to a matrix. Decorated by numba, for efficiency sake.

Parameters
  • list_of_lists (list) – A list containing ‘sub-‘lists (Note: the sub-list cannont contain lists; needs to contain numbers).

  • sublist_lengths (torch.Tensor) – Array containing lengths of each sublist.

  • matrix (torch.Tensor) – Matrix in which to mould the list (Note: the matrix must contain as many columns as required to encapsulate the largest sub-list of list_of_lists).

static load_mtl(filename_mtl: str)[source]

Returns all colours and texture files found in an mtl files.

Parameters

filename_mtl (str) – mtl file name

classmethod load_textures(filename_obj: str, filename_mtl: str, texture_res: int)[source]

Returns texture for a given obj file, where texture is defined using vertex texture uvs.

Parameters
  • filename_obj (str) – obj file name

  • filename_mtl (str) – mtl file name

  • texture_res (int) – texture resolution for each face

Returns

texture values for each face

Return type

textures (torch.Tensor)

static normalize_zerosafe(matrix: torch.Tensor)[source]

Normalizes each row of a matrix in a ‘division by zero’-safe way.

Parameters

matrix (torch.tensor) – Matrix where each row contains a vector to be normalized.

save_tensors(filename: str)[source]

Saves the tensor information of the mesh in a numpy .npz format.

Parameters

filename – the file name to save the file under

Example

>>> mesh = Mesh.from_obj('model.obj')
>>> mesh.save_tensors()
show()[source]

Visuailizes the mesh.

Example

>>> mesh = Mesh.from_obj('model.obj')
>>> mesh.show()