Skip to content

ClipSphereCylinder

Repository source: ClipSphereCylinder


Description

Clipping is implemented in vtkClipPolyData . Each polygonal data primitive implements the operation in its Clip() method using case tables. vtkClipPolyData has methods to control whether an implicit function provides the scalar data or whether the dataset’s scalar data will be used. ComputeScalarDataOn() uses the implicit function and ComputeScalarDataOff() uses the dataset’s scalar data. Two output polygonal datasets are produced. These are accessed with GetOutput() and GetClippedOutput() methods. GetOutput() returns the polygonal data that is “inside” the clipping region while GetClippedOutput() returns polygonal data that is “outside” the region. (Note that GenerateClippedOutputOn() must be enabled if you are to get the clipped output.) The meaning of inside and outside can be reversed using the InsideOutOn().

This example shows a plane of quadrilaterals clipped with a boolean implicit function.

Other languages

See (Python), (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ClipSphereCylinder.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkClipPolyData.h>
#include <vtkCylinder.h>
#include <vtkImplicitBoolean.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphere.h>
#include <vtkTransform.h>

int main(int, char*[])
{
  // Demonstrate the use of clipping on polygonal data
  //

  // create pipeline
  //
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPlaneSource> plane;
  plane->SetXResolution(25);
  plane->SetYResolution(25);
  plane->SetOrigin(-1, -1, 0);
  plane->SetPoint1(1, -1, 0);
  plane->SetPoint2(-1, 1, 0);

  vtkNew<vtkTransform> transformSphere;
  transformSphere->Identity();
  transformSphere->Translate(.4, -.4, 0);
  transformSphere->Inverse();

  vtkNew<vtkSphere> sphere;
  sphere->SetTransform(transformSphere);
  sphere->SetRadius(.5);

  vtkNew<vtkTransform> transformCylinder;
  transformCylinder->Identity();
  transformCylinder->Translate(-.4, .4, 0);
  transformCylinder->RotateZ(30);
  transformCylinder->RotateY(60);
  transformCylinder->RotateX(90);
  transformCylinder->Inverse();

  vtkNew<vtkCylinder> cylinder;
  cylinder->SetTransform(transformCylinder);
  cylinder->SetRadius(.3);

  vtkNew<vtkImplicitBoolean> boolean;
  boolean->AddFunction(cylinder);
  boolean->AddFunction(sphere);

  vtkNew<vtkClipPolyData> clipper;
  clipper->SetInputConnection(plane->GetOutputPort());
  clipper->SetClipFunction(boolean);
  clipper->GenerateClippedOutputOn();
  clipper->GenerateClipScalarsOn();
  clipper->SetValue(0);

  vtkNew<vtkPolyDataMapper> clipMapper;
  clipMapper->SetInputConnection(clipper->GetOutputPort());
  clipMapper->ScalarVisibilityOff();

  vtkNew<vtkActor> clipActor;
  clipActor->SetMapper(clipMapper);
  clipActor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("MidnightBlue").GetData());
  clipActor->GetProperty()->SetRepresentationToWireframe();

  vtkNew<vtkPolyDataMapper> clipInsideMapper;
  clipInsideMapper->SetInputData(clipper->GetClippedOutput());
  clipInsideMapper->ScalarVisibilityOff();

  vtkNew<vtkActor> clipInsideActor;
  clipInsideActor->SetMapper(clipInsideMapper);
  clipInsideActor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("LightBlue").GetData());

  // Create graphics stuff
  //
  vtkNew<vtkRenderer> ren1;

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

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

  ren1->AddActor(clipInsideActor);
  ren1->SetBackground(colors->GetColor3d("Wheat").GetData());
  ren1->ResetCamera();
  ren1->GetActiveCamera()->Dolly(1.4);
  ren1->ResetCameraClippingRange();

  renWin->SetSize(640, 480);
  renWin->SetWindowName("ClipSphereCylinder");

  // render the image
  //
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ClipSphereCylinder)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ClipSphereCylinder: 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(ClipSphereCylinder MACOSX_BUNDLE ClipSphereCylinder.cxx )
  target_link_libraries(ClipSphereCylinder PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ClipSphereCylinder
  MODULES ${VTK_LIBRARIES}
)

Download and Build ClipSphereCylinder

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

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

./ClipSphereCylinder

WINDOWS USERS

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