depth_points.py 2.82 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
4
5
6
from typing import Optional, Sequence, Union

import numpy as np
from torch import Tensor

7
8
9
10
11
12
13
from .base_points import BasePoints


class DepthPoints(BasePoints):
    """Points of instances in DEPTH coordinates.

    Args:
14
15
16
17
18
19
        tensor (Tensor or np.ndarray or Sequence[Sequence[float]]): The points
            data with shape (N, points_dim).
        points_dim (int): Integer indicating the dimension of a point. Each row
            is (x, y, z, ...). Defaults to 3.
        attribute_dims (dict, optional): Dictionary to indicate the meaning of
            extra dimension. Defaults to None.
20
21

    Attributes:
22
23
24
25
26
        tensor (Tensor): Float matrix with shape (N, points_dim).
        points_dim (int): Integer indicating the dimension of a point. Each row
            is (x, y, z, ...).
        attribute_dims (dict, optional): Dictionary to indicate the meaning of
            extra dimension. Defaults to None.
27
        rotation_axis (int): Default rotation axis for points rotation.
28
29
    """

30
31
32
33
    def __init__(self,
                 tensor: Union[Tensor, np.ndarray, Sequence[Sequence[float]]],
                 points_dim: int = 3,
                 attribute_dims: Optional[dict] = None) -> None:
34
35
        super(DepthPoints, self).__init__(
            tensor, points_dim=points_dim, attribute_dims=attribute_dims)
36
        self.rotation_axis = 2
37

38
    def flip(self, bev_direction: str = 'horizontal') -> None:
39
40
41
42
        """Flip the points along given BEV direction.

        Args:
            bev_direction (str): Flip direction (horizontal or vertical).
43
                Defaults to 'horizontal'.
44
        """
45
        assert bev_direction in ('horizontal', 'vertical')
46
47
48
49
50
        if bev_direction == 'horizontal':
            self.tensor[:, 0] = -self.tensor[:, 0]
        elif bev_direction == 'vertical':
            self.tensor[:, 1] = -self.tensor[:, 1]

51
52
53
54
    def convert_to(self,
                   dst: int,
                   rt_mat: Optional[Union[Tensor,
                                          np.ndarray]] = None) -> 'BasePoints':
55
56
57
        """Convert self to ``dst`` mode.

        Args:
58
59
            dst (int): The target Point mode.
            rt_mat (Tensor or np.ndarray, optional): The rotation and
60
                translation matrix between different coordinates.
61
62
63
64
                Defaults to None. The conversion from ``src`` coordinates to
                ``dst`` coordinates usually comes along the change of sensors,
                e.g., from camera to LiDAR. This requires a transformation
                matrix.
65
66

        Returns:
67
68
            :obj:`BasePoints`: The converted point of the same type in the
            ``dst`` mode.
69
        """
70
        from mmdet3d.structures.bbox_3d import Coord3DMode
71
72
        return Coord3DMode.convert_point(
            point=self, src=Coord3DMode.DEPTH, dst=dst, rt_mat=rt_mat)