Skip to content

Commit 354a180

Browse files
bottlerfacebook-github-bot
authored andcommitted
Fix save_ply with noncontiguous faces
Summary: As noted in #710, save_ply was failing with some values of the faces tensor. It was assuming the faces were contiguous in using view() to change them. Here we avoid doing that. Reviewed By: patricklabatut Differential Revision: D29159655 fbshipit-source-id: 47214a7ce915bab8d81f109c2b97cde464fd57d8
1 parent 8006842 commit 354a180

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pytorch3d/io/ply_io.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,14 @@ def _save_ply(
11541154
if ascii:
11551155
np.savetxt(f, faces_array, "3 %d %d %d")
11561156
else:
1157-
# rows are 13 bytes: a one-byte 3 followed by three four-byte face indices.
1158-
faces_uints = np.full((len(faces_array), 13), 3, dtype=np.uint8)
1159-
faces_uints[:, 1:] = faces_array.astype(np.uint32).view(np.uint8)
1157+
faces_recs = np.zeros(
1158+
len(faces_array),
1159+
dtype=[("count", np.uint8), ("vertex_indices", np.uint32, 3)],
1160+
)
1161+
faces_recs["count"] = 3
1162+
faces_recs["vertex_indices"] = faces_array
1163+
faces_uints = faces_recs.view(np.uint8)
1164+
11601165
if isinstance(f, BytesIO):
11611166
f.write(faces_uints.tobytes())
11621167
else:

tests/test_io_ply.py

+5
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ def test_normals_save(self):
353353
save_ply(file, verts=verts, faces=faces, verts_normals=normals)
354354
file.close()
355355

356+
def test_contiguity_unimportant(self):
357+
verts = torch.rand(32, 3)
358+
self._test_save_load(verts, torch.randint(30, size=(10, 3)))
359+
self._test_save_load(verts, torch.randint(30, size=(3, 10)).T)
360+
356361
def test_empty_save_load(self):
357362
# Vertices + empty faces
358363
verts = torch.tensor([[0.1, 0.2, 0.3]])

0 commit comments

Comments
 (0)