vtk example cube显示立方体

现象

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++标准。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值