[vtkusers] Re: Behavior I can't explain with vtkProgrammableSource
Kenneth Evans, Jr.
evans at aps.anl.gov
Wed Sep 27 23:21:37 EDT 2006
> I am having a problem with vtkProgrammableSource with a
> vtkStructuredGrid dataset. Nothing appears on the screen if I just use
> source.GetStructuredGridOutput(). However, the dataset is OK. If I write
> it out and then read it back, all is well. If I create the same dataset
> using new instead of the programmable source, it is also OK. I can't
> explain this.
Ok, I didn't get any responses. Perhaps it is because of Java. I've now
tried it in C++, and it does the same thing. When the mapper gets its input
from the programmable source, it doesn't work. When it gets its input from
a new vtkStructuredPoints, it does work. Both are created almost exactly
the same way.
The C code is below. Most of it is boilerplate except the start of main()
and the func. Thanks,
-Ken
#include "vtkActor.h"
#include "vtkBoxWidget.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkDataSetMapper.h"
#include "vtkFloatArray.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkMath.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkProgrammableSource.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkStructuredgrid.h"
#include "vtkTransform.h"
#include <math.h>
static void func(void *arg);
static vtkProgrammableSource *source = NULL;
static vtkStructuredGrid *sgrid1 = NULL;
// Define a callback for interaction.
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New() {
return new vtkMyCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void*) {
vtkTransform *t = vtkTransform::New();
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
widget->GetTransform(t);
widget->GetProp3D()->SetUserTransform(t);
t->Delete();
}
};
int main() {
// Create the structured grid dataset with the appropriate function
source = vtkProgrammableSource::New();
source->SetExecuteMethod(func, NULL);
source->Update(); // So it will create sgrid1
vtkStructuredGrid *sgrid = source->GetStructuredGridOutput();
vtkDataSetMapper *mapper = vtkDataSetMapper::New();
int mode = 1;
switch(mode) {
case 0:
mapper->SetInput(sgrid);
break;
case 1:
mapper->SetInput(sgrid1);
break;
}
vtkActor *actor = vtkActor::New();
actor->SetMapper(mapper);
//
// Create the Renderer and assign actors to it. A renderer is like a
// viewport. It is part or all of a window on the screen and it is
// responsible for drawing the actors it has. We also set the background
// color here.
//
vtkRenderer *ren1= vtkRenderer::New();
ren1->AddActor(actor);
ren1->SetBackground( 0.1, 0.2, 0.4 );
// ren1->SetBackground( 1.0, 1.0, 1.0 );
//
// Finally we create the render window which will show up on the screen.
// We put our renderer into the render window using AddRenderer. We also
// set the size to be 300 pixels by 300.
//
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer( ren1 );
renWin->SetSize( 300, 300 );
//
// The vtkRenderWindowInteractor class watches for events (e.g., keypress,
// mouse) in the vtkRenderWindow. These events are translated into
// event invocations that VTK understands (see VTK/Common/vtkCommand.h
// for all events that VTK processes). Then observers of these VTK
// events can process them as appropriate.
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkInteractorStyleTrackballCamera *style =
vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
// Initialize the event loop and then start it.
iren->Initialize();
iren->Start();
//
// Free up any objects we created. All instances in VTK are deleted by
// using the Delete() method.
//
source->Delete();
mapper->Delete();
actor->Delete();
ren1->Delete();
renWin->Delete();
style->Delete();
iren->Delete();
sgrid1->Delete();
return 0;
}
void func(void *arg) {
// Make arrays for vectors, scalars, and points
vtkFloatArray *scalars = vtkFloatArray::New();
scalars->SetNumberOfComponents(1);
scalars->SetName("Scalars");
vtkPoints *points = vtkPoints::New();
int nPoints1 = 100;
int nPoints2 = 100;
// Get the programmable source output
vtkStructuredGrid *output = source->GetStructuredGridOutput();
output->SetDimensions(nPoints1, nPoints2, 1);
output->SetPoints(points);
output->GetPointData()->SetScalars(scalars);
// Create a new structured grid with the same data
sgrid1 = vtkStructuredGrid::New();
sgrid1->SetDimensions(nPoints1, nPoints2, 1);
sgrid1->SetPoints(points);
sgrid1->GetPointData()->SetScalars(scalars);
double a1 = 3;
double sigma1 = .5;
double mean1 = 0;
double upper1 = 3.0;
double lower1 = -3.0;
double delta1 = (upper1 - lower1) / (nPoints1 - 1);
double a2 = 1;
double sigma2 = 2.0;
double mean2 = 0;
double upper2 = 3.0;
double lower2 = -3.0;
double delta2 = (upper2 - lower2) / (nPoints2 - 1);
double x, y, z, arg1, arg2;
for(int j = 0; j < nPoints2; j++) {
y = lower2 + delta2 * j;
arg2 = (y - mean2) / sigma2;
for(int i = 0; i < nPoints1; i++) {
x = lower1 + delta1 * i;
arg1 = (x - mean1) / sigma1;
z = a1 * exp(-.5 * arg1 * arg1) * a2 * exp(-.5 * arg2 * arg2);
points->InsertNextPoint(x, y, 0);
scalars->InsertNextTuple1(z);
}
}
scalars->Delete();
points->Delete();
}
More information about the vtkusers
mailing list