| 70 | |
| 71 | |
| 72 | def pcd2range(pcd, size, fov, depth_range, remission=None, labels=None, **kwargs): |
| 73 | # laser parameters |
| 74 | fov_up = fov[0] / 180.0 * np.pi # field of view up in rad |
| 75 | fov_down = fov[1] / 180.0 * np.pi # field of view down in rad |
| 76 | fov_range = abs(fov_down) + abs(fov_up) # get field of view total in rad |
| 77 | |
| 78 | # get depth (distance) of all points |
| 79 | depth = np.linalg.norm(pcd, 2, axis=1) |
| 80 | |
| 81 | # mask points out of range |
| 82 | mask = np.logical_and(depth > depth_range[0], depth < depth_range[1]) |
| 83 | depth, pcd = depth[mask], pcd[mask] |
| 84 | |
| 85 | # get scan components |
| 86 | scan_x, scan_y, scan_z = pcd[:, 0], pcd[:, 1], pcd[:, 2] |
| 87 | |
| 88 | # get angles of all points |
| 89 | yaw = -np.arctan2(scan_y, scan_x) |
| 90 | pitch = np.arcsin(scan_z / depth) |
| 91 | |
| 92 | # get projections in image coords |
| 93 | proj_x = 0.5 * (yaw / np.pi + 1.0) # in [0.0, 1.0] |
| 94 | proj_y = 1.0 - (pitch + abs(fov_down)) / fov_range # in [0.0, 1.0] |
| 95 | |
| 96 | # scale to image size using angular resolution |
| 97 | proj_x *= size[1] # in [0.0, W] |
| 98 | proj_y *= size[0] # in [0.0, H] |
| 99 | |
| 100 | # round and clamp for use as index |
| 101 | proj_x = np.maximum(0, np.minimum(size[1] - 1, np.floor(proj_x))).astype(np.int32) # in [0,W-1] |
| 102 | proj_y = np.maximum(0, np.minimum(size[0] - 1, np.floor(proj_y))).astype(np.int32) # in [0,H-1] |
| 103 | |
| 104 | # order in decreasing depth |
| 105 | order = np.argsort(depth)[::-1] |
| 106 | proj_x, proj_y = proj_x[order], proj_y[order] |
| 107 | |
| 108 | # project depth |
| 109 | depth = depth[order] |
| 110 | proj_range = np.full(size, -1, dtype=np.float32) |
| 111 | proj_range[proj_y, proj_x] = depth |
| 112 | |
| 113 | # project point feature |
| 114 | if remission is not None: |
| 115 | remission = remission[mask][order] |
| 116 | proj_feature = np.full(size, -1, dtype=np.float32) |
| 117 | proj_feature[proj_y, proj_x] = remission |
| 118 | elif labels is not None: |
| 119 | labels = labels[mask][order] |
| 120 | proj_feature = np.full(size, 0, dtype=np.float32) |
| 121 | proj_feature[proj_y, proj_x] = labels |
| 122 | else: |
| 123 | proj_feature = None |
| 124 | |
| 125 | return proj_range, proj_feature |
| 126 | |
| 127 | |
| 128 | def range2xyz(range_img, fov, depth_range, depth_scale, log_scale=True, **kwargs): |