VTK/Examples/Boneyard/Cxx/PolyData/ConvexHullDelaunay3D

From KitwarePublic
Jump to navigationJump to search

This example shows how to compute the convex hull of a point cloud. It computes the volumetric tessellation using Delaunay3D and saves this result delaunay.vtu. This output contains tetrahedron, where we are interested in the bounding surface of the points. vtkDataSetSurfaceFilter is then used to get the surfaces on the outside of this volume. This result is written to output.vtp.

The example data set is created in the code. It consists of 5 points, 4 of which should lie on the convex hull. Note that delaunay.vtp contains 5 points and 4 3D cells, while output.vtp contains only 4 points (the points on the hull) and 4 2D cells.

ConvexHullDelaunay3D.cxx

#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkDelaunay3D.h>
#include <vtkUnstructuredGrid.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkCellArray.h>
#include <vtkDataSetSurfaceFilter.h>

int main(int, char *[])
{ 
  //Create some random points
  
  vtkSmartPointer<vtkPoints> points = 
    vtkSmartPointer< vtkPoints >::New();
  
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(0.1, 0.1, 0.1); //this point will be inside the convex hull
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 1.0);
    
  vtkSmartPointer< vtkPolyData> polydata = 
    vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
  //polydata->SetVerts(vertices);
  
  vtkSmartPointer<vtkXMLPolyDataWriter> pointsWriter = 
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  pointsWriter->SetFileName("points.vtp");
  pointsWriter->SetInput(polydata);
  pointsWriter->Write();
    
  // Create the convex hull of the pointcloud
  vtkSmartPointer<vtkDelaunay3D> delaunay = 
    vtkSmartPointer< vtkDelaunay3D >::New();
  delaunay->SetInput(polydata);
  delaunay->Update();
  
  vtkSmartPointer<vtkXMLUnstructuredGridWriter> ugWriter = 
    vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
  ugWriter->SetInputConnection(delaunay->GetOutputPort());
  ugWriter->SetFileName("delaunay.vtu");
  ugWriter->Write();
  
  vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = 
    vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
  surfaceFilter->SetInputConnection(delaunay->GetOutputPort());
  surfaceFilter->Update();  
 
  vtkSmartPointer<vtkXMLPolyDataWriter> outputWriter = 
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  outputWriter->SetFileName("output.vtp");
  outputWriter->SetInput(surfaceFilter->GetOutput());
  outputWriter->Write();
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(ConvexHullDelaunay3D)

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

add_executable(ConvexHullDelaunay3D MACOSX_BUNDLE ConvexHullDelaunay3D.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(ConvexHullDelaunay3D ${VTK_LIBRARIES})
else()
  target_link_libraries(ConvexHullDelaunay3D vtkHybrid vtkWidgets)
endif()

Download and Build ConvexHullDelaunay3D

Click here to download ConvexHullDelaunay3D. and its CMakeLists.txt file.

Once the tarball ConvexHullDelaunay3D.tar has been downloaded and extracted,

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

./ConvexHullDelaunay3D

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.

<--

Generated Images

Pointcloud:

ConvexHull pointcloud.jpg


Convex Hull:

ConvexHull convexhull.jpg -->

Remove above images in favor of our new div tags.