VTK中导入并显示STL、3DS文件
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()
参考:
vtk3DSImporter Class Reference
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文件的更多相关文章
- VTK中获取STL模型点的坐标以及对其进行变换
VTK是一个基于面向对象的开源三维绘图软件包,和其它的的三维绘图引擎如OSG.OGRE不同之处在于,VTK可视化对象主要是各种数据,更加注重对数据分析处理后的可视化,可视化的内容是人们无法直接感受到的 ...
- [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之一:OBJ格式分析
[iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之一:OBJ文件格式分析作者:yuezang - iTyran 在iOS的3D开发中常常需要导入通过3DS MAX之类 ...
- python从Microsoft Excel文件中导入数据
excel中后缀为csv和xls,二者区别如下:1.xls 文件就是Microsoft excel电子表格的文件格式.2.csv是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. ...
- 从navicat中导入sql文件过大:Got a packet bigger than 'max_allowed_packet' bytes
失败背景:刚才通过navicat向本地mysql数据库中导入sql文件.第一个sql文件(多个表)大小为967M,导入成功: 第二个sql(单个表)大小为50.1M,导入失败. 1.在navicat中 ...
- 在Google Colab中导入一个本地模块或.py文件
模块与单个.py文件的区别,模块中含有__init__.py文件,其中函数调用使用的是相对路径,如果使用导入.py文件的方法在Google Colab中导入模块 会报错:Attempted relat ...
- 向 wmware workstation pro 的 MS-DOS 操作系统中导入文件(masm debug edit)(详细图解)
一般MS-DOS中不包含masm.debug和edit这三个程序. 我们想要把这几个文件导入到wmware workstation pro中的DOS虚拟机中怎么做呢? [尝试] 1.我试过用共享文件夹 ...
- [iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型
[iTyran原创]iPhone中OpenGL ES显示3DS MAX模型之二:lib3ds加载模型 作者:u0u0 - iTyran 在上一节中,我们分析了OBJ格式.OBJ格式优点是文本形式,可读 ...
- 向IPython Notebook中导入.py文件
IPython Notebook使用起来简洁方便,但是有时候如果需要导入一个现有的.py文件,则需要注意选择导入的方法以达到不同的效果.目前遇到3种方法. (1) 将文件保存为.ipynb格式,直接拖 ...
- 自制C#版3DS文件的解析器并用SharpGL显示3DS模型
自制C#版3DS文件的解析器并用SharpGL显示3DS模型 我已经重写了3ds解析器,详情在此(http://www.cnblogs.com/bitzhuwei/p/CSharpGL-2-parse ...
随机推荐
- Caffe的solver参数介绍
版权声明:转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/59109447 1. Parameters solver.p ...
- 2014年.net程序员年终总结
2014年经历了3家公司,感觉这一年工作不怎么顺利,在2013年1月进入一家外企从事软件架构设计.开发测试.部署实施的相关工作,在2013年感觉工作很充实,在2014年由于项目的原因被迫去做项目维护, ...
- gradlew 命令行 build 调试 构建错误 Manifest merger failed MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 什么是JSP (转)
http://blog.csdn.net/javaloveiphone/article/details/7937170 一.什么是JSP(JavaServer Pages),原来是没有jsp的,只有s ...
- Centos配置为驱动程序开发环境
安装完centos后,写了一个驱动测试程序Hello.编译过程出现如下错误: make: *** /lib/modules/2.6.32-220.4.1.el6.i686/build: No such ...
- 转: git使用时让http记住帐号密码
见 http://git.mydoc.io/?t=154710 https 方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受 https 带来的极速 按照以下设置记住密 ...
- 将iPhone投影到Mac上
将iPhone投影到Mac上 有时候, 出于演示须要, 又或者嫌弃iPhone屏幕太小了, 我想把画面弄到mac上. 这时候, 就须要将iPhone投影到Mac上. 至于怎样做呢? 这就是本文要说明的 ...
- ORA-16038: log 3 sequence# 103 cannot be archived
[size=large]今天在自己机器做了个实验,插入10万条,由于空间少,重启数据库时出现: [size=x-large]SQL> startup ORACLE instance starte ...
- .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解
本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...