openmesh基础操作 numpy关于向量的运算
程序员文章站
2024-03-16 17:44:52
...
环境:python3.7
第三方库:openmesh、numpy
import openmesh as om
# 读取模型
mesh = om.read_trimesh(r'D:\Desktop\bunny.off')
# 保存模型
om.write_mesh( r'D:\Desktop\bunny1.off', mesh)
# 获取顶点、边、面的总数
print('顶点总数:', mesh.n_vertices())
print('面总数 :', mesh.n_faces())
print('边总数 :', mesh.n_edges())
顶点总数: 953
面总数 : 1902
边总数 : 2853
# 遍历所有的顶点,获取每个vertex的坐标
for vertex in mesh.vertices():
print('顶点的数据类型:', type(vertex), '顶点坐标:', mesh.point(vertex), '顶点坐标的数据类型', type(mesh.point(vertex)))
break
# 遍历所有的边和面
for edge in mesh.edges():
print(type(edge))
break
for face in mesh.faces():
print(type(face))
break
顶点的数据类型: <class 'openmesh.VertexHandle'> 顶点坐标: [-5.00492001 -7.67860889 -0.35177901] 顶点坐标的数据类型 <class 'numpy.ndarray'>
<class 'openmesh.EdgeHandle'>
<class 'openmesh.FaceHandle'>
半边结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2vPt6azu-1601372917175)(attachment:image.png)]
# 半边结构
# 遍历每一条半边 获取每个半边的起点和终点 获取半边周围的元素
for halfedge in mesh.halfedges():
fvertex = mesh.from_vertex_handle(halfedge); tvertex = mesh.to_vertex_handle(halfedge)
print(type(halfedge), '半边的起点和终点:', mesh.point(fvertex), mesh.point(tvertex))
print(' '*33, '半边对应的面:', mesh.face_handle(halfedge))
print(' '*33, '半边的下一条半边:', mesh.next_halfedge_handle(halfedge))
print(' '*33, '半边相对的半边:', mesh.opposite_halfedge_handle(halfedge))
break
<class 'openmesh.VertexHandle'> 顶点的坐标: [-5.00492001 -7.67860889 -0.35177901]
<class 'openmesh.HalfedgeHandle'> 半边的起点和终点: [-4.35409498 -7.676548 -0.610264 ] [-4.35129881 -7.65309906 1.08651805]
半边对应的面: <openmesh.FaceHandle object at 0x000001C02703C308>
半边的下一条半边: <openmesh.HalfedgeHandle object at 0x000001C02703C308>
半边相对的半边: <openmesh.HalfedgeHandle object at 0x000001C026792180>
<class 'openmesh.EdgeHandle'>
<class 'openmesh.FaceHandle'>
# 邻域遍历
for vertex in mesh.vertices():
# 遍历顶点周围的1-邻域面
for face in mesh.vf(vertex):
print(face)
print('1-邻域面的个数:', sum(1 for _ in mesh.vf(vertex)))
# 遍历顶点周围的1-邻域顶点
for vertex in mesh.vv(vertex):
print(vertex)
print('1-邻域顶点的个数:', sum(1 for _ in mesh.vv(vertex)))
break
for face in mesh.faces():
# 遍历面周围的1-邻域顶点和边
for v in mesh.fv(face):
print(v)
for e in mesh.fe(face):
print(e)
break
<openmesh.FaceHandle object at 0x000001C027379378>
<openmesh.FaceHandle object at 0x000001C0273797D8>
<openmesh.FaceHandle object at 0x000001C027379378>
<openmesh.FaceHandle object at 0x000001C0273797D8>
<openmesh.FaceHandle object at 0x000001C027379378>
<openmesh.FaceHandle object at 0x000001C0273797D8>
1-邻域面的个数: 6
<openmesh.VertexHandle object at 0x000001C027379378>
<openmesh.VertexHandle object at 0x000001C027379308>
<openmesh.VertexHandle object at 0x000001C027379378>
<openmesh.VertexHandle object at 0x000001C02679CCA8>
<openmesh.VertexHandle object at 0x000001C02679C4C8>
<openmesh.VertexHandle object at 0x000001C02679CCA8>
1-邻域顶点的个数: 5
<openmesh.VertexHandle object at 0x000001C02679CF80>
<openmesh.VertexHandle object at 0x000001C026FF03E8>
<openmesh.VertexHandle object at 0x000001C02679CF80>
<openmesh.EdgeHandle object at 0x000001C026FF0148>
<openmesh.EdgeHandle object at 0x000001C026FF0298>
<openmesh.EdgeHandle object at 0x000001C026FF0378>
# 计算顶点和面的法向量
for f in mesh.faces():
print(mesh.normal(f))
break
for v in mesh.vertices():
print(mesh.normal(v))
break
[2.12199579e-314 0.00000000e+000 4.54540394e-321]
[2.12199579e-314 0.00000000e+000 4.68374232e-321]
numpy中关于向量的各种操作
import numpy as np
a = 1.0; b = 2.0
print(a,b)
print(a/b)
print(np.sqrt(b))
print(np.cos(-1))
print(np.sin(30.0/180.0*np.cos(-1)))
print('-'*50)
a = np.array([1.0, 0.0, 5.0]); b = np.array([1.0, 0.0, 3.0])
print('a=', a, 'b=', b)
print('a+b=', a+b)
print('向量点乘(内积)和叉乘(外积、向量积')
c = np.cross(a,b)
d = np.cross(b,a)
print('a cross b=', c)
print('b cross a=', d)
print('a dot b =', np.dot(a,b), a.dot(b), (a*b).sum()) # 点乘有很多种求法
print('向量a的长度=', np.linalg.norm(a), np.sqrt(a.T.dot(a)))
print('向量a的单位向量=', a/(np.linalg.norm(a)))
print('向量a和b的夹角余弦=', a.dot(b)/(np.linalg.norm(a) * np.linalg.norm(b)))
1.0 2.0
0.5
1.4142135623730951
0.5403023058681397
0.08992872947650446
--------------------------------------------------
a= [1. 0. 5.] b= [1. 0. 3.]
a+b= [2. 0. 8.]
向量点乘(内积)和叉乘(外积、向量积
a cross b= [0. 2. 0.]
b cross a= [ 0. -2. 0.]
a dot b = 16.0 16.0 16.0
向量a的长度= 5.0990195135927845 5.0990195135927845
向量a的单位向量= [0.19611614 0. 0.98058068]
向量a和b的夹角余弦= 0.9922778767136677
上一篇: 在vue项目中使用md5加密
下一篇: Unity 2D游戏基本知识笔记2