はじめに
3D形式のデータをもつstlファイルをボクセル形式に変換して、matplotlib のvoxelで表示する。
コード

解説
stlファイルのダウンロード

Thingiverse - Digital Designs for Physical Objects
Download millions of 3D models and files for your 3D printer, laser cutter, or CNC. From custom parts to unique designs,...
上記サイトからstlファイルをダウンロードして、下記サイトでボクセル形式に変換し、txtデータとして、ダウンロードした。

online html5 voxelizer
convert your 3D model or image into voxels
モジュールのインポート
データの読み込み
np.loadtxt(‘Cactus1.txt’,delimiter=’,’)と、ファイル名と区切りの形式を指定して読み込む。データの構造としては、(#,#,#)のデータが1列に連なったデータとなっている。
ボクセルデータの生成
dataの最大値を求めて、最大値+1のnp.indices()配列を生成する。ここでは、形状が(61,61,61)の、要素としてインデックスをもつ配列を作成し、それをそのまま、x,y,zとしている。
x[int(data[i,0]),int(data[i,1]),int(data[i,2])] = -1のfor文で、データと適合するx,y,zの位置を-1に変換する。適合する位置をすべて-1にしてから、voxels = x < 0 とすることで、voxelはdataの位置のみがTrueとなったbool型配列となる。
3Dグラフの表示
ax = fig.gca(projection=’3d’)で3Dグラフが表示できるようになる。
ax.set_aspect(‘equal’)でx,y,zの比率を揃えている。
ax.voxels(voxels, linewidth=0.3, facecolor=’lightgreen’,edgecolors=’k’)でボクセルを表示。
ax.view_init(elev=-160, azim=30)で視点の設定を行っている。
ax.set_axis_off()で軸を非表示にした。
その他の例



参考

Thingiverse - Digital Designs for Physical Objects
Download millions of 3D models and files for your 3D printer, laser cutter, or CNC. From custom parts to unique designs,...

online html5 voxelizer
convert your 3D model or image into voxels
コメント
[…] [matplotlib 3D] 47.ボクセルサボテン(Voxel Cactus)3D形式のデータをもつstlファイルをボクセル形式に変換して、matplotlib のvoxelで表示する。sabopy.com2019.03.14 […]