# ConvexHullShrinkWrap

VTKExamples/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 approximately a convex hull.

### Code

ConvexHullShrinkWrap.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPolyData.h>
    #include <vtkSphereSource.h>
    #include <vtkGlyph3D.h>
    #include <vtkPointSource.h>
    #include <vtkSmoothPolyDataFilter.h>
    #include <vtkNamedColors.h>

    #include <vtkDataSetMapper.h>
    #include <vtkActor.h>
    #include <vtkProperty.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>

    int main(int, char *[])
    {
      vtkSmartPointer<vtkNamedColors> namedColors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetRadius(10);
      sphereSource->SetPhiResolution(50);
      sphereSource->SetThetaResolution(50);

      vtkSmartPointer<vtkPointSource> pointSource =
        vtkSmartPointer<vtkPointSource>::New();
      pointSource->SetNumberOfPoints(60);
      pointSource->SetRadius(2);

      vtkSmartPointer<vtkSmoothPolyDataFilter> smoothFilter =
        vtkSmartPointer<vtkSmoothPolyDataFilter>::New();
      smoothFilter->SetInputConnection(0, sphereSource->GetOutputPort());
      smoothFilter->SetInputConnection(1, pointSource->GetOutputPort());

      vtkSmartPointer<vtkSphereSource> glyphSource =
        vtkSmartPointer<vtkSphereSource>::New();
      glyphSource->SetRadius(.04);

      vtkSmartPointer<vtkGlyph3D> glyph3D =
        vtkSmartPointer<vtkGlyph3D>::New();
      glyph3D->SetInputConnection(pointSource->GetOutputPort());
      glyph3D->SetSourceConnection(glyphSource->GetOutputPort());
      glyph3D->ScalingOff();

      vtkSmartPointer<vtkDataSetMapper> glyph3DMapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());
      glyph3DMapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> glyph3DActor =
        vtkSmartPointer<vtkActor>::New();
      glyph3DActor->SetMapper(glyph3DMapper);
      glyph3DActor->GetProperty()->SetColor(
        namedColors->GetColor3d("Banana").GetData());

      vtkSmartPointer<vtkDataSetMapper> mapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      mapper->SetInputConnection(smoothFilter->GetOutputPort());
      mapper->ScalarVisibilityOff();

      // Create an actor for the surface
      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      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();

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      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 2.8)

    PROJECT(ConvexHullShrinkWrap)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(ConvexHullShrinkWrap MACOSX_BUNDLE ConvexHullShrinkWrap.cxx )

    target_link_libraries(ConvexHullShrinkWrap ${VTK_LIBRARIES})

### 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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.