Visualize a Sphere or a VTP File

From KitwarePublic
Jump to navigationJump to search

SphereDemo() shows the basics of rendering to a window. VTPDemo shows how to open a vtp file and render it in a window.

<source lang="cpp">

  1. include "vtkQuadric.h"
  2. include "vtkSampleFunction.h"
  3. include "vtkContourFilter.h"
  4. include "vtkOutlineFilter.h"
  5. include "vtkPolyDataMapper.h"
  6. include "vtkActor.h"
  7. include "vtkProperty.h"
  8. include "vtkRenderWindow.h"
  9. include "vtkRenderer.h"
  10. include "vtkRenderWindowInteractor.h"
  11. include "vtkImageData.h"
  12. include "vtkSphereSource.h"
  1. include "vtkPolyData.h"
  2. include "vtkXMLPolyDataReader.h"
  3. include "vtkSmartPointer.h"

void SphereDemo(); void VTPDemo();

//pressing 'p' (pick) while the mouse is over an object displays its bounding box //'s' change to Surface rendering //'w' change to Wireframe rendering

int main () { std::cout << "test"; //SphereDemo(); VTPDemo(); return 0; }

void SphereDemo() { vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New(); sphere->SetCenter(0.0, 0.0, 0.0); sphere->SetRadius(5.0);

	vtkPolyData* polydata = sphere->GetOutput();

// map the contours to graphical primitives vtkPolyDataMapper *contMapper = vtkPolyDataMapper::New(); contMapper->SetInput(polydata);

 	// create an actor for the contours

vtkActor *contActor = vtkActor::New(); contActor->SetMapper(contMapper);

 	// a renderer and render window

vtkRenderer *ren1 = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(ren1);

 	// an interactor

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWin);

// add the actors to the scene ren1->AddActor(contActor); ren1->SetBackground(1,1,1); // Background color white

 	// render an image (lights and cameras are created automatically)

renWin->Render();

 	// begin mouse interaction

iren->Start(); }

void VTPDemo() { vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New(); reader->SetFileName("car.vtp"); reader->Update();

vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();

// map the contours to graphical primitives vtkPolyDataMapper *contMapper = vtkPolyDataMapper::New(); contMapper->SetInput(polydata);

	 // create an actor for the contours

vtkActor *contActor = vtkActor::New(); contActor->SetMapper(contMapper);

// a renderer and render window vtkRenderer *ren1 = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(ren1);

 	// an interactor

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWin);

// add the actors to the scene ren1->AddActor(contActor); ren1->SetBackground(1,1,1); // Background color white

 	// render an image (lights and cameras are created automatically)

renWin->Render();

 	// begin mouse interaction

iren->Start();

} </source>