Skip to content

ConvexHullShrinkWrap

vtk-examples/Cxx/PolyData/ConvexHullShrinkWrap


Description

This example creates a point cloud, and a sphere larger than the point cloud which fully contains the cloud. It then "shrink wraps" the sphere onto the points. This produces an approximation of a convex hull.

Other languages

See (Java)

Question

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

Code

ConvexHullShrinkWrap.cxx

#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmoothPolyDataFilter.h>
#include <vtkSphereSource.h>

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

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetRadius(10);
  sphereSource->SetPhiResolution(50);
  sphereSource->SetThetaResolution(50);

  vtkNew<vtkPointSource> pointSource;
  pointSource->SetNumberOfPoints(60);
  pointSource->SetRadius(2);

  vtkNew<vtkSmoothPolyDataFilter> smoothFilter;
  smoothFilter->SetInputConnection(0, sphereSource->GetOutputPort());
  smoothFilter->SetInputConnection(1, pointSource->GetOutputPort());

  vtkNew<vtkSphereSource> glyphSource;
  glyphSource->SetRadius(.04);

  vtkNew<vtkGlyph3D> glyph3D;
  glyph3D->SetInputConnection(pointSource->GetOutputPort());
  glyph3D->SetSourceConnection(glyphSource->GetOutputPort());
  glyph3D->ScalingOff();

  vtkNew<vtkDataSetMapper> glyph3DMapper;
  glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());
  glyph3DMapper->ScalarVisibilityOff();

  vtkNew<vtkActor> glyph3DActor;
  glyph3DActor->SetMapper(glyph3DMapper);
  glyph3DActor->GetProperty()->SetColor(
      namedColors->GetColor3d("Banana").GetData());

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(smoothFilter->GetOutputPort());
  mapper->ScalarVisibilityOff();

  // Create an actor for the surface
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetDiffuseColor(
      namedColors->GetColor3d("Tomato").GetData());
  actor->GetProperty()->SetEdgeColor(
      namedColors->GetColor3d("IvoryBlack").GetData());
  actor->GetProperty()->SetOpacity(1.0);
  actor->GetProperty()->EdgeVisibilityOff();

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("ConvexHullShrinkWrap");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->AddActor(glyph3DActor);
  renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ConvexHullShrinkWrap)

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

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

Download and Build ConvexHullShrinkWrap

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

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

./ConvexHullShrinkWrap

WINDOWS USERS

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