Efficient use of ::New() for outputs ?

John Biddiscombe j.biddiscombe at rl.ac.uk
Wed Sep 8 11:30:43 EDT 1999


Dear People,

if you have a filter such as vtkDoSomethingUsefulFilter embedded in some code


vtkDoSomethingUsefulFilter *f1 = vtkDoSomethingUsefulFilter::New();
	f1->SetParam1(...);

for (int i=0; i<65536; i++) {
	f1->SetParam2( i*a + b); // or something nice
	f1->Update();
	
	float num = f1->GetOutput()->GetPointData()->GetScalars()->GetScalar(57);
	// or something else of this kind
}

and inside 
vtkDoSomethingUsefulFilter::Execute() {
	
	vtkStructuredPoints *output = this->GetOutput();
	vtkScalars *outscalars = vtkScalars::New();
		outscalars->SetDimensions(1);
		outscalars->SetOrigin(...);
		outscalars->SetSpacing(...);
	output->GetPointData()->SetScalars(outscalars);

	// Perform tedious calculation

}

Now every time the loop calls Update() - Execute is called and a new Scalar
object is created. It may be the case that inside this loop, the
dimensions/spacing/origin etc etc of the scalar output array simply don't
change and only the values that vtkDoSomethingUsefulFilter puts ionto it
are changing.

QUESTION

is it OK to simply do this

vtkDoSomethingUsefulFilter::Execute() {
	
	vtkStructuredPoints *output = this->GetOutput();
	
	vtkScalars *outscalars = output->GetPointData()->GetScalars();
	if (dimensions origin or something needs changing)
		outscalars = vtkScalars::New();
	} else {
		leave the array as it was and just overwrite the values
	}
	output->GetPointData()->SetScalars(outscalars);

}

?????????

In the above example it may save 65534 calls to New() etc etc.

Is it safe to do this, or is there some naughty reason (eg multithreading)
why we should avoid this sort of behaviour?

Thanks

John B




-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------





More information about the vtkusers mailing list