Skip to content

Commit 66b97a0

Browse files
bottlerfacebook-github-bot
authored andcommitted
has_verts_normals for Meshes
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
1 parent 2bbca5f commit 66b97a0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pytorch3d/structures/meshes.py

+6
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ def verts_padded_to_packed_idx(self):
732732
)
733733
return self._verts_padded_to_packed_idx
734734

735+
def has_verts_normals(self) -> bool:
736+
"""
737+
Check whether vertex normals are already present.
738+
"""
739+
return self._verts_normals_packed is not None
740+
735741
def verts_normals_packed(self):
736742
"""
737743
Get the packed representation of the vertex normals.

tests/test_meshes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ def test_compute_normals(self):
10031003
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=torch.int64
10041004
)
10051005
mesh = Meshes(verts=[verts], faces=[faces])
1006-
1006+
self.assertFalse(mesh.has_verts_normals())
10071007
verts_normals_expected = torch.tensor(
10081008
[
10091009
[0.0, 0.0, 1.0],
@@ -1025,6 +1025,7 @@ def test_compute_normals(self):
10251025
self.assertTrue(
10261026
torch.allclose(mesh.verts_normals_list()[0], verts_normals_expected)
10271027
)
1028+
self.assertTrue(mesh.has_verts_normals())
10281029
self.assertTrue(
10291030
torch.allclose(mesh.faces_normals_list()[0], faces_normals_expected)
10301031
)
@@ -1141,11 +1142,14 @@ def test_compute_normals(self):
11411142
def test_assigned_normals(self):
11421143
verts = torch.rand(2, 6, 3)
11431144
faces = torch.randint(6, size=(2, 4, 3))
1145+
no_normals = Meshes(verts=verts, faces=faces)
1146+
self.assertFalse(no_normals.has_verts_normals())
11441147

11451148
for verts_normals in [list(verts.unbind(0)), verts]:
11461149
yes_normals = Meshes(
11471150
verts=verts.clone(), faces=faces, verts_normals=verts_normals
11481151
)
1152+
self.assertTrue(yes_normals.has_verts_normals())
11491153
self.assertClose(yes_normals.verts_normals_padded(), verts)
11501154
yes_normals.offset_verts_(torch.FloatTensor([1, 2, 3]))
11511155
self.assertClose(yes_normals.verts_normals_padded(), verts)

0 commit comments

Comments
 (0)