Skip to content

PolyDataConnectivityFilter LargestRegion

vtk-examples/Cxx/PolyData/PolyDataConnectivityFilter_LargestRegion


Description

This example uses vtkPolyDataConnectivityFilter to keep the largest connected region of a mesh. Note that this is based on the number of points, not the actual area of the region.

The original vtkPolyData is red, on top. The extracted region is yellow, on the bottom.

Note

Contributed by: Jinyoung Hwang

Other languages

See (CSharp)

Question

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

Code

PolyDataConnectivityFilter_LargestRegion.cxx

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

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

  // Small sphere
  vtkNew<vtkSphereSource> sphereSource1;
  sphereSource1->SetRadius(5);

  // Large sphere
  vtkNew<vtkSphereSource> sphereSource2;
  sphereSource2->SetRadius(10);
  sphereSource2->SetCenter(25, 0, 0);
  sphereSource2->SetThetaResolution(10);
  sphereSource2->SetPhiResolution(10);

  vtkNew<vtkAppendPolyData> appendFilter;
  appendFilter->AddInputConnection(sphereSource1->GetOutputPort());
  appendFilter->AddInputConnection(sphereSource2->GetOutputPort());

  vtkNew<vtkPolyDataConnectivityFilter> connectivityFilter;
  connectivityFilter->SetInputConnection(appendFilter->GetOutputPort());
  connectivityFilter->SetExtractionModeToLargestRegion();

  // Create a mapper and actor for original data
  vtkNew<vtkPolyDataMapper> originalMapper;
  originalMapper->SetInputConnection(appendFilter->GetOutputPort());

  vtkNew<vtkActor> originalActor;
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(
      colors->GetColor3d("Tomato").GetData());

  // Create a mapper and actor for extracted data
  vtkNew<vtkPolyDataMapper> extractedMapper;
  extractedMapper->SetInputConnection(connectivityFilter->GetOutputPort());
  extractedMapper->Update();

  vtkNew<vtkActor> extractedActor;
  extractedActor->GetProperty()->SetColor(
      colors->GetColor3d("Banana").GetData());
  extractedActor->SetMapper(extractedMapper);
  extractedActor->SetPosition(0, -20, 0);

  // Visualization
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(originalActor);
  renderer->AddActor(extractedActor);
  renderer->GradientBackgroundOn();
  renderer->SetBackground(colors->GetColor3d("Gold").GetData());
  renderer->SetBackground2(colors->GetColor3d("Silver").GetData());

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

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PolyDataConnectivityFilter_LargestRegion)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PolyDataConnectivityFilter_LargestRegion: 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(PolyDataConnectivityFilter_LargestRegion MACOSX_BUNDLE PolyDataConnectivityFilter_LargestRegion.cxx )
  target_link_libraries(PolyDataConnectivityFilter_LargestRegion PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PolyDataConnectivityFilter_LargestRegion
  MODULES ${VTK_LIBRARIES}
)

Download and Build PolyDataConnectivityFilter_LargestRegion

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

cd PolyDataConnectivityFilter_LargestRegion/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:

./PolyDataConnectivityFilter_LargestRegion

WINDOWS USERS

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