PLY with uint face data (#1104)

Summary: Fix assumption that face indices are signed in the PLY file, as reported in #1104.

Reviewed By: nikhilaravi

Differential Revision: D34892598

fbshipit-source-id: a8b23bfac1357bdc11bbbf752098319142239804
This commit is contained in:
Jeremy Reizenstein 2022-03-16 05:42:34 -07:00 committed by Facebook GitHub Bot
parent 1701b76a31
commit 9b5a3ffa6c
2 changed files with 40 additions and 1 deletions

View File

@ -1040,7 +1040,7 @@ def _load_ply(f, *, path_manager: PathManager) -> _PlyData:
if face.shape[1] < 3:
raise ValueError("Faces must have at least 3 vertices.")
face_arrays = [face[:, [0, i + 1, i + 2]] for i in range(face.shape[1] - 2)]
faces = torch.LongTensor(np.vstack(face_arrays))
faces = torch.LongTensor(np.vstack(face_arrays).astype(np.int64))
else:
face_list = []
for face_item in face:

View File

@ -510,6 +510,45 @@ class TestMeshPlyIO(TestCaseMixin, unittest.TestCase):
torch.FloatTensor([3, 4, 5]) + 7 * torch.arange(8)[:, None],
)
def test_load_open3d_mesh(self):
# Header based on issue #1104
header = "\n".join(
[
"ply",
"format binary_little_endian 1.0",
"comment Created by Open3D",
"element vertex 3",
"property double x",
"property double y",
"property double z",
"property double nx",
"property double ny",
"property double nz",
"property uchar red",
"property uchar green",
"property uchar blue",
"element face 1",
"property list uchar uint vertex_indices",
"end_header",
"",
]
).encode("ascii")
vert_data = struct.pack("<" + "ddddddBBB" * 3, *range(9 * 3))
face_data = struct.pack("<" + "BIII", 3, 0, 1, 2)
io = IO()
with NamedTemporaryFile(mode="wb", suffix=".ply") as f:
f.write(header)
f.write(vert_data)
f.write(face_data)
f.flush()
mesh = io.load_mesh(f.name)
self.assertClose(mesh.faces_padded(), torch.arange(3)[None, None])
self.assertClose(
mesh.verts_padded(),
(torch.arange(3) + 9.0 * torch.arange(3)[:, None])[None],
)
def test_save_pointcloud(self):
header = "\n".join(
[