[vtkusers] Animated plot?

Torquil Macdonald Sørensen torquil at gmail.com
Fri Jun 27 09:44:25 EDT 2008


Hello

I am trying to plot some polygons, change their positions, then plot the new 
polygons, and so on. The first plot is correct, a pair of 8-vertex polygons, 
one on the lower left and one on the upper right. In the for-loop I translate 
the polygons a bit using a sine function. But the plot stays the same every 
time...  I expected the for-loop to result in an animated plot.

Here is the source:

#include <blitz/array.h>

#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"


#include "vtkRenderer.h"

using namespace std; using namespace blitz;

// N polygons of M vertices in D dimensions
const int N = 2, M = 8, D = 3;

void initialize(Array<double,3>& R);
void setPlotData(Array<double,3>& R,
				 vtkPoints* points, vtkCellArray* lines, vtkPolyData* polyData);

int main()
{
	Array<double,3> R(N,M,D); // Holds the polygon data
	
	initialize(R); // Initialize polygon positions

	vtkPoints* points = vtkPoints::New();
	vtkCellArray* lines = vtkCellArray::New();
	vtkPolyData* polyData = vtkPolyData::New();
	vtkPolyDataMapper* polyDataMapper = vtkPolyDataMapper::New();
	vtkActor* polyDataActor = vtkActor::New();
	vtkRenderer* renderer= vtkRenderer::New();
	vtkRenderWindow* renderWindow = vtkRenderWindow::New();

	setPlotData(R, points, lines, polyData);

	polyData->SetPoints(points);
	polyData->SetLines(lines);
	polyDataMapper->SetInput(polyData);
	polyDataActor->SetMapper(polyDataMapper);
	renderer->AddActor(polyDataActor);
	renderer->SetBackground(0.0, 0.0, 0.0);
	renderWindow->AddRenderer(renderer);
	renderWindow->SetSize(500, 500);

	for (int i = 0; i < 10; ++i) {
		renderWindow->Render();
		cout << "Sleeping for 1 sec..." << endl;
		sleep(1);
		
		R += sin(double(i)); // Modify R by translating the polygons a bit.
		setPlotData(R, points, lines, polyData);
	}
	
	points->Delete();
	lines->Delete();
	polyDataMapper->Delete();
	polyDataActor->Delete();
	renderer->Delete();
	renderWindow->Delete();
	
	return(0);
}

void initialize(Array<double,3>& R)
{
	for(int n = 0; n != N; ++n) {
		for(int m = 0; m != M; ++m) {
			R(n,m,0) = 0.25*cos(2*double(m)/M*M_PI) + n - 1;
			R(n,m,1) = 0.25*sin(2*double(m)/M*M_PI) + n - 1;
			R(n,m,2) = 0.0;
		}
	}
}

void setPlotData(Array<double,3>& R, vtkPoints* points, vtkCellArray* lines, 		
vtkPolyData* polyData)
{
	points->Initialize();
	lines->Initialize();
	int pointID = 0;
	double p[D];
	for(int n = 0; n != N; ++n) {
		for(int m = 0; m != M; ++m) {
			for(int d = 0; d != D; ++d) {
				p[d] = R(n,m,d);
			}
			points->InsertNextPoint(p);
			for(int d = 0; d != D; ++d) {
				p[d] = R(n,(m+1)%M,d);
			}
			points->InsertNextPoint(p);

			lines->InsertNextCell(2);
			lines->InsertCellPoint(pointID);
			lines->InsertCellPoint(pointID+1);
			pointID += 2;
		}
	}
	polyData->SetPoints(points);
	polyData->SetLines(lines);
}



More information about the vtkusers mailing list