Skip to content

SolidClip

Repository source: 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), (PythonicAPI), (CSharp)

Question

If you have a question about this example, 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 <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.12 FATAL_ERROR)

project(SolidClip)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "SolidClip: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.