test_io_gltf.py 12.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.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
6

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
7
import os.path
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
8
9
10
11
12
13
14
import unittest
from math import radians

import numpy as np
import torch
from PIL import Image
from pytorch3d.io import IO
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
15
from pytorch3d.io.experimental_gltf_io import _read_header, MeshGlbFormat
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
16
17
18
19
from pytorch3d.renderer import (
    AmbientLights,
    BlendParams,
    FoVPerspectiveCameras,
20
    look_at_view_transform,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
21
22
23
24
25
26
27
28
29
30
31
32
    PointLights,
    RasterizationSettings,
    rotate_on_spot,
)
from pytorch3d.renderer.mesh import (
    HardPhongShader,
    MeshRasterizer,
    MeshRenderer,
    TexturesVertex,
)
from pytorch3d.structures import Meshes
from pytorch3d.transforms import axis_angle_to_matrix
Jiali Duan's avatar
Jiali Duan committed
33
from pytorch3d.utils import ico_sphere
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
34
35
from pytorch3d.vis.texture_vis import texturesuv_image_PIL

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
36
37
from .common_testing import get_pytorch3d_dir, get_tests_dir, TestCaseMixin

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
38
39
40
41
42
43
44
45
46
47
48
49

DATA_DIR = get_tests_dir() / "data"
TUTORIAL_DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data"
DEBUG = False


def _load(path, **kwargs) -> Meshes:
    io = IO()
    io.register_meshes_format(MeshGlbFormat())
    return io.load_mesh(path, **kwargs)


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
50
def _write(mesh, path, **kwargs) -> None:
Jiali Duan's avatar
Jiali Duan committed
51
52
    io = IO()
    io.register_meshes_format(MeshGlbFormat())
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
53
54
55
56
57
    io.save_mesh(mesh, path, **kwargs)

    with open(path, "rb") as f:
        _, stored_length = _read_header(f)
    assert stored_length == os.path.getsize(path)
Jiali Duan's avatar
Jiali Duan committed
58
59


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
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
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
def _render(
    mesh: Meshes,
    name: str,
    dist: float = 3.0,
    elev: float = 10.0,
    azim: float = 0,
    image_size: int = 256,
    pan=None,
    RT=None,
    use_ambient=False,
):
    device = mesh.device
    if RT is not None:
        R, T = RT
    else:
        R, T = look_at_view_transform(dist, elev, azim)
        if pan is not None:
            R, T = rotate_on_spot(R, T, pan)
    cameras = FoVPerspectiveCameras(device=device, R=R, T=T)

    raster_settings = RasterizationSettings(
        image_size=image_size, blur_radius=0.0, faces_per_pixel=1
    )

    # Init shader settings
    if use_ambient:
        lights = AmbientLights(device=device)
    else:
        lights = PointLights(device=device)
        lights.location = torch.tensor([0.0, 0.0, 2.0], device=device)[None]

    blend_params = BlendParams(
        sigma=1e-1,
        gamma=1e-4,
        background_color=torch.tensor([1.0, 1.0, 1.0], device=device),
    )
    # Init renderer
    renderer = MeshRenderer(
        rasterizer=MeshRasterizer(cameras=cameras, raster_settings=raster_settings),
        shader=HardPhongShader(
            device=device, lights=lights, cameras=cameras, blend_params=blend_params
        ),
    )

    output = renderer(mesh)

    image = (output[0, ..., :3].cpu().numpy() * 255).astype(np.uint8)

    if DEBUG:
        Image.fromarray(image).save(DATA_DIR / f"glb_{name}_.png")

    return image


class TestMeshGltfIO(TestCaseMixin, unittest.TestCase):
    def test_load_apartment(self):
        """
        This is the example habitat example scene from inside
        http://dl.fbaipublicfiles.com/habitat/habitat-test-scenes.zip

        The scene is "already lit", i.e. the textures reflect the lighting
        already, so we want to render them with full ambient light.
        """
123

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        self.skipTest("Data not available")

        glb = DATA_DIR / "apartment_1.glb"
        self.assertTrue(glb.is_file())
        device = torch.device("cuda:0")
        mesh = _load(glb, device=device)

        if DEBUG:
            texturesuv_image_PIL(mesh.textures).save(DATA_DIR / "out_apartment.png")

        for i in range(19):
            # random locations in the apartment
            eye = ((np.random.uniform(-6, 0.5), np.random.uniform(-8, 2), 0),)
            at = ((np.random.uniform(-6, 0.5), np.random.uniform(-8, 2), 0),)
            up = ((0, 0, -1),)
            RT = look_at_view_transform(eye=eye, at=at, up=up)
            _render(mesh, f"apartment_eau{i}", RT=RT, use_ambient=True)

        for i in range(12):
            # panning around the inner room from one location
            pan = axis_angle_to_matrix(torch.FloatTensor([0, radians(30 * i), 0]))
            _render(mesh, f"apartment{i}", 1.0, -90, pan, use_ambient=True)

    def test_load_cow(self):
        """
        Load the cow as converted to a single mesh in a glb file.
        """
        glb = DATA_DIR / "cow.glb"
        self.assertTrue(glb.is_file())
        device = torch.device("cuda:0")
        mesh = _load(glb, device=device)
        self.assertEqual(mesh.device, device)

        self.assertEqual(mesh.faces_packed().shape, (5856, 3))
        self.assertEqual(mesh.verts_packed().shape, (3225, 3))
        mesh_obj = _load(TUTORIAL_DATA_DIR / "cow_mesh/cow.obj")
Jiali Duan's avatar
Jiali Duan committed
160
        self.assertClose(mesh.get_bounding_boxes().cpu(), mesh_obj.get_bounding_boxes())
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
161

162
163
164
165
166
167
168
169
170
171
172
173
        self.assertClose(
            mesh.textures.verts_uvs_padded().cpu(), mesh_obj.textures.verts_uvs_padded()
        )

        self.assertClose(
            mesh.textures.faces_uvs_padded().cpu(), mesh_obj.textures.faces_uvs_padded()
        )

        self.assertClose(
            mesh.textures.maps_padded().cpu(), mesh_obj.textures.maps_padded()
        )

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
174
175
176
        if DEBUG:
            texturesuv_image_PIL(mesh.textures).save(DATA_DIR / "out_cow.png")

177
178
179
            image = _render(mesh, "cow", azim=4)
            with Image.open(DATA_DIR / "glb_cow.png") as f:
                expected = np.array(f)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
180

181
            self.assertClose(image, expected)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
182

Jiali Duan's avatar
Jiali Duan committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
234
235
236
237
238
239
240
241
242
243
244
245
    def test_save_cow(self):
        """
        Save the cow mesh to a glb file
        """
        # load cow mesh from a glb file
        glb = DATA_DIR / "cow.glb"
        self.assertTrue(glb.is_file())
        device = torch.device("cuda:0")
        mesh = _load(glb, device=device)

        # save the mesh to a glb file
        glb = DATA_DIR / "cow_write.glb"
        _write(mesh, glb)

        # load again
        glb_reload = DATA_DIR / "cow_write.glb"
        self.assertTrue(glb_reload.is_file())
        device = torch.device("cuda:0")
        mesh_reload = _load(glb_reload, device=device)

        # assertions
        self.assertEqual(mesh_reload.faces_packed().shape, (5856, 3))
        self.assertEqual(mesh_reload.verts_packed().shape, (3225, 3))
        self.assertClose(
            mesh_reload.get_bounding_boxes().cpu(), mesh.get_bounding_boxes().cpu()
        )

        self.assertClose(
            mesh_reload.textures.verts_uvs_padded().cpu(),
            mesh.textures.verts_uvs_padded().cpu(),
        )

        self.assertClose(
            mesh_reload.textures.faces_uvs_padded().cpu(),
            mesh.textures.faces_uvs_padded().cpu(),
        )

        self.assertClose(
            mesh_reload.textures.maps_padded().cpu(), mesh.textures.maps_padded().cpu()
        )

    def test_save_ico_sphere(self):
        """
        save the ico_sphere mesh in a glb file
        """
        ico_sphere_mesh = ico_sphere(level=3)
        glb = DATA_DIR / "ico_sphere.glb"
        _write(ico_sphere_mesh, glb)

        # reload the ico_sphere
        device = torch.device("cuda:0")
        mesh_reload = _load(glb, device=device, include_textures=False)

        self.assertClose(
            ico_sphere_mesh.verts_padded().cpu(),
            mesh_reload.verts_padded().cpu(),
        )

        self.assertClose(
            ico_sphere_mesh.faces_padded().cpu(),
            mesh_reload.faces_padded().cpu(),
        )

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    def test_load_cow_no_texture(self):
        """
        Load the cow as converted to a single mesh in a glb file.
        """
        glb = DATA_DIR / "cow.glb"
        self.assertTrue(glb.is_file())
        device = torch.device("cuda:0")
        mesh = _load(glb, device=device, include_textures=False)
        self.assertEqual(len(mesh), 1)
        self.assertIsNone(mesh.textures)

        self.assertEqual(mesh.faces_packed().shape, (5856, 3))
        self.assertEqual(mesh.verts_packed().shape, (3225, 3))
        mesh_obj = _load(TUTORIAL_DATA_DIR / "cow_mesh/cow.obj")
Jiali Duan's avatar
Jiali Duan committed
260
        self.assertClose(mesh.get_bounding_boxes().cpu(), mesh_obj.get_bounding_boxes())
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
261
262
263
264
265
266
267
268
269

        mesh.textures = TexturesVertex(0.5 * torch.ones_like(mesh.verts_padded()))

        image = _render(mesh, "cow_gray")

        with Image.open(DATA_DIR / "glb_cow_gray.png") as f:
            expected = np.array(f)

        self.assertClose(image, expected)
270
271
272
273
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

    def test_load_save_load_cow_texturesvertex(self):
        """
        Load the cow as converted to a single mesh in a glb file and then save it to a glb file.
        """

        glb = DATA_DIR / "cow.glb"
        self.assertTrue(glb.is_file())
        device = torch.device("cuda:0")
        mesh = _load(glb, device=device, include_textures=False)
        self.assertEqual(len(mesh), 1)
        self.assertIsNone(mesh.textures)

        self.assertEqual(mesh.faces_packed().shape, (5856, 3))
        self.assertEqual(mesh.verts_packed().shape, (3225, 3))
        mesh_obj = _load(TUTORIAL_DATA_DIR / "cow_mesh/cow.obj")
        self.assertClose(mesh.get_bounding_boxes().cpu(), mesh_obj.get_bounding_boxes())

        mesh.textures = TexturesVertex(0.5 * torch.ones_like(mesh.verts_padded()))

        image = _render(mesh, "cow_gray")

        with Image.open(DATA_DIR / "glb_cow_gray.png") as f:
            expected = np.array(f)

        self.assertClose(image, expected)

        # save the mesh to a glb file
        glb = DATA_DIR / "cow_write_texturesvertex.glb"
        _write(mesh, glb)

        # reload the mesh glb file saved in TexturesVertex format
        glb = DATA_DIR / "cow_write_texturesvertex.glb"
        self.assertTrue(glb.is_file())
        mesh_dash = _load(glb, device=device)
        self.assertEqual(len(mesh_dash), 1)

        self.assertEqual(mesh_dash.faces_packed().shape, (5856, 3))
        self.assertEqual(mesh_dash.verts_packed().shape, (3225, 3))
        self.assertEqual(mesh_dash.textures.verts_features_list()[0].shape, (3225, 3))

        # check the re-rendered image with expected
        image_dash = _render(mesh, "cow_gray_texturesvertex")
        self.assertClose(image_dash, expected)

    def test_save_toy(self):
        """
        Construct a simple mesh and save it to a glb file in TexturesVertex mode.
        """

        example = {}
        example["POSITION"] = torch.tensor(
            [
                [
                    [0.0, 0.0, 0.0],
                    [-1.0, 0.0, 0.0],
                    [-1.0, 0.0, 1.0],
                    [0.0, 0.0, 1.0],
                    [0.0, 1.0, 0.0],
                    [-1.0, 1.0, 0.0],
                    [-1.0, 1.0, 1.0],
                    [0.0, 1.0, 1.0],
                ]
            ]
        )
        example["indices"] = torch.tensor(
            [
                [
                    [1, 4, 2],
                    [4, 3, 2],
                    [3, 7, 2],
                    [7, 6, 2],
                    [3, 4, 7],
                    [4, 8, 7],
                    [8, 5, 7],
                    [5, 6, 7],
                    [5, 2, 6],
                    [5, 1, 2],
                    [1, 5, 4],
                    [5, 8, 4],
                ]
            ]
        )
        example["indices"] -= 1
        example["COLOR_0"] = torch.tensor(
            [
                [
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                ]
            ]
        )
        # example['prop'] = {'material':
        #                       {'pbrMetallicRoughness':
        #                           {'baseColorFactor':
        #                                torch.tensor([[0.7, 0.7, 1, 0.5]]),
        #                            'metallicFactor': torch.tensor([1]),
        #                            'roughnessFactor': torch.tensor([0.1])},
        #                    'alphaMode': 'BLEND',
        #                    'doubleSided': True}}

        texture = TexturesVertex(example["COLOR_0"])
        mesh = Meshes(
            verts=example["POSITION"], faces=example["indices"], textures=texture
        )

        glb = DATA_DIR / "example_write_texturesvertex.glb"
        _write(mesh, glb)