title: VTK Toolkit - vtkCutter Performance
body: <p>I use the VTK Toolkit to load an OBJ file and a vtkCutter to cut through the data set with a play and then draw the outline of the cut. For large objects this is can become quite slow as another user pointed out in the <a href="http://vtk.1045678.n5.nabble.com/algorithm-performance-td5722391.html" rel="nofollow">VTK Users Forum</a>. </p>
  
  <p>Is there a way to make the cutter use a hierarchical data structure to gain better performance?</p>
  
  <p>This is the code: </p>
  
  <pre><code>#include &lt;vtkSmartPointer.h&gt;
  #include &lt;vtkCubeSource.h&gt;
  #include &lt;vtkPolyDataMapper.h&gt;
  #include &lt;vtkPlane.h&gt;
  #include &lt;vtkCutter.h&gt;
  #include &lt;vtkProperty.h&gt;
  #include &lt;vtkActor.h&gt;
  #include &lt;vtkRenderer.h&gt;
  #include &lt;vtkRenderWindow.h&gt;
  #include &lt;vtkRenderWindowInteractor.h&gt;
  #include &lt;vtkOBJReader.h&gt;
  
  int main(int argc, char *argv[])
  {
      // Parse command line arguments
      if (argc != 2) {
          std::cout &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " Filename(.obj)" &lt;&lt; std::endl;
          return EXIT_FAILURE;
      }
  
      std::string filename = argv[1];
      vtkSmartPointer&lt;vtkOBJReader&gt; obj = vtkSmartPointer&lt;vtkOBJReader&gt;::New();
      obj-&gt;SetFileName(filename.c_str());
      obj-&gt;Update();
  
      vtkSmartPointer&lt;vtkPolyDataMapper&gt; mapper = vtkSmartPointer&lt;vtkPolyDataMapper&gt;::New();
      mapper-&gt;SetInputConnection(obj-&gt;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&lt;vtkPlane&gt; plane = vtkSmartPointer&lt;vtkPlane&gt;::New();
      plane-&gt;SetOrigin(0, 0, 0);
      plane-&gt;SetNormal(1, 0, 0);
  
      // Create cutter
      vtkSmartPointer&lt;vtkCutter&gt; cutter = vtkSmartPointer&lt;vtkCutter&gt;::New();
      cutter-&gt;SetCutFunction(plane);
      cutter-&gt;SetInputConnection(obj-&gt;GetOutputPort());
      cutter-&gt;Update();
  
      vtkSmartPointer&lt;vtkPolyDataMapper&gt; cutterMapper = vtkSmartPointer&lt;vtkPolyDataMapper&gt;::New();
      cutterMapper-&gt;SetInputConnection(cutter-&gt;GetOutputPort());
  
      // Create plane actor
      vtkSmartPointer&lt;vtkActor&gt; planeActor = vtkSmartPointer&lt;vtkActor&gt;::New();
      planeActor-&gt;GetProperty()-&gt;SetColor(1.0, 1, 0);
      planeActor-&gt;GetProperty()-&gt;SetLineWidth(2);
      planeActor-&gt;SetMapper(cutterMapper);
  
      // Create cube actor
      vtkSmartPointer&lt;vtkActor&gt; cubeActor = vtkSmartPointer&lt;vtkActor&gt;::New();
      cubeActor-&gt;GetProperty()-&gt;SetColor(0.5, 1, 0.5);
      cubeActor-&gt;GetProperty()-&gt;SetOpacity(0.5);
      cubeActor-&gt;SetMapper(mapper);
  
      // Create renderers and add actors of plane and cube
      vtkSmartPointer&lt;vtkRenderer&gt; renderer = vtkSmartPointer&lt;vtkRenderer&gt;::New();
      renderer-&gt;AddActor(planeActor); //display the rectangle resulting from the cut
      renderer-&gt;AddActor(cubeActor); //display the cube
  
      // Add renderer to renderwindow and render
      vtkSmartPointer&lt;vtkRenderWindow&gt; renderWindow = vtkSmartPointer&lt;vtkRenderWindow&gt;::New();
      renderWindow-&gt;AddRenderer(renderer);
      renderWindow-&gt;SetSize(600, 600);
  
      vtkSmartPointer&lt;vtkRenderWindowInteractor&gt; interactor = vtkSmartPointer&lt;
              vtkRenderWindowInteractor&gt;::New();
      interactor-&gt;SetRenderWindow(renderWindow);
      renderer-&gt;SetBackground(0, 0, 0);
      renderWindow-&gt;Render();
  
      interactor-&gt;Start();
  
      return EXIT_SUCCESS;
  }
  </code></pre>