0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

3Dndarrayのボクセルを3Dメッシュに保存する

Posted at

下のような3次元のNdarrayのボクセルデータをstlなどの3Dデータに変換する

ボクセルデータ

np.array(
    [[[0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 1]],
     [[0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 1],
      [0, 0, 1, 2]],
     [[0, 0, 0, 0],
      [0, 0, 0, 1],
      [0, 0, 1, 2],
      [0, 1, 2, 2]],
     [[0, 0, 0, 1],
      [0, 0, 1, 2],
      [0, 1, 2, 2],
      [1, 2, 2, 1]],
     [[0, 0, 1, 2],
      [0, 1, 2, 2],
      [1, 2, 2, 1],
      [2, 2, 1, 0]],
     [[0, 1, 2, 2],
      [1, 2, 2, 1],
      [2, 2, 1, 0],
      [2, 1, 0, 0]],
     [[1, 2, 2, 1],
      [2, 2, 1, 0],
      [2, 1, 0, 0],
      [1, 0, 0, 0]],
     [[2, 2, 1, 0],
      [2, 1, 0, 0],
      [1, 0, 0, 0],
      [0, 0, 0, 0]],
     [[2, 1, 0, 0],
      [1, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0]],
     [[1, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0]]])

image.png

コード

voxcel2mesh.py
import numpy as np
import pyvista as pv


voxcel_data = np.array(
    [[[0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 1]],
     [[0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 1],
      [0, 0, 1, 2]],
     [[0, 0, 0, 0],
      [0, 0, 0, 1],
      [0, 0, 1, 2],
      [0, 1, 2, 2]],
     [[0, 0, 0, 1],
      [0, 0, 1, 2],
      [0, 1, 2, 2],
      [1, 2, 2, 1]],
     [[0, 0, 1, 2],
      [0, 1, 2, 2],
      [1, 2, 2, 1],
      [2, 2, 1, 0]],
     [[0, 1, 2, 2],
      [1, 2, 2, 1],
      [2, 2, 1, 0],
      [2, 1, 0, 0]],
     [[1, 2, 2, 1],
      [2, 2, 1, 0],
      [2, 1, 0, 0],
      [1, 0, 0, 0]],
     [[2, 2, 1, 0],
      [2, 1, 0, 0],
      [1, 0, 0, 0],
      [0, 0, 0, 0]],
     [[2, 1, 0, 0],
      [1, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0]],
     [[1, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0]]])

grid = pv.ImageData()
grid.dimensions = np.array(voxcel_data.shape) + 1
grid.origin = (0, 0, 0)
grid.spacing = (1, 1, 1)
grid.cell_data["values"] = voxcel_data.flatten(order="F")  

threshed = grid.threshold(value=1)

surf = threshed.extract_surface()

surf.save("mesh.stl")

pl = pv.Plotter()
pl.add_mesh(grid, style = 'wireframe')
pl.add_mesh(surf)
pl.show()

image.png

メッシュになりました

解説

.py
voxcel_data = np.array(
~~
)

3次元ボクセルデータ好きな形状を定義してください

.py
grid = pv.ImageData()
grid.dimensions = np.array(voxcel_data.shape) + 1
grid.origin = (0, 0, 0)
grid.spacing = (1, 1, 1)

グリッドデータのクラスの定義、縦横奥行、原点、ボクセルサイズの指定

.py
grid.cell_data["values"] = voxcel_data.flatten(order="F")  

グリッドのセルに3Dndarrayの数値データを格納

.py
threshed = grid.threshold(value=1)

value以下の値のセルを削除
今回のデータの場合
value = 0
image.png
value = 1
image.png
value = 2
image.png

.py
surf = threshed.extract_surface()

ボクセルデータからメッシュを作成します

.py
surf.save("mesh.stl")

メッシュデータを保存します

.py
pl = pv.Plotter()
pl.add_mesh(grid, style = 'wireframe')
pl.add_mesh(surf)
pl.show()

3Dモデルをプロットします

以上

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?