VTK 8.2.0编译与配置
参考:https://blog.csdn.net/Pure_vv/article/details/102058609
概要
一、下载VTK
-
VTK-8.2.0
-
VTK Data
二、建立文件结构
──VTK
├─Data (将Data.tgz解压到Data中)
├─Files (存放编译结果)
├─Install
├─VTK-8.2.0 (存放源码,将VTK-8.2.0.zip解压到此处)
三、CMake 编译
-
设置源码路径和编译路径如下
-
点击Configure,选择自己对应的VS版本,点击Finish
-
配置结束后,勾选BUILD_EXAMPLES,修改CMAKE_INSTALL_PREFIX路径为我们建立的文件目录中的Install文件夹,之后点击Configure
-
配置完成后点击Generate,完成后点击Open Project,会自动在VS中打开项目
-
进入VS后,右键ALL_BUILD,选择生成;完成后,找到INSTALL,同样右键,选择生成
四、运行demo
-
右键Cube,选择设为启动项目
-
上方设置为Debug与x64后,点击本地Windows调试
-
运行结束,成功显示demo!
五、新建并配置VTK项目
-
打开VS,创建一个空项目,命名为VTK_test
- 右键项目名,点击属性
-
修改配置为Debug,平台为x64,之后在VC++ 目录下,编辑包含目录为Install文件夹下的include\vtk-8.2,库目录为Install文件夹下lib文件夹
-
随后,点击链接器→输入→附加依赖项→编辑,在弹出的窗口中填入opengl32.lib和Install/lib文件夹下所有.lib文件的名字,点击确定
我的文件名如下:
opengl32.lib vtkChartsCore-8.2.lib vtkCommonColor-8.2.lib vtkCommonComputationalGeometry-8.2.lib vtkCommonCore-8.2.lib vtkCommonDataModel-8.2.lib vtkCommonExecutionModel-8.2.lib vtkCommonMath-8.2.lib vtkCommonMisc-8.2.lib vtkCommonSystem-8.2.lib vtkCommonTransforms-8.2.lib vtkDICOMParser-8.2.lib vtkDomainsChemistry-8.2.lib vtkDomainsChemistryOpenGL2-8.2.lib vtkdoubleconversion-8.2.lib vtkexodusII-8.2.lib vtkexpat-8.2.lib vtkFiltersAMR-8.2.lib vtkFiltersCore-8.2.lib vtkFiltersExtraction-8.2.lib vtkFiltersFlowPaths-8.2.lib vtkFiltersGeneral-8.2.lib vtkFiltersGeneric-8.2.lib vtkFiltersGeometry-8.2.lib vtkFiltersHybrid-8.2.lib vtkFiltersHyperTree-8.2.lib vtkFiltersImaging-8.2.lib vtkFiltersModeling-8.2.lib vtkFiltersParallel-8.2.lib vtkFiltersParallelImaging-8.2.lib vtkFiltersPoints-8.2.lib vtkFiltersProgrammable-8.2.lib vtkFiltersSelection-8.2.lib vtkFiltersSMP-8.2.lib vtkFiltersSources-8.2.lib vtkFiltersStatistics-8.2.lib vtkFiltersTexture-8.2.lib vtkFiltersTopology-8.2.lib vtkFiltersVerdict-8.2.lib vtkfreetype-8.2.lib vtkGeovisCore-8.2.lib vtkgl2ps-8.2.lib vtkglew-8.2.lib vtkhdf5-8.2.lib vtkhdf5_hl-8.2.lib vtkImagingColor-8.2.lib vtkImagingCore-8.2.lib vtkImagingFourier-8.2.lib vtkImagingGeneral-8.2.lib vtkImagingHybrid-8.2.lib vtkImagingMath-8.2.lib vtkImagingMorphological-8.2.lib vtkImagingSources-8.2.lib vtkImagingStatistics-8.2.lib vtkImagingStencil-8.2.lib vtkInfovisCore-8.2.lib vtkInfovisLayout-8.2.lib vtkInteractionImage-8.2.lib vtkInteractionStyle-8.2.lib vtkInteractionWidgets-8.2.lib vtkIOAMR-8.2.lib vtkIOAsynchronous-8.2.lib vtkIOCityGML-8.2.lib vtkIOCore-8.2.lib vtkIOEnSight-8.2.lib vtkIOExodus-8.2.lib vtkIOExport-8.2.lib vtkIOExportOpenGL2-8.2.lib vtkIOExportPDF-8.2.lib vtkIOGeometry-8.2.lib vtkIOImage-8.2.lib vtkIOImport-8.2.lib vtkIOInfovis-8.2.lib vtkIOLegacy-8.2.lib vtkIOLSDyna-8.2.lib vtkIOMINC-8.2.lib vtkIOMovie-8.2.lib vtkIONetCDF-8.2.lib vtkIOParallel-8.2.lib vtkIOParallelXML-8.2.lib vtkIOPLY-8.2.lib vtkIOSegY-8.2.lib vtkIOSQL-8.2.lib vtkIOTecplotTable-8.2.lib vtkIOVeraOut-8.2.lib vtkIOVideo-8.2.lib vtkIOXML-8.2.lib vtkIOXMLParser-8.2.lib vtkjpeg-8.2.lib vtkjsoncpp-8.2.lib vtklibharu-8.2.lib vtklibxml2-8.2.lib vtkLocalExample-8.2.lib vtklz4-8.2.lib vtklzma-8.2.lib vtkmetaio-8.2.lib vtkNetCDF-8.2.lib vtkogg-8.2.lib vtkParallelCore-8.2.lib vtkpng-8.2.lib vtkproj-8.2.lib vtkpugixml-8.2.lib vtkRenderingAnnotation-8.2.lib vtkRenderingContext2D-8.2.lib vtkRenderingContextOpenGL2-8.2.lib vtkRenderingCore-8.2.lib vtkRenderingFreeType-8.2.lib vtkRenderingGL2PSOpenGL2-8.2.lib vtkRenderingImage-8.2.lib vtkRenderingLabel-8.2.lib vtkRenderingLOD-8.2.lib vtkRenderingOpenGL2-8.2.lib vtkRenderingVolume-8.2.lib vtkRenderingVolumeOpenGL2-8.2.lib vtksqlite-8.2.lib vtksys-8.2.lib vtktheora-8.2.lib vtktiff-8.2.lib vtkverdict-8.2.lib vtkViewsContext2D-8.2.lib vtkViewsCore-8.2.lib vtkViewsInfovis-8.2.lib vtkzlib-8.2.lib
-
配置完成后点击应用
-
在系统变量中,添加Install/bin到Path环境变量中
-
右键源文件, 选择添加, 新建项
-
选择C++文件,命名为test.cpp,点击添加
-
复制cube中Cube.cxx的代码至test.cpp中,用于测试。
代码内容贴在此处:
/*========================================================================= Program: Visualization Toolkit Module: Cube.cxx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ // This example shows how to manually create vtkPolyData. // For a python version, please see: // [Cube](https://lorensen.github.io/VTKExamples/site/Python/DataManipulation/Cube/) #include <vtkActor.h> #include <vtkCamera.h> #include <vtkCellArray.h> #include <vtkFloatArray.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkPointData.h> #include <vtkPoints.h> #include <vtkPolyData.h> #include <vtkPolyDataMapper.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <array> int main() { vtkNew<vtkNamedColors> colors; std::array<std::array<double, 3>, 8> pts = { {{{0, 0, 0}}, {{1, 0, 0}}, {{1, 1, 0}}, {{0, 1, 0}}, {{0, 0, 1}}, {{1, 0, 1}}, {{1, 1, 1}}, {{0, 1, 1}}} }; // The ordering of the corner points on each face. std::array<std::array<vtkIdType, 4>, 6> ordering = { {{{0, 1, 2, 3}}, {{4, 5, 6, 7}}, {{0, 1, 5, 4}}, {{1, 2, 6, 5}}, {{2, 3, 7, 6}}, {{3, 0, 4, 7}}} }; // We'll create the building blocks of polydata including data attributes. vtkNew<vtkPolyData> cube; vtkNew<vtkPoints> points; vtkNew<vtkCellArray> polys; vtkNew<vtkFloatArray> scalars; // Load the point, cell, and data attributes. for (auto i = 0ul; i < pts.size(); ++i) { points->InsertPoint(i, pts[i].data()); scalars->InsertTuple1(i, i); } for (auto&& i : ordering) { polys->InsertNextCell(vtkIdType(i.size()), i.data()); } // We now assign the pieces to the vtkPolyData. cube->SetPoints(points); cube->SetPolys(polys); cube->GetPointData()->SetScalars(scalars); // Now we'll look at it. vtkNew<vtkPolyDataMapper> cubeMapper; cubeMapper->SetInputData(cube); cubeMapper->SetScalarRange(cube->GetScalarRange()); vtkNew<vtkActor> cubeActor; cubeActor->SetMapper(cubeMapper); // The usual rendering stuff. vtkNew<vtkCamera> camera; camera->SetPosition(1, 1, 1); camera->SetFocalPoint(0, 0, 0); vtkNew<vtkRenderer> renderer; vtkNew<vtkRenderWindow> renWin; renWin->AddRenderer(renderer); vtkNew<vtkRenderWindowInteractor> iren; iren->SetRenderWindow(renWin); renderer->AddActor(cubeActor); renderer->SetActiveCamera(camera); renderer->ResetCamera(); renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData()); renWin->SetSize(600, 600); // interact with data renWin->Render(); iren->Start(); return EXIT_SUCCESS; }
-
上方设置为Debug与x64后,点击本地Windows调试器,运行程序
-
报错解决!
-
上述步骤完成后,点击运行却提示引发异常,同时弹出窗口显示Error: no override found for 'vtkPolyDataMapper'
-
解决方法如下:
-
查看Install/lib文件夹中与OpenGL有关的.lib文件名,我的如下所示
-
在代码中添加如下三行代码
#include <vtkautoinit.h> VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle);
注意:如果.lib文件结尾为OpenGL,没有2,则第二行代码也需对应写作
VTK_MODULE_INIT(vtkRenderingOpenGL);
-
最终程序成功运行!
-
-
补充
查询时,发现其他人在完成上面步骤后,运行会出现下方的错误
他们的解决方法为:在链接器→输入→附加依赖项中添加opengl32.lib库文件
-