[vtkusers] vtkCutter->Update doesn't update the vtkCutter->GetOutput()

Bill Lorensen bill.lorensen at gmail.com
Thu Nov 4 00:50:14 EDT 2010


Here is a complete working copy. I had to put the plane in the loop also:

// slicing of a sphere by a plane. the plane is iteratively rotated all
// around the sphere

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkTransform.h>
#include <vtkPlane.h>
#include <vtkCutter.h>
#include <vtkMath.h>
#include <vtkAppendPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vector>

int main (int argc, char *argv[])
{
  // The object to cut
  vtkSmartPointer<vtkSphereSource> sph =
    vtkSmartPointer<vtkSphereSource>::New();
  sph->SetRadius(25.);
  sph->SetPhiResolution(50);
  sph->SetThetaResolution(50);

  vtkPolyData *polydata = sph->GetOutput();

  // Init the cutting plane
  double bounds[6], center[3];
  polydata->GetBounds(bounds);
  polydata->GetCenter(center);

  int nbsections = 10;
  double angle = 360./(double)nbsections;

  // Init the transformation
  vtkSmartPointer<vtkTransform> transf =
    vtkSmartPointer<vtkTransform>::New();
  transf->RotateWXYZ(angle, 0.0, 0.0, 1.0);

  vtkSmartPointer<vtkMatrix4x4> mat =
    vtkSmartPointer<vtkMatrix4x4>::New();
  transf->GetMatrix(mat);

  vtkSmartPointer<vtkAppendPolyData> appdata =
    vtkSmartPointer<vtkAppendPolyData>::New();

  double normal[4]={
    bounds[0]-center[0],
    bounds[2]-center[1],
    0,
    1};

  double new_normal[4];

  for(int step=0; step<nbsections; step++)
    {
    vtkSmartPointer<vtkPlane> plane =
      vtkSmartPointer<vtkPlane>::New();

    // Rotation of the cutting plane around normal
    mat->MultiplyPoint(normal, new_normal);
    plane->SetNormal(new_normal);
    plane->SetOrigin(center);
    plane->GetNormal(normal);

    vtkSmartPointer<vtkCutter> cutter =
      vtkSmartPointer<vtkCutter>::New();
    cutter->SetInput(polydata);
    cutter->SetCutFunction(plane);

    appdata->AddInput(cutter->GetOutput());

    }

  //visualisation
  //1. All slices
  vtkSmartPointer<vtkPolyDataMapper> appmapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  appmapper->SetInput(appdata->GetOutput());

  vtkSmartPointer<vtkActor> appactor =
    vtkSmartPointer<vtkActor>::New();
  appactor->SetMapper(appmapper);
  appactor->GetProperty()->SetColor(1,0,0);

  //2. polydata
  vtkSmartPointer<vtkPolyDataMapper> teethmapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  teethmapper->SetInput(polydata);

  vtkSmartPointer<vtkActor> teethactor =
    vtkSmartPointer<vtkActor>::New();
  teethactor->SetMapper(teethmapper);
  teethactor->GetProperty()->SetColor(0.8,0.8,0.8);

  // The usual renderer, render window and interactor
  vtkSmartPointer<vtkRenderer> ren1 =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();

  ren1->SetBackground(.1, .2, .3);
  renWin->AddRenderer(ren1);
  iren->SetRenderWindow(renWin);

  ren1->AddActor(appactor);
  ren1->AddActor(teethactor);

  renWin->Render();
  iren->Start();
}



More information about the vtkusers mailing list