Skip to content

Commit 0c32f09

Browse files
gkioxarifacebook-github-bot
authored andcommitted
NDC/screen cameras API fix, compatibility with renderer
Summary: API fix for NDC/screen cameras and compatibility with PyTorch3D renderers. With this new fix: * Users can define cameras and `transform_points` under any coordinate system conventions. The transformation applies the camera K and RT to the input points, not regarding for PyTorch3D conventions. So this makes cameras completely independent from PyTorch3D renderer. * Cameras can be defined either in NDC space or screen space. For existing ones, FoV cameras are in NDC space. Perspective/Orthographic can be defined in NDC or screen space. * The interface with PyTorch3D renderers happens through `transform_points_ndc` which transforms points to the NDC space and assumes that input points are provided according to PyTorch3D conventions. * Similarly, `transform_points_screen` transforms points to screen space and again assumes that input points are under PyTorch3D conventions. * For Orthographic/Perspective cameras, if they are defined in screen space, the `get_ndc_camera_transform` allows points to be converted to NDC for use for the renderers. Reviewed By: nikhilaravi Differential Revision: D26932657 fbshipit-source-id: 1a964e3e7caa54d10c792cf39c4d527ba2fb2e79
1 parent 9a14f54 commit 0c32f09

File tree

6 files changed

+502
-222
lines changed

6 files changed

+502
-222
lines changed

docs/notes/cameras.md

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ This is the system the object/scene lives - the world.
1313
* **Camera view coordinate system**
1414
This is the system that has its origin on the image plane and the `Z`-axis perpendicular to the image plane. In PyTorch3D, we assume that `+X` points left, and `+Y` points up and `+Z` points out from the image plane. The transformation from world to view happens after applying a rotation (`R`) and translation (`T`).
1515
* **NDC coordinate system**
16-
This is the normalized coordinate system that confines in a volume the rendered part of the object/scene. Also known as view volume. Under the PyTorch3D convention, `(+1, +1, znear)` is the top left near corner, and `(-1, -1, zfar)` is the bottom right far corner of the volume. The transformation from view to NDC happens after applying the camera projection matrix (`P`).
16+
This is the normalized coordinate system that confines in a volume the rendered part of the object/scene. Also known as view volume. Under the PyTorch3D convention, `(+1, +1, znear)` is the top left near corner, and `(-1, -1, zfar)` is the bottom right far corner of the volume. For non-square volumes, the side of the volume in `XY` with the smallest length ranges from `[-1, 1]` while the larger side from `[-s, s]`, where `s` is the aspect ratio and `s > 1` (larger divided by smaller side).
17+
The transformation from view to NDC happens after applying the camera projection matrix (`P`).
1718
* **Screen coordinate system**
1819
This is another representation of the view volume with the `XY` coordinates defined in pixel space instead of a normalized space.
1920

@@ -22,47 +23,78 @@ An illustration of the 4 coordinate systems is shown below
2223

2324
## Defining Cameras in PyTorch3D
2425

25-
Cameras in PyTorch3D transform an object/scene from world to NDC by first transforming the object/scene to view (via transforms `R` and `T`) and then projecting the 3D object/scene to NDC (via the projection matrix `P`, else known as camera matrix). Thus, the camera parameters in `P` are assumed to be in NDC space. If the user has camera parameters in screen space, which is a common use case, the parameters should transformed to NDC (see below for an example)
26+
Cameras in PyTorch3D transform an object/scene from world to view by first transforming the object/scene to view (via transforms `R` and `T`) and then projecting the 3D object/scene to a normalized space via the projection matrix `P = K[R | T]`, where `K` is the intrinsic matrix. The camera parameters in `K` define the normalized space. If users define the camera parameters in NDC space, then the transform projects points to NDC. If the camera parameters are defined in screen space, the transformed points are in screen space.
2627

27-
We describe the camera types in PyTorch3D and the convention for the camera parameters provided at construction time.
28+
Note that the base `CamerasBase` class makes no assumptions about the coordinate systems. All the above transforms are geometric transforms defined purely by `R`, `T` and `K`. This means that users can define cameras in any coordinate system and for any transforms. The method `transform_points` will apply `K` , `R` and `T` to the input points as a simple matrix transformation. However, if users wish to use cameras with the PyTorch3D renderer, they need to abide to PyTorch3D's coordinate system assumptions (read below).
29+
30+
We provide instantiations of common camera types in PyTorch3D and how users can flexibly define the projection space below.
31+
32+
## Interfacing with the PyTorch3D Renderer
33+
34+
The PyTorch3D renderer for both meshes and point clouds assumes that the camera transformed points, meaning the points passed as input to the rasterizer, are in PyTorch3D's NDC space. So to get the expected rendering outcome, users need to make sure that their 3D input data and cameras abide by these PyTorch3D coordinate system assumptions. The PyTorch3D coordinate system assumes `+X:left`, `+Y: up` and `+Z: from us to scene` (right-handed) . Confusions regarding coordinate systems are common so we advise that you spend some time understanding your data and the coordinate system they live in and transform them accordingly before using the PyTorch3D renderer.
35+
36+
Examples of cameras and how they interface with the PyTorch3D renderer can be found in our tutorials.
2837

2938
### Camera Types
3039

3140
All cameras inherit from `CamerasBase` which is a base class for all cameras. PyTorch3D provides four different camera types. The `CamerasBase` defines methods that are common to all camera models:
3241
* `get_camera_center` that returns the optical center of the camera in world coordinates
33-
* `get_world_to_view_transform` which returns a 3D transform from world coordinates to the camera view coordinates (R, T)
34-
* `get_full_projection_transform` which composes the projection transform (P) with the world-to-view transform (R, T)
42+
* `get_world_to_view_transform` which returns a 3D transform from world coordinates to the camera view coordinates `(R, T)`
43+
* `get_full_projection_transform` which composes the projection transform (`K`) with the world-to-view transform `(R, T)`
3544
* `transform_points` which takes a set of input points in world coordinates and projects to NDC coordinates ranging from [-1, -1, znear] to [+1, +1, zfar].
45+
* `get_ndc_camera_transform` which defines the conversion to PyTorch3D's NDC space and is called when interfacing with the PyTorch3D renderer. If the camera is defined in NDC space, then the identity transform is returned. If the cameras is defined in screen space, the conversion from screen to NDC is returned. If users define their own camera in screen space, they need to think of the screen to NDC conversion. We provide examples for the `PerspectiveCameras` and `OrthographicCameras`.
46+
* `transform_points_ndc` which takes a set of points in world coordinates and projects them to PyTorch3D's NDC space
3647
* `transform_points_screen` which takes a set of input points in world coordinates and projects them to the screen coordinates ranging from [0, 0, znear] to [W-1, H-1, zfar]
3748

3849
Users can easily customize their own cameras. For each new camera, users should implement the `get_projection_transform` routine that returns the mapping `P` from camera view coordinates to NDC coordinates.
3950

4051
#### FoVPerspectiveCameras, FoVOrthographicCameras
4152
These two cameras follow the OpenGL convention for perspective and orthographic cameras respectively. The user provides the near `znear` and far `zfar` field which confines the view volume in the `Z` axis. The view volume in the `XY` plane is defined by field of view angle (`fov`) in the case of `FoVPerspectiveCameras` and by `min_x, min_y, max_x, max_y` in the case of `FoVOrthographicCameras`.
53+
These cameras are by default in NDC space.
4254

4355
#### PerspectiveCameras, OrthographicCameras
4456
These two cameras follow the Multi-View Geometry convention for cameras. The user provides the focal length (`fx`, `fy`) and the principal point (`px`, `py`). For example, `camera = PerspectiveCameras(focal_length=((fx, fy),), principal_point=((px, py),))`
4557

46-
As mentioned above, the focal length and principal point are used to convert a point `(X, Y, Z)` from view coordinates to NDC coordinates, as follows
58+
The camera projection of a 3D point `(X, Y, Z)` in view coordinates to a point `(x, y, z)` in projection space (either NDC or screen) is
4759

4860
```
49-
# for perspective
50-
x_ndc = fx * X / Z + px
51-
y_ndc = fy * Y / Z + py
52-
z_ndc = 1 / Z
53-
54-
# for orthographic
55-
x_ndc = fx * X + px
56-
y_ndc = fy * Y + py
57-
z_ndc = Z
61+
# for perspective camera
62+
x = fx * X / Z + px
63+
y = fy * Y / Z + py
64+
z = 1 / Z
65+
66+
# for orthographic camera
67+
x = fx * X + px
68+
y = fy * Y + py
69+
z = Z
70+
```
71+
72+
The user can define the camera parameters in NDC or in screen space. Screen space camera parameters are common and for that case the user needs to set `in_ndc` to `False` and also provide the `image_size=(height, width)` of the screen, aka the image.
73+
74+
The `get_ndc_camera_transform` provides the transform from screen to NDC space in PyTorch3D. Note that the screen space assumes that the principal point is provided in the space with `+X left`, `+Y down` and origin at the top left corner of the image. To convert to NDC we need to account for the scaling of the normalized space as well as the change in `XY` direction.
75+
76+
Below are example of equivalent `PerspectiveCameras` instantiations in NDC and screen space, respectively.
77+
78+
```python
79+
# NDC space camera
80+
fcl_ndc = (1.2,)
81+
prp_ndc = ((0.2, 0.5),)
82+
cameras_ndc = PerspectiveCameras(focal_length=fcl_ndc, principal_point=prp_ndc)
83+
84+
# Screen space camera
85+
image_size = ((128, 256),) # (h, w)
86+
fcl_screen = (76.2,) # fcl_ndc * (min(image_size) - 1) / 2
87+
prp_screen = ((114.8, 31.75), ) # (w - 1) / 2 - px_ndc * (min(image_size) - 1) / 2, (h - 1) / 2 - py_ndc * (min(image_size) - 1) / 2
88+
cameras_screen = PerspectiveCameras(focal_length=fcl_screen, principal_point=prp_screen, in_ndc=False, image_size=image_size)
5889
```
5990

60-
Commonly, users have access to the focal length (`fx_screen`, `fy_screen`) and the principal point (`px_screen`, `py_screen`) in screen space. In that case, to construct the camera the user needs to additionally provide the `image_size = ((image_width, image_height),)`. More precisely, `camera = PerspectiveCameras(focal_length=((fx_screen, fy_screen),), principal_point=((px_screen, py_screen),), image_size = ((image_width, image_height),))`. Internally, the camera parameters are converted from screen to NDC as follows:
91+
The relationship between screen and NDC specifications of a camera's `focal_length` and `principal_point` is given by the following equations, where `s = min(image_width, image_height)`.
92+
The transformation of x and y coordinates between screen and NDC is exactly the same as for px and py.
6193

6294
```
63-
fx = fx_screen * 2.0 / image_width
64-
fy = fy_screen * 2.0 / image_height
95+
fx_ndc = fx_screen * 2.0 / (s - 1)
96+
fy_ndc = fy_screen * 2.0 / (s - 1)
6597
66-
px = - (px_screen - image_width / 2.0) * 2.0 / image_width
67-
py = - (py_screen - image_height / 2.0) * 2.0/ image_height
98+
px_ndc = - (px_screen - (image_width - 1) / 2.0) * 2.0 / (s - 1)
99+
py_ndc = - (py_screen - (image_height - 1) / 2.0) * 2.0 / (s - 1)
68100
```

0 commit comments

Comments
 (0)