Allow single offset in offset_verts

Summary:
It is common when trying things out to want to move a whole mesh or point cloud by the same amount. Here we allow the offset functions to broadcast.

Also add a sanity check to join_meshes_as_scene which it is easy to call wrongly.

Reviewed By: nikhilaravi

Differential Revision: D25980593

fbshipit-source-id: cdf1568e1317e3b81ad94ed4e608ba7eef81290b
This commit is contained in:
Jeremy Reizenstein
2021-01-22 07:31:50 -08:00
committed by Facebook GitHub Bot
parent d60c52df4a
commit ddebdfbcd7
5 changed files with 36 additions and 14 deletions

View File

@@ -1255,12 +1255,15 @@ class Meshes(object):
Add an offset to the vertices of this Meshes. In place operation.
Args:
vert_offsets_packed: A Tensor of the same shape as self.verts_packed
giving offsets to be added to all vertices.
vert_offsets_packed: A Tensor of shape (3,) or the same shape as
self.verts_packed, giving offsets to be added
to all vertices.
Returns:
self.
"""
verts_packed = self.verts_packed()
if vert_offsets_packed.shape == (3,):
vert_offsets_packed = vert_offsets_packed.expand_as(verts_packed)
if vert_offsets_packed.shape != verts_packed.shape:
raise ValueError("Verts offsets must have dimension (all_v, 3).")
# update verts packed
@@ -1581,6 +1584,12 @@ def join_meshes_as_scene(
Returns:
new Meshes object containing a single mesh
"""
if not isinstance(include_textures, (bool, int)):
# We want to avoid letting join_meshes_as_scene(mesh1, mesh2) silently
# do the wrong thing.
raise ValueError(
f"include_textures argument cannot be {type(include_textures)}"
)
if isinstance(meshes, List):
meshes = join_meshes_as_batch(meshes, include_textures=include_textures)

View File

@@ -793,12 +793,16 @@ class Pointclouds(object):
Translate the point clouds by an offset. In place operation.
Args:
offsets_packed: A Tensor of the same shape as self.points_packed
giving offsets to be added to all points.
offsets_packed: A Tensor of shape (3,) or the same shape
as self.points_packed giving offsets to be added to
all points.
Returns:
self.
"""
points_packed = self.points_packed()
if offsets_packed.shape == (3,):
offsets_packed = offsets_packed.expand_as(points_packed)
if offsets_packed.shape != points_packed.shape:
raise ValueError("Offsets must have dimension (all_p, 3).")
self._points_packed = points_packed + offsets_packed