Skip to content

IntersectionPolyDataFilter

Repository source: IntersectionPolyDataFilter


Other languages

See (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

IntersectionPolyDataFilter.cxx

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

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkSphereSource> sphereSource1;
  sphereSource1->SetCenter(0.0, 0.0, 0.0);
  sphereSource1->SetRadius(2.0);
  sphereSource1->Update();
  vtkNew<vtkPolyDataMapper> sphere1Mapper;
  sphere1Mapper->SetInputConnection(sphereSource1->GetOutputPort());
  sphere1Mapper->ScalarVisibilityOff();
  vtkNew<vtkActor> sphere1Actor;
  sphere1Actor->SetMapper(sphere1Mapper);
  sphere1Actor->GetProperty()->SetOpacity(.3);
  sphere1Actor->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());

  vtkNew<vtkSphereSource> sphereSource2;
  sphereSource2->SetCenter(1.0, 0.0, 0.0);
  sphereSource2->SetRadius(2.0);
  vtkNew<vtkPolyDataMapper> sphere2Mapper;
  sphere2Mapper->SetInputConnection(sphereSource2->GetOutputPort());
  sphere2Mapper->ScalarVisibilityOff();
  vtkNew<vtkActor> sphere2Actor;
  sphere2Actor->SetMapper(sphere2Mapper);
  sphere2Actor->GetProperty()->SetOpacity(.3);
  sphere2Actor->GetProperty()->SetColor(colors->GetColor3d("Lime").GetData());

  vtkNew<vtkIntersectionPolyDataFilter> intersectionPolyDataFilter;
  intersectionPolyDataFilter->SetInputConnection(
      0, sphereSource1->GetOutputPort());
  intersectionPolyDataFilter->SetInputConnection(
      1, sphereSource2->GetOutputPort());
  intersectionPolyDataFilter->Update();

  vtkNew<vtkPolyDataMapper> intersectionMapper;
  intersectionMapper->SetInputConnection(
      intersectionPolyDataFilter->GetOutputPort());
  intersectionMapper->ScalarVisibilityOff();

  vtkNew<vtkActor> intersectionActor;
  intersectionActor->SetMapper(intersectionMapper);
  intersectionActor->GetProperty()->SetColor(
      colors->GetColor3d("White").GetData());

  vtkNew<vtkRenderer> renderer;
  renderer->AddViewProp(sphere1Actor);
  renderer->AddViewProp(sphere2Actor);
  renderer->AddViewProp(intersectionActor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("IntersectionPolyDataFilter");

  vtkNew<vtkRenderWindowInteractor> renWinInteractor;
  renWinInteractor->SetRenderWindow(renderWindow);

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(IntersectionPolyDataFilter)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersGeneral
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "IntersectionPolyDataFilter: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(IntersectionPolyDataFilter MACOSX_BUNDLE IntersectionPolyDataFilter.cxx )
  target_link_libraries(IntersectionPolyDataFilter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS IntersectionPolyDataFilter
  MODULES ${VTK_LIBRARIES}
)

Download and Build IntersectionPolyDataFilter

Click here to download IntersectionPolyDataFilter and its CMakeLists.txt file. Once the tarball IntersectionPolyDataFilter.tar has been downloaded and extracted,

cd IntersectionPolyDataFilter/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./IntersectionPolyDataFilter

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.