test_io_ply.py 35.2 KB
Newer Older
1
# Copyright (c) Meta Platforms, Inc. and affiliates.
Patrick Labatut's avatar
Patrick Labatut committed
2
3
4
5
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
facebook-github-bot's avatar
facebook-github-bot committed
6

7
import itertools
facebook-github-bot's avatar
facebook-github-bot committed
8
9
10
import struct
import unittest
from io import BytesIO, StringIO
11
from tempfile import NamedTemporaryFile, TemporaryFile
facebook-github-bot's avatar
facebook-github-bot committed
12

13
import numpy as np
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
14
import pytorch3d.io.ply_io
15
import torch
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
16
from iopath.common.file_io import PathManager
17
from pytorch3d.io import IO
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
18
from pytorch3d.io.ply_io import load_ply, save_ply
19
20
from pytorch3d.renderer.mesh import TexturesVertex
from pytorch3d.structures import Meshes, Pointclouds
21
from pytorch3d.utils import torus
facebook-github-bot's avatar
facebook-github-bot committed
22

23
from .common_testing import get_tests_dir, TestCaseMixin
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
24

facebook-github-bot's avatar
facebook-github-bot committed
25

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
26
global_path_manager = PathManager()
27
DATA_DIR = get_tests_dir() / "data"
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
28
29
30
31
32
33


def _load_ply_raw(stream):
    return pytorch3d.io.ply_io._load_ply_raw(stream, global_path_manager)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
CUBE_PLY_LINES = [
    "ply",
    "format ascii 1.0",
    "comment made by Greg Turk",
    "comment this file is a cube",
    "element vertex 8",
    "property float x",
    "property float y",
    "property float z",
    "element face 6",
    "property list uchar int vertex_index",
    "end_header",
    "0 0 0",
    "0 0 1",
    "0 1 1",
    "0 1 0",
    "1 0 0",
    "1 0 1",
    "1 1 1",
    "1 1 0",
    "4 0 1 2 3",
    "4 7 6 5 4",
    "4 0 4 5 1",
    "4 1 5 6 2",
    "4 2 6 7 3",
    "4 3 7 4 0",
]

CUBE_VERTS = [
    [0, 0, 0],
    [0, 0, 1],
    [0, 1, 1],
    [0, 1, 0],
    [1, 0, 0],
    [1, 0, 1],
    [1, 1, 1],
    [1, 1, 0],
]
CUBE_FACES = [
    [0, 1, 2],
    [7, 6, 5],
    [0, 4, 5],
    [1, 5, 6],
    [2, 6, 7],
    [3, 7, 4],
    [0, 2, 3],
    [7, 5, 4],
    [0, 5, 1],
    [1, 6, 2],
    [2, 7, 3],
    [3, 4, 0],
]


facebook-github-bot's avatar
facebook-github-bot committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class TestMeshPlyIO(TestCaseMixin, unittest.TestCase):
    def test_raw_load_simple_ascii(self):
        ply_file = "\n".join(
            [
                "ply",
                "format ascii 1.0",
                "comment made by Greg Turk",
                "comment this file is a cube",
                "element vertex 8",
                "property float x",
                "property float y",
                "property float z",
                "element face 6",
                "property list uchar int vertex_index",
                "element irregular_list 3",
                "property list uchar int vertex_index",
                "end_header",
                "0 0 0",
                "0 0 1",
                "0 1 1",
                "0 1 0",
                "1 0 0",
                "1 0 1",
                "1 1 1",
                "1 1 0",
                "4 0 1 2 3",
                "4 7 6 5 4",
                "4 0 4 5 1",
                "4 1 5 6 2",
                "4 2 6 7 3",
                "4 3 7 4 0",  # end of faces
                "4 0 1 2 3",
                "4 7 6 5 4",
                "3 4 5 1",
            ]
        )
        for line_ending in [None, "\n", "\r\n"]:
            if line_ending is None:
                stream = StringIO(ply_file)
            else:
                byte_file = ply_file.encode("ascii")
                if line_ending == "\r\n":
                    byte_file = byte_file.replace(b"\n", b"\r\n")
                stream = BytesIO(byte_file)
            header, data = _load_ply_raw(stream)
            self.assertTrue(header.ascii)
            self.assertEqual(len(data), 3)
            self.assertTupleEqual(data["face"].shape, (6, 4))
            self.assertClose([0, 1, 2, 3], data["face"][0])
            self.assertClose([3, 7, 4, 0], data["face"][5])
138
139
            [vertex0] = data["vertex"]
            self.assertTupleEqual(vertex0.shape, (8, 3))
facebook-github-bot's avatar
facebook-github-bot committed
140
141
142
143
144
145
146
147
148
149
150
            irregular = data["irregular_list"]
            self.assertEqual(len(irregular), 3)
            self.assertEqual(type(irregular), list)
            [x] = irregular[0]
            self.assertClose(x, [0, 1, 2, 3])
            [x] = irregular[1]
            self.assertClose(x, [7, 6, 5, 4])
            [x] = irregular[2]
            self.assertClose(x, [4, 5, 1])

    def test_load_simple_ascii(self):
151
        ply_file = "\n".join(CUBE_PLY_LINES)
facebook-github-bot's avatar
facebook-github-bot committed
152
153
154
155
156
157
158
159
160
161
162
        for line_ending in [None, "\n", "\r\n"]:
            if line_ending is None:
                stream = StringIO(ply_file)
            else:
                byte_file = ply_file.encode("ascii")
                if line_ending == "\r\n":
                    byte_file = byte_file.replace(b"\n", b"\r\n")
                stream = BytesIO(byte_file)
            verts, faces = load_ply(stream)
            self.assertEqual(verts.shape, (8, 3))
            self.assertEqual(faces.shape, (12, 3))
163
164
165
166
167
168
            self.assertClose(verts, torch.FloatTensor(CUBE_VERTS))
            self.assertClose(faces, torch.LongTensor(CUBE_FACES))

    def test_pluggable_load_cube(self):
        """
        This won't work on Windows due to NamedTemporaryFile being reopened.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
169
        Use the testpath package instead?
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        """
        ply_file = "\n".join(CUBE_PLY_LINES)
        io = IO()
        with NamedTemporaryFile(mode="w", suffix=".ply") as f:
            f.write(ply_file)
            f.flush()
            mesh = io.load_mesh(f.name)
        self.assertClose(mesh.verts_padded(), torch.FloatTensor(CUBE_VERTS)[None])
        self.assertClose(mesh.faces_padded(), torch.LongTensor(CUBE_FACES)[None])

        device = torch.device("cuda:0")

        with NamedTemporaryFile(mode="w", suffix=".ply") as f2:
            io.save_mesh(mesh, f2.name)
            f2.flush()
            mesh2 = io.load_mesh(f2.name, device=device)
        self.assertEqual(mesh2.verts_padded().device, device)
        self.assertClose(mesh2.verts_padded().cpu(), mesh.verts_padded())
        self.assertClose(mesh2.faces_padded().cpu(), mesh.faces_padded())

        with NamedTemporaryFile(mode="w") as f3:
            with self.assertRaisesRegex(
                ValueError, "No mesh interpreter found to write to"
            ):
                io.save_mesh(mesh, f3.name)
            with self.assertRaisesRegex(
                ValueError, "No mesh interpreter found to read "
            ):
                io.load_mesh(f3.name)
facebook-github-bot's avatar
facebook-github-bot committed
199

200
201
202
203
204
205
206
207
208
    def test_heterogenous_verts_per_face(self):
        # The cube but where one face is pentagon not square.
        text = CUBE_PLY_LINES.copy()
        text[-1] = "5 3 7 4 0 1"
        stream = StringIO("\n".join(text))
        verts, faces = load_ply(stream)
        self.assertEqual(verts.shape, (8, 3))
        self.assertEqual(faces.shape, (13, 3))

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    def test_save_too_many_colors(self):
        verts = torch.tensor(
            [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32
        )
        faces = torch.tensor([[0, 1, 2], [0, 2, 3]])
        vert_colors = torch.rand((4, 7))
        texture_with_seven_colors = TexturesVertex(verts_features=[vert_colors])

        mesh = Meshes(
            verts=[verts],
            faces=[faces],
            textures=texture_with_seven_colors,
        )

        io = IO()
        msg = "Texture will not be saved as it has 7 colors, not 3."
        with NamedTemporaryFile(mode="w", suffix=".ply") as f:
            with self.assertWarnsRegex(UserWarning, msg):
                io.save_mesh(mesh.cuda(), f.name)

    def test_save_load_meshes(self):
        verts = torch.tensor(
            [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32
        )
        faces = torch.tensor([[0, 1, 2], [0, 2, 3]])
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
234
235
236
        normals = torch.tensor(
            [[0, 1, 0], [1, 0, 0], [1, 4, 1], [1, 0, 0]], dtype=torch.float32
        )
237
238
239
        vert_colors = torch.rand_like(verts)
        texture = TexturesVertex(verts_features=[vert_colors])

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
240
        for do_textures, do_normals in itertools.product([True, False], [True, False]):
241
242
243
244
            mesh = Meshes(
                verts=[verts],
                faces=[faces],
                textures=texture if do_textures else None,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
245
                verts_normals=[normals] if do_normals else None,
246
247
248
249
250
251
252
253
254
255
256
257
            )
            device = torch.device("cuda:0")

            io = IO()
            with NamedTemporaryFile(mode="w", suffix=".ply") as f:
                io.save_mesh(mesh.cuda(), f.name)
                f.flush()
                mesh2 = io.load_mesh(f.name, device=device)
            self.assertEqual(mesh2.device, device)
            mesh2 = mesh2.cpu()
            self.assertClose(mesh2.verts_padded(), mesh.verts_padded())
            self.assertClose(mesh2.faces_padded(), mesh.faces_padded())
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
258
259
260
261
262
263
264
265
266
267
            if do_normals:
                self.assertTrue(mesh.has_verts_normals())
                self.assertTrue(mesh2.has_verts_normals())
                self.assertClose(
                    mesh2.verts_normals_padded(), mesh.verts_normals_padded()
                )
            else:
                self.assertFalse(mesh.has_verts_normals())
                self.assertFalse(mesh2.has_verts_normals())
                self.assertFalse(torch.allclose(mesh2.verts_normals_padded(), normals))
268
269
270
271
272
273
            if do_textures:
                self.assertIsInstance(mesh2.textures, TexturesVertex)
                self.assertClose(mesh2.textures.verts_features_list()[0], vert_colors)
            else:
                self.assertIsNone(mesh2.textures)

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    def test_save_load_with_normals(self):
        points = torch.tensor(
            [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32
        )
        normals = torch.tensor(
            [[0, 1, 0], [1, 0, 0], [1, 4, 1], [1, 0, 0]], dtype=torch.float32
        )
        features = torch.rand_like(points)

        for do_features, do_normals in itertools.product([True, False], [True, False]):
            cloud = Pointclouds(
                points=[points],
                features=[features] if do_features else None,
                normals=[normals] if do_normals else None,
            )
            device = torch.device("cuda:0")

            io = IO()
            with NamedTemporaryFile(mode="w", suffix=".ply") as f:
                io.save_pointcloud(cloud.cuda(), f.name)
                f.flush()
                cloud2 = io.load_pointcloud(f.name, device=device)
            self.assertEqual(cloud2.device, device)
            cloud2 = cloud2.cpu()
            self.assertClose(cloud2.points_padded(), cloud.points_padded())
            if do_normals:
                self.assertClose(cloud2.normals_padded(), cloud.normals_padded())
            else:
                self.assertIsNone(cloud.normals_padded())
                self.assertIsNone(cloud2.normals_padded())
            if do_features:
                self.assertClose(cloud2.features_packed(), features)
            else:
                self.assertIsNone(cloud2.features_packed())

309
310
    def test_save_ply_invalid_shapes(self):
        # Invalid vertices shape
Jeremy Reizenstein's avatar
lint  
Jeremy Reizenstein committed
311
312
        verts = torch.FloatTensor([[0.1, 0.2, 0.3, 0.4]])  # (V, 4)
        faces = torch.LongTensor([[0, 1, 2]])
313
        with self.assertRaises(ValueError) as error:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
314
            save_ply(BytesIO(), verts, faces)
315
316
317
318
319
320
        expected_message = (
            "Argument 'verts' should either be empty or of shape (num_verts, 3)."
        )
        self.assertTrue(expected_message, error.exception)

        # Invalid faces shape
Jeremy Reizenstein's avatar
lint  
Jeremy Reizenstein committed
321
322
        verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
        faces = torch.LongTensor([[0, 1, 2, 3]])  # (F, 4)
323
        with self.assertRaises(ValueError) as error:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
324
            save_ply(BytesIO(), verts, faces)
325
326
327
328
329
330
331
332
333
334
        expected_message = (
            "Argument 'faces' should either be empty or of shape (num_faces, 3)."
        )
        self.assertTrue(expected_message, error.exception)

    def test_save_ply_invalid_indices(self):
        message_regex = "Faces have invalid indices"
        verts = torch.FloatTensor([[0.1, 0.2, 0.3]])
        faces = torch.LongTensor([[0, 1, 2]])
        with self.assertWarnsRegex(UserWarning, message_regex):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
335
            save_ply(BytesIO(), verts, faces)
336
337
338

        faces = torch.LongTensor([[-1, 0, 1]])
        with self.assertWarnsRegex(UserWarning, message_regex):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
339
            save_ply(BytesIO(), verts, faces)
340
341

    def _test_save_load(self, verts, faces):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
342
        f = BytesIO()
343
344
345
346
347
348
349
350
        save_ply(f, verts, faces)
        f.seek(0)
        # raise Exception(f.getvalue())
        expected_verts, expected_faces = verts, faces
        if not len(expected_verts):  # Always compare with a (V, 3) tensor
            expected_verts = torch.zeros(size=(0, 3), dtype=torch.float32)
        if not len(expected_faces):  # Always compare with an (F, 3) tensor
            expected_faces = torch.zeros(size=(0, 3), dtype=torch.int64)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
351

352
353
        actual_verts, actual_faces = load_ply(f)
        self.assertClose(expected_verts, actual_verts)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
354
355
356
357
        if len(actual_verts):
            self.assertClose(expected_faces, actual_faces)
        else:
            self.assertEqual(actual_faces.numel(), 0)
358

David Novotny's avatar
David Novotny committed
359
360
361
362
363
364
365
366
    def test_normals_save(self):
        verts = torch.tensor(
            [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32
        )
        faces = torch.tensor([[0, 1, 2], [0, 2, 3]])
        normals = torch.tensor(
            [[0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0]], dtype=torch.float32
        )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
367
        file = BytesIO()
David Novotny's avatar
David Novotny committed
368
369
370
        save_ply(file, verts=verts, faces=faces, verts_normals=normals)
        file.close()

371
372
373
374
375
    def test_contiguity_unimportant(self):
        verts = torch.rand(32, 3)
        self._test_save_load(verts, torch.randint(30, size=(10, 3)))
        self._test_save_load(verts, torch.randint(30, size=(3, 10)).T)

376
377
378
379
380
381
382
383
384
385
    def test_empty_save_load(self):
        # Vertices + empty faces
        verts = torch.tensor([[0.1, 0.2, 0.3]])
        faces = torch.LongTensor([])
        self._test_save_load(verts, faces)

        faces = torch.zeros(size=(0, 3), dtype=torch.int64)
        self._test_save_load(verts, faces)

        # Faces + empty vertices
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
386
        # => We don't save the faces
387
388
        verts = torch.FloatTensor([])
        faces = torch.LongTensor([[0, 1, 2]])
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
389
        message_regex = "Empty 'verts' provided"
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts, faces)

        verts = torch.zeros(size=(0, 3), dtype=torch.float32)
        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts, faces)

        # Empty vertices + empty faces
        verts0 = torch.FloatTensor([])
        faces0 = torch.LongTensor([])
        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts0, faces0)

        faces3 = torch.zeros(size=(0, 3), dtype=torch.int64)
        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts0, faces3)

        verts3 = torch.zeros(size=(0, 3), dtype=torch.float32)
        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts3, faces0)

        with self.assertWarnsRegex(UserWarning, message_regex):
            self._test_save_load(verts3, faces3)

facebook-github-bot's avatar
facebook-github-bot committed
414
415
    def test_simple_save(self):
        verts = torch.tensor(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
416
            [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 2, 0]], dtype=torch.float32
facebook-github-bot's avatar
facebook-github-bot committed
417
418
        )
        faces = torch.tensor([[0, 1, 2], [0, 3, 4]])
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
        for filetype in BytesIO, TemporaryFile:
            lengths = {}
            for ascii in [True, False]:
                file = filetype()
                save_ply(file, verts=verts, faces=faces, ascii=ascii)
                lengths[ascii] = file.tell()

                file.seek(0)
                verts2, faces2 = load_ply(file)
                self.assertClose(verts, verts2)
                self.assertClose(faces, faces2)

                file.seek(0)
                if ascii:
                    file.read().decode("ascii")
                else:
                    with self.assertRaises(UnicodeDecodeError):
                        file.read().decode("ascii")

                if filetype is TemporaryFile:
                    file.close()
            self.assertLess(lengths[False], lengths[True], "ascii should be longer")
facebook-github-bot's avatar
facebook-github-bot committed
441

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
442
    def test_heterogeneous_property(self):
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
        ply_file_ascii = "\n".join(
            [
                "ply",
                "format ascii 1.0",
                "element vertex 8",
                "property float x",
                "property int y",
                "property int z",
                "end_header",
                "0 0 0",
                "0 0 1",
                "0 1 1",
                "0 1 0",
                "1 0 0",
                "1 0 1",
                "1 1 1",
                "1 1 0",
            ]
        )
        ply_file_binary = "\n".join(
            [
                "ply",
                "format binary_little_endian 1.0",
                "element vertex 8",
                "property uchar x",
                "property char y",
                "property char z",
                "end_header",
                "",
            ]
        )
        data = [0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0]
        stream_ascii = StringIO(ply_file_ascii)
        stream_binary = BytesIO(ply_file_binary.encode("ascii") + bytes(data))
        X = np.array([[0, 0, 0, 0, 1, 1, 1, 1]]).T
        YZ = np.array([0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0])
        for stream in (stream_ascii, stream_binary):
            header, elements = _load_ply_raw(stream)
            [x, yz] = elements["vertex"]
            self.assertClose(x, X)
            self.assertClose(yz, YZ.reshape(8, 2))

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
    def test_load_cloudcompare_pointcloud(self):
        """
        Test loading a pointcloud styled like some cloudcompare output.
        cloudcompare is an open source 3D point cloud processing software.
        """
        header = "\n".join(
            [
                "ply",
                "format binary_little_endian 1.0",
                "obj_info Not a key-value pair!",
                "element vertex 8",
                "property double x",
                "property double y",
                "property double z",
                "property uchar red",
                "property uchar green",
                "property uchar blue",
                "property float my_Favorite",
                "end_header",
                "",
            ]
        ).encode("ascii")
        data = struct.pack("<" + "dddBBBf" * 8, *range(56))
        io = IO()
        with NamedTemporaryFile(mode="wb", suffix=".ply") as f:
            f.write(header)
            f.write(data)
            f.flush()
            pointcloud = io.load_pointcloud(f.name)

        self.assertClose(
            pointcloud.points_padded()[0],
            torch.FloatTensor([0, 1, 2]) + 7 * torch.arange(8)[:, None],
        )
        self.assertClose(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
520
            pointcloud.features_padded()[0] * 255,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
521
522
523
            torch.FloatTensor([3, 4, 5]) + 7 * torch.arange(8)[:, None],
        )

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    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],
        )

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
    def test_save_pointcloud(self):
        header = "\n".join(
            [
                "ply",
                "format binary_little_endian 1.0",
                "element vertex 8",
                "property float x",
                "property float y",
                "property float z",
                "property float red",
                "property float green",
                "property float blue",
                "end_header",
                "",
            ]
        ).encode("ascii")
        data = struct.pack("<" + "f" * 48, *range(48))
        points = torch.FloatTensor([0, 1, 2]) + 6 * torch.arange(8)[:, None]
581
582
583
584
585
        features_large = torch.FloatTensor([3, 4, 5]) + 6 * torch.arange(8)[:, None]
        features = features_large / 255.0
        pointcloud_largefeatures = Pointclouds(
            points=[points], features=[features_large]
        )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
586
587
588
589
        pointcloud = Pointclouds(points=[points], features=[features])

        io = IO()
        with NamedTemporaryFile(mode="rb", suffix=".ply") as f:
590
            io.save_pointcloud(data=pointcloud_largefeatures, path=f.name)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
591
592
593
594
595
596
597
            f.flush()
            f.seek(0)
            actual_data = f.read()
            reloaded_pointcloud = io.load_pointcloud(f.name)

        self.assertEqual(header + data, actual_data)
        self.assertClose(reloaded_pointcloud.points_list()[0], points)
598
599
600
601
602
603
604
605
606
607
608
        self.assertClose(reloaded_pointcloud.features_list()[0], features_large)
        # Test the load-save cycle leaves file completely unchanged
        with NamedTemporaryFile(mode="rb", suffix=".ply") as f:
            io.save_pointcloud(
                data=reloaded_pointcloud,
                path=f.name,
            )
            f.flush()
            f.seek(0)
            data2 = f.read()
            self.assertEqual(data2, actual_data)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
609
610

        with NamedTemporaryFile(mode="r", suffix=".ply") as f:
611
612
613
            io.save_pointcloud(
                data=pointcloud, path=f.name, binary=False, decimal_places=9
            )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
614
615
616
617
618
619
            reloaded_pointcloud2 = io.load_pointcloud(f.name)
            self.assertEqual(f.readline(), "ply\n")
            self.assertEqual(f.readline(), "format ascii 1.0\n")
        self.assertClose(reloaded_pointcloud2.points_list()[0], points)
        self.assertClose(reloaded_pointcloud2.features_list()[0], features)

620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
        for binary in [True, False]:
            with NamedTemporaryFile(mode="rb", suffix=".ply") as f:
                io.save_pointcloud(
                    data=pointcloud, path=f.name, colors_as_uint8=True, binary=binary
                )
                f.flush()
                f.seek(0)
                actual_data = f.read()
                reloaded_pointcloud3 = io.load_pointcloud(f.name)
            self.assertClose(reloaded_pointcloud3.features_list()[0], features)
            self.assertIn(b"property uchar green", actual_data)

            # Test the load-save cycle leaves file completely unchanged
            with NamedTemporaryFile(mode="rb", suffix=".ply") as f:
                io.save_pointcloud(
                    data=reloaded_pointcloud3,
                    path=f.name,
                    binary=binary,
                    colors_as_uint8=True,
                )
                f.flush()
                f.seek(0)
                data2 = f.read()
                self.assertEqual(data2, actual_data)

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
    def test_load_pointcloud_bad_order(self):
        """
        Ply file with a strange property order
        """
        file = "\n".join(
            [
                "ply",
                "format ascii 1.0",
                "element vertex 1",
                "property uchar green",
                "property float x",
                "property float z",
                "property uchar red",
                "property float y",
                "property uchar blue",
                "end_header",
                "1 2 3 4 5 6",
            ]
        )

        io = IO()
        pointcloud_gpu = io.load_pointcloud(StringIO(file), device="cuda:0")
        self.assertEqual(pointcloud_gpu.device, torch.device("cuda:0"))
        pointcloud = pointcloud_gpu.to(torch.device("cpu"))
        expected_points = torch.tensor([[[2, 5, 3]]], dtype=torch.float32)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
670
        expected_features = torch.tensor([[[4, 1, 6]]], dtype=torch.float32) / 255.0
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
671
672
673
        self.assertClose(pointcloud.points_padded(), expected_points)
        self.assertClose(pointcloud.features_padded(), expected_features)

facebook-github-bot's avatar
facebook-github-bot committed
674
675
676
    def test_load_simple_binary(self):
        for big_endian in [True, False]:
            verts = (
677
                "0 0 0 " "0 0 1 " "0 1 1 " "0 1 0 " "1 0 0 " "1 0 1 " "1 1 1 " "1 1 0"
facebook-github-bot's avatar
facebook-github-bot committed
678
679
680
681
682
683
684
685
686
687
688
689
690
            ).split()
            faces = (
                "4 0 1 2 3 "
                "4 7 6 5 4 "
                "4 0 4 5 1 "
                "4 1 5 6 2 "
                "4 2 6 7 3 "
                "4 3 7 4 0 "  # end of first 6
                "4 0 1 2 3 "
                "4 7 6 5 4 "
                "3 4 5 1"
            ).split()
            short_one = b"\00\01" if big_endian else b"\01\00"
691
            mixed_data = b"\00\00" b"\03\03" + (short_one + b"\00\01\01\01" b"\00\02")
facebook-github-bot's avatar
facebook-github-bot committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
            minus_one_data = b"\xff" * 14
            endian_char = ">" if big_endian else "<"
            format = (
                "format binary_big_endian 1.0"
                if big_endian
                else "format binary_little_endian 1.0"
            )
            vertex_pattern = endian_char + "24f"
            vertex_data = struct.pack(vertex_pattern, *map(float, verts))
            vertex1_pattern = endian_char + "fdffdffdffdffdffdffdffdf"
            vertex1_data = struct.pack(vertex1_pattern, *map(float, verts))
            face_char_pattern = endian_char + "44b"
            face_char_data = struct.pack(face_char_pattern, *map(int, faces))
            header = "\n".join(
                [
                    "ply",
                    format,
                    "element vertex 8",
                    "property float x",
711
                    "property float32 y",
facebook-github-bot's avatar
facebook-github-bot committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
                    "property float z",
                    "element vertex1 8",
                    "property float x",
                    "property double y",
                    "property float z",
                    "element face 6",
                    "property list uchar uchar vertex_index",
                    "element irregular_list 3",
                    "property list uchar uchar vertex_index",
                    "element mixed 2",
                    "property list short uint foo",
                    "property short bar",
                    "element minus_ones 1",
                    "property char 1",
                    "property uchar 2",
                    "property short 3",
                    "property ushort 4",
                    "property int 5",
                    "property uint 6",
                    "end_header\n",
                ]
            )
            ply_file = b"".join(
                [
                    header.encode("ascii"),
                    vertex_data,
                    vertex1_data,
                    face_char_data,
                    mixed_data,
                    minus_one_data,
                ]
            )
            metadata, data = _load_ply_raw(BytesIO(ply_file))
            self.assertFalse(metadata.ascii)
            self.assertEqual(len(data), 6)
            self.assertTupleEqual(data["face"].shape, (6, 4))
            self.assertClose([0, 1, 2, 3], data["face"][0])
            self.assertClose([3, 7, 4, 0], data["face"][5])

751
752
753
754
755
            [vertex0] = data["vertex"]
            self.assertTupleEqual(vertex0.shape, (8, 3))
            self.assertEqual(len(data["vertex1"]), 3)
            self.assertClose(vertex0, np.column_stack(data["vertex1"]))
            self.assertClose(vertex0.flatten(), list(map(float, verts)))
facebook-github-bot's avatar
facebook-github-bot committed
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778

            irregular = data["irregular_list"]
            self.assertEqual(len(irregular), 3)
            self.assertEqual(type(irregular), list)
            [x] = irregular[0]
            self.assertClose(x, [0, 1, 2, 3])
            [x] = irregular[1]
            self.assertClose(x, [7, 6, 5, 4])
            [x] = irregular[2]
            self.assertClose(x, [4, 5, 1])

            mixed = data["mixed"]
            self.assertEqual(len(mixed), 2)
            self.assertEqual(len(mixed[0]), 2)
            self.assertEqual(len(mixed[1]), 2)
            self.assertEqual(mixed[0][1], 3 * 256 + 3)
            self.assertEqual(len(mixed[0][0]), 0)
            self.assertEqual(mixed[1][1], (2 if big_endian else 2 * 256))
            base = 1 + 256 + 256 * 256
            self.assertEqual(len(mixed[1][0]), 1)
            self.assertEqual(mixed[1][0][0], base if big_endian else 256 * base)

            self.assertListEqual(
779
                data["minus_ones"], [-1, 255, -1, 65535, -1, 4294967295]
facebook-github-bot's avatar
facebook-github-bot committed
780
781
            )

782
783
784
785
786
787
788
789
790
791
792
793
794
    def test_load_uvs(self):
        io = IO()
        mesh = io.load_mesh(DATA_DIR / "uvs.ply")
        self.assertEqual(mesh.textures.verts_uvs_padded().shape, (1, 8, 2))
        self.assertClose(
            mesh.textures.verts_uvs_padded()[0],
            torch.tensor([[0, 0]] + [[0.2, 0.3]] * 6 + [[0.4, 0.5]]),
        )
        self.assertEqual(
            mesh.textures.faces_uvs_padded().shape, mesh.faces_padded().shape
        )
        self.assertEqual(mesh.textures.maps_padded().shape, (1, 512, 512, 3))

facebook-github-bot's avatar
facebook-github-bot committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
    def test_bad_ply_syntax(self):
        """Some syntactically bad ply files."""
        lines = [
            "ply",
            "format ascii 1.0",
            "comment dashfadskfj;k",
            "element vertex 1",
            "property float x",
            "element listy 1",
            "property list uint int x",
            "end_header",
            "0",
            "0",
        ]
        lines2 = lines.copy()
        # this is ok
        _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[0] = "PLY"
        with self.assertRaisesRegex(ValueError, "Invalid file header."):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[2] = "#this is a comment"
        with self.assertRaisesRegex(ValueError, "Invalid line.*"):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[3] = lines[4]
        lines2[4] = lines[3]
        with self.assertRaisesRegex(
            ValueError, "Encountered property before any element."
        ):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[8] = "1 2"
833
        with self.assertRaisesRegex(ValueError, "Inconsistent data for vertex."):
facebook-github-bot's avatar
facebook-github-bot committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines[:-1]
        with self.assertRaisesRegex(ValueError, "Not enough data for listy."):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[5] = "element listy 2"
        with self.assertRaisesRegex(ValueError, "Not enough data for listy."):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2.insert(4, "property short x")
        with self.assertRaisesRegex(
            ValueError, "Cannot have two properties called x in vertex."
        ):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2.insert(4, "property zz short")
        with self.assertRaisesRegex(ValueError, "Invalid datatype: zz"):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2.append("3")
        with self.assertRaisesRegex(ValueError, "Extra data at end of file."):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2.append("comment foo")
        with self.assertRaisesRegex(ValueError, "Extra data at end of file."):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2.insert(4, "element bad 1")
869
        with self.assertRaisesRegex(ValueError, "Found an element with no properties."):
facebook-github-bot's avatar
facebook-github-bot committed
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[-1] = "3 2 3 3"
        _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[-1] = "3 1 2 3 4"
        msg = "A line of listy data did not have the specified length."
        with self.assertRaisesRegex(ValueError, msg):
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2 = lines.copy()
        lines2[3] = "element vertex one"
        msg = "Number of items for vertex was not a number."
        with self.assertRaisesRegex(ValueError, msg):
            _load_ply_raw(StringIO("\n".join(lines2)))

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
888
        # Heterogeneous cases
facebook-github-bot's avatar
facebook-github-bot committed
889
890
891
        lines2 = lines.copy()
        lines2.insert(4, "property double y")

892
        with self.assertRaisesRegex(ValueError, "Inconsistent data for vertex."):
facebook-github-bot's avatar
facebook-github-bot committed
893
894
895
896
897
898
            _load_ply_raw(StringIO("\n".join(lines2)))

        lines2[-2] = "3.3 4.2"
        _load_ply_raw(StringIO("\n".join(lines2)))

        lines2[-2] = "3.3 4.3 2"
899
        with self.assertRaisesRegex(ValueError, "Inconsistent data for vertex."):
facebook-github-bot's avatar
facebook-github-bot committed
900
901
            _load_ply_raw(StringIO("\n".join(lines2)))

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
902
        with self.assertRaisesRegex(ValueError, "Invalid vertices in file."):
facebook-github-bot's avatar
facebook-github-bot committed
903
904
905
906
907
908
909
910
911
912
            load_ply(StringIO("\n".join(lines)))

        lines2 = lines.copy()
        lines2[5] = "element face 1"
        with self.assertRaisesRegex(ValueError, "Invalid vertices in file."):
            load_ply(StringIO("\n".join(lines2)))

        lines2.insert(5, "property float z")
        lines2.insert(5, "property float y")
        lines2[-2] = "0 0 0"
913
914
915
916
917
        lines2[-1] = ""
        with self.assertRaisesRegex(ValueError, "Not enough data for face."):
            load_ply(StringIO("\n".join(lines2)))

        lines2[-1] = "2 0 0"
918
        with self.assertRaisesRegex(ValueError, "Faces must have at least 3 vertices."):
facebook-github-bot's avatar
facebook-github-bot committed
919
920
921
922
923
924
925
            load_ply(StringIO("\n".join(lines2)))

        # Good one
        lines2[-1] = "3 0 0 0"
        load_ply(StringIO("\n".join(lines2)))

    @staticmethod
926
    def _bm_save_ply(verts: torch.Tensor, faces: torch.Tensor, decimal_places: int):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
927
928
929
930
931
932
933
        return lambda: save_ply(
            BytesIO(),
            verts=verts,
            faces=faces,
            ascii=True,
            decimal_places=decimal_places,
        )
facebook-github-bot's avatar
facebook-github-bot committed
934

935
    @staticmethod
936
    def _bm_load_ply(verts: torch.Tensor, faces: torch.Tensor, decimal_places: int):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
937
938
        f = BytesIO()
        save_ply(f, verts=verts, faces=faces, ascii=True, decimal_places=decimal_places)
939
940
        s = f.getvalue()
        # Recreate stream so it's unaffected by how it was created.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
941
        return lambda: load_ply(BytesIO(s))
942
943
944
945
946
947
948

    @staticmethod
    def bm_save_simple_ply_with_init(V: int, F: int):
        verts = torch.tensor(V * [[0.11, 0.22, 0.33]]).view(-1, 3)
        faces = torch.tensor(F * [[0, 1, 2]]).view(-1, 3)
        return TestMeshPlyIO._bm_save_ply(verts, faces, decimal_places=2)

facebook-github-bot's avatar
facebook-github-bot committed
949
    @staticmethod
950
    def bm_load_simple_ply_with_init(V: int, F: int):
facebook-github-bot's avatar
facebook-github-bot committed
951
952
        verts = torch.tensor([[0.1, 0.2, 0.3]]).expand(V, 3)
        faces = torch.tensor([[0, 1, 2]], dtype=torch.int64).expand(F, 3)
953
954
955
956
957
958
959
960
961
962
963
964
965
        return TestMeshPlyIO._bm_load_ply(verts, faces, decimal_places=2)

    @staticmethod
    def bm_save_complex_ply(N: int):
        meshes = torus(r=0.25, R=1.0, sides=N, rings=2 * N)
        [verts], [faces] = meshes.verts_list(), meshes.faces_list()
        return TestMeshPlyIO._bm_save_ply(verts, faces, decimal_places=5)

    @staticmethod
    def bm_load_complex_ply(N: int):
        meshes = torus(r=0.25, R=1.0, sides=N, rings=2 * N)
        [verts], [faces] = meshes.verts_list(), meshes.faces_list()
        return TestMeshPlyIO._bm_load_ply(verts, faces, decimal_places=5)