Skip to content


# ConnectivityFilter

VTKEx/Cxx/Filtering/ConnectivityFilter

Other languages

See (Python)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

ConnectivityFilter.cxx

    #include <vtkSmartPointer.h>
    #include <vtkUnstructuredGrid.h>
    #include <vtkSphereSource.h>
    #include <vtkConnectivityFilter.h>
    #include <vtkDataSetMapper.h>
    #include <vtkActor.h>
    #include <vtkProperty.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkAppendFilter.h>
    #include <vtkDelaunay3D.h>

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

      vtkSmartPointer<vtkDelaunay3D> delaunay1 =
        vtkSmartPointer<vtkDelaunay3D>::New();
      delaunay1->SetInputConnection(sphereSource1->GetOutputPort());
      delaunay1->Update();

      vtkSmartPointer<vtkSphereSource> sphereSource2 =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource2->SetCenter(5,0,0);
      sphereSource2->Update();

      vtkSmartPointer<vtkDelaunay3D> delaunay2 =
        vtkSmartPointer<vtkDelaunay3D>::New();
      delaunay2->SetInputConnection(sphereSource2->GetOutputPort());
      delaunay2->Update();

      vtkSmartPointer<vtkAppendFilter> appendFilter =
        vtkSmartPointer<vtkAppendFilter>::New();
      appendFilter->AddInputConnection(delaunay1->GetOutputPort());
      appendFilter->AddInputConnection(delaunay2->GetOutputPort());
      appendFilter->Update();

      vtkSmartPointer<vtkConnectivityFilter> connectivityFilter =
        vtkSmartPointer<vtkConnectivityFilter>::New();
      connectivityFilter->SetInputConnection(appendFilter->GetOutputPort());
      connectivityFilter->SetExtractionModeToAllRegions();
      connectivityFilter->ColorRegionsOn();
      connectivityFilter->Update();

      // Visualize
      vtkSmartPointer<vtkDataSetMapper> mapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      mapper->SetInputConnection(connectivityFilter->GetOutputPort());
      mapper->Update();

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

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->AddActor(actor);

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

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renwin);
      iren->Initialize();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ConnectivityFilter)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersSources
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ConnectivityFilter: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(ConnectivityFilter MACOSX_BUNDLE ConnectivityFilter.cxx )
      target_link_libraries(ConnectivityFilter PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ConnectivityFilter MACOSX_BUNDLE ConnectivityFilter.cxx )
      target_link_libraries(ConnectivityFilter PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ConnectivityFilter
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ConnectivityFilter

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

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

    ./ConnectivityFilter

WINDOWS USERS

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