torchdrivesim.mesh#

Custom classes for representing various triangular meshes as tensors.

Module Contents#

Classes#

BaseMesh

Basic triangle mesh in a space of arbitrary dimensions Dim.

AttributeMesh

Endows each vertex with an attribute.

RGBMesh

AttributeMesh where the attribute is an RGB color in [0,1] range.

BirdviewMesh

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

Functions#

tensor_color(→ torch.Tensor)

Converts all supported color representations to the tensor representation.

rendering_mesh(→ BirdviewMesh)

Assigns a category to a given mesh.

generate_trajectory_mesh(→ BaseMesh)

Create a triangle mesh used to visualize a given trajectory.

generate_annulus_polygon_mesh(→ BaseMesh)

For a given polygon, generates a mesh covering the space between the polygon

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

For a given radius, it will create a disc mesh using num_triangles triangles.

Attributes#

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

Converts all supported color representations to the tensor representation.

Parameters:
  • 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

Returns:

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

exception torchdrivesim.mesh.BadMeshFormat[source]#

Bases: RuntimeError

The mesh received had the wrong format, usually indicating loading from old cache.

class torchdrivesim.mesh.BaseMesh[source]#

Basic triangle mesh in a space of arbitrary dimensions Dim. Only specifies triangles, with no additional properties. Always includes exactly one batch dimension.

property dim: int[source]#

Dimension of the space in which the mesh lives, usually 2 or 3.

property batch_size: int[source]#
property verts_count: int[source]#
property faces_count: int[source]#
property device: torch.device[source]#
property center: torch.Tensor[source]#

A BxDim center of the mesh, calculated as the average of min and max vertex coordinates for each dimension. Note that if the vertices are padded, the padding value may distort this result.

verts: torch.Tensor[source]#
faces: torch.Tensor[source]#
_verts_fill: float[source]#
_faces_fill: int[source]#
__post_init__()[source]#
_validate_input()[source]#
to(device: torch.device)[source]#

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

clone()[source]#

Deep copy of the mesh.

expand(size: int)[source]#

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

select_batch_elements(idx: torch.Tensor)[source]#

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

__getitem__(item)[source]#
classmethod collate(meshes)[source]#

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

classmethod concat(meshes)[source]#

Concatenates multiple meshes to form a single scene.

merge(other)[source]#

Merges the current mesh with another to form a single scene, returning a new mesh.

offset(offset: torch.Tensor)[source]#

Shifts the mesh by a given distance, returning a new mesh. Missing dimensions in the argument are padded with zeros if needed.

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.

pickle(mesh_file_path: str)[source]#

Store this mesh to a given file.

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

Load a mesh of this type from the given file.

serialize()[source]#
save(file_save_path: str)[source]#

Save the attributes of the mesh object in a json where tensors are converted to lists.

classmethod _deserialize_tensors(data: Dict) Dict[source]#

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

classmethod deserialize(data: Dict) typing_extensions.Self[source]#
classmethod load(filepath)[source]#
classmethod empty(dim: int = 2, batch_size: int = 1)[source]#

Create empty mesh.

_trim_and_return_verts_and_faces(vertices_to_keep: torch.Tensor, trim_face_only=False)[source]#
trim(polygon: torch.Tensor, trim_face_only: bool = 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.

Parameters:
  • 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

class torchdrivesim.mesh.AttributeMesh[source]#

Bases: BaseMesh

Endows each vertex with an attribute. An attribute is a vector of arbitrary dimension Attr, usually a color or something similar. Typically, in any given face all vertices have the same attribute values.

property attr_dim: int[source]#

Size of the attribute dimension.

attrs: torch.Tensor[source]#
_attrs_fill: float[source]#
_validate_input()[source]#
classmethod set_attr(mesh: BaseMesh, attr: torch.Tensor)[source]#

Sets a given attribute value for all vertices in a given mesh.

to(device: torch.device)[source]#

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

expand(size: int)[source]#

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

select_batch_elements(idx: int)[source]#

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

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.

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

PyTorch3D uses per-face textures, which are obtained by averaging attributes of the face. The resulting texture for each face is constant.

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

Load a mesh of this type from the given file.

serialize()[source]#
classmethod _deserialize_tensors(data: Dict) Dict[source]#

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

classmethod load(filepath)[source]#
classmethod empty(dim=2, batch_size=1, attr_dim=3)[source]#

Create empty mesh.

trim(polygon: torch.Tensor, trim_face_only: bool = 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.

Parameters:
  • 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

class torchdrivesim.mesh.RGBMesh[source]#

Bases: AttributeMesh

AttributeMesh where the attribute is an RGB color in [0,1] range.

_validate_input()[source]#
classmethod set_color(mesh: BaseMesh, color: Color)[source]#

Sets a constant color for all vertices in a given mesh.

class torchdrivesim.mesh.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.

property num_categories: int[source]#
categories: List[str][source]#
colors: Dict[str, torch.Tensor][source]#
zs: Dict[str, float][source]#
vert_category: torch.Tensor[source]#
_cat_fill: int = 0[source]#
_validate_input()[source]#
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.

to(device)[source]#

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

expand(size)[source]#

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

select_batch_elements(idx)[source]#

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.

serialize()[source]#
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.

Parameters:
  • 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.mesh.rendering_mesh(mesh: BaseMesh, category: str) BirdviewMesh[source]#

Assigns a category to a given mesh.

torchdrivesim.mesh.generate_trajectory_mesh(points: torch.Tensor, category: str | None = None, edge_length: float = 1) BaseMesh[source]#

Create a triangle mesh used to visualize a given trajectory. Each point is converted to a triangle matching its position and orientation.

Parameters:
  • points – Bx3 tensor of x-y coordinates and orientations in radians

  • category – if specified, produces BirdviewMesh

  • edge_length – specifies the size of the resulting triangle

torchdrivesim.mesh.generate_annulus_polygon_mesh(polygon: torch.Tensor, scaling_factor: float, origin: torch.Tensor, category: str | None = None) BaseMesh[source]#

For a given polygon, generates a mesh covering the space between the polygon and its scaled version.

Parameters:
  • polygon – tensor of size Nx2 defining subsequent points of the polygon hull

  • scaling_factor – determines the side of the annulus, should be larger than 1

  • origin – tensor of size (2,) defining the point around which scaling is performed

  • category – if specified, BirdviewMesh will be returned

torchdrivesim.mesh.generate_disc_mesh(radius: float = 2, num_triangles: int = 10, device: str = 'cpu') Tuple[torch.Tensor, torch.Tensor][source]#

For a given radius, it will create a disc mesh using num_triangles triangles.

Parameters:
  • radius – float defining the radius of the disc

  • num_triangles – int defining the number of triangles to be used for creating the disc

  • device – the device to be used for the generated PyTorch tensors