使用Plotly Mesh3D绘制OFF文件3D网格结果与原模型不符问题求助
问题原因
你已经正确读取了OFF文件的顶点和面数据,但调用Mesh3d时没有传入定义网格拓扑的面索引参数。Plotly的Mesh3d在仅传入顶点坐标时,会自动对顶点执行Delaunay三角剖分生成面,和OFF文件中预先定义的原始面结构完全不符,因此输出的可视化结果错误。
解决方法
将读取到的faces数组拆分为Mesh3d要求的三个面索引参数i、j、k,分别对应每个三角面的三个顶点索引,初始化Mesh3d时一并传入即可。
修正后代码
import plotly.graph_objects as go import numpy as np # loading the mesh and getting the vertices def ExtractRawData(filepath): # read the file path with open(filepath) as file: if 'OFF' != file.readline().strip(): raise Exception('Not a valid OFF header') n_verts, n_faces, n_dontknow = tuple([int(s) for s in file.readline().strip().split(' ')]) verts = [[float(s) for s in file.readline().strip().split(' ')] for i_vert in range(n_verts)] faces = [[int(s) for s in file.readline().strip().split(' ')][1:] for i_face in range(n_faces)] return np.array(verts), np.array(faces) # extracting the vertices from the off file verts, faces = ExtractRawData('Turbine blade.off') # getting the x, y and z coordinates from the vertices x = verts[:,0] y = verts[:,1] z = verts[:,2] # 拆分面索引为i、j、k参数 i = faces[:, 0] j = faces[:, 1] k = faces[:, 2] # 传入面索引参数绘制网格 fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k)]) fig.show()
内容的提问来源于stack exchange,提问作者Aman Savaria
相关产品推荐
相关产品推荐

