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.

File Format Read Write
3D Studio vtk3DSImporter  
AVS "UCD" format vtkAVSucdReader  
Movie BYU vtkBYUReader vtkBYUWriter
Renderman   vtkRIBExporter
Open Inventor 2.0   vtkIVExporter/vtkIVWriter
CAD STL vtkSTLReader vtkSTLWriter
Fluent GAMBIT ASCII vtkGAMBITReader  
Unigraphics Facet Files vtkUGFacetReader  
Marching Cubes vtkMCubesReader vtkMCubesWriter
Wavefront OBJ   vtkOBJExporter
VRML 2.0   vtkVRMLExporter
VTK Structured Grid † vtkStructuredGridReader vtkStructuredWriter
VTK Poly Data † vtkPolyDataReader vtkPolyDataWriter
PLOT3D vtkPLOT3DReader  
CGM   vtkCGMWriter
OBJ vtkOBJReader  
Particle vtkParticleReader  
PDB vtkPDBReader  
PLY vtkPLYReader vtkPLYWriter
Gaussian vtkGaussianCubeReader  
Facet vtkFacetReader vtkFacetWriter
XYZ vtkXYZMolReader  
Ensight ‡ vtkGenericEnSightReader  

  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()

C++版代码如下:

#include <vtkPolyData.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h> int main ( int argc, char *argv[] )
{
if ( argc != )
{
cout << "Required parameters: Filename" << endl;
return EXIT_FAILURE;
} std::string inputFilename = argv[]; vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update(); // Visualize
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort()); vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper); vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow); renderer->AddActor(actor);
renderer->SetBackground(., ., .); // Background color green renderWindow->Render();
renderWindowInteractor->Start(); return EXIT_SUCCESS;
}

  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.

#!/usr/bin/env python

# 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()

参考:

VTK/Examples/Python/STLReader

vtk3DSImporter Class Reference

常见三维文件格式之STL, VRML, X3D

3D模型文件--STL,OBJ,3DS

读取3ds文件

Example demonstrates the use of vtk3DSImporter

http://public.kitware.com/pipermail/vtkusers/2011-June/068231.html

http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/Rendering/Python/CADPart.py

VTK中导入并显示STL、3DS文件的更多相关文章

  1. VTK中获取STL模型点的坐标以及对其进行变换

    VTK是一个基于面向对象的开源三维绘图软件包,和其它的的三维绘图引擎如OSG.OGRE不同之处在于,VTK可视化对象主要是各种数据,更加注重对数据分析处理后的可视化,可视化的内容是人们无法直接感受到的 ...

  2. [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之一:OBJ格式分析

    [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之一:OBJ文件格式分析作者:yuezang - iTyran     在iOS的3D开发中常常需要导入通过3DS MAX之类 ...

  3. python从Microsoft Excel文件中导入数据

    excel中后缀为csv和xls,二者区别如下:1.xls 文件就是Microsoft excel电子表格的文件格式.2.csv是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. ...

  4. 从navicat中导入sql文件过大:Got a packet bigger than 'max_allowed_packet' bytes

    失败背景:刚才通过navicat向本地mysql数据库中导入sql文件.第一个sql文件(多个表)大小为967M,导入成功: 第二个sql(单个表)大小为50.1M,导入失败. 1.在navicat中 ...

  5. 在Google Colab中导入一个本地模块或.py文件

    模块与单个.py文件的区别,模块中含有__init__.py文件,其中函数调用使用的是相对路径,如果使用导入.py文件的方法在Google Colab中导入模块 会报错:Attempted relat ...

  6. 向 wmware workstation pro 的 MS-DOS 操作系统中导入文件(masm debug edit)(详细图解)

    一般MS-DOS中不包含masm.debug和edit这三个程序. 我们想要把这几个文件导入到wmware workstation pro中的DOS虚拟机中怎么做呢? [尝试] 1.我试过用共享文件夹 ...

  7. [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型

    [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型 作者:u0u0 - iTyran 在上一节中,我们分析了OBJ格式.OBJ格式优点是文本形式,可读 ...

  8. 向IPython Notebook中导入.py文件

    IPython Notebook使用起来简洁方便,但是有时候如果需要导入一个现有的.py文件,则需要注意选择导入的方法以达到不同的效果.目前遇到3种方法. (1) 将文件保存为.ipynb格式,直接拖 ...

  9. 自制C#版3DS文件的解析器并用SharpGL显示3DS模型

    自制C#版3DS文件的解析器并用SharpGL显示3DS模型 我已经重写了3ds解析器,详情在此(http://www.cnblogs.com/bitzhuwei/p/CSharpGL-2-parse ...

随机推荐

  1. anaconda、pip配置国内镜像

    一.anaconda配置镜像查看源:conda config --show-sources在Mac and Linux下:conda config --add channels https://mir ...

  2. 在 JDK 9 中更简洁使用 try-with-resources 语句

    本文详细介绍了自 JDK 7 引入的 try-with-resources 语句的原理和用法,以及介绍了 JDK 9 对 try-with-resources 的改进,使得用户可以更加方便.简洁的使用 ...

  3. Error: Program type already present: android.arch.lifecycle.LifecycleRegistry$1

    com.firebaseui:firebase-ui-firestore:3.1.0 depends on android.arch.lifecycle:extensions:1.0.0-beta1. ...

  4. 100base-T

    100Base-T是一种以100Mbps速率工作的局域网(LAN)标准,它通常被称为快速以太网标准,并使用两对UTP(非屏蔽双绞线)铜质电缆. 快速以太网 : 与10BASE-T的区别在于网络速率是1 ...

  5. 标准输出中stderr和stdout的区别

    一.首先介绍一下三者printf,sprintf,fprintf的功能 1,printf就是标准输出,在屏幕上打印出一段字符串来. 2,sprintf就是把格式化的数据写入到某个字符串中.返回值字符串 ...

  6. 怎么选择软件许可证,Apache, MIT, BSD, GPL, Mozilla, LGPL

  7. window.open不被拦截的实现代码

    $("#last").click(function(){ var w=window.open(); setTimeout(function(){ w.location=" ...

  8. 为Linux操作系统所在的logical volumn扩容

    感谢Lieven和Tom的协助,这个问题才得以解决.我在这里把解决问题的步骤总结一下,帮助自己学习. 问题描述 =========== 笔者有一台linux的物理机,其上名为centos-root的l ...

  9. C# Win32控制台应用程序忽略 Ctrl + C,阻止程序退出

    C# Win32控制台应用程序忽略 Ctrl + C,阻止程序退出,这里使用到了Windows API SetConsoleCtrlHandler函数 注意:在VS中调试执行时,在处理程序例程中设置断 ...

  10. 求职之C++小知识点整理

    1.顺序容器 1.顺序容器:vector,deque,list,forward_list,array,string.其中除list和forward_list外,其它都支持快速随机访问. deque a ...