[vtkusers] VTKXYPlot slows down

Hughes, Mike msh at cmrl.wustl.edu
Sun Mar 9 18:54:21 EDT 2008


I'm a newbie trying to use vtkXYPlot to monitor output of an ultrasonic
measurement system.  The test program below starts out updating at over 100
frames per second but then slows down monotonically.
 
Is there a way to eliminate the slow down?
 
 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include <time.h>

#include <vtkImageWriter.h>
#include <vtkImageReader.h>
#include <vtkPointData.h>
//#include <vtkRenderer.h>
#include <vtkImageViewer.h>
//#include <vtkRenderWindowInteractor.h>
#include <vtkStructuredPoints.h>
#include <vtkFloatArray.h>
#include <vtkContourFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkXYPlotActor.h>

#include "vtkFloatArray.h"
#include "vtkFieldData.h"
#include "vtkDataObject.h"
#include "vtkXYPlotActor.h"
#include "vtkXYPlotWidget.h"
#include "vtkMath.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"


#include "memoryallocations.h"

const long NumberOfScales=5, NumberOfPoints=1024;
float ScaleArray[NumberOfScales];

int main(int argc, char *argv[]);

int main(int argc, char *argv[])
{	
	char FramesOutput[1024];
	long	i,j,k,Scale;
	float 	y,t;
	double	*TimeSeriesData=NULL,z;
	clock_t startTime, stopTime;
	FILE 	*fp_InputFile=NULL, *fp_OutputFile=NULL;
	
	// Allocate Data Arrays	
	vtkDataArray *dataArray1 = vtkDataArray::CreateDataArray(VTK_FLOAT);
	dataArray1->SetNumberOfTuples(NumberOfPoints);
	
	vtkDataArray *dataArray2 = vtkDataArray::CreateDataArray(VTK_FLOAT);
	dataArray2->SetNumberOfTuples(NumberOfPoints);
	
	// Set up the data pipeline?
	vtkFieldData *fieldData = vtkFieldData::New();
	fieldData->AllocateArrays(2);
	fieldData->AddArray(dataArray1);
	fieldData->AddArray(dataArray2);

	vtkDataObject *dataObject = vtkDataObject::New();
	dataObject->SetFieldData(fieldData);
	
	vtkXYPlotActor *plot = vtkXYPlotActor::New();
	vtkRenderer *renderer = vtkRenderer::New();
	renderer->AddActor2D(plot);

	vtkRenderWindow *renderWindow = vtkRenderWindow::New();
	renderWindow->AddRenderer(renderer);

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

	// Set up for Plot Interactions
	vtkXYPlotWidget* vtkXYPlotWidget = vtkXYPlotWidget::New(); 
	vtkXYPlotWidget->SetXYPlotActor( plot );
	vtkXYPlotWidget->SetInteractor( iren );
	vtkXYPlotWidget->SetEnabled( true );

	// Set Plot attributes
	plot->AddDataObjectInput(dataObject);
	plot->SetTitle("Received Signal");
	plot->SetXTitle("Time (us)");
	plot->SetYTitle("Volts");
	plot->SetXValuesToValue();
	plot->SetWidth(0.9);
	plot->SetHeight(0.9);
	plot->SetPosition(0.05, 0.05);
	plot->LegendOn();
	plot->PickableOff();
	plot->SetXRange( 0, NumberOfPoints );
	plot->SetYRange( -0.5, 2.0 );
	// Must state the component for the X & Y otherwise 0 is assumed for
both!!
	plot->SetDataObjectXComponent(0, 0);
	plot->SetDataObjectYComponent(0, 1);
	plot->SetPlotColor(0, 1.0, 0.0, 0.0);// Red
	plot->SetPlotLabel(0, "Time Data");
	
	renderWindow->SetSize(550, 450);
	
	iren->Initialize();
	
	// Set values for X-axis
	for ( i = 0; i < NumberOfPoints; i++)
	{
		t = i;
		dataArray1->SetTuple(i, &t);
	}
	fieldData->AddArray(dataArray1);

	int Frame, NumberOfFrames=10000;
	double FramesPerSec,ElapsedTime;
	startTime = clock ();
	for(Frame=0;Frame<NumberOfFrames;Frame++)
	{
		for ( i = 0; i < NumberOfPoints; i++)
		{
			y=((float)(random())+1.0)/((float)RAND_MAX+1.0);
			// Uniformly distributed in (0,1]
			dataArray2->SetTuple(i, &y); 
			// Update data, this does NOT lead to eventual
execution slowdown ...
			// checked by commenting out update lines
		}
		fieldData->AddArray(dataArray2);

		stopTime = clock ();
	
ElapsedTime=((double)(stopTime-startTime))/((double)CLOCKS_PER_SEC);
		FramesPerSec=((double)Frame)/ElapsedTime;
		sprintf(FramesOutput,"FPS=%lf",FramesPerSec);
		plot->SetYTitle(FramesOutput);

		renderWindow->Render();
		//if((Frame % 100 )==0) cout << "Frame: " <<  Frame << endl;
	}
	
	dataArray1->Delete();
	dataArray2->Delete();
	fieldData->Delete();
	dataObject->Delete();
	plot->Delete();
	renderer->Delete();
	renderWindow->Delete();
	iren->Delete();

	fprintf(stderr,"Program Execution Successful\n");
	return 0;
}



More information about the vtkusers mailing list