2017-05-13 5 views
4

グレイスケールで画像を取り込み、拡大縮小し、各ピクセルの高さを決定する3Dモデルを出力するPythonコードを作成しています。グレースケールの値私は3Dモデルの出力を除いてすべての作業をしています。私はnumpy-stlを使ってイメージから派生した値の配列に基づいてそれを作成しています。 numpy-stlライブラリを使用して、ボックスを作成し、画像に必要な回数だけコピーします。私はそれぞれを画像に対応する位置と高さに翻訳します。これはすべて動作します。問題は、すべてを1つの.stlファイルとして保存しようとするときに発生します。キューブの個々のメッシュをすべて1つに結合する方法を理解できません。numpy-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], 
            [0, 0, 1]]) 
data['vectors'][1] = numpy.array([[1, 0, 1], 
            [0, 1, 1], 
            [1, 1, 1]]) 
# Right face 
data['vectors'][2] = numpy.array([[1, 0, 0], 
            [1, 0, 1], 
            [1, 1, 0]]) 
data['vectors'][3] = numpy.array([[1, 1, 1], 
            [1, 0, 1], 
            [1, 1, 0]]) 
# Left face 
data['vectors'][4] = numpy.array([[0, 0, 0], 
            [1, 0, 0], 
            [1, 0, 1]]) 
data['vectors'][5] = numpy.array([[0, 0, 0], 
            [0, 0, 1], 
            [1, 0, 1]]) 
# Bottem of the cube 
data['vectors'][6] = numpy.array([[0, 1, 0], 
            [1, 0, 0], 
            [0, 0, 0]]) 
data['vectors'][7] = numpy.array([[1, 0, 0], 
            [0, 1, 0], 
            [1, 1, 0]]) 
# Right back 
data['vectors'][8] = numpy.array([[0, 0, 0], 
            [0, 0, 1], 
            [0, 1, 0]]) 
data['vectors'][9] = numpy.array([[0, 1, 1], 
            [0, 0, 1], 
            [0, 1, 0]]) 
# Left back 
data['vectors'][10] = numpy.array([[0, 1, 0], 
            [1, 1, 0], 
            [1, 1, 1]]) 
data['vectors'][11] = numpy.array([[0, 1, 0], 
            [0, 1, 1], 
            [1, 1, 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() 

答えて

0
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, 3)) 
mesh_final = mesh.Mesh(data.copy()) 
mesh_final.save('whatever_filename') 

私は、よりエレガントな解決策があるはずです知っているが、これはあなたが達成したいものをやっているようです。

関連する問題