凸包算法--物体表面积/体积计算--python版
文章目录
- 环境:
- 1.1 凸包法介绍:
- 2.1 python代码
- 3.1 可视化
- 4.1 体积/表面积Calculation
环境:
Open3D
1.1 凸包法介绍:
用于找到包围给定点集的最小凸多边形或凸多面体
常用的凸包算法:
Grabam扫描法(适用三维层面)
Jarvis卷包裹法(不适用三维层面)
Clarkson-Shor
QuickHull
2.1 python代码
import open3d as o3d
import numpy as npdef 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 = 1000points = 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, _ = restm.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()
3.1 可视化




4.1 体积/表面积Calculation
