Skip to content

DistancePolyDataFilter

vtk-examples/Cxx/PolyData/DistancePolyDataFilter


Question

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

Code

DistancePolyDataFilter.cxx

#include <vtkActor.h>
#include <vtkCleanPolyData.h>
#include <vtkDistancePolyDataFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkScalarBarActor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.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;
    vtkNew<vtkPolyDataReader> reader1;
    reader1->SetFileName(argv[1]);
    reader1->Update();
    input1 = reader1->GetOutput();

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

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

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkCleanPolyData> clean1;
  clean1->SetInputData(input1);

  vtkNew<vtkCleanPolyData> clean2;
  clean2->SetInputData(input2);

  vtkNew<vtkDistancePolyDataFilter> distanceFilter;

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

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

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkScalarBarActor> scalarBar;
  scalarBar->SetLookupTable(mapper->GetLookupTable());
  scalarBar->SetTitle("Distance");
  scalarBar->SetNumberOfLabels(4);
  scalarBar->UnconstrainedFontSizeOn();

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());
  renderer->SetBackground2(colors->GetColor3d("Gold").GetData());
  renderer->GradientBackgroundOn();

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(renderer);
  renWin->SetSize(600, 500);
  renWin->SetWindowName("DistancePolyDataFilter");

  vtkNew<vtkRenderWindowInteractor> renWinInteractor;
  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
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  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.