前言
从底部两点和顶部两点组成的四点生mesh面片;Open3d自带的mesh生成的api没有通过点直接生成的,要么就是box或者凸包
一、思路
三角面片就是结构化有索引的点,给四个点编号编号就可以了
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import open3d as o3d
2.核心代码
代码如下(示例):
# 创建立方体网格
cube_mesh = o3d.geometry.TriangleMesh()
cube_mesh.vertices = o3d.utility.Vector3dVector(bottom)
cube_mesh.triangles = o3d.utility.Vector3iVector(cube_triangles)
3.完整代码
代码如下(示例):
import numpy as np
import open3d as o3d
'''
作者 : cscec;
日期 : 2024年1月27日;
功能 : 从底部两点和顶部两点组成的四点生mesh面片;
输入 : numpy格式四个点;
输出 : cube_mesh = 面片
'''
# 从底部两点和顶部两点组成的四边形生成mesh
def create_mesh_from_4_points(bottom_points_np):
# 从点云中提取点的坐标
# obox = point_cloud.get_oriented_bounding_box()
bottom_points_np = np.array(bottom_points_np)
# 根据 Z 值对点进行排序
sorted_points = bottom_points_np[np.argsort(bottom_points_np[:, 2])]
# 获取 Z 值最小的四个点
points = sorted_points[:2]
# 获取 Z 值最大的四个点
points_max = sorted_points[2:]
# 计算中心点坐标
center = np.mean(points, axis=0)
center_max = np.mean(points_max, axis=0)
# 计算每个点相对于中心点的极坐标角度
angles = np.arctan2(points[:, 1] - center[1], points[:, 0] - center[0])
angles_max = np.arctan2(points_max[:, 1] - center_max[1], points_max[:, 0] - center_max[0])
# 对角度进行排序
sorted_indices = np.argsort(angles)
sorted_indices_max = np.argsort(angles_max)
points = points[sorted_indices]
points_max = points_max[sorted_indices_max]
# 按排序后的顺序连接点
bottom = np.empty([4, 3], dtype=float)
bottom[0] = points[0]
bottom[1] = points[1]
bottom[2] = points_max[1]
bottom[3] = points_max[0]
# 获取立方体面的索引
cube_triangles = [
[0, 1, 2], [2, 3, 0],
[2, 1, 0], [0, 3, 2]
]
# 创建立方体网格
cube_mesh = o3d.geometry.TriangleMesh()
cube_mesh.vertices = o3d.utility.Vector3dVector(bottom)
cube_mesh.triangles = o3d.utility.Vector3iVector(cube_triangles)
cube_mesh.paint_uniform_color([1, 0, 0])
# lineset = create_lines_from_points(cube_points_np)
return cube_mesh
if __name__ == "__main__":
# 【加载点云】
array = np.array([[0,0,0],[0,0,1000],[1000,0,0],[1000,0,1000]])
obx2 = create_mesh_from_4_points(array)
o3d.visualization.draw_geometries([obx2.paint_uniform_color([1, 0, 0])],
window_name='Open3D', width=1920, height=1080, left=50, top=50,
point_show_normal=True, mesh_show_wireframe=True, mesh_show_back_face=True)
该处使用np.array([[0,0,0],[0,0,1000],[1000,0,0],[1000,0,1000]])初始化点云
测试效果

总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了从底部两点和顶部两点组成的四点生mesh面片的使用,而open3d提供了大量能使我们快速便捷地处理面片数据的函数和方法,详情查看专栏其他文章。
2万+



被折叠的 条评论
为什么被折叠?



