torchdrivesim.mesh ================== .. py:module:: torchdrivesim.mesh .. autoapi-nested-parse:: Custom classes for representing various triangular meshes as tensors. Attributes ---------- .. autoapisummary:: torchdrivesim.mesh.logger torchdrivesim.mesh.Color Exceptions ---------- .. autoapisummary:: torchdrivesim.mesh.BadMeshFormat Classes ------- .. autoapisummary:: torchdrivesim.mesh.BaseMesh torchdrivesim.mesh.AttributeMesh torchdrivesim.mesh.RGBMesh torchdrivesim.mesh.BirdviewMesh Functions --------- .. autoapisummary:: torchdrivesim.mesh.tensor_color torchdrivesim.mesh.rendering_mesh torchdrivesim.mesh.generate_trajectory_mesh torchdrivesim.mesh.generate_annulus_polygon_mesh torchdrivesim.mesh.generate_disc_mesh Module Contents --------------- .. py:data:: logger .. py:data:: Color .. py:function:: tensor_color(color: Color, device: torch.device = 'cpu', dtype=torch.float) -> torch.Tensor Converts all supported color representations to the tensor representation. :param color: RGB color as either an int 3-tuple in [0,255] range or tensor of shape (3,) in [0,1] range :param device: device to place the resulting tensor on, ignored if color is already a tensor :param dtype: element type for the resulting tensor, ignored if color is already a tensor :returns: RGB tensor of shape (3,) in [0,1] range .. py:exception:: BadMeshFormat Bases: :py:obj:`RuntimeError` The mesh received had the wrong format, usually indicating loading from old cache. .. py:class:: BaseMesh Basic triangle mesh in a space of arbitrary dimensions Dim. Only specifies triangles, with no additional properties. Always includes exactly one batch dimension. .. py:attribute:: verts :type: torch.Tensor .. py:attribute:: faces :type: torch.Tensor .. py:attribute:: _verts_fill :type: float .. py:attribute:: _faces_fill :type: int .. py:method:: __post_init__() .. py:method:: _validate_input() .. py:property:: dim :type: int Dimension of the space in which the mesh lives, usually 2 or 3. .. py:property:: batch_size :type: int .. py:property:: verts_count :type: int .. py:property:: faces_count :type: int .. py:property:: device :type: torch.device .. py:property:: center :type: torch.Tensor 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. .. py:method:: to(device: torch.device) Moves all tensors to a given device, returning a new mesh object. Does not modify the existing mesh object. .. py:method:: clone() Deep copy of the mesh. .. py:method:: expand(size: int) Expands batch dimension on the right by the given factor, returning a new mesh. .. py:method:: select_batch_elements(idx: torch.Tensor) Selects given indices from batch dimension, possibly with repetitions, returning a new mesh .. py:method:: __getitem__(item) .. py:method:: collate(meshes) :classmethod: Batches a collection of meshes with appropriate padding. All input meshes must have a singleton batch dimension. .. py:method:: concat(meshes) :classmethod: Concatenates multiple meshes to form a single scene. .. py:method:: merge(other) Merges the current mesh with another to form a single scene, returning a new mesh. .. py:method:: offset(offset: torch.Tensor) Shifts the mesh by a given distance, returning a new mesh. Missing dimensions in the argument are padded with zeros if needed. .. py:method:: pytorch3d(include_textures=True) -> pytorch3d.structures.Meshes 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. .. py:method:: pickle(mesh_file_path: str) Store this mesh to a given file. .. py:method:: unpickle(mesh_file_path: str, pickle_module: Any = default_pickle_module) :classmethod: Load a mesh of this type from the given file. .. py:method:: serialize() .. py:method:: save(file_save_path: str) Save the attributes of the mesh object in a json where tensors are converted to lists. .. py:method:: _deserialize_tensors(data: Dict) -> Dict :classmethod: Convert list attributes to tensor attributes if there is any tensor attribute .. py:method:: deserialize(data: Dict) -> typing_extensions.Self :classmethod: .. py:method:: load(filepath) :classmethod: .. py:method:: empty(dim: int = 2, batch_size: int = 1) :classmethod: Create empty mesh. .. py:method:: _trim_and_return_verts_and_faces(vertices_to_keep: torch.Tensor, trim_face_only=False) .. py:method:: trim(polygon: torch.Tensor, trim_face_only: bool = False) 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. :param polygon: BxPx2 tensor specifying a convex polygon in either clockwise or counterclockwise fashion :param trim_face_only: whether to keep all vertices, including those unused by any faces .. py:class:: AttributeMesh Bases: :py:obj:`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. .. py:attribute:: attrs :type: torch.Tensor .. py:attribute:: _attrs_fill :type: float .. py:method:: _validate_input() .. py:property:: attr_dim :type: int Size of the attribute dimension. .. py:method:: set_attr(mesh: BaseMesh, attr: torch.Tensor) :classmethod: Sets a given attribute value for all vertices in a given mesh. .. py:method:: to(device: torch.device) Moves all tensors to a given device, returning a new mesh object. Does not modify the existing mesh object. .. py:method:: expand(size: int) Expands batch dimension on the right by the given factor, returning a new mesh. .. py:method:: select_batch_elements(idx: int) Selects given indices from batch dimension, possibly with repetitions, returning a new mesh .. py:method:: concat(meshes) :classmethod: Concatenates multiple meshes to form a single scene. .. py:method:: collate(meshes) :classmethod: Batches a collection of meshes with appropriate padding. All input meshes must have a singleton batch dimension. .. py:method:: pytorch3d(include_textures=True) -> pytorch3d.structures.Meshes PyTorch3D uses per-face textures, which are obtained by averaging attributes of the face. The resulting texture for each face is constant. .. py:method:: unpickle(mesh_file_path: str, pickle_module: Any = default_pickle_module) :classmethod: Load a mesh of this type from the given file. .. py:method:: serialize() .. py:method:: _deserialize_tensors(data: Dict) -> Dict :classmethod: Convert list attributes to tensor attributes if there is any tensor attribute .. py:method:: load(filepath) :classmethod: .. py:method:: empty(dim=2, batch_size=1, attr_dim=3) :classmethod: Create empty mesh. .. py:method:: trim(polygon: torch.Tensor, trim_face_only: bool = False) 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. :param polygon: BxPx2 tensor specifying a convex polygon in either clockwise or counterclockwise fashion :param trim_face_only: whether to keep all vertices, including those unused by any faces .. py:class:: RGBMesh Bases: :py:obj:`AttributeMesh` AttributeMesh where the attribute is an RGB color in [0,1] range. .. py:method:: _validate_input() .. py:method:: set_color(mesh: BaseMesh, color: Color) :classmethod: Sets a constant color for all vertices in a given mesh. .. py:class:: BirdviewMesh Bases: :py:obj:`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. .. py:attribute:: categories :type: List[str] .. py:attribute:: colors :type: Dict[str, torch.Tensor] .. py:attribute:: zs :type: Dict[str, float] .. py:attribute:: vert_category :type: torch.Tensor .. py:attribute:: _cat_fill :type: int :value: 0 .. py:method:: _validate_input() .. py:property:: num_categories :type: int .. py:method:: set_properties(mesh: BaseMesh, category: str, color: Optional[Color] = None, z: Optional[float] = None) :classmethod: Lifts a BaseMesh into a BirdviewMesh with a single category. .. py:method:: to(device) Moves all tensors to a given device, returning a new mesh object. Does not modify the existing mesh object. .. py:method:: expand(size) Expands batch dimension on the right by the given factor, returning a new mesh. .. py:method:: select_batch_elements(idx) Selects given indices from batch dimension, possibly with repetitions, returning a new mesh .. py:method:: unify(meshes) :classmethod: Generates meshes equivalent to input meshes, only all sharing the same category definitions. .. py:method:: concat(meshes) :classmethod: Concatenates multiple meshes to form a single scene. .. py:method:: collate(meshes) :classmethod: Batches a collection of meshes with appropriate padding. All input meshes must have a singleton batch dimension. .. py:method:: fill_attr() -> RGBMesh Computes explicit color for each vertex and augments vertices with z values corresponding to rendering priority. .. py:method:: pytorch3d(include_textures=True) -> pytorch3d.structures.Meshes 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. .. py:method:: unpickle(mesh_file_path: str, pickle_module: Any = default_pickle_module) :classmethod: Load a mesh of this type from the given file. .. py:method:: serialize() .. py:method:: _deserialize_tensors(data: Dict) -> Dict :classmethod: Convert list attributes to tensor attributes if there is any tensor attribute .. py:method:: empty(dim=2, batch_size=1) :classmethod: Create empty mesh. .. py:method:: trim(polygon: torch.Tensor, trim_face_only=False) 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. :param polygon: BxPx2 tensor specifying a convex polygon in either clockwise or counterclockwise fashion :param trim_face_only: whether to keep all vertices, including those unused by any faces .. py:method:: separate_by_category() -> Dict[str, BaseMesh] Splits the mesh into meshes representing different categories. .. py:function:: rendering_mesh(mesh: BaseMesh, category: str) -> BirdviewMesh Assigns a category to a given mesh. .. py:function:: generate_trajectory_mesh(points: torch.Tensor, category: Optional[str] = None, edge_length: float = 1) -> BaseMesh Create a triangle mesh used to visualize a given trajectory. Each point is converted to a triangle matching its position and orientation. :param points: Bx3 tensor of x-y coordinates and orientations in radians :param category: if specified, produces BirdviewMesh :param edge_length: specifies the size of the resulting triangle .. py:function:: generate_annulus_polygon_mesh(polygon: torch.Tensor, scaling_factor: float, origin: torch.Tensor, category: Optional[str] = None) -> BaseMesh For a given polygon, generates a mesh covering the space between the polygon and its scaled version. :param polygon: tensor of size Nx2 defining subsequent points of the polygon hull :param scaling_factor: determines the side of the annulus, should be larger than 1 :param origin: tensor of size (2,) defining the point around which scaling is performed :param category: if specified, BirdviewMesh will be returned .. py:function:: generate_disc_mesh(radius: float = 2, num_triangles: int = 10, device: str = 'cpu') -> Tuple[torch.Tensor, torch.Tensor] For a given radius, it will create a disc mesh using `num_triangles` triangles. :param radius: float defining the radius of the disc :param num_triangles: int defining the number of triangles to be used for creating the disc :param device: the device to be used for the generated PyTorch tensors