fastdev.xform.utils

Module Contents

fastdev.xform.utils.coord_conversion(src_spec: str, dst_spec: str, check_handness: bool = True, return_tensors: Literal['pt'] = 'pt') jaxtyping.Float[torch.Tensor, 3 3][source]
fastdev.xform.utils.coord_conversion(src_spec: str, dst_spec: str, check_handness: bool = True, return_tensors: Literal['np'] = 'np') jaxtyping.Float[numpy.ndarray, 3 3]

Construct a rotation matrix based on given source and destination coordinate specifications.

Parameters:
  • src_spec – Source coordinate specification, e.g., “x: right, y: down, z: front” or “opencv”.

  • dst_spec – Destination coordinate specification, e.g., “x: right, y: up, z: back” or “opengl”.

  • check_handness – If True, checks if the rotation matrix preserves right-handedness.

  • return_tensors – Return type of the rotation matrix, either “np” for NumPy array or “pt” for PyTorch tensor.

Returns:

A 3x3 rotation matrix converting coordinates from the source to the destination specification.

Examples

>>> coord_conversion("opencv", "opengl")
array([[ 1.,  0.,  0.],
       [ 0., -1.,  0.],
       [ 0.,  0., -1.]], dtype=float32)
>>> coord_conversion("x: front, y: left, z: up", "x: left, y: up, z: front")
array([[0., 1., 0.],
       [0., 0., 1.],
       [1., 0., 0.]], dtype=float32)
>>> coord_conversion("x: right, y: down, z: front", "x: left, y: up, z: front")
array([[-1.,  0.,  0.],
       [ 0., -1.,  0.],
       [ 0.,  0.,  1.]], dtype=float32)
>>> coord_conversion("x: left, y: up, z: front", "x: front, y: left, z: up", return_tensors="pt")
tensor([[0., 0., 1.],
        [1., 0., 0.],
        [0., 1., 0.]])
fastdev.xform.utils.compose_intr_mat(fu: float, fv: float, cu: float, cv: float, skew: float = 0.0) numpy.ndarray[source]
Parameters:
  • fu (float) – horizontal focal length (width)

  • fv (float) – vertical focal length (height)

  • cu (float) – horizontal principal point (width)

  • cv (float) – vertical principal point (height)

  • skew (float) – skew coefficient, default to 0

Return type:

numpy.ndarray

fastdev.xform.utils.look_at_rotation(camera_position: jaxtyping.Float[torch.Tensor, *batch 3], at: jaxtyping.Float[torch.Tensor, *batch 3], up: jaxtyping.Float[torch.Tensor, *batch 3]) jaxtyping.Float[torch.Tensor, *batch 3 3][source]

This function takes a vector camera_position which specifies the location of the camera in world coordinates and two vectors at and up which indicate the position of the object and the up directions of the world coordinate system respectively.

The output is a rotation matrix representing the rotation from camera coordinates to world coordinates.

We use the OpenGL coordinate in this function, i.e. x -> right, y -> up, z -> backward. Hence, z_axis: pos - at, x_axis: cross(up, z_axis), y_axis: cross(z_axis, x_axis)

Note that our implementation differs from pytorch3d.
  1. our matrix is in the OpenGL coordinate

  2. our matrix is column-major

  3. our matrix is the camera-to-world transformation

Parameters:
  • camera_position (jaxtyping.Float[torch.Tensor, *batch 3]) – position of the camera in world coordinates

  • at (jaxtyping.Float[torch.Tensor, *batch 3]) – position of the object in world coordinates

  • up (jaxtyping.Float[torch.Tensor, *batch 3]) – vector specifying the up direction in the world coordinate frame.

Returns:

rotation matrices of shape […, 3, 3]

Return type:

R

fastdev.xform.utils.camera_position_from_spherical_angles(distance: float, elevation: float, azimuth: float, degrees: bool = True, device: Device = 'cpu') torch.Tensor[source]

Calculate the location of the camera based on the distance away from the target point, the elevation and azimuth angles.

Parameters:
  • distance (float) – distance of the camera from the object.

  • elevation (float) –

    angles. The inputs distance, elevation and azimuth can be one of the following

    • Python scalar

    • Torch scalar

    • Torch tensor of shape (N) or (1)

  • azimuth (float) –

    angles. The inputs distance, elevation and azimuth can be one of the following

    • Python scalar

    • Torch scalar

    • Torch tensor of shape (N) or (1)

  • degrees (bool) – bool, whether the angles are specified in degrees or radians.

  • device (Device) – str or torch.device, device for new tensors to be placed on.

Return type:

torch.Tensor

The vectors are broadcast against each other so they all have shape (N, 1).

Returns:

(N, 3) xyz location of the camera.

Return type:

camera_position

Parameters:
  • distance (float)

  • elevation (float)

  • azimuth (float)

  • degrees (bool)

  • device (Device)