[vtkusers] Behavior I can't explain with vtkProgrammableSource

Kenneth Evans, Jr. evans at aps.anl.gov
Sun Sep 24 13:50:30 EDT 2006


Hi,

     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.  The relevant parts of the code are below.  It is Java, and I
am not set up to easily see if the same occurs with C++.

     The data is a 2D Gaussian (x, y) that should be colored by z.  I make
the programmable source, write its output to a file, and then read the file.
I also create a vtkStructuredGrid using new at the same time and in the same
way as the source.  I then send one of the three to the mapper (see the
switch on int mode).  mode=0 gives a blank screen.  The others work as
expected.  When written to a file both sgrid and sgrid1 give identical
files.  Thus the programmable source works, it just doesn't map.  It could
be a glitch on my part that I'm not seeing, but I've worked on it a long
time.

     The other modes are there only for testing.  I only want to use the
source.

     There is only one vtkProgrammableSource example, and it doesn't do
quite this.  (The example works OK for me when converted to Java.  It also
has intermediate steps, and mine too works when there are intermediate
steps.) 

     Thanks in advance.

	-Ken

---------------------------------------------------------------
  private vtkRenderer renderer = null;
  private vtkProgrammableSource source = null;
  private vtkStructuredGrid sgrid = null;
  private vtkStructuredGrid sgrid1 = null;
  private vtkActor actor = null;

  public void createPanel() {
    // Make a JPanel
    JPanel panel = new JPanel();
    BorderLayout layout = new BorderLayout();
    panel.setLayout(layout);
    setPanel(panel);

    // Make a vtkPanel and add it to the JPanel
    vtkPanel renWin = new vtkPanel();
    panel.add(renWin, BorderLayout.CENTER);

    // Get the renderer
    renderer = renWin.GetRenderer();
    renderer.SetBackground(1, 1, 1); // background color white

    // Create the structured grid dataset with the appropriate function
    source = new vtkProgrammableSource();
    source.SetExecuteMethod(this, "func");
    sgrid = source.GetStructuredGridOutput();
    if(DEBUG_LEVEL > 0) {
      sgrid.Update();
      System.out.println("sgrid:");
      System.out.print(sgrid);
    }
    String fileName = "Source.test.txt";
    if(true) {
      // Write the file
      System.out.println("Written to " + fileName);
      vtkDataSetWriter writer = new vtkDataSetWriter();
      writer.SetInput(sgrid);
      writer.SetFileName(fileName);
      
      writer.Write();
    }
    
    // Read the file
    vtkDataSetReader reader = new vtkDataSetReader();
    reader.SetFileName(fileName);

    // Make a dataset mapper
    vtkDataSetMapper mapper = new vtkDataSetMapper();
    int mode = 0;
    switch(mode) {
    case 0:
      System.out.println("Using source");
      mapper.SetInput(sgrid);
      break;
    case 1:
      System.out.println("Using sgrid1");
      mapper.SetInput(sgrid1);
      break;
    case 2:
      System.out.println("Using reader with " + fileName);
      mapper.SetInput(reader.GetStructuredGridOutput());
      break;
    }

    // Make the actor
    actor = new vtkActor();
    actor.SetMapper(mapper);
    renderer.AddActor(actor);

    // Reset the camera
    renderer.ResetCamera();

    created = true;
  }

  /**
   * The function for the programmable source.
   */
  public void func() {
    // Make arrays for vectors, scalars, and points
    vtkFloatArray scalars = new vtkFloatArray();
    scalars.SetNumberOfComponents(1);
    scalars.SetName("Scalars");
    vtkPoints points = new vtkPoints();
    
    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 = new vtkStructuredGrid();
    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 * Math.exp(-.5 * arg1 * arg1) * a2 * Math.exp(-.5 * arg2 *
arg2);
        points.InsertNextPoint(x, y, 0);
        scalars.InsertNextTuple1(z);
      }
    }
  }




More information about the vtkusers mailing list