|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. |
| 3 | +""" |
| 4 | +This example demonstrates the most trivial use of the pulsar PyTorch3D |
| 5 | +interface for sphere renderering. It renders and saves an image with |
| 6 | +10 random spheres. |
| 7 | +Output: basic-pt3d.png. |
| 8 | +""" |
| 9 | +from os import path |
| 10 | + |
| 11 | +import imageio |
| 12 | +import torch |
| 13 | +from pytorch3d.renderer import PerspectiveCameras # , look_at_view_transform |
| 14 | +from pytorch3d.renderer import ( |
| 15 | + PointsRasterizationSettings, |
| 16 | + PointsRasterizer, |
| 17 | + PulsarPointsRenderer, |
| 18 | +) |
| 19 | +from pytorch3d.structures import Pointclouds |
| 20 | + |
| 21 | + |
| 22 | +torch.manual_seed(1) |
| 23 | + |
| 24 | +n_points = 10 |
| 25 | +width = 1_000 |
| 26 | +height = 1_000 |
| 27 | +device = torch.device("cuda") |
| 28 | + |
| 29 | +# Generate sample data. |
| 30 | +vert_pos = torch.rand(n_points, 3, dtype=torch.float32, device=device) * 10.0 |
| 31 | +vert_pos[:, 2] += 25.0 |
| 32 | +vert_pos[:, :2] -= 5.0 |
| 33 | +vert_col = torch.rand(n_points, 3, dtype=torch.float32, device=device) |
| 34 | +pcl = Pointclouds(points=vert_pos[None, ...], features=vert_col[None, ...]) |
| 35 | +# Alternatively, you can also use the look_at_view_transform to get R and T: |
| 36 | +# R, T = look_at_view_transform( |
| 37 | +# dist=30.0, elev=0.0, azim=180.0, at=((0.0, 0.0, 30.0),), up=((0, 1, 0),), |
| 38 | +# ) |
| 39 | +cameras = PerspectiveCameras( |
| 40 | + # The focal length must be double the size for PyTorch3D because of the NDC |
| 41 | + # coordinates spanning a range of two - and they must be normalized by the |
| 42 | + # sensor width (see the pulsar example). This means we need here |
| 43 | + # 5.0 * 2.0 / 2.0 to get the equivalent results as in pulsar. |
| 44 | + focal_length=(5.0 * 2.0 / 2.0,), |
| 45 | + R=torch.eye(3, dtype=torch.float32, device=device)[None, ...], |
| 46 | + T=torch.zeros((1, 3), dtype=torch.float32, device=device), |
| 47 | + image_size=((width, height),), |
| 48 | + device=device, |
| 49 | +) |
| 50 | +vert_rad = torch.rand(n_points, dtype=torch.float32, device=device) |
| 51 | +raster_settings = PointsRasterizationSettings( |
| 52 | + image_size=(width, height), |
| 53 | + radius=vert_rad, |
| 54 | +) |
| 55 | +rasterizer = PointsRasterizer(cameras=cameras, raster_settings=raster_settings) |
| 56 | +renderer = PulsarPointsRenderer(rasterizer=rasterizer).to(device) |
| 57 | +# Render. |
| 58 | +image = renderer( |
| 59 | + pcl, |
| 60 | + gamma=(1.0e-1,), # Renderer blending parameter gamma, in [1., 1e-5]. |
| 61 | + znear=(1.0,), |
| 62 | + zfar=(45.0,), |
| 63 | + radius_world=True, |
| 64 | + bg_col=torch.ones((3,), dtype=torch.float32, device=device), |
| 65 | +)[0] |
| 66 | +print("Writing image to `%s`." % (path.abspath("basic-pt3d.png"))) |
| 67 | +imageio.imsave("basic-pt3d.png", (image.cpu().detach() * 255.0).to(torch.uint8).numpy()) |
0 commit comments