如何写VTK的Cmake文件

如果按照官方指导的步骤在Mac上成功编译安装了VTK,那么下一步如何使用VTK库呢?用Cmake是一种简单的途径,那么如何写CMakeLists.txt呢,参考官方的例子

必须熟悉新版本的vtk的cmake文件命令,比如老版本的例子中的vtk_module_configinclude(${VTK_USE_FILE})在新版8.90.0中都不适用了。

调用vtk的源代码: 绘制一个圆锥


#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include 
int main()
{
  
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );

  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );

  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );
  vtkSmartPointer renderWindowInteractor =
  vtkSmartPointer::New();
  renderWindowInteractor->SetRenderWindow(renWin);

  renWin->Render();
  ren1->GetActiveCamera()->Azimuth( 1 );

  renderWindowInteractor->Start();

  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren1->Delete();
  renWin->Delete();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(cone)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkFiltersSources
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping cone: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(conne MACOSX_BUNDLE Cone.cxx )
  target_link_libraries(cone PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(cone MACOSX_BUNDLE Cone.cxx )
  target_link_libraries(cone PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS cone
    MODULES ${VTK_LIBRARIES}
    )
endif () 

结果

make之后会生成一个名为cone.app的Mac程序包,直接双击就可以运行。.app的生成是由MACOSX_BUNDLE变量控制的

绘图结果

你可能感兴趣的:(如何写VTK的Cmake文件)