The post shows how to find 2D convex hull and 3D convex hull from points cloud by VTK.
Here are some points on screen, finally I add a specail point that z value is not 0 for test 3D convex hull.
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <QString>
#include <vtkConvexHull2D.h>
#include <vtkDelaunay3D.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkCamera.h>
#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();
using namespace std;
int main()
{
vtkSPtrNew( points, vtkPoints );
points->InsertNextPoint( 0, 0, 0 );
points->InsertNextPoint( 0, 1, 0 );
points->InsertNextPoint( 1, 0, 0 );
points->InsertNextPoint( 1, 1, 0 );
points->InsertNextPoint( 2, 1, 0 );
points->InsertNextPoint( 2, 2, 0 );
points->InsertNextPoint( 1, 2, 0 );
points->InsertNextPoint( 1, 2, -1 );
vtkSPtrNew( polyData, vtkPolyData );
polyData->SetPoints( points );
// Create the convex hull of the pointcloud
// ---------------- 2D convex hull -----------------
vtkSPtrNew( hull2D, vtkConvexHull2D );
hull2D->SetHullShape( 1 ); //0: BoundingRectangle or 1: ConvexHull.
hull2D->SetInputData( polyData );
hull2D->Update();
vtkSPtrNew( mapper, vtkPolyDataMapper );
mapper->SetInputData( hull2D->GetOutput() );
vtkSPtrNew( actor, vtkActor );
actor->SetMapper( mapper );
// ---------------- 3D convex hull -----------------
vtkSmartPointer<vtkDelaunay3D> delaunay = vtkSmartPointer<vtkDelaunay3D>::New();
delaunay->SetInputData( polyData );
delaunay->Update();
vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
surfaceFilter->SetInputConnection(delaunay->GetOutputPort());
surfaceFilter->Update();
vtkSPtrNew( mapper3D, vtkPolyDataMapper );
mapper3D->SetInputData( surfaceFilter->GetOutput() );
vtkSPtrNew( actor3D, vtkActor );
actor3D->SetMapper( mapper3D );
// --------------- two renderer -----------------
vtkSPtrNew( renderWindow, vtkRenderWindow );
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 with same camera
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
vtkSmartPointer<vtkRenderer> leftRenderer = vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(.6, .5, .4);
leftRenderer->SetActiveCamera( camera );
leftRenderer->AddActor( actor );
vtkSmartPointer<vtkRenderer> rightRenderer = vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(.4, .5, .6);
rightRenderer->SetActiveCamera( camera );
rightRenderer->AddActor( actor3D );
renderWindow->AddRenderer( leftRenderer );
renderWindow->AddRenderer( rightRenderer );
renderWindow->SetSize( 800, 400 );
vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
renderWindowInteractor->SetRenderWindow( renderWindow );
leftRenderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}