欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【VTK】在Mac上学习VTK

程序员文章站 2022-04-01 08:44:00
...

MAC OS X上的编译:

https://www.vtk.org/Wiki/VTK/Building/MacOSX

编译vtk使得QT版本为5

cmake configure:


cmake ./ -G "Unix Makefiles" \

-DVTK_USE_QVTK:BOOL=ON \

-DCMAKE_INSTALL_PREFIX=/usr/local \

-DVTK_USE_GUISUPPORT:BOOL=ON \

-DVTK_QT_VERSION=5 \

-DModule_vtkGUISupportQt:BOOL=ON \

-DModule_vtkGUISupportQtOpenGL:BOOL=ON \

-DModule_vtkGUISupportQtSQL:BOOL=ON \

-DModule_vtkGUISupportQtWebkit:BOOL=ON \

-DModule_vtkRenderingQt:BOOL=ON \

-DModule_vtkViewsQt:BOOL=ON

then make and make install

vtk在mac上的编译安装就完成了。

doxgen文档:

https://www.vtk.org/doc/nightly/html/index.html

CSDN博客推荐:

https://blog.csdn.net/www_doling_net/article/details/8763686

Unix平台编译,查看CMakeCache.txt中的值是有效的。链接路径为LD_LIBRARY_PATH。

The basic setup of source -> mapper -> actor -> renderer -> renderwindow is typical of most VTK programs.

step1 练习:/Users/weiyang/Downloads/VTK-8.1.1/Examples/Tutorial/Step1/Cxx


➜ cmake ./

➜ make

Scanning dependencies of target Cone

[ 50%] Building CXX object CMakeFiles/Cone.dir/Cone.cxx.o

[100%] Linking CXX executable Cone.app/Contents/MacOS/Cone

[100%] Built target Cone

搜索cpp 的类:

vtkRegularPolygonSource类对应的URL是:

https://www.vtk.org/doc/nightly/html/classvtkRegularPolygonSource.html

如果要搜索其他的类介绍,可以直接将classvtkRegularPolygonSource换成class + 类名 即可。

cmake 在线查询:https://cmake.org/cmake/help/v3.10/search.html
官网:https://cmake.org/cmake-tutorial/
教程推荐:https://www.jianshu.com/p/bbf68f9ddffa

例子:
写、读STL文件

#include <vtkPolyData.h>
#include <vtkSTLWriter.h>
#include <vtkSTLReader.h>
#include <vtkConeSource.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>

void generateSTL( std::string filename )
{
    vtkSmartPointer<vtkConeSource> coneSource =
    vtkSmartPointer<vtkConeSource>::New();
    coneSource->Update();

    vtkSmartPointer<vtkSTLWriter> stlWriter =
    vtkSmartPointer<vtkSTLWriter>::New();
    stlWriter->SetFileName(filename.c_str());
    stlWriter->SetInputConnection(coneSource->GetOutputPort());
    stlWriter->Write();
}

void readSTL( std::string inputFilename )
{
    vtkSmartPointer<vtkSTLReader> reader =
            vtkSmartPointer<vtkSTLReader>::New();
    reader->SetFileName( inputFilename.c_str() );
    reader->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection( reader->GetOutputPort() );

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->GetProperty()->SetColor( 0, 1, 0 );
    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 );
    actor->SetPosition( 0 , 0, 0 );
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 ); // Background color green

    renderWindow->Render();
    renderWindowInteractor->Start();
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        std::cout << "Required parameters: filename.stl" << std::endl;
        return EXIT_FAILURE;
    }

    generateSTL( argv[1] );

    readSTL( argv[1] );

    return EXIT_SUCCESS;
}

相关CMakeLists.txt为:

cmake_minimum_required(VERSION 2.8)

PROJECT(vtkLearn)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(${PROJECT_NAME} main.cpp )

target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})

编译运行:

cmake ./
make
./vtkLearn cone.stl

得到:
【VTK】在Mac上学习VTK

关于vtkObject中的PrintSelf:
PrintSelf的接口是这样的:
void PrintSelf(ostream& os, vtkIndent indent) override;
想要打印到标准输出中,我们可以这样:

ostream out( std::cout.rdbuf() );
renWin->PrintSelf( out, *vtkIndent::New() );
// renWin是指向vtkRenderWindow对象的指针

VTK在线学习例子:
https://lorensen.github.io/VTKExamples/site/Cxx/
涉及到向量,矩阵的时候可能自己需要写写笔记,常用的latex数学符号:
http://www.*.org/info/symbols/symbols.htm

相关标签: VTK