PolyDataConnectivityFilter SpecifiedRegion

VTKExamples/Cxx/PolyData/PolyDataConnectivityFilter_SpecifiedRegion


Description

This example creates two spheres and combines them together into one polydata. The vtkPolyDataConnectivityFilter is used to find the two spheres as they are separate regions (disconnected). If region 0 is selected, the small sphere is extracted. If region 1 is selected, the large sphere is extracted.

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

Note

Contrast this with PolyDataConnectivityFilter_LargestRegion.

Note

Contributed by: Jinyoung Hwang

Code

PolyDataConnectivityFilter_SpecifiedRegion.cxx

#include <vtkSmartPointer.h>
#include <vtkPolyDataConnectivityFilter.h>

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

#include <vtkNamedColors.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  vtkSmartPointer<vtkSphereSource> sphereSource1 = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource1->SetRadius(5);

  vtkSmartPointer<vtkSphereSource> sphereSource2 = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource2->SetRadius(10);
  sphereSource2->SetCenter(25,0,0);

  vtkSmartPointer<vtkAppendPolyData> appendFilter = 
    vtkSmartPointer<vtkAppendPolyData>::New();
  appendFilter->AddInputConnection(sphereSource1->GetOutputPort());
  appendFilter->AddInputConnection(sphereSource2->GetOutputPort());

  vtkSmartPointer<vtkPolyDataConnectivityFilter> connectivityFilter = 
    vtkSmartPointer<vtkPolyDataConnectivityFilter>::New();
  connectivityFilter->SetInputConnection(appendFilter->GetOutputPort());
  connectivityFilter->SetExtractionModeToSpecifiedRegions(); 
  connectivityFilter->AddSpecifiedRegion(0); //select the region to extract here
  connectivityFilter->Update();

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

  vtkSmartPointer<vtkActor> originalActor = 
    vtkSmartPointer<vtkActor>::New();
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

  // create a mapper and actor for extracted data
  vtkSmartPointer<vtkPolyDataMapper> extractedMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  extractedMapper->SetInputConnection(connectivityFilter->GetOutputPort());

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

  // create a renderer
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(originalActor);
  renderer->AddActor(extractedActor);
  renderer->GradientBackgroundOn();
  renderer->SetBackground (colors->GetColor3d("Gold").GetData());
  renderer->SetBackground2 (colors->GetColor3d("Silver").GetData());

  // create a render window
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  // create an interactor
  vtkSmartPointer<vtkRenderWindowInteractor> interactor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);
  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(PolyDataConnectivityFilter_SpecifiedRegion)

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

add_executable(PolyDataConnectivityFilter_SpecifiedRegion MACOSX_BUNDLE PolyDataConnectivityFilter_SpecifiedRegion.cxx )

target_link_libraries(PolyDataConnectivityFilter_SpecifiedRegion ${VTK_LIBRARIES})

Download and Build PolyDataConnectivityFilter_SpecifiedRegion

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

cd PolyDataConnectivityFilter_SpecifiedRegion/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_SpecifiedRegion

WINDOWS USERS

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