VTK/Examples/BoneyardCxx/PolyData/ConvexHullRealData

From KitwarePublic
Jump to navigationJump to search
VTK Examples Baseline PolyData TestConvexHullReadData.png

ConvexHullRealData.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkDelaunay3D.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkCellArray.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkParametricFunctionSource.h>

#include <string>

int main(int argc, char *argv[])
{ 
  // Parse command line arguments
  if(argc != 2)
    {
    std::cerr << "Usage: " << argv[0] << " Filename(.vtp)" << std::endl;
    return EXIT_FAILURE;
    }
    
  std::string inputFilename = argv[1];
 
  vtkSmartPointer<vtkXMLPolyDataReader> reader = 
      vtkSmartPointer<vtkXMLPolyDataReader>::New();
  reader->SetFileName(inputFilename.c_str());
  reader->Update();

  // Create the convex hull of the pointcloud
  vtkSmartPointer<vtkDelaunay3D> delaunay = 
      vtkSmartPointer< vtkDelaunay3D >::New();
  delaunay->SetInput(reader->GetOutput());
  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();
  
  // Setup actor and mapper
  vtkSmartPointer<vtkPolyDataMapper> mapper = 
      vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(surfaceFilter->GetOutputPort());
  
  vtkSmartPointer<vtkActor> actor = 
      vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->SetInterpolationToFlat();

  // Setup render window, renderer, and interactor
  vtkSmartPointer<vtkRenderer> renderer = 
      vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderer->AddActor(actor);
  renderer->SetBackground(.1, .2, .3);

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

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(ConvexHullRealData)

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

add_executable(ConvexHullRealData MACOSX_BUNDLE ConvexHullRealData.cxx)

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

Download and Build ConvexHullRealData

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

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

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

./ConvexHullRealData

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.