|
15 | 15 | import torch
|
16 | 16 | from common_testing import TestCaseMixin, get_tests_dir, load_rgb_image
|
17 | 17 | from pytorch3d.io import save_obj
|
18 |
| -from pytorch3d.renderer.cameras import FoVPerspectiveCameras, look_at_view_transform |
| 18 | +from pytorch3d.renderer.cameras import ( |
| 19 | + FoVPerspectiveCameras, |
| 20 | + look_at_view_transform, |
| 21 | + PerspectiveCameras, |
| 22 | +) |
19 | 23 | from pytorch3d.renderer.lighting import PointLights
|
20 | 24 | from pytorch3d.renderer.mesh import (
|
21 | 25 | ClipFrustum,
|
|
27 | 31 | from pytorch3d.renderer.mesh.rasterizer import MeshRasterizer, RasterizationSettings
|
28 | 32 | from pytorch3d.renderer.mesh.renderer import MeshRenderer
|
29 | 33 | from pytorch3d.renderer.mesh.shader import SoftPhongShader
|
| 34 | +from pytorch3d.renderer.mesh.textures import TexturesVertex |
30 | 35 | from pytorch3d.structures.meshes import Meshes
|
31 |
| - |
| 36 | +from pytorch3d.utils import torus |
32 | 37 |
|
33 | 38 | # If DEBUG=True, save out images generated in the tests for debugging.
|
34 | 39 | # All saved images have prefix DEBUG_
|
@@ -97,9 +102,9 @@ def load_cube_mesh_with_texture(self, device="cpu", with_grad: bool = False):
|
97 | 102 | return mesh, verts
|
98 | 103 | return mesh
|
99 | 104 |
|
100 |
| - def test_cube_mesh_render(self): |
| 105 | + def debug_cube_mesh_render(self): |
101 | 106 | """
|
102 |
| - End-End test of rendering a cube mesh with texture |
| 107 | + End-End debug run of rendering a cube mesh with texture |
103 | 108 | from decreasing camera distances. The camera starts
|
104 | 109 | outside the cube and enters the inside of the cube.
|
105 | 110 | """
|
@@ -132,22 +137,16 @@ def test_cube_mesh_render(self):
|
132 | 137 | # the camera enters the cube. Check the output looks correct.
|
133 | 138 | images_list = []
|
134 | 139 | dists = np.linspace(0.1, 2.5, 20)[::-1]
|
| 140 | + |
135 | 141 | for d in dists:
|
136 | 142 | R, T = look_at_view_transform(d, 0, 0)
|
137 | 143 | T[0, 1] -= 0.1 # move down in the y axis
|
138 | 144 | cameras = FoVPerspectiveCameras(device=device, R=R, T=T, fov=90)
|
139 | 145 | images = renderer(mesh, cameras=cameras)
|
140 | 146 | rgb = images[0, ..., :3].cpu().detach()
|
141 |
| - filename = "DEBUG_cube_dist=%.1f.jpg" % d |
142 | 147 | im = (rgb.numpy() * 255).astype(np.uint8)
|
143 | 148 | images_list.append(im)
|
144 | 149 |
|
145 |
| - # Check one of the images where the camera is inside the mesh |
146 |
| - if d == 0.5: |
147 |
| - filename = "test_render_mesh_clipped_cam_dist=0.5.jpg" |
148 |
| - image_ref = load_rgb_image(filename, DATA_DIR) |
149 |
| - self.assertClose(rgb, image_ref, atol=0.05) |
150 |
| - |
151 | 150 | # Save a gif of the output - this should show
|
152 | 151 | # the camera moving inside the cube.
|
153 | 152 | if DEBUG:
|
@@ -655,3 +654,25 @@ def test_case_4_no_duplicates(self):
|
655 | 654 | double_hit = torch.tensor([0, 0, -1], device=device)
|
656 | 655 | check_double_hit = any(torch.allclose(i, double_hit) for i in unique_vals)
|
657 | 656 | self.assertFalse(check_double_hit)
|
| 657 | + |
| 658 | + def test_mesh_outside_frustrum(self): |
| 659 | + """ |
| 660 | + Test the case where the mesh is completely outside the view |
| 661 | + frustrum so all faces are culled and z_clip_value = None. |
| 662 | + """ |
| 663 | + device = "cuda:0" |
| 664 | + mesh = torus(20.0, 85.0, 32, 16, device=device) |
| 665 | + tex = TexturesVertex(verts_features=torch.rand_like(mesh.verts_padded())) |
| 666 | + mesh.textures = tex |
| 667 | + raster_settings = RasterizationSettings(image_size=512, cull_to_frustum=True) |
| 668 | + R, T = look_at_view_transform(1.0, 0.0, 0.0) |
| 669 | + cameras = PerspectiveCameras(device=device, R=R, T=T) |
| 670 | + renderer = MeshRenderer( |
| 671 | + rasterizer=MeshRasterizer(cameras=cameras, raster_settings=raster_settings), |
| 672 | + shader=SoftPhongShader(cameras=cameras, device=device), |
| 673 | + ) |
| 674 | + images = renderer(mesh) |
| 675 | + |
| 676 | + # Mesh is completely outside the view frustrum |
| 677 | + # The image should be white. |
| 678 | + self.assertClose(images[0, ..., :3], torch.ones_like(images[0, ..., :3])) |
0 commit comments