VTK and QT source code compilation and examples

One: Introduction

VTK: is an open source software library for 3D computer graphics, image processing, data visualization, and scientific visualization. VTK provides a wealth of basic algorithms and data structures, including points, lines, polygons, 2D and 3D images, volume rendering, etc., as well as many advanced algorithms, such as surface reconstruction, fluid dynamics simulation, fitting and interpolation, image segmentation and imaging processing etc.

QT: Provides a complete set of libraries, including GUI, network, database, XML, Bluetooth, multimedia, OpenGL, etc., which can be used to write desktop, mobile device, embedded and real-time applications. Qt's code portability is very good, developers can write a code, compile and run on Windows, Linux, Mac OS and other platforms without changing the code. Qt is characterized by easy learning and use, rich documentation and examples, rich third-party library support, fast development speed, efficient code generation, beautiful and easy-to-use interface, and is a very popular cross-platform development framework.

Two: environment

Win10

VS2019

VTK9.0.3 https://vtk.org/files/release/9.0/VTK-9.0.3.tar.gz

QT5.14.2 https://download.qt.io/archive/qt/5.14/5.14.2/

b6d5e5343c69c912f6db6829b4437459.png

After the download is complete, VTK will be decompressed, and QT will be installed.

Three: Install and compile

1. Create a folder first

Create two folders, build and install, under the VTK directory. build stores compilation results and executable files, and install stores compiled include and lib.

56fa0354d9fd79ce7d9126ce6e75c640.png

2. cmake settings

(1) Select the location where vtk was decompressed just now. (2) Select the build folder you just created. (3) configure Among them, Advanced needs to be checked, and here cmake is checked by default.

5b0a3ba3b5a82d7fb702b0eeecdec518.png

(1) Select vs2019 (2) Select x64 (3) Finish

ce6d8ba991ce5ffd37a30dc5182e5144.png

wait for it to finish

5129ff53ef6f20039115d73a7a9c9097.png

There will be a bunch of red warnings after the end. There are some areas that need modification. CMAKE_INSTALL_PREFIX needs to change the location to the previously created install folder.

4bb44b566b41c997226395ae7274bb68.png

VTK_BUILD_EXAMPLES needs to be checked

e05691262e42d06f10a8d09a8516b4fe.png

Search QT and change all Values ​​to YES

483c75218e866a2124124033854b99be.png

Next press Configure. A red warning will also pop up, and the QT location will not be found. Fill in the positions below with the previously installed QT positions accordingly. (QT5.14.2 does not have a VS2019 folder, and it is the same to replace it with VS2017)

175ca7b1ddab0c929c75adf971421ae3.png

Press Configure again and all will pass without red warnings. Press Generate again and it will be generated as shown in the figure below.

500e17bcf5fb69476440d83239024ff7.png

3. compile

Find VTK.sln in the previously built build folder and open it.

ebb30f711e2be11ab3dacd714e96607c.png

Open the interface as follows, adjust release or debug. Find ALL_BUILD right-click - build.

3cd420e730511bbd573b5a865311eb72.png

After the previous step is over, find INSTALL and right click to generate

3ed5372063c1575ccf694c8eba690353.png

After the installation is complete, the following files will be generated in the install folder.

afbf3a54662a6b44ddbe01c67d11a7f3.png

Four: VS configuration QT

1. Create a new C++ project, then search for QT in Extensions--Manage Extensions, download and install Qt Visual Studio Tools.

e90ced04cf005daf1f261d2ebb6052ff.png

2. After the installation is complete, open the extension--Qt VS Tools--Qt Versions

f37a39e7410a57a803da57ce7125c03d.png

3. Fill in the location where Qt was installed before

1bfc5b55398d670a664cc1ff580a25ca.png

The above operation VS configuration QT is completed.

Five: Example

1. Create a new VS project

At this time, searching for QT will pop up the corresponding QT project that can be created.

bc9ea438defac7d39d3bdc399db61a7a.png

Here we can directly select DEBUG to enter, and then adjust to Release through VS later.

7c9298c2eda8890b84df0869190e4f1f.png

After the newly created project enters, there are already some basic codes, which can be run directly. You will get the following window.

13be305d454dbc38caea5506f97816e6.png

2. Configure the VTK environment

VC++ directory--include directory   

db37d8268b6e1ceb9a7e03c0c8ead610.png

VC++ directory--library directory

d6a40c2d98afbb942200903eb5b01c57.png

linker --input --additional dependencies

be26b36e72178c92039c01a71a474194.png

Debugging--environment (the environment I added here did not work for some reason, so I copied all the dlls under the bin to the release folder of the newly created QT project).

ebcfd01f697333eeb47018eb84635639.png

The VTK environment configuration is complete.

3. Test example

Official website classic example

#include <QVTKOpenGLNativeWidget.h>
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkPointData.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>


#include <QApplication>
#include <QDockWidget>
#include <QGridLayout>
#include <QLabel>
#include <QMainWindow>
#include <QPointer>
#include <QPushButton>
#include <QVBoxLayout>


#include <cmath>
#include <cstdlib>
#include <random>


namespace {
/**
 * Deform the sphere source using a random amplitude and modes and render it in
 * the window
 *
 * @param sphere the original sphere source
 * @param mapper the mapper for the scene
 * @param window the window to render to
 * @param randEng the random number generator engine
 */
void Randomize(vtkSphereSource* sphere, vtkMapper* mapper,
               vtkGenericOpenGLRenderWindow* window, std::mt19937& randEng);
} // namespace


int main(int argc, char* argv[])
{
  QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());


  QApplication app(argc, argv);


  // main window
  QMainWindow mainWindow;
  mainWindow.resize(1200, 900);


  // control area
  QDockWidget controlDock;
  mainWindow.addDockWidget(Qt::LeftDockWidgetArea, &controlDock);


  QLabel controlDockTitle("Control Dock");
  controlDockTitle.setMargin(20);
  controlDock.setTitleBarWidget(&controlDockTitle);


  QPointer<QVBoxLayout> dockLayout = new QVBoxLayout();
  QWidget layoutContainer;
  layoutContainer.setLayout(dockLayout);
  controlDock.setWidget(&layoutContainer);


  QPushButton randomizeButton;
  randomizeButton.setText("Randomize");
  dockLayout->addWidget(&randomizeButton);


  // render area
  QPointer<QVTKOpenGLNativeWidget> vtkRenderWidget =
      new QVTKOpenGLNativeWidget();
  mainWindow.setCentralWidget(vtkRenderWidget);


  // VTK part
  vtkNew<vtkGenericOpenGLRenderWindow> window;
  vtkRenderWidget->setRenderWindow(window.Get());


  vtkNew<vtkSphereSource> sphere;
  sphere->SetRadius(1.0);
  sphere->SetThetaResolution(100);
  sphere->SetPhiResolution(100);


  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(sphere->GetOutputPort());


  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetEdgeVisibility(true);
  actor->GetProperty()->SetRepresentationToSurface();


  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);


  window->AddRenderer(renderer);


  // setup initial status
  std::mt19937 randEng(0);
  ::Randomize(sphere, mapper, window, randEng);


  // connect the buttons
  QObject::connect(&randomizeButton, &QPushButton::released,
                   [&]() { ::Randomize(sphere, mapper, window, randEng); });


  mainWindow.show();


  return app.exec();
}


namespace {
void Randomize(vtkSphereSource* sphere, vtkMapper* mapper,
               vtkGenericOpenGLRenderWindow* window, std::mt19937& randEng)
{
  // generate randomness
  double randAmp = 0.2 + ((randEng() % 1000) / 1000.0) * 0.2;
  double randThetaFreq = 1.0 + (randEng() % 9);
  double randPhiFreq = 1.0 + (randEng() % 9);


  // extract and prepare data
  sphere->Update();
  vtkSmartPointer<vtkPolyData> newSphere;
  newSphere.TakeReference(sphere->GetOutput()->NewInstance());
  newSphere->DeepCopy(sphere->GetOutput());
  vtkNew<vtkDoubleArray> height;
  height->SetName("Height");
  height->SetNumberOfComponents(1);
  height->SetNumberOfTuples(newSphere->GetNumberOfPoints());
  newSphere->GetPointData()->AddArray(height);


  // deform the sphere
  for (int iP = 0; iP < newSphere->GetNumberOfPoints(); iP++)
  {
    double pt[3] = {0.0};
    newSphere->GetPoint(iP, pt);
    double theta = std::atan2(pt[1], pt[0]);
    double phi =
        std::atan2(pt[2], std::sqrt(std::pow(pt[0], 2) + std::pow(pt[1], 2)));
    double thisAmp =
        randAmp * std::cos(randThetaFreq * theta) * std::sin(randPhiFreq * phi);
    height->SetValue(iP, thisAmp);
    pt[0] += thisAmp * std::cos(theta) * std::cos(phi);
    pt[1] += thisAmp * std::sin(theta) * std::cos(phi);
    pt[2] += thisAmp * std::sin(phi);
    newSphere->GetPoints()->SetPoint(iP, pt);
  }
  newSphere->GetPointData()->SetScalars(height);


  // reconfigure the pipeline to take the new deformed sphere
  mapper->SetInputDataObject(newSphere);
  mapper->SetScalarModeToUsePointData();
  mapper->ColorByArrayComponent("Height", 0);
  window->Render();
}
} // namespace

c3fc4b4104e2839df91ae1042c3c198f.png

Guess you like

Origin blog.csdn.net/weixin_41202834/article/details/130920132