Learning VTK on a Mac

Posted by stuartc1 on Fri, 03 Jan 2020 11:27:07 +0100

Compilation on MAC OS X:

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

Compile vtk to make QT version 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

The compilation and installation of vtk on mac is completed.

doxgen document:

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

CSDN blog recommendation:

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

For Unix platform compilation, it is valid to check the value in CMakeCache.txt. The link path is LD? Library? Path.

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

Step1 exercise / 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

Search for cpp classes:

The URL corresponding to the vtkRegularPolygonSource class is:

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

If you want to search for other class introductions, you can directly change the classvtkRegularPolygonSource to class + class name.

cmake online query: https://cmake.org/cmake/help/v3.10/search.html
Official website: https://cmake.org/cmake-tutorial/
Tutorial recommendations: https://www.jianshu.com/p/bbf68f9ddffa

Example:
Write and read STL files

#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;
}

The related CMakeLists.txt is:

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

Compile run:

cmake ./
make
./vtkLearn cone.stl

Get:

About PrintSelf in vtkObject:
The interface of PrintSelf is as follows:
void PrintSelf(ostream& os, vtkIndent indent) override;
To print to standard output, we can do this:

ostream out( std::cout.rdbuf() );
renWin->PrintSelf( out, *vtkIndent::New() );
// renWin is a pointer to the vtkRenderWindow object

Example of VTK online learning:
https://lorensen.github.io/VTKExamples/site/Cxx/
When it comes to vectors and matrices, you may need to write notes. The common latex mathematical symbols are:
http://www.mohu.org/info/symbols/symbols.htm

Topics: cmake Mac Unix OS X