1. 程式人生 > >Ubuntu16.04 VTK6.3.0 測試

Ubuntu16.04 VTK6.3.0 測試

新建一個測試資料夾,在其中新建下面兩個即可。

1.Plane.cxx

#include <vtkVersion.h>
#include <vtkPlaneSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

int main(int, char *[])
{
  // Create a plane
  vtkSmartPointer<vtkPlaneSource> planeSource =
    vtkSmartPointer<vtkPlaneSource>::New();
  planeSource->SetCenter(1.0, 0.0, 0.0);
  planeSource->SetNormal(1.0, 0.0, 1.0);
  planeSource->Update();
  
  vtkPolyData* plane = planeSource->GetOutput();
    
  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  mapper->SetInput(plane);
#else
  mapper->SetInputData(plane);
#endif

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  // Create a renderer, render window and interactor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(.1,.2,.3); // Background color dark blue

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
  
  return EXIT_SUCCESS;
}

2.CMakeList.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(Plane)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(Plane MACOSX_BUNDLE Plane.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(Plane ${VTK_LIBRARIES})
else()
  target_link_libraries(Plane vtkHybrid vtkWidgets)
endif()

3.編譯和測試

cmake .
make

4.效果

在這裡插入圖片描述

參考官網:https://www.vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Plane