Open3D
用于找到包围给定点集的最小凸多边形或凸多面体
常用的凸包算法:
Grabam扫描法(适用三维层面)
Jarvis卷包裹法(不适用三维层面)
Clarkson-Shor
QuickHull
import open3d as o3d
import numpy as np
def draw_point_cloud(result):
for geometry in result:
o3d.visualization.draw_geometries([geometry], "result", 800, 600,
50, 50, False, False, True)
def main():
np.random.seed(42) # 设置随机种子以确保可重复性
# 随机生成点云数据
num_points = 1000
points = np.random.rand(num_points, 3) * 10 # 在[0, 10)范围内生成点
pc = o3d.geometry.PointCloud()
pc.points = o3d.utility.Vector3dVector(points)
result = [pc]
# 凸包算法
tm, tm_ls = o3d.geometry.TriangleMesh(), o3d.geometry.LineSet()
res = pc.compute_convex_hull()
tm, _ = res
tm.compute_vertex_normals(True)
tm_ls = o3d.geometry.LineSet.create_from_triangle_mesh(tm)
color = np.random.rand(3)
# 将tm_ls.lines转换为NumPy数组
lines_np = np.asarray(tm_ls.lines)
colors = np.tile(color, (lines_np.shape[0], 1))
tm_ls.colors = o3d.utility.Vector3dVector(colors)
print(f"凸包表面积为: {tm.get_surface_area()}")
print(f"凸包体积为: {tm.get_volume()}")
convex_hull_index = res[1]
convex_hull_point = pc.select_by_index(convex_hull_index)
convex_hull_point.paint_uniform_color([1, 0, 0])
result.extend([convex_hull_point, tm_ls, tm])
draw_point_cloud(result)
if __name__ == "__main__":
main()