VTK/Examples/Boneyard/Cxx/Modelling/Delaunay3DAlpha

From KitwarePublic
Jump to: navigation, search

Create a solid mesh from unorganized points Delaunay3D with Alpha)

This example creates a tetrahedral mesh from unorganized points. The example uses the vtkDelaunay3D filter with a non-zero Alpha. Only tetrahedra, triangles, edges and vertices that lie within the alpha radius are output. The resulting mesh is an unstructured grid that contains tetrahedra, triangles, edges and vertices. The example takes the points from a XML PolyData file (.vtp) and produces an XML Unstructured Grid file (.vtu).

Once you generate the file, you can view the results with Paraview.

Also, look at the contents of the file with DumpXMLFile.

Contrast the results from this example and Create a solid mesh from unorganized points (Delaunay3D).

Click here to download a sample input file

Delaunay3DAlpha.cxx

#include <vtkSmartPointer.h>
#include <vtkCleanPolyData.h>
#include <vtkDelaunay3D.h>
#include <vtkXMLDataSetWriter.h>
#include <vtkXMLPolyDataReader.h>
 
 
int main ( int argc, char *argv[] )
{
  if (argc != 4)
    {
    cout << "Usage: " << argv[0]
         << " Alpha InputPolyDataFile(.vtp) OutputDataSetFile(.vtu)" << endl;
    return EXIT_FAILURE;
    }
 
  //Read a file
  vtkSmartPointer<vtkXMLPolyDataReader> reader =
    vtkSmartPointer<vtkXMLPolyDataReader>::New();
  reader->SetFileName (argv[2]);
 
  // Clean the polydata. This will remove duplicate points that may be
  // present in the input data.
  vtkSmartPointer<vtkCleanPolyData> cleaner =
    vtkSmartPointer<vtkCleanPolyData>::New();
  cleaner->SetInputConnection (reader->GetOutputPort());
 
  // Generate a mesh from the input points. If Alpha is non-zero, then
  // tetrahedra, triangles, edges and vertices that lie within the
  // alpha radius are output.
  vtkSmartPointer<vtkDelaunay3D> delaunay3D =
    vtkSmartPointer<vtkDelaunay3D>::New();
  delaunay3D->SetInputConnection (cleaner->GetOutputPort());
  delaunay3D->SetAlpha(atof(argv[1]));
 
  // Output the mesh
  vtkSmartPointer<vtkXMLDataSetWriter> writer =
    vtkSmartPointer<vtkXMLDataSetWriter>::New();
  writer->SetFileName ( argv[3] );
  writer->SetInputConnection ( delaunay3D->GetOutputPort() );
  writer->Write();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(Delaunay3DAlpha)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(Delaunay3DAlpha MACOSX_BUNDLE Delaunay3DAlpha.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(Delaunay3DAlpha ${VTK_LIBRARIES})
else()
  target_link_libraries(Delaunay3DAlpha vtkHybrid vtkWidgets)
endif()

Download and Build Delaunay3DAlpha

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

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

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

./Delaunay3DAlpha

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.