torchdrivesim.mesh
#
Custom classes for representing various triangular meshes as tensors.
Module Contents#
Classes#
Basic triangle mesh in a space of arbitrary dimensions Dim. |
|
Endows each vertex with an attribute. |
|
AttributeMesh where the attribute is an RGB color in [0,1] range. |
|
2D mesh with vertices and faces assigned to discrete categories, to facilitate rendering simple 2D worlds. |
Functions#
|
Converts all supported color representations to the tensor representation. |
|
Assigns a category to a given mesh. |
|
Create a triangle mesh used to visualize a given trajectory. |
|
For a given polygon, generates a mesh covering the space between the polygon |
|
For a given radius, it will create a disc mesh using num_triangles triangles. |
Attributes#
- 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 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.
- 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: torch.Tensor)[source]#
Selects given indices from batch dimension, possibly with repetitions, returning a new mesh
- classmethod collate(meshes)[source]#
Batches a collection of meshes with appropriate padding. All input meshes must have a singleton batch dimension.
- 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.
- classmethod unpickle(mesh_file_path: str, pickle_module: Any = default_pickle_module)[source]#
Load a mesh of this type from the given file.
- 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
- 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.
- 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 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.
- classmethod _deserialize_tensors(data: Dict) Dict [source]#
Convert list attributes to tensor attributes if there is any tensor attribute
- 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.
- 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.
- 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 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
- 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
- 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