[vtkusers] Using vtkImplicitDataSet to clip data

Roger Mason rmason at mun.ca
Mon May 16 08:59:08 EDT 2011


Hello,

I'm trying to use a vtkImplicitDataSet to apply an arbitrary surface as
a clipping function.  A minimal version of the code is given below:

[ -- read in data from file -- ]

  vtkSmartPointer<vtkPlane> plane 
    = vtkSmartPointer<vtkPlane>::New();
  plane->SetOrigin(0,0,0);
  plane->SetNormal(0,0,1);

  vtkSmartPointer<vtkImplicitSum> sum 
    = vtkSmartPointer<vtkImplicitSum>::New();
  sum->AddFunction(plane);

  vtkSmartPointer<vtkSampleFunction> sample
    = vtkSmartPointer<vtkSampleFunction>::New();
  sample->SetImplicitFunction(sum);
  sample->SetSampleDimensions(10, 10, 20);

  vtkSmartPointer<vtkContourFilter> surface
    = vtkSmartPointer<vtkContourFilter>::New();
  surface->SetInputConnection(sample->GetOutputPort());
  surface->SetValue(0, 0.0);

  vtkSmartPointer<vtkImplicitDataSet> cutfunction
    = vtkSmartPointer<vtkImplicitDataSet>::New();
  cutfunction->SetDataSet(surface->GetOutput());

  // Clip on the cut surface
  vtkSmartPointer<vtkClipPolyData> clipper =
    vtkSmartPointer<vtkClipPolyData>::New();
  clipper->SetInput(polyData);
  clipper->SetClipFunction(cutfunction);
  clipper->GenerateClipScalarsOn();
  clipper->GenerateClippedOutputOn();
  clipper->InsideOutOn();
  clipper->Update();
  polyData = clipper->GetClippedOutput();

  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInput(polyData);

  vtkSmartPointer<vtkActor> actor
    = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  // Graphics
  vtkSmartPointer<vtkRenderer> ren =
    vtkSmartPointer<vtkRenderer>::New();
  ren->SetBackground(.1, .2, .3);
 
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer(ren);
  renWin->SetSize(512,512);
 
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow(renWin);

  actor->GetProperty()->SetColor(1,0,0);
  actor->GetProperty()->SetInterpolationToFlat();
  ren->AddActor(actor);

  ren->ResetCamera();
  iren->Initialize();
  iren->Start();

The program compiles and runs and I have verified that the input data
are read correctly and the clipping surface is generated.  However, the
results obtained with GetClippedOutput() are not displayed.  I have
inserted a writer to write the clipped data as ascii to a file.  On
opening the file in a test program I see the error:

"Generic Warning: In /home/rmason/Software/VTK/IO/vtkDataReader.cxx, line 1383
Error reading ascii data. Possible mismatch of datasize with
declaration."

If anyone can suggest how to debug this further, I'd appreciate the
help.

Thanks,
Roger




More information about the vtkusers mailing list