Skip to content

Commit

Permalink
has_verts_normals for Meshes
Browse files Browse the repository at this point in the history
Summary: Add ability to ask a Meshes if it already has normals. If it does, then requesting normals will not trigger a calculation. MeshesFormatInterpreters will therefore be able to decide whether to save normals.

Reviewed By: theschnitz, nikhilaravi

Differential Revision: D27765261

fbshipit-source-id: 7c87dbf999d5616d20f5eb2c01039ee5ff65a830
  • Loading branch information
bottler authored and facebook-github-bot committed May 4, 2021
1 parent 2bbca5f commit 66b97a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pytorch3d/structures/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ def verts_padded_to_packed_idx(self):
)
return self._verts_padded_to_packed_idx

def has_verts_normals(self) -> bool:
"""
Check whether vertex normals are already present.
"""
return self._verts_normals_packed is not None

def verts_normals_packed(self):
"""
Get the packed representation of the vertex normals.
Expand Down
6 changes: 5 additions & 1 deletion tests/test_meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def test_compute_normals(self):
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=torch.int64
)
mesh = Meshes(verts=[verts], faces=[faces])

self.assertFalse(mesh.has_verts_normals())
verts_normals_expected = torch.tensor(
[
[0.0, 0.0, 1.0],
Expand All @@ -1025,6 +1025,7 @@ def test_compute_normals(self):
self.assertTrue(
torch.allclose(mesh.verts_normals_list()[0], verts_normals_expected)
)
self.assertTrue(mesh.has_verts_normals())
self.assertTrue(
torch.allclose(mesh.faces_normals_list()[0], faces_normals_expected)
)
Expand Down Expand Up @@ -1141,11 +1142,14 @@ def test_compute_normals(self):
def test_assigned_normals(self):
verts = torch.rand(2, 6, 3)
faces = torch.randint(6, size=(2, 4, 3))
no_normals = Meshes(verts=verts, faces=faces)
self.assertFalse(no_normals.has_verts_normals())

for verts_normals in [list(verts.unbind(0)), verts]:
yes_normals = Meshes(
verts=verts.clone(), faces=faces, verts_normals=verts_normals
)
self.assertTrue(yes_normals.has_verts_normals())
self.assertClose(yes_normals.verts_normals_padded(), verts)
yes_normals.offset_verts_(torch.FloatTensor([1, 2, 3]))
self.assertClose(yes_normals.verts_normals_padded(), verts)
Expand Down

0 comments on commit 66b97a0

Please sign in to comment.