|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +from pytorch3d.implicitron.tools.point_cloud_utils import get_rgbd_point_cloud |
| 11 | + |
| 12 | +from pytorch3d.renderer.cameras import PerspectiveCameras |
| 13 | +from tests.common_testing import TestCaseMixin |
| 14 | + |
| 15 | + |
| 16 | +class TestPointCloudUtils(TestCaseMixin, unittest.TestCase): |
| 17 | + def setUp(self): |
| 18 | + torch.manual_seed(42) |
| 19 | + |
| 20 | + def test_unproject(self): |
| 21 | + H, W = 50, 100 |
| 22 | + |
| 23 | + # Random RGBD image with depth 3 |
| 24 | + # (depth 0 = at the camera) |
| 25 | + # and purple in the upper right corner |
| 26 | + |
| 27 | + image = torch.rand(4, H, W) |
| 28 | + depth = 3 |
| 29 | + image[3] = depth |
| 30 | + image[1, H // 2 :, W // 2 :] *= 0.4 |
| 31 | + |
| 32 | + # two ways to define the same camera: |
| 33 | + # at the origin facing the positive z axis |
| 34 | + ndc_camera = PerspectiveCameras(focal_length=1.0) |
| 35 | + screen_camera = PerspectiveCameras( |
| 36 | + focal_length=H // 2, |
| 37 | + in_ndc=False, |
| 38 | + image_size=((H, W),), |
| 39 | + principal_point=((W / 2, H / 2),), |
| 40 | + ) |
| 41 | + |
| 42 | + for camera in (ndc_camera, screen_camera): |
| 43 | + # 1. z-depth |
| 44 | + cloud = get_rgbd_point_cloud( |
| 45 | + camera, |
| 46 | + image_rgb=image[:3][None], |
| 47 | + depth_map=image[3:][None], |
| 48 | + euclidean=False, |
| 49 | + ) |
| 50 | + [points] = cloud.points_list() |
| 51 | + self.assertConstant(points[:, 2], depth) # constant depth |
| 52 | + extremes = depth * torch.tensor([W / H - 1 / H, 1 - 1 / H]) |
| 53 | + self.assertClose(points[:, :2].min(0).values, -extremes) |
| 54 | + self.assertClose(points[:, :2].max(0).values, extremes) |
| 55 | + |
| 56 | + # 2. euclidean |
| 57 | + cloud = get_rgbd_point_cloud( |
| 58 | + camera, |
| 59 | + image_rgb=image[:3][None], |
| 60 | + depth_map=image[3:][None], |
| 61 | + euclidean=True, |
| 62 | + ) |
| 63 | + [points] = cloud.points_list() |
| 64 | + self.assertConstant(torch.norm(points, dim=1), depth, atol=1e-5) |
0 commit comments