VTK8.2 VS2017 Windows10 C++安装
1、安装
首先通过访问 https://vtk.org/download/ 官方网站获取所需软件包并完成解压操作。
如果当前系统中没有 cmave 软件可以选择在 Windows 上安装 cmave 软件 具备图形界面 真的很方便。
启动 cmake 并导航至 VTK 项目的根 目录 将该根 目录 设为源 项目 目录 创建一个新文件夹 存放项目的编译 构建文 件 并建议将其与源 项目 目录 放在同一级。
进入 cmake 配置 选项 其中默认选择了 Visual Studio 2017 作 为编译器。
通常情况下,默认的是win32也就是32位的应用程序。
若要在构建x64位的应用程序,请继续选择x64平台进行操作;否则,在继续改动win32项目的构建时会遇到诸多问题。
开始我并没有注意到这一问题;因为我要使用QT 64位版本的应用程序需求而选择了相应的开发环境;因此将项目的构建类型设置为x64;然而在更改的过程中发现了很多配置上的差异;与其手动逐步修改各个配置文件较为繁琐麻烦;不如在cmake编译器设置阶段就指定生成x64bit项目的选项;这样就能有效解决问题。
- 配置完成时会显示许多红色设置项,在点击一次"config"按钮后这些红色项会消失。除非有特殊需求,默认情况下这些设置可以保持原样以简化配置流程。特别注意的是,在此界面中有一个名为CMAKE_INSTALL_PREFIX的重要设置项,请确保该设置项被正确配置以便后续操作顺利进行。
该设置项决定了最终编译好的头文件(.h)、动态链接库(.lib)以及可执行文件(.dll)将会被放置的位置。您可以根据实际需求自定义路径并点击"generate"按钮启动CMake构建过程。
配置完成后会自动触发CMake的工作流程以生成所需的项目。 - 在创建新的构建目录后请找到并打开.sln文件开始操作由于该操作需要较长时间请稍等片刻
完成上述步骤后您将被引导至安装项目的创建界面
2、配置
- 创建一个新的空C++项目。
- 将包含头文件的路径导航至VC++项目的属性窗口中的配置属性选项卡,在VC++目录下选择"包含目录"字段并添加vtk相关头文件夹。
- 进入链接器选项卡后,在"附加库目录"字段中指定包含库文件路径,并将与VTK相关的lib文件夹加入其中。
- 在vtk lib文件夹内创建新的txt文档,并使用批处理脚本将*.lib转为*.bat格式(具体命令:DIR *.lib /B > LIBLIST.TXT),然后更改后缀名为.bat并运行该脚本以生成新的LIBLIST.TXT。
- 在链接器设置中选择"附加依赖项"字段并输入之前生成的所有VTK相关库名称。
- 进入调试选项卡后,在环境变量字段中设置PATH环境变量指向生成的VTK DLL存放位置(通常位于bin文件夹)。
这样.h,lib,dll文件都配置好了,可以写代码了。
3、测试
附录是官网提供的C++案例(例子),全部案例的网址为 https://lorensen.github.io/VTKExamples/site/Cxx/
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
因为官方提供了cmakelists文件,并使用cmake构建项目,在本项目中直接引用了这些代码内容。这与预期中的配置存在差异,并且需要执行必要的初始化步骤才能避免错误的发生。如果未进行相应的配置,则会导致错误出现。例如:
Error: no override found for 'vtkPolyDataMapper'.
很多用户遇到一个问题:具体解决方案及原因说明如下:
https://stackoverflow.com/questions/18642155/no-override-found-for-vtkpolydatamapper
运行成功后系统将生成一个圆柱体模型。
4、附录
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <array>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);//和官方的例子有些不同,下面解释
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char *[]) {
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// 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).
vtkSmartPointer<vtkCylinderSource> cylinder =
vtkSmartPointer<vtkCylinderSource>::New();
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.
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
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.
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
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
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
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
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
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.
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
