Skip to content

Commit d0cec02

Browse files
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: #230 Reviewed By: gkioxari Differential Revision: D23278126 Pulled By: nikhilaravi fbshipit-source-id: c06e891bc46de8222325ee7b37aa43cde44648e8
1 parent 778383e commit d0cec02

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pytorch3d/renderer/cameras.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,11 @@ def look_at_view_transform(
12801280
dist, elev, azim, at, up, device=device
12811281
)
12821282
dist, elev, azim, at, up = broadcasted_args
1283-
C = camera_position_from_spherical_angles(
1284-
dist, elev, azim, degrees=degrees, device=device
1283+
C = (
1284+
camera_position_from_spherical_angles(
1285+
dist, elev, azim, degrees=degrees, device=device
1286+
)
1287+
+ at
12851288
)
12861289

12871290
R = look_at_rotation(C, at, up, device=device)

tests/test_cameras.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ def test_look_at_view_transform_default_values(self):
167167
self.assertTrue(torch.allclose(R, R_default, atol=2e-7))
168168
self.assertTrue(torch.allclose(t, t_default, atol=2e-7))
169169

170+
def test_look_at_view_transform_non_default_at_position(self):
171+
dist = 1.0
172+
elev = 0.0
173+
azim = 0.0
174+
at = ((1, 1, 1),)
175+
# Using passed values for dist, elev, azim, at
176+
R, t = look_at_view_transform(dist, elev, azim, at=at)
177+
# Using default dist=1.0, elev=0.0, azim=0.0
178+
R_default, t_default = look_at_view_transform()
179+
# test default = passed = expected
180+
# R must be the same, t must be translated by (1,-1,1) with respect to t_default
181+
t_trans = torch.tensor([1, -1, 1], dtype=torch.float32).view(1, 3)
182+
self.assertTrue(torch.allclose(R, R_default, atol=2e-7))
183+
self.assertTrue(torch.allclose(t, t_default + t_trans, atol=2e-7))
184+
170185
def test_camera_position_from_angles_python_scalar(self):
171186
dist = 2.7
172187
elev = 90.0

0 commit comments

Comments
 (0)