Skip to content


# SolidClip

VTKEx/Cxx/Meshes/SolidClip

### Description

This example clips a mesh and applies a backface property to that mesh so that it appears to have a solid interior.

The "ghost" of the part clipped away is also shown.

Other languages

See (Python), (CSharp)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

SolidClip.cxx

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkClipPolyData.h>
    #include <vtkNamedColors.h>
    #include <vtkNew.h>
    #include <vtkPlane.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSuperquadricSource.h>

    int main(int, char*[])
    {
      vtkNew<vtkNamedColors> colors;

      // Create a superquadric
      vtkNew<vtkSuperquadricSource> superquadricSource;
      superquadricSource->SetPhiRoundness(3.1);
      superquadricSource->SetThetaRoundness(2.2);

      // Define a clipping plane
      vtkNew<vtkPlane> clipPlane;
      clipPlane->SetNormal(1.0, -1.0, -1.0);
      clipPlane->SetOrigin(0.0, 0.0, 0.0);

      // Clip the source with the plane
      vtkNew<vtkClipPolyData> clipper;
      clipper->SetInputConnection(superquadricSource->GetOutputPort());
      clipper->SetClipFunction(clipPlane);
      // This will give us the polygonal data that is clipped away
      clipper->GenerateClippedOutputOn();

      // Create a mapper and actor
      vtkNew<vtkPolyDataMapper> superquadricMapper;
      superquadricMapper->SetInputConnection(clipper->GetOutputPort());

      vtkNew<vtkActor> superquadricActor;
      superquadricActor->SetMapper(superquadricMapper);

      // Create a property to be used for the back faces. Turn off all
      // shading by specifying 0 weights for specular and diffuse. Max the
      // ambient.
      vtkNew<vtkProperty> backFaces;
      backFaces->SetSpecular(0.0);
      backFaces->SetDiffuse(0.0);
      backFaces->SetAmbient(1.0);
      backFaces->SetAmbientColor(colors->GetColor3d("Tomato").GetData());

      superquadricActor->SetBackfaceProperty(backFaces);

      // Here we get the the polygonal data that is clipped away
      vtkNew<vtkPolyDataMapper> clippedAwayMapper;
      clippedAwayMapper->SetInputData(clipper->GetClippedOutput());
      clippedAwayMapper->ScalarVisibilityOff();

      // Let us display it as a faint object
      vtkNew<vtkActor> clippedAwayActor;
      clippedAwayActor->SetMapper(clippedAwayMapper);
      clippedAwayActor->GetProperty()->SetDiffuseColor(
          colors->GetColor3d("Silver").GetData());
      clippedAwayActor->GetProperty()->SetOpacity(0.1);

      // Create a renderer
      vtkNew<vtkRenderer> renderer;

      vtkNew<vtkRenderWindow> renderWindow;
      renderWindow->SetWindowName("SolidClip");

      renderWindow->AddRenderer(renderer);

      vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // Add actors to the renderers
      renderer->AddActor(superquadricActor);
      renderer->AddActor(clippedAwayActor);
      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
      renderWindow->SetSize(600, 600);
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Dolly(1.5);
      renderer->ResetCameraClippingRange();
      renderWindow->Render();

      // Interact with the window
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(SolidClip)

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

### Download and Build SolidClip

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

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

    ./SolidClip

WINDOWS USERS

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