0
点赞
收藏
分享

微信扫一扫

VTK编程指南<二>:VTK9.3.1+VS2019+Qt5.15.2编译及环境配置

8052cf60ff5c 2024-11-26 阅读 153

  
  VTK(Visualization Toolkit)是一个开源的、跨平台的三维可视化开发库,用于处理和可视化三维数据。它提供了一系列算法和工具,用于创建、操作和渲染复杂的三维图形,并支持多种数据表示方式,包括点、线、面、体等。VTK提供了一套高效的算法,用于可视化医学图像、流体动力学模拟、地理信息系统等领域的数据。

1、VTK源码下载

  源码下载地址:https://vtk.org/download/
在这里插入图片描述

  将VTK-9.3.1.tar.gz ,VTKData-9.3.1.tar.gz,VTKLargeData-9.3.1.tar.gz解压到同一目录,并创建build和SDK文件夹。

在这里插入图片描述

在这里插入图片描述

2、CMake设置

  打开CMake-gui进行设置:
在这里插入图片描述
  开启支持Qt编译;电脑上会自动找到对应的Qt路径(Qt已经在电脑环境变量添加过路径)。

在这里插入图片描述

在这里插入图片描述

点击“config”按钮,没有错误提示后点击“Generate”按钮,最后点击“open project”按钮打开VS工程。

在这里插入图片描述

3、VS编译

  点击Open project 按钮打开VS项目工程,然后先选择 ALL_BUILD进行生成,随后选择INSTALL进行sdk整理。

在这里插入图片描述
在这里插入图片描述
编译后的VTK sdk如下所示:
在这里插入图片描述

编译好的SDK下载地址:https://download.csdn.net/download/m0_37251750/89908158

4、示例demo

  编译完成后我们来验证一下VTK编译的是否正确可用。此处可以参考VTK提供的示例代码。我们用了Qt+VTK来进行验证。https://examples.vtk.org/site/。

在这里插入图片描述

4.1 Qt_VTK演示效果

  演示效果如下图:

在这里插入图片描述

4.2 源码

#include <QtCore/QCoreApplication>

#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

普通示例:
在这里插入图片描述

// CylinderExample.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);// VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);


int main(int, char* [])
{
	vtkNew<vtkNamedColors> colors;

	// Set the background color.
	std::array<unsigned char, 4> bkg{ {26, 51, 102, 255} };
	colors->SetColor("BkgColor", bkg.data());

	// This creates a polygonal cylinder model with eight circumferential facets
	// (i.e, in practice an octagonal prism).
	vtkNew<vtkCylinderSource> cylinder;
	cylinder->SetResolution(8);

	// The mapper is responsible for pushing the geometry into the graphics
	// library. It may also do color mapping, if scalars or other attributes are
	// defined.
	vtkNew<vtkPolyDataMapper> cylinderMapper;
	cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

	// The actor is a grouping mechanism: besides the geometry (mapper), it
	// also has a property, transformation matrix, and/or texture map.
	// Here we set its color and rotate it around the X and Y axes.
	vtkNew<vtkActor> cylinderActor;
	cylinderActor->SetMapper(cylinderMapper);
	cylinderActor->GetProperty()->SetColor(
		colors->GetColor4d("Tomato").GetData());
	cylinderActor->RotateX(30.0);
	cylinderActor->RotateY(-45.0);

	// The renderer generates the image
	// which is then displayed on the render window.
	// It can be thought of as a scene to which the actor is added
	vtkNew<vtkRenderer> renderer;
	renderer->AddActor(cylinderActor);
	renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
	// Zoom in a little by accessing the camera and invoking its "Zoom" method.
	renderer->ResetCamera();
	renderer->GetActiveCamera()->Zoom(1.5);

	// The render window is the actual GUI window
	// that appears on the computer screen
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->SetSize(300, 300);
	renderWindow->AddRenderer(renderer);
	renderWindow->SetWindowName("Cylinder");

	// The render window interactor captures mouse events
	// and will perform appropriate camera or actor manipulation
	// depending on the nature of the events.
	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

	// This starts the event loop and as a side effect causes an initial render.
	renderWindow->Render();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

4.3 项目属性设定

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

如果不想麻烦去一个个*.lib匹配,可以粗暴一些把所有lib文件都放进依赖项,具体操作:

cmd 进入lib文件夹,输入下面命令:dir /b *.lib>out.txt 即可快速生成包含所有lib的txt文件。
vtkGUISupportQt-9.3d.lib
vtkRenderingCore-9.3d.lib
vtkRenderingOpenGL2-9.3d.lib
vtkCommonCore-9.3d.lib
vtkCommonDataModel-9.3d.lib
vtkCommonExecutionModel-9.3d.lib
vtkFiltersCore-9.3d.lib
vtkFiltersSources-9.3d.lib
vtksys-9.3d.lib
举报

相关推荐

0 条评论