"references/git@developer.sourcefind.cn:OpenDAS/vision.git" did not exist on "8520f0bea4a09e60a217fe3a8cf24b8f733ec16c"
Commit 66b97a0c authored by Jeremy Reizenstein's avatar Jeremy Reizenstein Committed by Facebook GitHub Bot
Browse files

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
parent 2bbca5f2
...@@ -732,6 +732,12 @@ class Meshes(object): ...@@ -732,6 +732,12 @@ class Meshes(object):
) )
return self._verts_padded_to_packed_idx 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): def verts_normals_packed(self):
""" """
Get the packed representation of the vertex normals. Get the packed representation of the vertex normals.
......
...@@ -1003,7 +1003,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase): ...@@ -1003,7 +1003,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=torch.int64 [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], dtype=torch.int64
) )
mesh = Meshes(verts=[verts], faces=[faces]) mesh = Meshes(verts=[verts], faces=[faces])
self.assertFalse(mesh.has_verts_normals())
verts_normals_expected = torch.tensor( verts_normals_expected = torch.tensor(
[ [
[0.0, 0.0, 1.0], [0.0, 0.0, 1.0],
...@@ -1025,6 +1025,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase): ...@@ -1025,6 +1025,7 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
self.assertTrue( self.assertTrue(
torch.allclose(mesh.verts_normals_list()[0], verts_normals_expected) torch.allclose(mesh.verts_normals_list()[0], verts_normals_expected)
) )
self.assertTrue(mesh.has_verts_normals())
self.assertTrue( self.assertTrue(
torch.allclose(mesh.faces_normals_list()[0], faces_normals_expected) torch.allclose(mesh.faces_normals_list()[0], faces_normals_expected)
) )
...@@ -1141,11 +1142,14 @@ class TestMeshes(TestCaseMixin, unittest.TestCase): ...@@ -1141,11 +1142,14 @@ class TestMeshes(TestCaseMixin, unittest.TestCase):
def test_assigned_normals(self): def test_assigned_normals(self):
verts = torch.rand(2, 6, 3) verts = torch.rand(2, 6, 3)
faces = torch.randint(6, size=(2, 4, 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]: for verts_normals in [list(verts.unbind(0)), verts]:
yes_normals = Meshes( yes_normals = Meshes(
verts=verts.clone(), faces=faces, verts_normals=verts_normals verts=verts.clone(), faces=faces, verts_normals=verts_normals
) )
self.assertTrue(yes_normals.has_verts_normals())
self.assertClose(yes_normals.verts_normals_padded(), verts) self.assertClose(yes_normals.verts_normals_padded(), verts)
yes_normals.offset_verts_(torch.FloatTensor([1, 2, 3])) yes_normals.offset_verts_(torch.FloatTensor([1, 2, 3]))
self.assertClose(yes_normals.verts_normals_padded(), verts) self.assertClose(yes_normals.verts_normals_padded(), verts)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment