Skip to content

Commit 502f15a

Browse files
bottlerfacebook-github-bot
authored andcommitted
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
1 parent 1763380 commit 502f15a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pytorch3d/structures/meshes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,10 @@ def offset_verts_(self, vert_offsets_packed):
12631263
"""
12641264
verts_packed = self.verts_packed()
12651265
if vert_offsets_packed.shape == (3,):
1266+
update_normals = False
12661267
vert_offsets_packed = vert_offsets_packed.expand_as(verts_packed)
1268+
else:
1269+
update_normals = True
12671270
if vert_offsets_packed.shape != verts_packed.shape:
12681271
raise ValueError("Verts offsets must have dimension (all_v, 3).")
12691272
# update verts packed
@@ -1284,12 +1287,12 @@ def offset_verts_(self, vert_offsets_packed):
12841287

12851288
# update face areas and normals and vertex normals
12861289
# only if the original attributes are computed
1287-
if any(
1290+
if update_normals and any(
12881291
v is not None
12891292
for v in [self._faces_areas_packed, self._faces_normals_packed]
12901293
):
12911294
self._compute_face_areas_normals(refresh=True)
1292-
if self._verts_normals_packed is not None:
1295+
if update_normals and self._verts_normals_packed is not None:
12931296
self._compute_vertex_normals(refresh=True)
12941297

12951298
return self

tests/test_meshes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def naive_offset_verts(mesh, vert_offsets_packed):
486486
self.assertClose(
487487
new_mesh.verts_normals_list()[i],
488488
new_mesh_naive.verts_normals_list()[i],
489+
atol=1e-6,
489490
)
490491
self.assertClose(
491492
new_mesh.faces_normals_list()[i],
@@ -533,10 +534,14 @@ def naive_offset_verts(mesh, vert_offsets_packed):
533534

534535
# check face areas, normals and vertex normals
535536
self.assertClose(
536-
new_mesh.verts_normals_packed(), new_mesh_naive.verts_normals_packed()
537+
new_mesh.verts_normals_packed(),
538+
new_mesh_naive.verts_normals_packed(),
539+
atol=1e-6,
537540
)
538541
self.assertClose(
539-
new_mesh.verts_normals_padded(), new_mesh_naive.verts_normals_padded()
542+
new_mesh.verts_normals_padded(),
543+
new_mesh_naive.verts_normals_padded(),
544+
atol=1e-6,
540545
)
541546
self.assertClose(
542547
new_mesh.faces_normals_packed(), new_mesh_naive.faces_normals_packed()

0 commit comments

Comments
 (0)