Skip to content

Commit eb517dd

Browse files
Amitav Baruahfacebook-github-bot
Amitav Baruah
authored andcommitted
Fix look_at corner case
Summary: When the camera is vertically oriented, calculating the look_at x-axis (also known as the "right" vector) does not succeed, resulting in the x-axis being placed at the origin. Adds a check to correctly calculate the x-axis if this case occurs. Reviewed By: nikhilaravi, sbranson Differential Revision: D23511859 fbshipit-source-id: ee5145cdbecdbe2f7c7d288588bd0899480cb327
1 parent f8ea590 commit eb517dd

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pytorch3d/renderer/cameras.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,12 @@ def look_at_rotation(
12261226
z_axis = F.normalize(at - camera_position, eps=1e-5)
12271227
x_axis = F.normalize(torch.cross(up, z_axis, dim=1), eps=1e-5)
12281228
y_axis = F.normalize(torch.cross(z_axis, x_axis, dim=1), eps=1e-5)
1229+
is_close = torch.isclose(x_axis, torch.tensor(0.0), atol=5e-3).all(
1230+
dim=1, keepdim=True
1231+
)
1232+
if is_close.any():
1233+
replacement = F.normalize(torch.cross(y_axis, z_axis, dim=1), eps=1e-5)
1234+
x_axis = torch.where(is_close, replacement, x_axis)
12291235
R = torch.cat((x_axis[:, None, :], y_axis[:, None, :], z_axis[:, None, :]), dim=1)
12301236
return R.transpose(1, 2)
12311237

tests/test_cameras.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ def test_view_transform(self):
433433
RT = get_world_to_view_transform(R=R, T=T)
434434
self.assertTrue(isinstance(RT, Transform3d))
435435

436+
def test_look_at_view_transform_corner_case(self):
437+
dist = 2.7
438+
elev = 90
439+
azim = 90
440+
expected_position = torch.tensor([0.0, 2.7, 0.0], dtype=torch.float32).view(
441+
1, 3
442+
)
443+
position = camera_position_from_spherical_angles(dist, elev, azim)
444+
self.assertClose(position, expected_position, atol=2e-7)
445+
R, _ = look_at_view_transform(eye=position)
446+
x_axis = R[:, :, 0]
447+
expected_x_axis = torch.tensor([0.0, 0.0, -1.0], dtype=torch.float32).view(1, 3)
448+
self.assertClose(x_axis, expected_x_axis, atol=5e-3)
449+
436450

437451
class TestCamerasCommon(TestCaseMixin, unittest.TestCase):
438452
def test_view_transform_class_method(self):

0 commit comments

Comments
 (0)