From d0cec028c7ddb59b8e4d0ee054d0ea8b1a493d46 Mon Sep 17 00:00:00 2001 From: Eduardo Henrique Arnold Date: Fri, 21 Aug 2020 23:42:40 -0700 Subject: [PATCH] Fix look_at_view_transform when object location is not (0,0,0) (#230) Summary: The look_at_view_transform did not give the correct results when the object location `at` was not (0,0,0). The problem was on computing the cameras' location in world's coordinate `C`. It only took into account the camera position from spherical angles, but ignored the object location in the world's coordinate system. I simply modified the C tensor to take into account the object's location which is not necessarily in the origin. I ran unit tests and all but 4 failed with the same error message: `RuntimeError: CUDA error: invalid device ordinal`. However the same happens before this patch, so I believe these errors are unrelated. Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/230 Reviewed By: gkioxari Differential Revision: D23278126 Pulled By: nikhilaravi fbshipit-source-id: c06e891bc46de8222325ee7b37aa43cde44648e8 --- pytorch3d/renderer/cameras.py | 7 +++++-- tests/test_cameras.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pytorch3d/renderer/cameras.py b/pytorch3d/renderer/cameras.py index 94683d06..dd435509 100644 --- a/pytorch3d/renderer/cameras.py +++ b/pytorch3d/renderer/cameras.py @@ -1280,8 +1280,11 @@ def look_at_view_transform( dist, elev, azim, at, up, device=device ) dist, elev, azim, at, up = broadcasted_args - C = camera_position_from_spherical_angles( - dist, elev, azim, degrees=degrees, device=device + C = ( + camera_position_from_spherical_angles( + dist, elev, azim, degrees=degrees, device=device + ) + + at ) R = look_at_rotation(C, at, up, device=device) diff --git a/tests/test_cameras.py b/tests/test_cameras.py index 88c78ea7..8755701b 100644 --- a/tests/test_cameras.py +++ b/tests/test_cameras.py @@ -167,6 +167,21 @@ class TestCameraHelpers(TestCaseMixin, unittest.TestCase): self.assertTrue(torch.allclose(R, R_default, atol=2e-7)) self.assertTrue(torch.allclose(t, t_default, atol=2e-7)) + def test_look_at_view_transform_non_default_at_position(self): + dist = 1.0 + elev = 0.0 + azim = 0.0 + at = ((1, 1, 1),) + # Using passed values for dist, elev, azim, at + R, t = look_at_view_transform(dist, elev, azim, at=at) + # Using default dist=1.0, elev=0.0, azim=0.0 + R_default, t_default = look_at_view_transform() + # test default = passed = expected + # R must be the same, t must be translated by (1,-1,1) with respect to t_default + t_trans = torch.tensor([1, -1, 1], dtype=torch.float32).view(1, 3) + self.assertTrue(torch.allclose(R, R_default, atol=2e-7)) + self.assertTrue(torch.allclose(t, t_default + t_trans, atol=2e-7)) + def test_camera_position_from_angles_python_scalar(self): dist = 2.7 elev = 90.0