Skip to content

DownsamplePointCloud

vtk-examples/Cxx/PolyData/DownsamplePointCloud


Description

This example downsamples a point cloud by specifying the minimum distance two points can be from each other. The filter will delete points so that this criterion is met.

Question

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

Code

DownsamplePointCloud.cxx

#include <vtkActor.h>
#include <vtkCleanPolyData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

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

  vtkNew<vtkPointSource> pointSource;
  pointSource->SetNumberOfPoints(1000);
  pointSource->SetRadius(1.0);
  pointSource->Update();
  auto pts = pointSource->GetNumberOfPoints();

  vtkNew<vtkCleanPolyData> cleanPolyData;
  cleanPolyData->SetInputConnection(pointSource->GetOutputPort());
  cleanPolyData->SetTolerance(0.1);
  cleanPolyData->Update();
  auto cleanPts = cleanPolyData->GetOutput()->GetNumberOfPoints();

  std::cout << "Original points" << pts << std::endl;
  std::cout << "Cleaned points " << cleanPts << std::endl;
  std::cout << "Reduction      "
            << (1.0 - static_cast<double>(cleanPts) / pts) * 100.0 << "%"
            << std::endl;

  vtkNew<vtkPolyDataMapper> inputMapper;
  inputMapper->SetInputConnection(pointSource->GetOutputPort());
  vtkNew<vtkActor> inputActor;
  inputActor->SetMapper(inputMapper);
  inputActor->GetProperty()->SetColor(colors->GetColor3d("Lime").GetData());
  inputActor->GetProperty()->SetPointSize(3);

  vtkNew<vtkPolyDataMapper> cleanedMapper;
  cleanedMapper->SetInputConnection(cleanPolyData->GetOutputPort());
  vtkNew<vtkActor> cleanedActor;
  cleanedActor->SetMapper(cleanedMapper);
  cleanedActor->GetProperty()->SetColor(colors->GetColor3d("Lime").GetData());
  cleanedActor->GetProperty()->SetPointSize(3);

  // There will be one render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->SetWindowName("DownsamplePointCloud");

  // And one interactor
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Setup both renderers
  vtkNew<vtkRenderer> leftRenderer;
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(colors->GetColor3d("van_dyke_brown").GetData());

  vtkNew<vtkRenderer> rightRenderer;
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(colors->GetColor3d("ultramarine").GetData());

  leftRenderer->AddActor(inputActor);
  rightRenderer->AddActor(cleanedActor);

  leftRenderer->ResetCamera();
  rightRenderer->ResetCamera();

  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DownsamplePointCloud)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping DownsamplePointCloud: ${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(DownsamplePointCloud MACOSX_BUNDLE DownsamplePointCloud.cxx )
  target_link_libraries(DownsamplePointCloud PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(DownsamplePointCloud MACOSX_BUNDLE DownsamplePointCloud.cxx )
  target_link_libraries(DownsamplePointCloud PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS DownsamplePointCloud
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build DownsamplePointCloud

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

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

./DownsamplePointCloud

WINDOWS USERS

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