现象
vtk在win10 + vs2019 + cmake 内用cmake编译了vtk9.2.6,编译成功但是运行cube的demo example程序,窗口没有显示,没有看到立方体。没有报错也没有任何输出。
cxx代码:
/*=========================================================================
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;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.8...3.12 FATAL_ERROR)
project(DataManipulation)
find_package(VTK
COMPONENTS
CommonColor
FiltersGeometry
RenderingCore)
if (NOT VTK_FOUND)
message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif ()
add_executable(Arrays MACOSX_BUNDLE Arrays.cxx)
add_executable(Cube MACOSX_BUNDLE Cube.cxx)
add_executable(SGrid MACOSX_BUNDLE SGrid.cxx)
add_executable(RGrid MACOSX_BUNDLE RGrid.cxx)
target_link_libraries(Arrays PRIVATE ${VTK_LIBRARIES})
target_link_libraries(Cube PRIVATE ${VTK_LIBRARIES})
target_link_libraries(SGrid PRIVATE ${VTK_LIBRARIES})
target_link_libraries(RGrid PRIVATE ${VTK_LIBRARIES})
vtk_module_autoinit(
TARGETS Arrays
Cube
SGrid
RGrid
MODULES ${VTK_LIBRARIES})
vs2019编译运行输出:
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\Examples\Build\DataManipulation\Cxx\Debug\Cube.exe”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\ntdll.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\kernel32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\KernelBase.dll”。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkRenderingCore-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonColor-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonDataModel-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonCore-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\user32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\win32u.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\gdi32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\gdi32full.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\msvcp_win.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\ucrtbase.dll”。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtksys-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\ws2_32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\rpcrt4.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\psapi.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\shell32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\advapi32.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\msvcrt.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\sechost.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\bcrypt.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\msvcp140d.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\vcruntime140d.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\vcruntime140_1d.dll”。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\ucrtbased.dll”。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkFiltersGeometry-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkFiltersSources-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkFiltersGeneral-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkFiltersCore-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonExecutionModel-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonComputationalGeometry-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonSystem-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonTransforms-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonMisc-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkCommonMath-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkpugixml-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkloguru-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\dbghelp.dll”。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkfmt-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Users\dell\Desktop\VTK-9.2.6\build\bin\Debug\vtkkissfft-9.2d.dll”。已加载符号。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\imm32.dll”。
线程 0x3f10 已退出,返回值为 0 (0x0)。
“Cube.exe”(Win32): 已加载“C:\Windows\System32\kernel.appcore.dll”。
线程 0x13b8 已退出,返回值为 0 (0x0)。
线程 0x5188 已退出,返回值为 0 (0x0)。
程序“[20660] Cube.exe”已退出,返回值为 0 (0x0)。
程序运行显示:

没有任何报错、闪退、崩溃,也没有看到vtk窗口。
解决
经过调研是官方提供的CMakeLists.txt少写了几个vtk的库。

正确的CMakeLists.txt
cmake_minimum_required(VERSION 3.8...3.12 FATAL_ERROR)
project(DataManipulation)
find_package(VTK
COMPONENTS
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif ()
add_executable(Arrays MACOSX_BUNDLE Arrays.cxx)
add_executable(Cube MACOSX_BUNDLE Cube.cxx)
add_executable(SGrid MACOSX_BUNDLE SGrid.cxx)
add_executable(RGrid MACOSX_BUNDLE RGrid.cxx)
target_link_libraries(Arrays PRIVATE ${VTK_LIBRARIES})
target_link_libraries(Cube PRIVATE ${VTK_LIBRARIES})
target_link_libraries(SGrid PRIVATE ${VTK_LIBRARIES})
target_link_libraries(RGrid PRIVATE ${VTK_LIBRARIES})
vtk_module_autoinit(
TARGETS Arrays
Cube
SGrid
RGrid
MODULES ${VTK_LIBRARIES})
再次使用cmake Configure 、Generate、打开vs2019 sln、编译运行。

可以看到vtk窗口 立方体。

结论
1.官方提供的demo cmake不一定正确。
2.vc项目缺少lib库,程序不报错。
3.可能会崩溃在开源库源代码,所以要用最新的开源库,要懂所有的c++语法和最新的c++标准。
231

被折叠的 条评论
为什么被折叠?



