torchdrivesim.utils#

Miscellaneous utilities, including for geometric operations on tensors.

Module Contents#

Functions#

isin(→ torch.Tensor)

Checks whether elements of tensor x are contained in tensor y.

normalize_angle(angle)

Normalize to <-pi, pi) range by shifting by a multiple of 2*pi.

rotation_matrix(→ torch.Tensor)

Counterclockwise rotation matrix in 2D.

rotate(→ torch.Tensor)

Rotate the vector counterclockwise (from x towards y).

relative(→ Tuple[torch.Tensor, torch.Tensor])

Computes position and orientation of the target relative to origin.

is_inside_polygon(→ torch.Tensor)

Checks whether a given point is inside a given convex polygon.

merge_dicts(→ Dict)

Merges a sequence of dictionaries, giving preference to entries earlier in the sequence.

assert_equal(x, y)

Attributes#

torchdrivesim.utils.Resolution[source]#
torchdrivesim.utils.isin(x: torch.Tensor, y: torch.Tensor) torch.Tensor[source]#

Checks whether elements of tensor x are contained in tensor y. This function is built-in in torch >= 1.10 and will be removed from here in the future.

Parameters:
  • x – any tensor

  • y – a one-dimensional tensor

Returns:

a boolean tensor with the same shape as x

torchdrivesim.utils.normalize_angle(angle)[source]#

Normalize to <-pi, pi) range by shifting by a multiple of 2*pi. Works with floats, numpy arrays, and torch tensors.

torchdrivesim.utils.rotation_matrix(theta: torch.Tensor) torch.Tensor[source]#

Counterclockwise rotation matrix in 2D.

Parameters:

theta – tensor of shape Sx1 with rotation angle in radians

Returns:

Sx2x2 tensor with the rotation matrix.

torchdrivesim.utils.rotate(v: torch.Tensor, angle: torch.Tensor) torch.Tensor[source]#

Rotate the vector counterclockwise (from x towards y). Works correctly in batch mode.

Parameters:
  • v – tensor of shape Sx2 representing points

  • angle – tensor of shape Sx1 representing rotation angle

Returns:

Sx2 tensor of rotated points

torchdrivesim.utils.relative(origin_xy: torch.Tensor, origin_psi: torch.Tensor, target_xy: torch.Tensor, target_psi: torch.Tensor) Tuple[torch.Tensor, torch.Tensor][source]#

Computes position and orientation of the target relative to origin. Points are represented as Sx2 tensors of coordinates and Sx1 tensors of orientations in radians.

torchdrivesim.utils.is_inside_polygon(point: torch.Tensor, polygon: torch.Tensor) torch.Tensor[source]#

Checks whether a given point is inside a given convex polygon. B and P can be zero or more batch dimensions, the former being batch and the latter points.

Parameters:
  • point – BxPx2 tensor of x-y coordinates

  • polygon – BxNx2 tensor of points specifying a convex polygon in either clockwise or counter-clockwise fashion.

Returns:

boolean tensor of shape BxP indicating whether the point is inside the polygon

torchdrivesim.utils.merge_dicts(ds: List[Dict]) Dict[source]#

Merges a sequence of dictionaries, giving preference to entries earlier in the sequence.

torchdrivesim.utils.assert_equal(x, y)[source]#