|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Dict, Optional, Type |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import numpy.typing as npt |
| 9 | + |
| 10 | +from d123.common.datatypes.sensor.lidar_index import LIDAR_INDEX_REGISTRY, LiDARIndex |
| 11 | +from d123.common.utils.enums import SerialIntEnum |
| 12 | + |
| 13 | + |
| 14 | +class LiDARType(SerialIntEnum): |
| 15 | + |
| 16 | + LIDAR_UNKNOWN = 0 |
| 17 | + LIDAR_MERGED = 1 |
| 18 | + LIDAR_TOP = 2 |
| 19 | + LIDAR_FRONT = 3 |
| 20 | + LIDAR_SIDE_LEFT = 4 |
| 21 | + LIDAR_SIDE_RIGHT = 5 |
| 22 | + LIDAR_BACK = 6 |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class LiDARMetadata: |
| 27 | + |
| 28 | + lidar_type: LiDARType |
| 29 | + lidar_index: Type[LiDARIndex] |
| 30 | + extrinsic: Optional[npt.NDArray[np.float64]] = None # 4x4 matrix |
| 31 | + |
| 32 | + # TODO: add identifier if point cloud is returned in lidar or ego frame. |
| 33 | + |
| 34 | + def to_dict(self) -> dict: |
| 35 | + return { |
| 36 | + "lidar_type": self.lidar_type.name, |
| 37 | + "lidar_index": self.lidar_index.__name__, |
| 38 | + "extrinsic": self.extrinsic.tolist() if self.extrinsic is not None else None, |
| 39 | + } |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def from_dict(cls, json_dict: dict) -> LiDARMetadata: |
| 43 | + lidar_type = LiDARType[json_dict["lidar_type"]] |
| 44 | + if json_dict["lidar_index"] not in LIDAR_INDEX_REGISTRY: |
| 45 | + raise ValueError(f"Unknown lidar index: {json_dict['lidar_index']}") |
| 46 | + lidar_index_class = LIDAR_INDEX_REGISTRY[json_dict["lidar_index"]] |
| 47 | + extrinsic = np.array(json_dict["extrinsic"]) if json_dict["extrinsic"] is not None else None |
| 48 | + return cls(lidar_type=lidar_type, lidar_index=lidar_index_class, extrinsic=extrinsic) |
| 49 | + |
| 50 | + |
| 51 | +def lidar_metadata_dict_to_json(lidar_metadata: Dict[LiDARType, LiDARMetadata]) -> str: |
| 52 | + """ |
| 53 | + Converts a dictionary of LiDARMetadata to a JSON-serializable format. |
| 54 | + :param lidar_metadata: Dictionary of LiDARMetadata. |
| 55 | + :return: JSON string. |
| 56 | + """ |
| 57 | + lidar_metadata_dict = { |
| 58 | + lidar_type.serialize(): metadata.to_dict() for lidar_type, metadata in lidar_metadata.items() |
| 59 | + } |
| 60 | + return json.dumps(lidar_metadata_dict) |
| 61 | + |
| 62 | + |
| 63 | +def lidar_metadata_dict_from_json(json_str: str) -> Dict[LiDARType, LiDARMetadata]: |
| 64 | + """ |
| 65 | + Converts a JSON string back to a dictionary of LiDARMetadata. |
| 66 | + :param json_str: JSON string. |
| 67 | + :return: Dictionary of LiDARMetadata. |
| 68 | + """ |
| 69 | + lidar_metadata_dict = json.loads(json_str) |
| 70 | + return { |
| 71 | + LiDARType.deserialize(lidar_type): LiDARMetadata.from_dict(metadata) |
| 72 | + for lidar_type, metadata in lidar_metadata_dict.items() |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class LiDAR: |
| 78 | + |
| 79 | + metadata: LiDARMetadata |
| 80 | + point_cloud: npt.NDArray[np.float32] |
| 81 | + |
| 82 | + @property |
| 83 | + def xyz(self) -> npt.NDArray[np.float32]: |
| 84 | + """ |
| 85 | + Returns the point cloud as an Nx3 array of x, y, z coordinates. |
| 86 | + """ |
| 87 | + return self.point_cloud[self.metadata.lidar_index.XYZ].T |
| 88 | + |
| 89 | + @property |
| 90 | + def xy(self) -> npt.NDArray[np.float32]: |
| 91 | + """ |
| 92 | + Returns the point cloud as an Nx2 array of x, y coordinates. |
| 93 | + """ |
| 94 | + return self.point_cloud[self.metadata.lidar_index.XY].T |
| 95 | + |
| 96 | + @property |
| 97 | + def intensity(self) -> Optional[npt.NDArray[np.float32]]: |
| 98 | + """ |
| 99 | + Returns the intensity values of the LiDAR point cloud if available. |
| 100 | + Returns None if intensity is not part of the point cloud. |
| 101 | + """ |
| 102 | + if hasattr(self.metadata.lidar_index, "INTENSITY"): |
| 103 | + return self.point_cloud[self.metadata.lidar_index.INTENSITY] |
| 104 | + return None |
| 105 | + |
| 106 | + @property |
| 107 | + def range(self) -> Optional[npt.NDArray[np.float32]]: |
| 108 | + """ |
| 109 | + Returns the range values of the LiDAR point cloud if available. |
| 110 | + Returns None if range is not part of the point cloud. |
| 111 | + """ |
| 112 | + if hasattr(self.metadata.lidar_index, "RANGE"): |
| 113 | + return self.point_cloud[self.metadata.lidar_index.RANGE] |
| 114 | + return None |
| 115 | + |
| 116 | + @property |
| 117 | + def elongation(self) -> Optional[npt.NDArray[np.float32]]: |
| 118 | + """ |
| 119 | + Returns the elongation values of the LiDAR point cloud if available. |
| 120 | + Returns None if elongation is not part of the point cloud. |
| 121 | + """ |
| 122 | + if hasattr(self.metadata.lidar_index, "ELONGATION"): |
| 123 | + return self.point_cloud[self.metadata.lidar_index.ELONGATION] |
| 124 | + return None |
0 commit comments