Skip to content

Commit 4372001

Browse files
Krzysztof Chalupkafacebook-github-bot
Krzysztof Chalupka
authored andcommitted
Make transform_points_screen's with_xyflip configurable
Summary: We'll need non-flipped screen coords in splatter. Reviewed By: bottler Differential Revision: D36337027 fbshipit-source-id: 897f88e8854bab215d2d0e502b25d15526ee86f1
1 parent 61e2b87 commit 4372001

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pytorch3d/renderer/cameras.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def transform_points_ndc(
323323
return world_to_ndc_transform.transform_points(points, eps=eps)
324324

325325
def transform_points_screen(
326-
self, points, eps: Optional[float] = None, **kwargs
326+
self, points, eps: Optional[float] = None, with_xyflip: bool = True, **kwargs
327327
) -> torch.Tensor:
328328
"""
329329
Transforms points from PyTorch3D world/camera space to screen space.
@@ -341,14 +341,19 @@ def transform_points_screen(
341341
stabilizes gradients since it leads to avoiding division
342342
by excessively low numbers for points close to the
343343
camera plane.
344+
with_xyflip: If True, flip x and y directions. In world/camera/ndc coords,
345+
+x points to the left and +y up. If with_xyflip is true, in screen
346+
coords +x points right, and +y down, following the usual RGB image
347+
convention. Warning: do not set to False unless you know what you're
348+
doing!
344349
345350
Returns
346351
new_points: transformed points with the same shape as the input.
347352
"""
348353
points_ndc = self.transform_points_ndc(points, eps=eps, **kwargs)
349354
image_size = kwargs.get("image_size", self.get_image_size())
350355
return get_ndc_to_screen_transform(
351-
self, with_xyflip=True, image_size=image_size
356+
self, with_xyflip=with_xyflip, image_size=image_size
352357
).transform_points(points_ndc, eps=eps)
353358

354359
def clone(self):

tests/test_camera_pixels.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ def test_camera(self):
204204
for cameras in (data.camera_ndc, data.camera_screen):
205205
ndc_points = cameras.transform_points_ndc(points)
206206
screen_points = cameras.transform_points_screen(points)
207+
screen_points_without_xyflip = cameras.transform_points_screen(
208+
points, with_xyflip=False
209+
)
207210
camera_points = cameras.transform_points(points)
208211
for batch_idx in range(2):
209212
# NDC space agrees with the original
@@ -214,6 +217,13 @@ def test_camera(self):
214217
torch.tensor([data.x + 0.5, data.y + 0.5, 1.0]),
215218
atol=1e-5,
216219
)
220+
# Screen coords without xyflip should have x, y that negate the non-
221+
# flipped values, and unchanged z.
222+
self.assertClose(
223+
screen_points_without_xyflip[batch_idx][0],
224+
torch.tensor([-(data.x + 0.5), -(data.y + 0.5), 1.0]),
225+
atol=1e-5,
226+
)
217227
# Second point in screen space is the center of the screen
218228
self.assertClose(
219229
screen_points[batch_idx][1],

0 commit comments

Comments
 (0)