# -*- coding: utf-8 -*-
'''
CSDN博客:https://blog.csdn.net/suiyingy
乐乐感知学堂公众号
长期分享三维、二维感知、大模型等AI算法和demo以及金融量化分析算法等。
本程序作用:open3d顶点法向量和表面法向量可视化
专栏地址:https://blog.csdn.net/suiyingy/category_12462636.html
'''
import numpy as np
import open3d as o3d
if __name__ == '__main__':
vertices = np.array([[0.0, 0.0, 1.0], [3.0, 0.0, 1.0], [0.0, 4.0, 1.0]])
faces = np.array([[1, 2, 3]])
faces = faces - 1 # 减1是因为索引从0开始
# 创建TriangleMesh对象
mesh = o3d.geometry.TriangleMesh()
# 设置顶点和面片
mesh.vertices = o3d.utility.Vector3dVector(vertices)
mesh.triangles = o3d.utility.Vector3iVector(faces)
mesh.compute_triangle_normals()
triangle_normals = np.array(mesh.triangle_normals)
print('triangle normals 1: ', triangle_normals)
mesh.compute_vertex_normals()
vertex_normals = np.array(mesh.vertex_normals)
print('vertex normals 1: ', vertex_normals)
# 创建法向量的起点和终点
face_centers = np.mean(vertices[faces], axis=1) # 三角面中心
arrow_start_points = np.vstack((vertices, face_centers))
arrow_end_points = arrow_start_points + np.vstack((vertex_normals, triangle_normals)) * 0.5 # 0.1为法向量长度
# 创建法向量可视化的 LineSet 对象
line_set = o3d.geometry.LineSet()
line_set.points = o3d.utility.Vector3dVector(np.concatenate([arrow_start_points, arrow_end_points], axis=0))
line_set.lines = o3d.utility.Vector2iVector(np.stack([np.arange(len(arrow_start_points)), np.arange(len(arrow_start_points)) + len(arrow_start_points)], axis=1))
line_colors = np.array([[0.0, 0.0, 1.0]] * len(arrow_start_points))
line_set.colors = o3d.utility.Vector3dVector(line_colors)
# 创建坐标系可视化的 LineSet 对象
# 显示坐标系,xyz - rgb
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5, origin=[0, 0, 0])
# 可视化 Mesh、法向量和坐标系
o3d.visualization.draw_geometries([mesh, line_set, coordinate_frame])
# 创建箭头的 TriangleMesh 对象,使用箭头显示法向量
arrow_meshes = []
for i in range(len(arrow_start_points)):
arrow_mesh = o3d.geometry.TriangleMesh.create_arrow(cylinder_height=0.8, cylinder_radius=0.01, cone_height=0.3, cone_radius=0.1)
# 平移和旋转箭头,使其与线段匹配
arrow_center = (arrow_start_points[i] + arrow_end_points[i]) / 2
arrow_mesh.translate(arrow_start_points[i].T)
vertex_colors = np.array([[0.0, 0.0, 1.0]] * len(arrow_mesh.vertices))
arrow_mesh.vertex_colors = o3d.utility.Vector3dVector(vertex_colors)
arrow_meshes.append(arrow_mesh)
# break
# 可视化 Mesh、LineSet 和箭头
o3d.visualization.draw_geometries([mesh]+arrow_meshes+[coordinate_frame])
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
程序中详细介绍博客专栏地址:https://blog.csdn.net/suiyingy/article/details/133816356。 01_normal_numpy.py:numpy计算法向量。 02_normal_open3d.py:open3d计算表面法向量和顶点法向量。 03_normal_open3d_vertex.py:open3d顶点法向量计算原理验证。 04_normal_open3d_viz.py:open3d顶点法向量和表面法向量可视化。
资源推荐
资源详情
资源评论
收起资源包目录
03_normal.rar (4个子文件)
03_normal
03_normal_open3d_vertex.py 2KB
01_normal_numpy.py 1KB
04_normal_open3d_viz.py 3KB
02_normal_open3d.py 1KB
共 4 条
- 1
资源评论
Coding的叶子
- 粉丝: 4w+
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功