上篇博文偷了个懒,编译安装完成后就没有写安装测试的内容,这两天有同学一块讨论了这个问题,所以想写出来和大家分享一下。
本篇博文的主要想说的内容是两个部分,一部分是如何用cmake来讲vtk和qt结合起来构建vs2010内的工程的具体方法(学会这个以后如果以后还想在这个里面添加itk就可以直接类比vtk的方法,会简单很多),另一部分主要介绍cmakelists.txt的写法。然后博文的组织上是分为单独vtk的测试vtk+qt的测试两部分。
1、cmake管理vtk工程
1.1新建工程
在自己工作的目录下新建文件夹Cylinder,该文件夹下新建一个cpp文件Cylinder.cpp,该文件为工程源文件,一个CMakeLists.txt文件,该文件为该工程的cmake配置文件。
打开Cylinder.cpp,拷贝下述代码进去保存:
//
// This simple example shows how to do basic rendering and pipeline
// creation using C++.
//
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
int main()
{
// This creates a polygonal cylinder model with eight circumferential facets.
//
vtkCylinderSource *cylinder = 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.
//
vtkPolyDataMapper *cylinderMapper = 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 -22.5 degrees.
vtkActor *cylinderActor = vtkActor::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// Create the graphics structure. The renderer renders into the
// render window. The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
//
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
// Add the actors to the renderer, set the background and size
//
ren1->AddActor(cylinderActor);
ren1->SetBackground(0.1, 0.2, 0.4);
renWin->SetSize(200, 200);
// We'll zoom in a little by accessing the camera and invoking a "Zoom"
// method on it.
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1.5);
renWin->Render();
// This starts the event loop and as a side effect causes an initial render.
iren->Start();
// Exiting from here, we have to delete all the instances that
// have been created.
cylinder->Delete();
cylinderMapper->Delete();
cylinderActor->Delete();
ren1->Delete();
renWin->Delete();
iren->Delete();
return 0;
}
打开cmakelists.txt,拷贝下述代码进去保存:
cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
project(Cylinder)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(${PROJECT_NAME} Cylinder.cpp)
target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
关于上面的代码的讲解,可以参考博文http://blog.csdn.net/www_doling_net/article/details/8532742中的介绍。
1.2 cmake配置生成工程文件
运行cmake的gui程序,打开界面,在where the source code中添加你的工程路径,然后添加你想要配置生成vs2010的工程的目录,如下图1: