zoukankan      html  css  js  c++  java
  • 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 FormatReadWrite
    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 != 2 )
        {
            cout << "Required parameters: Filename" << endl;
            return EXIT_FAILURE;
        }
    
        std::string inputFilename = argv[1];
    
        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(.3, .6, .3); // Background color green
    
        renderWindow->Render();
        renderWindowInteractor->Start();
    
        return EXIT_SUCCESS;
    }
    View Code

      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

  • 相关阅读:
    mongodb分片
    mongodb读写分离的一些选项的理解
    mongodb管理副本集(持续更新中)
    mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]
    mongodb副本集的基础概念和各种机制
    mongodb的查询
    mongodb文档的CRUD
    SQLServer2008数据库连接error40错误
    Did you forget about DBModel.InitializeModel the model [AAAdm] ?
    ERP中通过EDI导入资料的时候出现【Microsoft Office Excel不能访问文件‘C:WindowsTEMP433....’
  • 原文地址:https://www.cnblogs.com/21207-iHome/p/6429714.html
Copyright © 2011-2022 走看看