-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.py
42 lines (32 loc) · 1.46 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import open3d as o3d
import numpy as np
def point_cloud_to_mesh(point_cloud):
# 将点云转换为网格
mesh, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(point_cloud)
return mesh
def main():
# 读取点云文件
point_cloud = o3d.io.read_point_cloud("005.ply")
print("success load")
# 计算点云的法线
# point_cloud.estimate_normals()
point_cloud.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
print("success estimate_normals")
distances = point_cloud.compute_nearest_neighbor_distance()
avg_dist = np.mean(distances)
radius = 3 * avg_dist
bpa_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(point_cloud,o3d.utility.DoubleVector([radius, radius * 2]))
bpa_mesh.remove_degenerate_triangles()
bpa_mesh.remove_duplicated_triangles()
bpa_mesh.remove_duplicated_vertices()
bpa_mesh.remove_non_manifold_edges()
# 使用泊松表面重建算法生成网格
# mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(point_cloud, depth=9)[0]
# mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(point_cloud, depth=9, width=0, scale=1.1, linear_fit=False)[0]
# print("success TriangleMesh")
# 可视化网格
# o3d.visualization.draw_geometries([mesh])
# 保存网格为 PLY 文件
o3d.io.write_triangle_mesh("output_mesh.ply", bpa_mesh)
if __name__ == "__main__":
main()