2D mesh with vertices and faces assigned to discrete categories, to facilitate rendering simple 2D worlds.


Lightweight version of pytorch3d.renderer.FoVOrthographicCameras.


Determines behavior of the renderer.


Renderer based on OpenCV. Slow, but easy to install. Renders on CPU.


tensor_color(→ torch.Tensor)

Converts all supported color representations to the tensor representation.

Module Contents

class torchdrivesim.rendering.cv2.BirdviewMesh[source]

Bases: BaseMesh

2D mesh with vertices and faces assigned to discrete categories, to facilitate rendering simple 2D worlds. Category assignment is stored per vertex, and faces should not mix vertices from different categories. For each category there is a color and a rendering priority z (lower renders on top), but those don’t need to be specified until the mesh is converted to a different representation.

categories: List[str]
colors: Dict[str, torch.Tensor]
zs: Dict[str, float]
vert_category: torch.Tensor
_cat_fill: int = 0
property num_categories: int
classmethod set_properties(mesh: BaseMesh, category: str, color: Color | None = None, z: float | None = None)[source]

Lifts a BaseMesh into a BirdviewMesh with a single category.


Moves all tensors to a given device, returning a new mesh object. Does not modify the existing mesh object.


Expands batch dimension on the right by the given factor, returning a new mesh.


Selects given indices from batch dimension, possibly with repetitions, returning a new mesh

classmethod unify(meshes)[source]

Generates meshes equivalent to input meshes, only all sharing the same category definitions.

classmethod concat(meshes)[source]

Concatenates multiple meshes to form a single scene.

classmethod collate(meshes)[source]

Batches a collection of meshes with appropriate padding. All input meshes must have a singleton batch dimension.

fill_attr() RGBMesh[source]

Computes explicit color for each vertex and augments vertices with z values corresponding to rendering priority.

pytorch3d(include_textures=True) pytorch3d.structures.Meshes[source]

Converts the mesh to a PyTorch3D one. For the base class there are no textures, but subclasses may include them. Empty meshes are augmented with a single degenerate face on conversion, since PyTorch3D does not handle empty meshes correctly.

classmethod unpickle(mesh_file_path: str, pickle_module: Any = default_pickle_module)[source]

Load a mesh of this type from the given file.

classmethod _deserialize_tensors(data: Dict) Dict[source]

Convert list attributes to tensor attributes if there is any tensor attribute

classmethod empty(dim=2, batch_size=1)[source]

Create empty mesh.

trim(polygon: torch.Tensor, trim_face_only=False)[source]

Crops the mesh to a given 2D convex polygon, returning a new mesh. Faces where all vertices are outside the polygon are removed, even if the triangle intersects with the polygon. Vertices are removed if they are not used by any face.

  • polygon – BxPx2 tensor specifying a convex polygon in either clockwise or counterclockwise fashion

  • trim_face_only – whether to keep all vertices, including those unused by any faces

separate_by_category() Dict[str, BaseMesh][source]

Splits the mesh into meshes representing different categories.

torchdrivesim.rendering.cv2.tensor_color(color: Color, device: torch.device = 'cpu', dtype=torch.float) torch.Tensor[source]

Converts all supported color representations to the tensor representation.

  • color – RGB color as either an int 3-tuple in [0,255] range or tensor of shape (3,) in [0,1] range

  • device – device to place the resulting tensor on, ignored if color is already a tensor

  • dtype – element type for the resulting tensor, ignored if color is already a tensor


RGB tensor of shape (3,) in [0,1] range

class torchdrivesim.rendering.cv2.Cameras(xy: torch.Tensor, sc: torch.Tensor, scale: float)[source]

Lightweight version of pytorch3d.renderer.FoVOrthographicCameras. Used to define an interface that works without pytorch3d.

get_camera_center() torch.Tensor[source]
get_world_to_view_transform() torch.Tensor[source]
get_view_to_proj_transform() torch.Tensor[source]
project_world_to_clip_space(points: torch.Tensor) torch.Tensor[source]
transform_points_screen(points: torch.Tensor, res: torchdrivesim.utils.Resolution) torch.Tensor[source]
reverse_transform_points_screen(points: torch.Tensor, res: torchdrivesim.utils.Resolution) torch.Tensor[source]
class torchdrivesim.rendering.cv2.CV2RendererConfig[source]

Bases: torchdrivesim.rendering.RendererConfig

Determines behavior of the renderer. Subclasses determine renderer class used.

backend: str = 'cv2'[source]
trim_mesh_before_rendering: bool = True[source]
class torchdrivesim.rendering.cv2.CV2Renderer(cfg: CV2RendererConfig, *args, **kwargs)[source]

Bases: torchdrivesim.rendering.BirdviewRenderer

Renderer based on OpenCV. Slow, but easy to install. Renders on CPU.

render_mesh(mesh: torchdrivesim.mesh.BirdviewMesh, res: torchdrivesim.utils.Resolution, cameras: torchdrivesim.rendering.base.Cameras) torch.Tensor[source]

Renders a given mesh, producing BxHxWxC tensor image of float RGB values in [0,255] range.