Convert a vtkImageData to a vtkPolyData.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char *[])
{
  // Create an image
  vtkSmartPointer source1 = 
    vtkSmartPointer::New();
  source1->SetScalarTypeToUnsignedChar();
  source1->SetNumberOfScalarComponents(3);
  source1->SetExtent(0, 100, 0, 100, 0, 0);
  source1->SetDrawColor(0,0,0,1);
  source1->FillBox(0, 100, 0, 100);
  source1->SetDrawColor(255,0,0,1);
  source1->FillBox(10, 20, 10, 20);
  source1->FillBox(40, 50, 20, 30);
  source1->Update();

  // Convert the image to a polydata
  vtkSmartPointer imageDataGeometryFilter = 
    vtkSmartPointer::New();
  imageDataGeometryFilter->SetInputConnection(source1->GetOutputPort());
  imageDataGeometryFilter->Update();

  // Create a mapper and actor
  vtkSmartPointer mapper = 
    vtkSmartPointer::New();
  mapper->SetInputConnection(imageDataGeometryFilter->GetOutputPort());

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

  // Visualization
  vtkSmartPointer renderer = 
    vtkSmartPointer::New();
  vtkSmartPointer renderWindow = 
    vtkSmartPointer::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer renderWindowInteractor = 
    vtkSmartPointer::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

 

你可能感兴趣的:(VTK,C++)