# DistancePolyDataFilter

VTKExamples/Cxx/PolyData/DistancePolyDataFilter

### Code

DistancePolyDataFilter.cxx

    #include <vtkSmartPointer.h>

    #include <vtkActor.h>
    #include <vtkDistancePolyDataFilter.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkPolyDataReader.h>
    #include <vtkCleanPolyData.h>
    #include <vtkProperty.h>
    #include <vtkPointData.h>
    #include <vtkScalarBarActor.h>
    #include <vtkSphereSource.h>

    #include <vtkNamedColors.h>

    int main(int argc, char* argv[])
    {
      vtkSmartPointer<vtkPolyData> input1;
      vtkSmartPointer<vtkPolyData> input2;
      if (argc == 3)
      {
    //     std::cerr << "Usage: " << argv[0]
    //               << " filename1.vtk"
    //               << " filename2.vtk" << std::endl;
        vtkSmartPointer<vtkPolyDataReader> reader1 =
          vtkSmartPointer<vtkPolyDataReader>::New();
        reader1->SetFileName(argv[1]);
        reader1->Update();
        input1 = reader1->GetOutput();

        vtkSmartPointer<vtkPolyDataReader> reader2 =
          vtkSmartPointer<vtkPolyDataReader>::New();
        reader2->SetFileName(argv[2]);
        reader2->Update();
        input2 = reader2->GetOutput();
      }
      else
      {
        vtkSmartPointer<vtkSphereSource> sphereSource1 =
          vtkSmartPointer<vtkSphereSource>::New();
        sphereSource1->SetCenter(1, 0, 0);
        sphereSource1->SetPhiResolution(21);
        sphereSource1->SetThetaResolution(21);
        sphereSource1->Update();
        input1 = sphereSource1->GetOutput();

        vtkSmartPointer<vtkSphereSource> sphereSource2 =
          vtkSmartPointer<vtkSphereSource>::New();
        sphereSource2->SetPhiResolution(21);
        sphereSource2->SetThetaResolution(21);
        sphereSource2->Update();
        input2 = sphereSource2->GetOutput();
      }

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkCleanPolyData> clean1 =
        vtkSmartPointer<vtkCleanPolyData>::New();
      clean1->SetInputData( input1);

      vtkSmartPointer<vtkCleanPolyData> clean2 =
        vtkSmartPointer<vtkCleanPolyData>::New();
      clean2->SetInputData( input2);

      vtkSmartPointer<vtkDistancePolyDataFilter> distanceFilter =
        vtkSmartPointer<vtkDistancePolyDataFilter>::New();

      distanceFilter->SetInputConnection( 0, clean1->GetOutputPort() );
      distanceFilter->SetInputConnection( 1, clean2->GetOutputPort() );
      distanceFilter->Update();

      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection( distanceFilter->GetOutputPort() );
      mapper->SetScalarRange(
        distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[0],
        distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[1]);

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

      vtkSmartPointer<vtkScalarBarActor> scalarBar =
        vtkSmartPointer<vtkScalarBarActor>::New();
      scalarBar->SetLookupTable(mapper->GetLookupTable());
      scalarBar->SetTitle("Distance");
      scalarBar->SetNumberOfLabels(4);
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->SetBackground(colors->GetColor3d("Silver").GetData());
      renderer->SetBackground2(colors->GetColor3d("Gold").GetData());
      renderer->GradientBackgroundOn();

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

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

      renderer->AddActor( actor );
      renderer->AddActor2D(scalarBar);

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

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(DistancePolyDataFilter)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersGeneral
      vtkFiltersSources
      vtkIOLegacy
      vtkInteractionStyle
      vtkRenderingAnnotation
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping DistancePolyDataFilter: ${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(DistancePolyDataFilter MACOSX_BUNDLE DistancePolyDataFilter.cxx )
      target_link_libraries(DistancePolyDataFilter PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(DistancePolyDataFilter MACOSX_BUNDLE DistancePolyDataFilter.cxx )
      target_link_libraries(DistancePolyDataFilter PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS DistancePolyDataFilter
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build DistancePolyDataFilter

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

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

    ./DistancePolyDataFilter

WINDOWS USERS

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