[python] STL to VTK / VTK to STL
3D 데이터 타입 중 STL과 VTK 타입간 파일 포맷 변경에 대해 포스팅 해보겠습니다.
간단하게 두 포맷의 차이를 설명하자면,
- STL: 3D 데이터의 기하 정보를 담고 있음
- VTK: 기하 정보 뿐만 아니라, 사용자가 원한다면 속성 정보 또한 추가할 수 있음
어쨌든 이 두 포맷간 변환은 pyvista라는 모듈을 사용하여 진행할 수 있다.
0. pyvista 모듈 다운로드
- 다운로드 링크: https://docs.pyvista.org/
PyVista — PyVista 0.32.0 documentation
3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK) PyVista is… “VTK for humans”: a high-level API to the Visualization Toolkit (VTK) mesh data structures and filtering methods for spatial datasets 3D plo
docs.pyvista.org
pyvista는 3D 데이터를 다루다보면 저어어엉말 자주 마주치게 되고, 매우 유용하게 사용할 수 있는 라이브러리다.
데이터 다룰 때 유용한 것 뿐만 아니라, 시각화에도 아주 특화되어 있는 라이브러리이다.
1. 코드 설명
import os
import pyvista as pv
def vtk2stl(path, fname):
vtk_f = pv.read(os.path.join(path, fname))
pv.save_meshio(fname[:-4] + '.stl', vtk_f)
print(fname + ' is changed to STL !')
def stl2vtk(path, fname):
stl_f = pv.read(os.path.join(path, fname))
pv.save_meshio(fname[:-4] + '.vtk', stl_f)
print(fname + ' is changed to VTK !')
# stl to vtk
path = os.getcwd()
fname = 'bunny.stl'
stl2vtk(path, fname)
코드는 별 거 없다.
pv.read( )로 vtk, stl 파일 둘 다 불러올 수 있고,
불러온 파일을 기존 suffix를 제거하고, 고대로 pv.save_meshio( )를 사용하여 원하는 포맷으로 저장해주면 된다.
이렇게 두가지 파일 변환 함수를 생성하여 (stl2vtk, vtk2stl) 그때 그때 호출하여 사용하면 편하다.
2. 깃헙 링크
코드와 샘플 데이터를 사용하여 바로 실험할 수 있게 깃헙에 업로드 해두었으니 참고 바람!
https://github.com/henewsuh/convert_stl_vtk
GitHub - henewsuh/convert_stl_vtk: convert between stl and vtk
convert between stl and vtk. Contribute to henewsuh/convert_stl_vtk development by creating an account on GitHub.
github.com