VTK Learning Two

  • Post author:
  • Post category:其他




VTK 图形vtkIntersectionPolyDataFilter使用



引言

`vtkIntersectionPolyDataFilter`计算两个`vtkPolyData`对象的交集。
第一个输出两个`vtkPolyData`对象相交部分的线段集合。



描述

计算两个球体的交线。



CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(IntersectionPolyDataFilter)

find_package(VTK REQUIRED)

include(${VTK_USE_FILE})
add_executable(IntersectionPolyDataFilter IntersectionPolyDataFilter.cxx )
target_link_libraries(IntersectionPolyDataFilter ${VTK_LIBRARIES})



IntersectionPolyDataFilter.cxx

#include <vtkActor.h>
#include <vtkIntersectionPolyDataFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

#include <vtkPlaneSource.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource1 = vtkSmartPointer<vtkSphereSource>::New();
  sphereSource1->SetCenter(0.0, 0.0, 0.0);
  sphereSource1->SetRadius(2.0f);
  sphereSource1->Update();

  vtkSmartPointer<vtkPolyDataMapper> sphere1Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  sphere1Mapper->SetInputConnection( sphereSource1->GetOutputPort() );
  sphere1Mapper->ScalarVisibilityOff();
  vtkSmartPointer<vtkActor> sphere1Actor = vtkSmartPointer<vtkActor>::New();
  sphere1Actor->SetMapper( sphere1Mapper );
  sphere1Actor->GetProperty()->SetOpacity(.3);
  sphere1Actor->GetProperty()->SetColor(1,0,0);

  vtkSmartPointer<vtkSphereSource> sphereSource2 = vtkSmartPointer<vtkSphereSource>::New();
  sphereSource2->SetCenter(1.0, 0.0, 0.0);
  sphereSource2->SetRadius(2.0f);
  vtkSmartPointer<vtkPolyDataMapper> sphere2Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  sphere2Mapper->SetInputConnection( sphereSource2->GetOutputPort() );
  sphere2Mapper->ScalarVisibilityOff();
  vtkSmartPointer<vtkActor> sphere2Actor = vtkSmartPointer<vtkActor>::New();
  sphere2Actor->SetMapper( sphere2Mapper );
  sphere2Actor->GetProperty()->SetOpacity(.3);
  sphere2Actor->GetProperty()->SetColor(0,1,0);
 //测试平面的显示
  vtkSmartPointer<vtkPlaneSource> planeSource = vtkSmartPointer<vtkPlaneSource>::New();
  planeSource->SetOrigin(0.5, 0, 0);
  planeSource->SetNormal(1, 0, 0);
  planeSource->SetResolution(10,10);
  
  planeSource->Update();
  vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  planeMapper->SetInputConnection(planeSource->GetOutputPort());
  planeMapper->ScalarVisibilityOff();
  vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();
  planeActor->SetMapper(planeMapper);
  planeActor->GetProperty()->SetOpacity(.5);
  planeActor->GetProperty()->SetColor(0, 0, 1);

  vtkSmartPointer<vtkIntersectionPolyDataFilter> intersectionPolyDataFilter =
    vtkSmartPointer<vtkIntersectionPolyDataFilter>::New();
  intersectionPolyDataFilter->SetInputConnection( 0, sphereSource1->GetOutputPort() );
  intersectionPolyDataFilter->SetInputConnection( 1, sphereSource2->GetOutputPort() );
  intersectionPolyDataFilter->Update();

  vtkSmartPointer<vtkPolyDataMapper> intersectionMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  intersectionMapper->SetInputConnection( intersectionPolyDataFilter->GetOutputPort() );
  intersectionMapper->ScalarVisibilityOff();

  vtkSmartPointer<vtkActor> intersectionActor = vtkSmartPointer<vtkActor>::New();
  intersectionActor->SetMapper( intersectionMapper );

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  renderer->AddViewProp(sphere1Actor);
  renderer->AddViewProp(sphere2Actor);
  renderer->AddViewProp(intersectionActor);
  renderer->AddViewProp(planeActor);

  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer( renderer );

  vtkSmartPointer<vtkRenderWindowInteractor> renWinInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renWinInteractor->SetRenderWindow( renderWindow );

  renderWindow->Render();
  renWinInteractor->Start();

  return EXIT_SUCCESS;
}



输出结果

在这里插入图片描述



版权声明:本文为mrbaolong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。