# CapClip

VTKExamples/Cxx/Meshes/CapClip

### Description

This example shows how to generate a "cap" on a clipped polydata. It uses a clever ''trick'' to convert polylines into polygons. If a polydata file (.vtp) is provided, it will cap it. Otherwise it caps a clipped sphere.

Other Languages

See (CSharp)

### Code

CapClip.cxx

    #include <vtkSmartPointer.h>

    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkDataSetMapper.h>
    #include <vtkPolyData.h>
    #include <vtkStripper.h>
    #include <vtkFeatureEdges.h>
    #include <vtkActor.h>
    #include <vtkProperty.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkClipPolyData.h>
    #include <vtkPlane.h>
    #include <vtkXMLPolyDataReader.h>

    #include <vtkSphereSource.h>

    //
    // Demonstrate the use of clipping and capping on polyhedral data
    //

    int main (int argc, char *argv[])
    {
      // PolyData to process
      vtkSmartPointer<vtkPolyData> polyData;

      if (argc > 1)
      {
        vtkSmartPointer<vtkXMLPolyDataReader> reader =
          vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName(argv[1]);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else
      {
        // Create a sphere
        vtkSmartPointer<vtkSphereSource> sphereSource =
          vtkSmartPointer<vtkSphereSource>::New();
        sphereSource->SetThetaResolution(20);
        sphereSource->SetPhiResolution(11);

        vtkSmartPointer<vtkPlane> plane =
          vtkSmartPointer<vtkPlane>::New();
        plane->SetOrigin(0,0,0);
        plane->SetNormal(1.0, -1.0, -1.0);

        vtkSmartPointer<vtkClipPolyData> clipper =
          vtkSmartPointer<vtkClipPolyData>::New();
        clipper->SetInputConnection(sphereSource->GetOutputPort());
        clipper->SetClipFunction(plane);
        clipper->SetValue(0);
        clipper->Update();

        polyData = clipper->GetOutput();
      }

      vtkSmartPointer<vtkDataSetMapper> clipMapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      clipMapper->SetInputData(polyData);

      vtkSmartPointer<vtkActor> clipActor =
        vtkSmartPointer<vtkActor>::New();
      clipActor->SetMapper(clipMapper);
      clipActor->GetProperty()->SetColor(1.0000,0.3882,0.2784);
      clipActor->GetProperty()->SetInterpolationToFlat();

      // Now extract feature edges
      vtkSmartPointer<vtkFeatureEdges> boundaryEdges =
        vtkSmartPointer<vtkFeatureEdges>::New();
      boundaryEdges->SetInputData(polyData);
      boundaryEdges->BoundaryEdgesOn();
      boundaryEdges->FeatureEdgesOff();
      boundaryEdges->NonManifoldEdgesOff();
      boundaryEdges->ManifoldEdgesOff();

      vtkSmartPointer<vtkStripper> boundaryStrips =
        vtkSmartPointer<vtkStripper>::New();
      boundaryStrips->SetInputConnection(boundaryEdges->GetOutputPort());
      boundaryStrips->Update();

      // Change the polylines into polygons
      vtkSmartPointer<vtkPolyData> boundaryPoly =
        vtkSmartPointer<vtkPolyData>::New();
      boundaryPoly->SetPoints(boundaryStrips->GetOutput()->GetPoints());
      boundaryPoly->SetPolys(boundaryStrips->GetOutput()->GetLines());

      vtkSmartPointer<vtkPolyDataMapper> boundaryMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      boundaryMapper->SetInputData(boundaryPoly);

      vtkSmartPointer<vtkActor> boundaryActor =
        vtkSmartPointer<vtkActor>::New();
      boundaryActor->SetMapper(boundaryMapper);
      boundaryActor->GetProperty()->SetColor(0.8900,0.8100,0.3400);

      // Create graphics stuff
      //
      vtkSmartPointer<vtkRenderer> ren1 =
        vtkSmartPointer<vtkRenderer>::New();
      ren1->SetBackground(.1, .2, .3);

      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(clipActor);
      ren1->AddActor(boundaryActor);

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

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

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(CapClip)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersSources
      vtkIOXML
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping CapClip: ${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(CapClip MACOSX_BUNDLE CapClip.cxx )
      target_link_libraries(CapClip PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(CapClip MACOSX_BUNDLE CapClip.cxx )
      target_link_libraries(CapClip PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS CapClip
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build CapClip

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

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

    ./CapClip

WINDOWS USERS

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