# ExtractSurface

VTKExamples/Cxx/Points/ExtractSurface

### Description

This example loosely follows the most excellent paper by Curless and Levoy: "A Volumetric Method for Building Complex Models from Range Images." First it estimates normals from the points, then creates a signed distance field, followed by surface extraction of the zero-level set of the distance field.

If the example is run without an argument, the example uses random points on a spherical shell. With a filename, the example uses the points on the vtkPolyData.

The image was created using the Armadillo dataset

Info

CompareExtractSurface compares three surface extraction algorithms.

Seealso

PowercrustExtractSurface reconstructs surfaces and is implemented as a VTK remote module. PoissonExtractSurface reconstructs surfaces and is implemented as a VTK remote module.

Warning

If you experience extraneous lines in the reconstruction, update your VTK. A patch was made on September 5, 2017 to correct the issue.

Warning

The classes used in this example require vtk 7.1 or later.

### Code

ExtractSurface.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPLYReader.h>
    #include <vtkXMLPolyDataReader.h>
    #include <vtkOBJReader.h>
    #include <vtkSTLReader.h>
    #include <vtkPointSource.h>

    #include <vtkPCANormalEstimation.h>
    #include <vtkSignedDistance.h>
    #include <vtkExtractSurface.h>
    #include <vtkPointData.h>

    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkCamera.h>

    #include <vtkNamedColors.h>
    #include <vtksys/SystemTools.hxx>

    static vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);

    int main (int argc, char *argv[])
    {
      vtkSmartPointer<vtkPolyData> polyData = ReadPolyData(argc > 1 ? argv[1] : "");;
      std::cout << "# of points: " << polyData->GetNumberOfPoints() << std::endl;

      double bounds[6];
      polyData->GetBounds(bounds);
      double range[3];
      for (int i = 0; i < 3; ++i)
      {
        range[i] = bounds[2*i + 1] - bounds[2*i];
      }

      int sampleSize = polyData->GetNumberOfPoints() * .00005;
      if (sampleSize < 10)
      {
        sampleSize = 10;
      }
      std::cout << "Sample size is: " << sampleSize << std::endl;
      // Do we need to estimate normals?
      vtkSmartPointer<vtkSignedDistance> distance =
        vtkSmartPointer<vtkSignedDistance>::New();
      if (polyData->GetPointData()->GetNormals())
      {
        std::cout << "Using normals from input file" << std::endl;
        distance->SetInputData (polyData);
      }
      else
      {
        std::cout << "Estimating normals using PCANormalEstimation" << std::endl;
        vtkSmartPointer<vtkPCANormalEstimation> normals =
          vtkSmartPointer<vtkPCANormalEstimation>::New();
        normals->SetInputData (polyData);
        normals->SetSampleSize(sampleSize);
        normals->SetNormalOrientationToGraphTraversal();
        normals->FlipNormalsOn();
        distance->SetInputConnection (normals->GetOutputPort());
      }
      std::cout << "Range: "
                << range[0] << ", "
                << range[1] << ", "
                << range[2] << std::endl;
      int dimension = 256;
      double radius;
      radius = std::max(std::max(range[0], range[1]), range[2])
        / static_cast<double>(dimension) * 4; // ~4 voxels
      std::cout << "Radius: " << radius << std::endl;

      distance->SetRadius(radius);
      distance->SetDimensions(dimension, dimension, dimension);
      distance->SetBounds(
        bounds[0] - range[0] * .1,
        bounds[1] + range[0] * .1,
        bounds[2] - range[1] * .1,
        bounds[3] + range[1] * .1,
        bounds[4] - range[2] * .1,
        bounds[5] + range[2] * .1);

      vtkSmartPointer<vtkExtractSurface> surface =
        vtkSmartPointer<vtkExtractSurface>::New();
      surface->SetInputConnection (distance->GetOutputPort());
      surface->SetRadius(radius * .99);
      surface->Update();

      vtkSmartPointer<vtkPolyDataMapper> surfaceMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      surfaceMapper->SetInputConnection(surface->GetOutputPort());

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkProperty> back =
        vtkSmartPointer<vtkProperty>::New();
      back->SetColor(colors->GetColor3d("banana").GetData());

      vtkSmartPointer<vtkActor> surfaceActor =
        vtkSmartPointer<vtkActor>::New();
      surfaceActor->SetMapper(surfaceMapper);
      surfaceActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
      surfaceActor->SetBackfaceProperty(back);

      // Create graphics stuff
      //
      vtkSmartPointer<vtkRenderer> ren1 =
        vtkSmartPointer<vtkRenderer>::New();
      ren1->SetBackground(colors->GetColor3d("SlateGray").GetData());

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      renWin->AddRenderer(ren1);
      renWin->SetSize(512,512);

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renWin);

      // Add the actors to the renderer, set the background and size
      //
      ren1->AddActor(surfaceActor);

      // Generate an interesting view
      //
      ren1->ResetCamera();
      ren1->GetActiveCamera()->Azimuth(120);
      ren1->GetActiveCamera()->Elevation(30);
      ren1->GetActiveCamera()->Dolly(1.0);
      ren1->ResetCameraClippingRange();

      iren->Initialize();
      iren->Start();

      return EXIT_SUCCESS;
    }

    static vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName)
    {
      vtkSmartPointer<vtkPolyData> polyData;
      std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
      if (extension == ".ply")
      {
        vtkSmartPointer<vtkPLYReader> reader =
          vtkSmartPointer<vtkPLYReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".vtp")
      {
        vtkSmartPointer<vtkXMLPolyDataReader> reader =
          vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".obj")
      {
        vtkSmartPointer<vtkOBJReader> reader =
          vtkSmartPointer<vtkOBJReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".stl")
      {
        vtkSmartPointer<vtkSTLReader> reader =
          vtkSmartPointer<vtkSTLReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else
      {
        vtkSmartPointer<vtkPointSource> points =
          vtkSmartPointer<vtkPointSource>::New();
     points->SetNumberOfPoints(1000);
        points->SetRadius(1.0);
        points->SetCenter(vtkMath::Random(-1, 1),
                          vtkMath::Random(-1, 1),
                          vtkMath::Random(-1, 1));
        points->SetDistributionToShell();
        points->Update();
        polyData = points->GetOutput();
      }
      return polyData;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(ExtractSurface)

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

    add_executable(ExtractSurface MACOSX_BUNDLE ExtractSurface.cxx )

    target_link_libraries(ExtractSurface ${VTK_LIBRARIES})

### Download and Build ExtractSurface

Click here to download ExtractSurface and its CMakeLists.txt file. Once the tarball ExtractSurface.tar has been downloaded and extracted,

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

    ./ExtractSurface

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.