Install VTK on Ubuntu 20.04

Introduction

VTK (Visualization Toolkit) is a high-quality open-source software package for visualization and graphics processing that can be used to build applications, including medicine, manufacturing, oil and gas, and entertainment. Here are the steps to install VTK (The Visualization ToolKit) on Ubuntu 20.04 .

install dependencies

Install the necessary dependencies
Open a terminal and execute the following command to install the necessary dependencies:

sudo apt-get update
sudo apt install -y build-essential cmake git unzip qt5-default libqt5opengl5-dev libqt5x11extras5-dev libeigen3-dev libboost-all-dev libglew-dev libglvnd-dev

Download VTK source code

Download the latest version of the VTK source code from the VTK official website (https://vtk.org/download/), or use the command line to download:

git clone https://gitlab.kitware.com/vtk/vtk.git

Compile and install the VTK library

Enter the VTK source code directory, create a build directory, and use CMake to build:

cd vtk
mkdir build
cd build
cmake ..

After waiting for the configuration of CMake to complete, use the following commands to compile and install

make
sudo make install

This process may take a while, and errors may occur during the compilation process, which needs to be adjusted according to the error information.

Configure environment variables

Open the /etc/profile file and add the following lines:

export VTK_DIR=/usr/local/include/vtk-9.2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Save the file and exit (may require a reboot).

verify

Confirm that the installation is complete
Run the following command to confirm that the installation is complete:

#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2) // 初始化OpenGL渲染模块
VTK_MODULE_INIT(vtkInteractionStyle) // 初始化交互式模块

int main()
{
    
    
    vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();

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

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    interactor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    interactor->Start();

    return 0;
}

Makefile compile

CC = g++
CFLAGS = -I/usr/local/include/vtk-9.2/
LDFLAGS = -L/usr/local/lib
LIBS = -lvtkRenderingCore-9.2 -lvtkRenderingOpenGL2-9.2 -lvtkInteractionStyle-9.2 \
        -lvtkFiltersSources-9.2 -lvtkCommonExecutionModel-9.2 -lvtkCommonCore-9.2 \
        -lvtksys-9.2

SRC = vtk_test.cpp
OBJ = $(SRC:.cpp=.o)
EXECUTABLE = vtk_test

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJ)
	$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)

.cpp.o:
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(OBJ) $(EXECUTABLE)

If there is no error in the operation, it means that the VTK library has been installed successfully.
The above are the steps to install the VTK library on Ubuntu 20.04.

Guess you like

Origin blog.csdn.net/m0_58235748/article/details/130528211