Skip to content

Cutter

web-test/Cxx/VisualizationAlgorithms/Cutter



Other languages

See (Python), (Java)

Question

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

Code

Cutter.cxx

#include <vtkActor.h>
#include <vtkCamera.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>

int main(int, char*[])
{

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkCubeSource> cube;
  cube->SetXLength(40);
  cube->SetYLength(30);
  cube->SetZLength(20);
  vtkNew<vtkPolyDataMapper> cubeMapper;
  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)
  vtkNew<vtkPlane> plane;
  plane->SetOrigin(10, 0, 0);
  plane->SetNormal(1, 0, 0);

  // Create cutter
  vtkNew<vtkCutter> cutter;
  cutter->SetCutFunction(plane);
  cutter->SetInputConnection(cube->GetOutputPort());
  cutter->Update();

  vtkNew<vtkPolyDataMapper> cutterMapper;
  cutterMapper->SetInputConnection(cutter->GetOutputPort());
  cutterMapper->SetResolveCoincidentTopologyToPolygonOffset();

  // Create plane actor
  vtkNew<vtkActor> planeActor;
  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
  vtkNew<vtkActor> cubeActor;
  cubeActor->GetProperty()->SetColor(
      colors->GetColor3d("Aquamarine").GetData());
  cubeActor->GetProperty()->SetOpacity(0.5);
  cubeActor->SetMapper(cubeMapper);

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

  // Add renderer to renderwindow and render
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(600, 600);
  renderWindow->SetWindowName("Cutter");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());
  renderWindow->Render();

  auto camera = renderer->GetActiveCamera();
  camera->SetPosition(-37.2611, -86.2155, 44.841);
  camera->SetFocalPoint(0.569422, -1.65124, -2.49482);
  camera->SetViewUp(0.160129, 0.42663, 0.890138);
  camera->SetDistance(104.033);
  camera->SetClippingRange(55.2019, 165.753);

  renderWindow->Render();

  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Cutter)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "Cutter: 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(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}
)

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.