import open3d as o3d
import numpy as np

# Load .pts file
pcd = o3d.io.read_point_cloud("/home/worker/yt/3D/LIDAR_pointcloud.pts")

# Check the point cloud
o3d.visualization.draw_geometries([pcd])

# Convert Point Cloud to Mesh using Ball-Pivoting algorithm
distances = pcd.compute_nearest_neighbor_distance()
avg_distance = np.mean(distances)
radius = 3 * avg_distance

bpa_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
           pcd,
           o3d.utility.DoubleVector([radius, radius * 2]))

# Check the mesh visually
# o3d.visualization.draw_geometries([bpa_mesh])

# Save the mesh as .ply file
o3d.io.write_triangle_mesh("/home/worker/yt/3D/LIDAR_mesh.ply", bpa_mesh)