Skip to content

Commit

Permalink
avoid recalculating normals for simple move
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bottler authored and facebook-github-bot committed May 4, 2021
1 parent 1763380 commit 502f15a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions pytorch3d/structures/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,10 @@ def offset_verts_(self, vert_offsets_packed):
"""
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
Expand All @@ -1284,12 +1287,12 @@ def offset_verts_(self, vert_offsets_packed):

# 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
Expand Down
9 changes: 7 additions & 2 deletions tests/test_meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def naive_offset_verts(mesh, vert_offsets_packed):
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],
Expand Down Expand Up @@ -533,10 +534,14 @@ def naive_offset_verts(mesh, vert_offsets_packed):

# 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()
Expand Down

0 comments on commit 502f15a

Please sign in to comment.