2024-08-30 16:49:21 +08:00
|
|
|
import numpy as np
|
|
|
|
import open3d as o3d
|
2024-09-19 00:14:26 +08:00
|
|
|
import torch
|
2024-08-30 16:49:21 +08:00
|
|
|
|
|
|
|
class PtsUtil:
|
2024-08-30 19:21:18 +08:00
|
|
|
|
2024-08-30 16:49:21 +08:00
|
|
|
@staticmethod
|
|
|
|
def voxel_downsample_point_cloud(point_cloud, voxel_size=0.005):
|
2024-10-17 14:28:19 +00:00
|
|
|
voxel_indices = np.floor(point_cloud / voxel_size).astype(np.int32)
|
|
|
|
unique_voxels = np.unique(voxel_indices, axis=0, return_inverse=True)
|
|
|
|
return unique_voxels[0]*voxel_size
|
2024-08-30 16:49:21 +08:00
|
|
|
|
2024-08-30 19:21:18 +08:00
|
|
|
@staticmethod
|
2024-10-03 01:59:13 +08:00
|
|
|
def random_downsample_point_cloud(point_cloud, num_points, require_idx=False):
|
2024-10-02 16:24:13 +08:00
|
|
|
if point_cloud.shape[0] == 0:
|
2024-10-03 23:36:18 +08:00
|
|
|
if require_idx:
|
|
|
|
return point_cloud, np.array([])
|
2024-10-02 16:24:13 +08:00
|
|
|
return point_cloud
|
2024-09-10 20:12:46 +08:00
|
|
|
idx = np.random.choice(len(point_cloud), num_points, replace=True)
|
2024-10-03 01:59:13 +08:00
|
|
|
if require_idx:
|
|
|
|
return point_cloud[idx], idx
|
2024-09-19 00:14:26 +08:00
|
|
|
return point_cloud[idx]
|
|
|
|
|
2024-10-06 11:49:03 +08:00
|
|
|
@staticmethod
|
2024-10-06 13:48:54 +08:00
|
|
|
def fps_downsample_point_cloud(point_cloud, num_points, require_idx=False):
|
2024-10-06 11:49:03 +08:00
|
|
|
N = point_cloud.shape[0]
|
|
|
|
mask = np.zeros(N, dtype=bool)
|
|
|
|
|
|
|
|
sampled_indices = np.zeros(num_points, dtype=int)
|
|
|
|
sampled_indices[0] = np.random.randint(0, N)
|
|
|
|
distances = np.linalg.norm(point_cloud - point_cloud[sampled_indices[0]], axis=1)
|
|
|
|
for i in range(1, num_points):
|
|
|
|
farthest_index = np.argmax(distances)
|
|
|
|
sampled_indices[i] = farthest_index
|
|
|
|
mask[farthest_index] = True
|
|
|
|
|
|
|
|
new_distances = np.linalg.norm(point_cloud - point_cloud[farthest_index], axis=1)
|
|
|
|
distances = np.minimum(distances, new_distances)
|
|
|
|
|
|
|
|
sampled_points = point_cloud[sampled_indices]
|
2024-10-06 13:48:54 +08:00
|
|
|
if require_idx:
|
|
|
|
return sampled_points, sampled_indices
|
2024-10-06 11:49:03 +08:00
|
|
|
return sampled_points
|
|
|
|
|
2024-09-19 00:14:26 +08:00
|
|
|
@staticmethod
|
|
|
|
def random_downsample_point_cloud_tensor(point_cloud, num_points):
|
|
|
|
idx = torch.randint(0, len(point_cloud), (num_points,))
|
2024-10-03 01:59:13 +08:00
|
|
|
return point_cloud[idx]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def voxelize_points(points, voxel_size):
|
|
|
|
voxel_indices = np.floor(points / voxel_size).astype(np.int32)
|
|
|
|
unique_voxels = np.unique(voxel_indices, axis=0, return_inverse=True)
|
|
|
|
return unique_voxels
|
|
|
|
|
2024-10-06 11:49:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def transform_point_cloud(points, pose_mat):
|
|
|
|
points_h = np.concatenate([points, np.ones((points.shape[0], 1))], axis=1)
|
|
|
|
points_h = np.dot(pose_mat, points_h.T).T
|
|
|
|
return points_h[:, :3]
|
|
|
|
|
2024-10-03 01:59:13 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_overlapping_points(point_cloud_L, point_cloud_R, voxel_size=0.005, require_idx=False):
|
|
|
|
voxels_L, indices_L = PtsUtil.voxelize_points(point_cloud_L, voxel_size)
|
|
|
|
voxels_R, _ = PtsUtil.voxelize_points(point_cloud_R, voxel_size)
|
|
|
|
|
|
|
|
voxel_indices_L = voxels_L.view([("", voxels_L.dtype)] * 3)
|
|
|
|
voxel_indices_R = voxels_R.view([("", voxels_R.dtype)] * 3)
|
|
|
|
overlapping_voxels = np.intersect1d(voxel_indices_L, voxel_indices_R)
|
|
|
|
mask_L = np.isin(
|
|
|
|
indices_L, np.where(np.isin(voxel_indices_L, overlapping_voxels))[0]
|
|
|
|
)
|
|
|
|
overlapping_points = point_cloud_L[mask_L]
|
|
|
|
if require_idx:
|
|
|
|
return overlapping_points, mask_L
|
|
|
|
return overlapping_points
|
|
|
|
|
2024-10-05 12:24:53 -05:00
|
|
|
@staticmethod
|
2024-10-17 06:13:18 -05:00
|
|
|
def filter_points(points, normals, cam_pose, theta_limit=45, z_range=(0.2, 0.45)):
|
|
|
|
|
|
|
|
""" filter with normal """
|
|
|
|
normals_normalized = normals / np.linalg.norm(normals, axis=1, keepdims=True)
|
|
|
|
cos_theta = np.dot(normals_normalized, np.array([0, 0, 1]))
|
|
|
|
theta = np.arccos(cos_theta) * 180 / np.pi
|
|
|
|
idx = theta < theta_limit
|
|
|
|
filtered_sampled_points = points[idx]
|
2024-10-23 00:42:18 -05:00
|
|
|
filtered_normals = normals[idx]
|
2024-10-05 12:24:53 -05:00
|
|
|
|
|
|
|
""" filter with z range """
|
2024-10-17 06:13:18 -05:00
|
|
|
points_cam = PtsUtil.transform_point_cloud(filtered_sampled_points, np.linalg.inv(cam_pose))
|
2024-10-05 12:24:53 -05:00
|
|
|
idx = (points_cam[:, 2] > z_range[0]) & (points_cam[:, 2] < z_range[1])
|
2024-10-17 06:13:18 -05:00
|
|
|
z_filtered_points = filtered_sampled_points[idx]
|
2024-10-23 00:42:18 -05:00
|
|
|
z_filtered_normals = filtered_normals[idx]
|
|
|
|
return z_filtered_points[:, :3], z_filtered_normals
|
2024-10-17 14:28:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def point_to_hash(point, voxel_size):
|
|
|
|
return tuple(np.floor(point / voxel_size).astype(int))
|
|
|
|
|