Visualize a Sphere or a VTP File

From KitwarePublic

Jump to: navigation, search

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

#include "vtkQuadric.h"
#include "vtkSampleFunction.h"
#include "vtkContourFilter.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
#include "vtkSphereSource.h"
 
#include "vtkPolyData.h"
#include "vtkXMLPolyDataReader.h"
#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();
 
}
Personal tools