import open3d as o3d
import numpy as np

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

print("불러오기")

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

# Downsample the point cloud for denser point cloud
pcd = pcd.voxel_down_sample(voxel_size=0.01)

# Estimate normals with larger radius and max_nn
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.2, max_nn=50))
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 0.]))

# Poisson Surface Reconstruction with increased depth
poisson_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=20)[0]

print("check mesh")

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

print("save")

# Save the mesh as .ply file
o3d.io.write_triangle_mesh("LIDAR_mesh.ply", poisson_mesh)
