vtk介绍

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())
 
actor = vtk.vtkActor()
actor.SetMapper(mapper)
 
# Create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
 
# Create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
 
# Assign actor to the renderer
ren.AddActor(actor)
 
# Enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()

3ds文件是是Autodesk 3dsMax使用的一种二进制存储格式,VTK中可以使用vtk3DSImporter类导入3ds文件。

vtkImporter is an abstract class that specifies the protocol for importing actors, cameras, lights and properties into a vtkRenderWindow. The following takes place: 1) Create a RenderWindow and Renderer if none is provided. 2) Call ImportBegin, if ImportBegin returns False, return 3) Call ReadData, which calls: a) Import the Actors b) Import the cameras c) Import the lights d) Import the Properties 7) Call ImportEnd

Subclasses optionally implement the ImportActors, ImportCameras, ImportLights and ImportProperties or ReadData methods. An ImportBegin and ImportEnd can optionally be provided to perform Importer-specific initialization and termination. The Read method initiates the import process. If a RenderWindow is provided, its Renderer will contained the imported objects. If the RenderWindow has no Renderer, one is created. If no RenderWindow is provided, both a RenderWindow and Renderer will be created. Both the RenderWindow and Renderer can be accessed using Get methods.

# This example demonstrates the use of vtk3DSImporter.
# vtk3DSImporter is used to load 3D Studio files.  Unlike writers,
# importers can load scenes (data as well as lights, cameras, actors
# etc.). Importers will either generate an instance of vtkRenderWindow
# and/or vtkRenderer or will use the ones you specify.

import vtk

# Create the importer and read a file
importer = vtk.vtk3DSImporter()
importer.ComputeNormalsOn()
importer.SetFileName("myfile.3ds")
importer.Read()

# Here we let the importer create a renderer and a render window for
# us. We could have also create and assigned those ourselves like so:
# renWin = vtk.vtkRenderWindow()
# importer.SetRenderWindow(renWin)

# Assign an interactor.
# We have to ask the importer for it's render window.
renWin = importer.GetRenderWindow()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Set the render window's size
renWin.SetSize(500, 500)

# Set some properties on the renderer.
# We have to ask the importer for it's renderer.
ren = importer.GetRenderer()
ren.SetBackground(0.1, 0.2, 0.4)

iren.Initialize()
renWin.Render()
iren.Start()
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值