[visad] point shapes

Hello!  The attached java code plots one of two sets of data using the 4
symbols from Test46/47.  With one set of data, everything works fine.
With the other, nothing plots at all.  What am I doing wrong?

To run it with the data that works, use:

java -classpath build:./lib/visad-2.0.jar gui.TwoDPlot g

To run it with the data that fails, omit the "g" argument.

Thanks!
-- 
glen e. p. ropella, 971-222-9095, http://tempusdictum.com

/*
 * TwoDPlot.java
 *
 * Created on July 17, 2006, 4:33 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package gui;

import visad.RealType;
import visad.RealTupleType;
import visad.FlatField;
import visad.FunctionType;
import visad.Integer1DSet;
import visad.Irregular1DSet;
//import org.apache.log4j.Logger;

/**
 *
 * @author gepr
 */
public class TwoDPlot {

//   private static final Logger logger =
//           Logger.getLogger("ocusim");
   String depLabel = "DependentVariable"; // names cant have spaces
   String indLabel = "IndependentVariable";
   String depUnitsLabel = "(ng/g)";
   String indUnitsLabel = "(hrs)";
   visad.Unit depUnit = null;
   visad.Unit indUnit = null;
   visad.DisplayImpl display = null;
   public javax.swing.JPanel widgetPanel = null; // alt for visad's 
"getWidgetPanel()"
   // instance variables for convenient range resets
   double max_ind = Double.MIN_VALUE;
   double max_dep = Double.MIN_VALUE;
   double min_dep = Double.MAX_VALUE;
   visad.VisADLineArray cross = new visad.VisADLineArray();
   visad.VisADLineArray box = new visad.VisADLineArray();
   visad.VisADQuadArray square = new visad.VisADQuadArray();
   visad.VisADTriangleArray tri = new visad.VisADTriangleArray();

   visad.VisADGeometryArray[] shapes = { box, tri, square, cross };
   float[][] count_set_vals = {{0.0f, 1.0f, 2.0f, 3.0f}};  // for quantizing 
plot symbols

   public TwoDPlot() {

      cross.coordinates = new float[]
        {0.2f,  0.2f, 0.0f,    -0.2f, -0.2f, 0.0f,
         0.2f, -0.2f, 0.0f,    -0.2f,  0.2f, 0.0f};
      cross.vertexCount = cross.coordinates.length / 3;

      box.coordinates = new float[]
        {0.1f,  0.1f, 0.0f,     0.1f, -0.1f, 0.0f,
         0.1f, -0.1f, 0.0f,    -0.1f, -0.1f, 0.0f,
        -0.1f, -0.1f, 0.0f,    -0.1f,  0.1f, 0.0f,
        -0.1f,  0.1f, 0.0f,     0.1f,  0.1f, 0.0f};
      box.vertexCount = box.coordinates.length / 3;

      square.coordinates = new float[]
        {0.1f,  0.1f, 0.0f,     0.1f, -0.1f, 0.0f,
        -0.1f, -0.1f, 0.0f,    -0.1f,  0.1f, 0.0f};
      square.vertexCount = square.coordinates.length / 3;

      tri.coordinates = new float[]
         {-0.1f, -0.05f, 0.0f,    0.1f, -0.05f, 0.0f,
           0.0f,  0.1f,  0.0f};
      tri.vertexCount = tri.coordinates.length / 3;

   }

   public void setLabels(String i, String iu, String d, String du) {
      if (i != null)
         indLabel = i;
      if (d != null)
         depLabel = d;
      if (iu != null)
         indUnitsLabel = iu;
      if (du != null)
         depUnitsLabel = du;
   }

   public void plot(double ind[], double dep[], boolean useShapes, boolean 
plotLines)
           throws visad.VisADException, java.rmi.RemoteException {
      RealType independent = null;
      RealType dependent = null;
      RealTupleType tuple = null;
      RealType index = null;
      Integer1DSet index_set = null;
      Irregular1DSet independent_set = null;
      FunctionType points_func_i_tuple = null;
      FunctionType lines_func_independent_dep = null;
      FlatField points_ff = null;
      FlatField lines_ff = null;

      depUnit = new visad.DerivedUnit(new visad.DerivedUnit(visad.SI.kilogram), 
"ng").scale(1.0e-12);
      //indUnit = new visad.DerivedUnit(new 
visad.DerivedUnit(visad.SI.second),"hour").scale(1/3600);
      indUnit = visad.SI.second;

      independent = RealType.TimeInterval;
      dependent = RealType.getRealType(depLabel, depUnit);
      tuple = new RealTupleType(independent, dependent);
      index = RealType.getRealType(indLabel, indUnit);
      index_set = new Integer1DSet(index, ind.length);
      points_func_i_tuple = new FunctionType(index, tuple);
      lines_func_independent_dep = new FunctionType(independent, dependent);
      points_ff = new FlatField(points_func_i_tuple, index_set);

      final int INDEPENDENT_NDX = 0;
      final int DEPENDENT_NDX = 1;

      double[][] data = new double[2][ind.length];
      float[][] data_t = new float[1][ind.length];
      float[][] t2d = new float[1][ind.length];
      for (int i = 0; i < ind.length; i++) {
         data[INDEPENDENT_NDX][i] = ind[i];
         if (ind[i] > max_ind)
            max_ind = ind[i];
         data[DEPENDENT_NDX][i] = dep[i];
         if (data[DEPENDENT_NDX][i] > max_dep)
            max_dep = data[DEPENDENT_NDX][i];
         if (data[DEPENDENT_NDX][i] < min_dep)
            min_dep = data[DEPENDENT_NDX][i];

         t2d[0][i] = (float) ind[i];
         data_t[0][i] = (float) dep[i];
      }

      points_ff.setSamples(data);

      if (plotLines) {
         independent_set = new Irregular1DSet(independent, t2d);
         lines_ff = new FlatField(lines_func_independent_dep, independent_set);
         lines_ff.setSamples(data_t);
      }

      display = generateDisplay(independent, max_ind, dependent,
              min_dep, max_dep, tuple, points_ff, useShapes, lines_ff);

   }

   public visad.DisplayImpl generateDisplay(RealType independent, double 
max_indep,
           RealType dependent, double min_dep, double max_dep, RealTupleType 
tuple,
           FlatField points_ff, boolean useShapes, FlatField lines_ff)
           throws visad.VisADException, java.rmi.RemoteException {

      //visad.java2d.DisplayImplJ2D d = new 
visad.java2d.DisplayImplJ2D("display1");
      visad.java3d.DisplayImplJ3D d = new 
visad.java3d.DisplayImplJ3D("display1", new 
visad.java3d.TwoDDisplayRendererJ3D());
      visad.GraphicsModeControl dispGMC = (visad.GraphicsModeControl) 
d.getGraphicsModeControl();
      dispGMC.setScaleEnable(true);

      // set the display colors
      visad.DisplayRenderer dr = d.getDisplayRenderer();
      visad.RendererControl rc = dr.getRendererControl();
      rc.setBackgroundColor(java.awt.Color.white);
      rc.setBoxColor(java.awt.Color.black);
      rc.setForegroundColor(java.awt.Color.black);
      rc.setCursorColor(java.awt.Color.black);

      visad.ScalarMap independentMap = new visad.ScalarMap(independent, 
visad.Display.XAxis);
      independentMap.setScalarName(indLabel + " " + indUnitsLabel);
      independentMap.getAxisScale().setLabelAllTicks(true);
      visad.ScalarMap depMap = new visad.ScalarMap(dependent, 
visad.Display.YAxis);
      depMap.setScalarName(depLabel + " " + depUnitsLabel);
      depMap.getAxisScale().setLabelAllTicks(true);
      independentMap.setRange(0D, max_indep);
      float[] black = {0.0f, 0.0f, 0.0f};
      independentMap.setScaleColor(black);
      depMap.setRange(min_dep, max_dep);
      depMap.setScaleColor(black);
      d.addMap(independentMap);
      d.addMap(depMap);

      visad.DataReferenceImpl points_ref = new 
visad.DataReferenceImpl("points_ref");
      points_ref.setData(points_ff);
      visad.ConstantMap[] pointsCMap = {new visad.ConstantMap(1.0f, 
visad.Display.Red),
         new visad.ConstantMap(0.0f, visad.Display.Green),
         new visad.ConstantMap(0.0f, visad.Display.Blue),
         new visad.ConstantMap(3.50f, visad.Display.PointSize)
      };
      d.addReference(points_ref, pointsCMap);
      if (useShapes) setupShapes(d,dependent);

      if (lines_ff != null) {
         visad.DataReferenceImpl lines_ref = new 
visad.DataReferenceImpl("lines_ref");
         lines_ref.setData(lines_ff);
         visad.ConstantMap[] linesCMap = {new visad.ConstantMap(0.0f, 
visad.Display.Red),
            new visad.ConstantMap(1.0f, visad.Display.Green),
            new visad.ConstantMap(0.0f, visad.Display.Blue),
            new visad.ConstantMap(1.00f, visad.Display.LineWidth)
         };
         d.addReference(lines_ref, linesCMap);
      }

      setupBoxSelect(d, independent, independentMap, dependent, depMap, tuple);

      return d;
   }

   private void setupShapes(visad.Display d, visad.RealType rt) 
   throws visad.VisADException, java.rmi.RemoteException {
      visad.ScalarMap shape_map = new visad.ScalarMap(rt, visad.Display.Shape);
      d.addMap(shape_map);

      visad.Gridded1DSet count_set =
         new visad.Gridded1DSet(rt, count_set_vals, count_set_vals[0].length);
      visad.ShapeControl shape_control = (visad.ShapeControl) 
shape_map.getControl();
      shape_control.setShapeSet(count_set);
      shape_control.setShapes(shapes);
      shape_control.setScale(0.4f);
   }
   
   private void setupBoxSelect(visad.DisplayImpl display_local, RealType x, 
visad.ScalarMap xMap, RealType y, visad.ScalarMap yMap, RealTupleType xy)
           throws visad.VisADException, java.rmi.RemoteException {
      final visad.ScalarMap xMap_f = xMap;
      final visad.ScalarMap yMap_f = yMap;
      visad.Gridded2DSet dummy_set = new visad.Gridded2DSet(xy, null, 1);
      final visad.DataReferenceImpl set_ref = new 
visad.DataReferenceImpl("set");
      set_ref.setData(dummy_set);
      int m = 0; // use "java.awt.event.InputEvent.CTRL_MASK" to require 
ctrl-click
      display_local.addReferences(new visad.bom.RubberBandBoxRendererJ3D(x, y, 
m, m), set_ref);

      // visible ranges
      visad.ScalarMap x_srm = new visad.ScalarMap(x, visad.Display.SelectRange);
      display_local.addMap(x_srm);
      final visad.RangeControl x_rc = (visad.RangeControl) x_srm.getControl();
      visad.ScalarMap y_srm = new visad.ScalarMap(y, visad.Display.SelectRange);
      display_local.addMap(y_srm);
      final visad.RangeControl y_rc = (visad.RangeControl) y_srm.getControl();

      visad.CellImpl cell = new visad.CellImpl() {

         public void doAction() throws visad.VisADException, 
java.rmi.RemoteException {
            visad.Set set = (visad.Set) set_ref.getData();
            float[][] samples = set.getSamples();
            if (samples != null) {
               System.out.println("Set ranges to (" + samples[0][0] + ", " + 
samples[1][0] +
                       ") to (" + samples[0][1] + ", " + samples[1][1] + ")");

               // we have to sort the data, else every other selection will 
flip the plot
               float min = Float.MAX_VALUE, max = Float.MIN_VALUE;
               if (samples[0][0] > samples[0][1]) {
                  min = samples[0][1];
                  max = samples[0][0];
               } else {
                  min = samples[0][0];
                  max = samples[0][1];
               }
               xMap_f.setRange(min, max);
               x_rc.setRange(new float[]{min, max});

               if (samples[1][0] > samples[1][1]) {
                  min = samples[1][1];
                  max = samples[1][0];
               } else {
                  min = samples[1][0];
                  max = samples[1][1];
               }
               yMap_f.setRange(min, max);
               y_rc.setRange(new float[]{min, max});
            }
         }
      };
      cell.addReference(set_ref);

      // add a scale reset button
      javax.swing.JButton scaleResetButton = new javax.swing.JButton("Reset 
Scale");
      scaleResetButton.setFont(scaleResetButton.getFont().deriveFont(8.0f));
      scaleResetButton.addActionListener(new java.awt.event.ActionListener() {

         public void actionPerformed(java.awt.event.ActionEvent evt) {
            try {
               xMap_f.setRange(0.0d, max_ind);
               x_rc.setRange(new double[]{0.0d, max_ind});
               yMap_f.setRange(min_dep, max_dep);
               y_rc.setRange(new double[]{min_dep, max_dep});
            } catch (visad.VisADException e) {
               e.printStackTrace();
            } catch (java.rmi.RemoteException e) {
               e.printStackTrace();
            }
         }
      });

      if (widgetPanel == null) {
         widgetPanel = new javax.swing.JPanel();
         widgetPanel.setLayout(new javax.swing.BoxLayout(widgetPanel, 
javax.swing.BoxLayout.Y_AXIS));
      }
      widgetPanel.add(scaleResetButton);
      widgetPanel.setVisible(true);

   }

   public visad.DisplayImpl getDisplay() {
      return display;
   }
   
   public static void main(String[] args) {
      double[] x, y;

      if (args.length > 0 && args[0].equals("g")) {
            x = new double[40]; y = new double[40];
            for (int xNdx=0 ; xNdx<10 ; xNdx++) {
               double xval = new Integer(xNdx).doubleValue();
               for (int yNdx=0 ; yNdx<4 ; yNdx++){
                  x[xNdx*4+yNdx] = xval;
                  y[xNdx*4+yNdx] = new Integer(yNdx).doubleValue();
               }
         }
      } else {
         x = new double[12];
         y = new double[12];
         x[0] = 5.75;
         x[1] = 5.75;
         x[2] = 5.75;
         x[3] = 5.75;
         x[4] = 7.5;
         x[5] = 7.5;
         x[6] = 7.5;
         x[7] = 7.5;
         x[8] = 13.5;
         x[9] = 13.5;
         x[10] = 13.5;
         x[11] = 13.5;

         y[0] = 22239.0;
         y[1] = 21530.0;
         y[2] = 21500.0;
         y[3] = 20887.0;
         y[4] = 7737.0;
         y[5] = 4436.0;
         y[6] = 3133.0;
         y[7] = 9997.0;
         y[8] = 3983.0;
         y[9] = 1886.0;
         y[10] = 1950.0;
         y[11] = 3485.0;
      }
      
      System.out.println("x,y = ");
      for (int ndx=0 ; ndx<x.length ; ndx++) {
         System.out.println("\t" + x[ndx] + ", " + y[ndx]);
      }
              
      javax.swing.JLabel jlabel = new javax.swing.JLabel("TwoDPlot Test", 
javax.swing.SwingConstants.CENTER);

      java.awt.GridBagConstraints gbc = null;
      javax.swing.JPanel panel = null;

      TwoDPlot twoDPlot = new TwoDPlot();
      twoDPlot.setLabels("X", "(hrs)", "Y", "()");

      java.awt.GridBagLayout gb = new java.awt.GridBagLayout();
      panel = new javax.swing.JPanel(gb);
      gbc = new java.awt.GridBagConstraints();

      gbc.gridx = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 0.0;
      panel.add(jlabel, gbc);
      
      try {
         twoDPlot.plot(x, y, true, false);
      } catch (Exception e) {
         System.err.println(e.getMessage() + " while plotting ");
         System.exit(-1);
      }

      // put it in the panel
      visad.DisplayImpl display = twoDPlot.getDisplay();
      //logger.info("display.component = " + 
display.getComponent().getClass().toString());
      java.awt.Component component = display.getComponent();
      component.setVisible(true);

      gbc.gridx = 2;
      // first add the widgets created in TwoDPlot to the panel
      if (twoDPlot.widgetPanel != null) {
         panel.add(twoDPlot.widgetPanel, gbc);
      }


      gbc.fill = java.awt.GridBagConstraints.BOTH;
      gbc.gridwidth = 3;
      gbc.gridx = 0;
      gbc.weighty = 1.0;
      // add the TwoDPlot to the panel
      panel.add(component, gbc);

      panel.setVisible(true);
      // put the panel in the frame
      javax.swing.JFrame frame = new javax.swing.JFrame("Testing TwoDPlot");
      frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
      frame.setSize(200, 200);
      frame.add(panel);
      frame.setVisible(true);
   }

}