如何使用numpy-stl在python中保存多个网格
发布时间:2020-12-20 13:17:43 所属栏目:Python 来源:网络整理
导读:我正在研究一段 python代码,它将采用灰度图像,缩放它,并输出一个3d模型,每个像素的高度由灰度值决定.除了3d模型的输出外,我的一切都在工作.我正在使用numpy-stl基于从图像派生的值数组来创建它.使用numpy-stl库我创建一个框,然后根据图像需要复制它多次.然后
我正在研究一段
python代码,它将采用灰度图像,缩放它,并输出一个3d模型,每个像素的高度由灰度值决定.除了3d模型的输出外,我的一切都在工作.我正在使用numpy-stl基于从图像派生的值数组来创建它.使用numpy-stl库我创建一个框,然后根据图像需要复制它多次.然后我将每一个翻译成与图像对应的位置和高度.这一切都有效.当我尝试将它们全部保存为一个.stl文件时出现问题.我无法弄清楚如何将立方体的所有单个网格组合成一个.
这里只是处理3d数组创建的代码.我可以绘制创建的网格但不保存它们. from stl import mesh import math import numpy test = [[1,2],[2,1]] a = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] # Create 6 faces of a cube,2 triagles per face data = numpy.zeros(12,dtype=mesh.Mesh.dtype) #cube defined in stl format # Top of the cube data['vectors'][0] = numpy.array([[0,1,1],[1,[0,1]]) data['vectors'][1] = numpy.array([[1,1]]) # Right face data['vectors'][2] = numpy.array([[1,0],0]]) data['vectors'][3] = numpy.array([[1,0]]) # Left face data['vectors'][4] = numpy.array([[0,1]]) data['vectors'][5] = numpy.array([[0,1]]) # Bottem of the cube data['vectors'][6] = numpy.array([[0,0]]) data['vectors'][7] = numpy.array([[1,0]]) # Right back data['vectors'][8] = numpy.array([[0,0]]) data['vectors'][9] = numpy.array([[0,0]]) # Left back data['vectors'][10] = numpy.array([[0,1]]) data['vectors'][11] = numpy.array([[0,1]]) # Generate 4 different meshes so we can rotate them later meshes = [mesh.Mesh(data.copy()) for _ in range(16)] #iterates through the array and translates cube in the x and y direction according #to position in array and in the z direction according to eh value stored in the array def ArrayToSTL(array,STLmesh): y_count = 0 x_count = 0 count = 0 for row in array: x_count = 0 for item in row: meshes[count].x += x_count meshes[count].y += y_count meshes[count].z += item x_count +=1 count += 1 y_count += 1 ArrayToSTL(a,meshes) # Optionally render the rotated cube faces from matplotlib import pyplot from mpl_toolkits import mplot3d # Create a new plot figure = pyplot.figure() axes = mplot3d.Axes3D(figure) # Render the cube faces for m in meshes: axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m.vectors)) # Auto scale to the mesh size scale = numpy.concatenate([m.points for m in meshes]).flatten(-1) axes.auto_scale_xyz(scale,scale,scale) # Show the plot to the screen pyplot.show() 解决方法total_length_data = 0 for i in range(len(meshes)): total_length_data += len(meshes[i].data) data = numpy.zeros(total_length_data,dtype = mesh.Mesh.dtype) data['vectors'] = np.array(meshes).reshape((-1,9)).reshape((-1,3)) mesh_final = mesh.Mesh(data.copy()) mesh_final.save('whatever_filename') 我知道应该有一个更优雅的解决方案,但这似乎做了你想要实现的目标. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |