Cutter

VTKExamples/Cxx/VisualizationAlgorithms/Cutter


Other Languages

See (Python), (Java)

Code

Cutter.cxx

#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <vtkCutter.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 <vtkSmartPointer.h>

int main(int, char *[]) {

  vtkNew<vtkNamedColors> colors;

  vtkSmartPointer<vtkCubeSource> cube =
      vtkSmartPointer<vtkCubeSource>::New();
  cube->SetXLength(40);
  cube->SetYLength(30);
  cube->SetZLength(20);
  vtkSmartPointer<vtkPolyDataMapper> cubeMapper =
      vtkSmartPointer<vtkPolyDataMapper>::New();
  cubeMapper->SetInputConnection(cube->GetOutputPort());

  // Create a plane to cut,here it cuts in the XZ direction (xz
  // normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
  vtkSmartPointer<vtkPlane> plane = 
      vtkSmartPointer<vtkPlane>::New();
  plane->SetOrigin(10, 0, 0);
  plane->SetNormal(1, 0, 0);

  // Create cutter
  vtkSmartPointer<vtkCutter> cutter = 
      vtkSmartPointer<vtkCutter>::New();
  cutter->SetCutFunction(plane);
  cutter->SetInputConnection(cube->GetOutputPort());
  cutter->Update();

  vtkSmartPointer<vtkPolyDataMapper> cutterMapper =
      vtkSmartPointer<vtkPolyDataMapper>::New();
  cutterMapper->SetInputConnection(cutter->GetOutputPort());
  cutterMapper->SetResolveCoincidentTopologyToPolygonOffset();

  // Create plane actor
  vtkSmartPointer<vtkActor> planeActor = 
      vtkSmartPointer<vtkActor>::New();
  planeActor->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());
  planeActor->GetProperty()->SetLineWidth(2);
  planeActor->GetProperty()->SetAmbient(1.0);
  planeActor->GetProperty()->SetDiffuse(0.0);
  planeActor->SetMapper(cutterMapper);

  // Create cube actor
  vtkSmartPointer<vtkActor> cubeActor = 
      vtkSmartPointer<vtkActor>::New();
  cubeActor->GetProperty()->SetColor(
      colors->GetColor3d("Aquamarine").GetData());
  cubeActor->GetProperty()->SetOpacity(0.5);
  cubeActor->SetMapper(cubeMapper);

  // Create renderers and add actors of plane and cube
  vtkSmartPointer<vtkRenderer> renderer = 
      vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(planeActor); // display the rectangle resulting from the
                                  // cut
  renderer->AddActor(cubeActor); // display the cube

  // Add renderer to renderwindow and render
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(600, 600);

  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());
  renderWindow->Render();

  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Cutter)

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

Download and Build Cutter

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

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

./Cutter

WINDOWS USERS

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