# FillHoles

VTKExamples/Cxx/Meshes/FillHoles

### Description

This filter finds holes in a mesh and closes them.

Seealso

IdentifyHoles fills the holes and then identifies each hole.

Other Languages

See (CSharp)

### Code

FillHoles.cxx

    #include <vtkSmartPointer.h>
    #include <vtkFillHolesFilter.h>
    #include <vtkPolyDataNormals.h>
    #include <vtkCleanPolyData.h>

    #include <vtkSelectionNode.h>
    #include <vtkInformation.h>
    #include <vtkUnstructuredGrid.h>
    #include <vtkPolyData.h>
    #include <vtkPointData.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSelection.h>
    #include <vtkSelectionNode.h>
    #include <vtkSphereSource.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkProperty.h>
    #include <vtkIdTypeArray.h>
    #include <vtkExtractSelection.h>
    #include <vtkDataSetSurfaceFilter.h>

    #include <vtkBYUReader.h>
    #include <vtkOBJReader.h>
    #include <vtkPLYReader.h>
    #include <vtkPolyDataReader.h>
    #include <vtkSTLReader.h>
    #include <vtkXMLPolyDataReader.h>
    #include <vtkSphereSource.h>
    #include <vtksys/SystemTools.hxx>

    #include <vtkNamedColors.h>

    namespace
    {
    vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);
    }

    int main(int argc, char *argv[])
    {
      vtkSmartPointer<vtkPolyData> input =
        ReadPolyData(argc > 1 ? argv[1] : "");

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

      vtkSmartPointer<vtkFillHolesFilter> fillHolesFilter =
        vtkSmartPointer<vtkFillHolesFilter>::New();
      fillHolesFilter->SetInputData(input);
      fillHolesFilter->SetHoleSize(100000.0);
      fillHolesFilter->Update();

      // Make the triangle winding order consistent
      vtkSmartPointer<vtkPolyDataNormals> normals =
        vtkSmartPointer<vtkPolyDataNormals>::New();
      normals->SetInputData(fillHolesFilter->GetOutput());
      normals->ConsistencyOn();
      normals->SplittingOff();
      normals->Update();

    #if 0
      // Restore the original normals
      normals->GetOutput()->GetPointData()->
        SetNormals(input->GetPointData()->GetNormals());
    #endif
      // Visualize
      // Define viewport ranges
      // (xmin, ymin, xmax, ymax)
      double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
      double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

      // Create a mapper and actor
      vtkSmartPointer<vtkPolyDataMapper> originalMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      originalMapper->SetInputData(input);

      vtkSmartPointer<vtkProperty> backfaceProp =
        vtkSmartPointer<vtkProperty>::New();
      backfaceProp->SetDiffuseColor(colors->GetColor3d("Banana").GetData());

      vtkSmartPointer<vtkActor> originalActor =
        vtkSmartPointer<vtkActor>::New();
      originalActor->SetMapper(originalMapper);
      originalActor->SetBackfaceProperty(backfaceProp);
      originalActor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("Flesh").GetData());

      vtkSmartPointer<vtkPolyDataMapper> filledMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      filledMapper->SetInputData(fillHolesFilter->GetOutput());

      vtkSmartPointer<vtkActor> filledActor =
        vtkSmartPointer<vtkActor>::New();
      filledActor->SetMapper(filledMapper);
      filledActor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("Flesh").GetData());
      filledActor->SetBackfaceProperty(backfaceProp);

      // Create a renderer, render window, and interactor
      vtkSmartPointer<vtkRenderer> leftRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      leftRenderer->SetViewport(leftViewport);

      vtkSmartPointer<vtkRenderer> rightRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      rightRenderer->SetViewport(rightViewport);

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetSize(600,300);

      renderWindow->AddRenderer(leftRenderer);
      renderWindow->AddRenderer(rightRenderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // Add the actor to the scene
      leftRenderer->AddActor(originalActor);
      rightRenderer->AddActor(filledActor);
      leftRenderer->SetBackground(colors->GetColor3d("PaleGreen").GetData());

      leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
      leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
      leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
      leftRenderer->GetActiveCamera()->Azimuth(30);
      leftRenderer->GetActiveCamera()->Elevation(30);

      leftRenderer->ResetCamera();

      rightRenderer->SetBackground(colors->GetColor3d("LightGreen").GetData());

      // Share the camera

      rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
      // Render and interact
      renderWindow->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

    void GenerateData(vtkPolyData* input)
    {
      // Create a sphere
      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->Update();

      // Remove some cells
      vtkSmartPointer<vtkIdTypeArray> ids =
        vtkSmartPointer<vtkIdTypeArray>::New();
      ids->SetNumberOfComponents(1);

      // Set values
      ids->InsertNextValue(2);
      ids->InsertNextValue(10);

      vtkSmartPointer<vtkSelectionNode> selectionNode =
        vtkSmartPointer<vtkSelectionNode>::New();
      selectionNode->SetFieldType(vtkSelectionNode::CELL);
      selectionNode->SetContentType(vtkSelectionNode::INDICES);
      selectionNode->SetSelectionList(ids);
      selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(), 1); //invert the selection

      vtkSmartPointer<vtkSelection> selection =
          vtkSmartPointer<vtkSelection>::New();
      selection->AddNode(selectionNode);

      vtkSmartPointer<vtkExtractSelection> extractSelection =
          vtkSmartPointer<vtkExtractSelection>::New();
      extractSelection->SetInputConnection(0, sphereSource->GetOutputPort());
      extractSelection->SetInputData(1, selection);
      extractSelection->Update();

      // In selection
      vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter =
        vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
      surfaceFilter->SetInputConnection(extractSelection->GetOutputPort());
      surfaceFilter->Update();

      input->ShallowCopy(surfaceFilter->GetOutput());
    }
    // Snippets
    namespace
    {
    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 if (extension == ".vtk")
      {
        vtkSmartPointer<vtkPolyDataReader> reader =
          vtkSmartPointer<vtkPolyDataReader>::New();
        reader->SetFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".g")
      {
        vtkSmartPointer<vtkBYUReader> reader =
          vtkSmartPointer<vtkBYUReader>::New();
        reader->SetGeometryFileName (fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else
      {
        vtkSmartPointer<vtkSphereSource> source =
          vtkSmartPointer<vtkSphereSource>::New();
        source->Update();
        polyData = source->GetOutput();
      }
      return polyData;
    }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(FillHoles)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersExtraction
      vtkFiltersGeometry
      vtkFiltersModeling
      vtkFiltersSources
      vtkIOGeometry
      vtkIOLegacy
      vtkIOPLY
      vtkIOXML
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping FillHoles: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(FillHoles MACOSX_BUNDLE FillHoles.cxx )
      target_link_libraries(FillHoles PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(FillHoles MACOSX_BUNDLE FillHoles.cxx )
      target_link_libraries(FillHoles PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS FillHoles
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build FillHoles

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

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

    ./FillHoles

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.