TriangleMesh相关方法
文章目录
1. 查看mesh三角形面信息
def view_hull_triangles(hull: o3d.geometry.TriangleMesh):
"""
查看mesh三角形面信息(顶点索引、顶点坐标)
:param hull:
:return:
"""
# 获取三角形的顶点索引
triangles = np.asarray(hull.triangles) # 每个三角形由三个顶点的索引组成
# 获取顶点坐标
vertices = np.asarray(hull.vertices) # 所有顶点的坐标
print(f"总共有 {len(triangles)} 个三角形面")
for i, triangle in enumerate(triangles):
print(f"三角形 {i + 1}: 顶点索引 {triangle}, 顶点坐标:")
for idx in triangle:
print(f" 顶点 {idx}: {vertices[idx]}")
return triangles, vertices
2. 可视化三角形
def highlight_triangle(hull: o3d.geometry.TriangleMesh, triangle_idx: int):
"""
可视化单个三角形
:param hull:
:param triangle_idx:
:return:
"""
# 获取三角形索引
triangles = np.asarray(hull.triangles)
vertices = np.asarray(hull.vertices)
# 获取特定三角形的顶点
triangle = triangles[triangle_idx]
triangle_vertices = vertices[triangle]
# 创建一个新点云,表示高亮的三角形
highlight = o3d.geometry.TriangleMesh()
highlight.vertices = o3d.utility.Vector3dVector(triangle_vertices)
highlight.triangles = o3d.utility.Vector3iVector([[0, 1, 2]])
highlight.paint_uniform_color([1, 0, 0]) # 高亮颜色:红色
# 绘制原始凸包和高亮三角形
o3d.visualization.draw_geometries([hull, highlight], window_name="Highlight Triangle")
def highlight_all_triangles(hull: o3d.geometry.TriangleMesh, show=False):
"""
可视化所有三角形
:param hull:
:param show: 是否直接显示
:return:
"""
# 获取三角形索引和顶点坐标
triangles = np.asarray(hull.triangles)
vertices = np.asarray(hull.vertices)
# 创建一个新三角网格,用于高亮显示
highlight_mesh = o3d.geometry.TriangleMesh()
# 存储所有高亮的顶点和三角形
all_highlighted_vertices = []
all_highlighted_triangles = []
colors = []
for i, triangle in enumerate(triangles):
# 获取三角形顶点
triangle_vertices = vertices[triangle]
# 偏移索引(因为顶点需要连续编号)
base_index = len(all_highlighted_vertices)
# 添加当前三角形的顶点
all_highlighted_vertices.extend(triangle_vertices)
# 添加当前三角形的索引(注意偏移)
all_highlighted_triangles.append([base_index, base_index + 1, base_index + 2])
# 为每个三角形分配随机颜色
random_color = np.random.rand(3) # RGB 随机颜色
colors.append(random_color)
# 设置高亮网格的顶点、三角形和颜色
highlight_mesh.vertices = o3d.utility.Vector3dVector(all_highlighted_vertices)
highlight_mesh.triangles = o3d.utility.Vector3iVector(all_highlighted_triangles)
highlight_mesh.vertex_colors = o3d.utility.Vector3dVector(
np.repeat(colors, 3, axis=0) # 每个顶点与其三角形共享同样的颜色
)
# 绘制原始凸包和高亮网格
# o3d.visualization.draw_geometries([hull, highlight_mesh], window_name="Highlight All Triangles")
if show:
o3d.visualization.draw_geometries([highlight_mesh], window_name="Highlight All Triangles")
return highlight_mesh
显示效果:
3. 上采样
def upsample_by_mesh(mesh: o3d.geometry.TriangleMesh, number_of_points: int = 10000):
"""
通过mesh进行上采样
:param mesh:
:param number_of_points:
:return:
"""
dense_pcd = mesh.sample_points_uniformly(number_of_points=number_of_points)
return dense_pcd
原始数据:
上采样后:
4. 计算mesh形成的面积和体积
def calculate_hull_metrics(hull: o3d.geometry.TriangleMesh):
surface_area = 0
volume = 0
try:
if not hull.is_watertight():
# print("网格不是水密的!尝试简化修复")
hull = hull.simplify_quadric_decimation(target_number_of_triangles=1000)
# print("网格是水密的!")
# 计算表面积
surface_area = hull.get_surface_area()
# 计算体积
volume = hull.get_volume()
return surface_area, volume
except Exception as e:
print(f"Error occurred: {str(e)}")
return surface_area, volume
标签:triangle,Python,hull,vertices,mesh,005Mesh,Open3D,三角形,triangles
From: https://blog.csdn.net/m0_51496369/article/details/144144123