VTK(visualization toolkit)是一个开源的免费软件系统,主要用于三维计算机图形学、图像处理和科学计算可视化。VTK是在三维函数库OpenGL 的基础上采用面向对象的设计方法发展起来的,它将我们在可视化开发过程中会经常遇到的细节屏蔽起来,并将一些常用的算法封装起来。它包含一个C++类库,和解释封装层,包括Tcl/Tk、Java、Python等。 采用这种架构的优势是我们能使用C++语言建立高效的算法,用其他的脚本语言(如TCL、Python)可以进行快速的开发。
VTK中可以导入/导出或读/写多种三维格式的文件,可以参考What 3D file formats can VTK import and export? The following table identifies the file formats that VTK can read and write. Importer and Exporter classes move full scene information into or out of VTK. Reader and Writer classes move just geometry.
STL格式是一种3D模型文件格式,它采用三角形离散地近似表示三维模型,目前已被工业界认为是快速成形领域的标准描述文件格式。这种文件不包括模型的材质等信息。下面的代码将读入一个STL文件将其显示在窗口中,并可以用鼠标和键盘进行一些简单的交互。
#!/usr/bin/env python
import vtk
filename = "myfile.stl"
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort