From 502f15aca73ffb88286a9341512f78e4a52b1a98 Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Tue, 4 May 2021 05:35:24 -0700 Subject: [PATCH] avoid recalculating normals for simple move Summary: If offset_verts_ is used to move meshes with a single vector, the normals won't change so don't need to recalculate. I am planning to allow user-specified vertex normals. This change means that user-specified vertex normals won't get overwritten when they don't need to be. Reviewed By: nikhilaravi Differential Revision: D27765256 fbshipit-source-id: f6e4d308ac9ac023030325cb75a18d39b966cf88 --- pytorch3d/structures/meshes.py | 7 +++++-- tests/test_meshes.py | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pytorch3d/structures/meshes.py b/pytorch3d/structures/meshes.py index 7998aa06..dffcb3c5 100644 --- a/pytorch3d/structures/meshes.py +++ b/pytorch3d/structures/meshes.py @@ -1263,7 +1263,10 @@ class Meshes(object): """ verts_packed = self.verts_packed() if vert_offsets_packed.shape == (3,): + update_normals = False vert_offsets_packed = vert_offsets_packed.expand_as(verts_packed) + else: + update_normals = True if vert_offsets_packed.shape != verts_packed.shape: raise ValueError("Verts offsets must have dimension (all_v, 3).") # update verts packed @@ -1284,12 +1287,12 @@ class Meshes(object): # update face areas and normals and vertex normals # only if the original attributes are computed - if any( + if update_normals and any( v is not None for v in [self._faces_areas_packed, self._faces_normals_packed] ): self._compute_face_areas_normals(refresh=True) - if self._verts_normals_packed is not None: + if update_normals and self._verts_normals_packed is not None: self._compute_vertex_normals(refresh=True) return self diff --git a/tests/test_meshes.py b/tests/test_meshes.py index 2f8a712e..7dc7a15c 100644 --- a/tests/test_meshes.py +++ b/tests/test_meshes.py @@ -486,6 +486,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase): self.assertClose( new_mesh.verts_normals_list()[i], new_mesh_naive.verts_normals_list()[i], + atol=1e-6, ) self.assertClose( new_mesh.faces_normals_list()[i], @@ -533,10 +534,14 @@ class TestMeshes(TestCaseMixin, unittest.TestCase): # check face areas, normals and vertex normals self.assertClose( - new_mesh.verts_normals_packed(), new_mesh_naive.verts_normals_packed() + new_mesh.verts_normals_packed(), + new_mesh_naive.verts_normals_packed(), + atol=1e-6, ) self.assertClose( - new_mesh.verts_normals_padded(), new_mesh_naive.verts_normals_padded() + new_mesh.verts_normals_padded(), + new_mesh_naive.verts_normals_padded(), + atol=1e-6, ) self.assertClose( new_mesh.faces_normals_packed(), new_mesh_naive.faces_normals_packed()