pulsar_basic.py 1.69 KB
Newer Older
Christoph Lassner's avatar
Christoph Lassner committed
1
2
3
4
5
6
7
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
"""
This example demonstrates the most trivial, direct interface of the pulsar
sphere renderer. It renders and saves an image with 10 random spheres.
Output: basic.png.
"""
Christoph Lassner's avatar
Christoph Lassner committed
8
import math
Christoph Lassner's avatar
Christoph Lassner committed
9
10
11
12
13
14
15
from os import path

import imageio
import torch
from pytorch3d.renderer.points.pulsar import Renderer


Christoph Lassner's avatar
Christoph Lassner committed
16
17
torch.manual_seed(1)

Christoph Lassner's avatar
Christoph Lassner committed
18
19
20
21
n_points = 10
width = 1_000
height = 1_000
device = torch.device("cuda")
Christoph Lassner's avatar
Christoph Lassner committed
22
23
24
# The PyTorch3D system is right handed; in pulsar you can choose the handedness.
# For easy reproducibility we use a right handed coordinate system here.
renderer = Renderer(width, height, n_points, right_handed_system=True).to(device)
Christoph Lassner's avatar
Christoph Lassner committed
25
26
27
28
29
30
31
32
33
34
35
36
# Generate sample data.
vert_pos = torch.rand(n_points, 3, dtype=torch.float32, device=device) * 10.0
vert_pos[:, 2] += 25.0
vert_pos[:, :2] -= 5.0
vert_col = torch.rand(n_points, 3, dtype=torch.float32, device=device)
vert_rad = torch.rand(n_points, dtype=torch.float32, device=device)
cam_params = torch.tensor(
    [
        0.0,
        0.0,
        0.0,  # Position 0, 0, 0 (x, y, z).
        0.0,
Christoph Lassner's avatar
Christoph Lassner committed
37
        math.pi,  # Because of the right handed system, the camera must look 'back'.
Christoph Lassner's avatar
Christoph Lassner committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        0.0,  # Rotation 0, 0, 0 (in axis-angle format).
        5.0,  # Focal length in world size.
        2.0,  # Sensor size in world size.
    ],
    dtype=torch.float32,
    device=device,
)
# Render.
image = renderer(
    vert_pos,
    vert_col,
    vert_rad,
    cam_params,
    1.0e-1,  # Renderer blending parameter gamma, in [1., 1e-5].
    45.0,  # Maximum depth.
)
print("Writing image to `%s`." % (path.abspath("basic.png")))
imageio.imsave("basic.png", (image.cpu().detach() * 255.0).to(torch.uint8).numpy())