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

@@ -1,5 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import itertools
import random
import unittest
@@ -445,7 +446,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
mesh = TestMeshes.init_mesh(N, 10, 100)
all_v = mesh.verts_packed().size(0)
verts_per_mesh = mesh.num_verts_per_mesh()
for force in [0, 1]:
for force, deform_shape in itertools.product([0, 1], [(all_v, 3), 3]):
if force:
# force mesh to have computed attributes
mesh._compute_packed(refresh=True)
@@ -455,7 +456,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
mesh._compute_face_areas_normals(refresh=True)
mesh._compute_vertex_normals(refresh=True)
deform = torch.rand((all_v, 3), dtype=torch.float32, device=mesh.device)
deform = torch.rand(deform_shape, dtype=torch.float32, device=mesh.device)
# new meshes class to hold the deformed mesh
new_mesh_naive = naive_offset_verts(mesh, deform)
@@ -465,10 +466,14 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
verts_cumsum = torch.cumsum(verts_per_mesh, 0).tolist()
verts_cumsum.insert(0, 0)
for i in range(N):
item_offset = (
deform
if deform.ndim == 1
else deform[verts_cumsum[i] : verts_cumsum[i + 1]]
)
self.assertClose(
new_mesh.verts_list()[i],
mesh.verts_list()[i]
+ deform[verts_cumsum[i] : verts_cumsum[i + 1]],
mesh.verts_list()[i] + item_offset,
)
self.assertClose(
new_mesh.verts_list()[i], new_mesh_naive.verts_list()[i]

View File

@@ -1,6 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import itertools
import random
import unittest
@@ -516,13 +516,13 @@ class TestPointclouds(TestCaseMixin, unittest.TestCase):
clouds = self.init_cloud(N, 100, 10)
all_p = clouds.points_packed().size(0)
points_per_cloud = clouds.num_points_per_cloud()
for force in (False, True):
for force, deform_shape in itertools.product((0, 1), [(all_p, 3), 3]):
if force:
clouds._compute_packed(refresh=True)
clouds._compute_padded()
clouds.padded_to_packed_idx()
deform = torch.rand((all_p, 3), dtype=torch.float32, device=clouds.device)
deform = torch.rand(deform_shape, dtype=torch.float32, device=clouds.device)
new_clouds_naive = naive_offset(clouds, deform)
new_clouds = clouds.offset(deform)
@@ -530,10 +530,14 @@ class TestPointclouds(TestCaseMixin, unittest.TestCase):
points_cumsum = torch.cumsum(points_per_cloud, 0).tolist()
points_cumsum.insert(0, 0)
for i in range(N):
item_offset = (
deform
if deform.ndim == 1
else deform[points_cumsum[i] : points_cumsum[i + 1]]
)
self.assertClose(
new_clouds.points_list()[i],
clouds.points_list()[i]
+ deform[points_cumsum[i] : points_cumsum[i + 1]],
clouds.points_list()[i] + item_offset,
)
self.assertClose(
clouds.normals_list()[i], new_clouds_naive.normals_list()[i]