Adding join_mesh in pytorch3d.structures.meshes

Summary: Adding a function in pytorch3d.structures.meshes to join multiple meshes into a Meshes object representing a single mesh. The function currently ignores all textures.

Reviewed By: nikhilaravi

Differential Revision: D21876908

fbshipit-source-id: 448602857e9d3d3f774d18bb4e93076f78329823
This commit is contained in:
Luya Gao
2020-06-09 08:31:49 -07:00
committed by Facebook GitHub Bot
parent 4b78e95eeb
commit e053d7c456
6 changed files with 132 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
from typing import List
from typing import List, Union
import torch
@@ -1539,3 +1539,28 @@ def join_meshes_as_batch(meshes: List[Meshes], include_textures: bool = True):
tex = Textures(**kwargs)
return Meshes(verts=verts, faces=faces, textures=tex)
def join_mesh(meshes: Union[Meshes, List[Meshes]]) -> Meshes:
"""
Joins a batch of meshes in the form of a Meshes object or a list of Meshes
objects as a single mesh. If the input is a list, the Meshes objects in the list
must all be on the same device. This version ignores all textures in the input mehses.
Args:
meshes: Meshes object that contains a batch of meshes or a list of Meshes objects
Returns:
new Meshes object containing a single mesh
"""
if isinstance(meshes, List):
meshes = join_meshes_as_batch(meshes, include_textures=False)
if len(meshes) == 1:
return meshes
verts = meshes.verts_packed() # (sum(V_n), 3)
# Offset automatically done by faces_packed
faces = meshes.faces_packed() # (sum(F_n), 3)
mesh = Meshes(verts=verts.unsqueeze(0), faces=faces.unsqueeze(0))
return mesh