Re: Still having pblms with 3D bar plot

  • To: Arvind Lakshmikumar <kla@xxxxxxxxxx>
  • Subject: Re: Still having pblms with 3D bar plot
  • From: Jim Koutsovasilis <jimk@xxxxxxxxxx>
  • Date: Tue, 27 May 2003 13:07:41 +1000
Hello Arvind,

sorry for taking so long to get back to you, but this week has
been a bit busy...

Please find attached "BarGraph.java" which shows you one way
to achieve what you want.

This approach uses volume rendering, which you can test with
visad/examples/Test61.java in your visad release.

Please note that you'll have to rotate the display in order to see
the 3D bars.  Rotate the display so that the utility-axis is pointing up,
and you will see the volume-rendering.

Hope this helps,
Jim.
---
Jim Koutsovasilis
Bureau of Meteorology, Australia
jimk@xxxxxxxxxx



Arvind Lakshmikumar wrote:

Hi Jim,
  Thanks for the post..What you suggested would create the maps. But how
would I draw 3D bars for values on the Z-axis. Say I have flow1=20,
flow2=30, utility =0.6. I want to represent utility as a bar of height
0.6.

Thanks a lot,
Arvind

////////////////////////////////////////////////////////////////////////////
                        Arvind Lakshmikumar

Research Scientist                              Research Scholar
Intelligent Automation, Inc                     Robotics Institute, CMU
7519 Standish Place                             5000 Forbes Avenue
Rockville MD                                    Pittsburgh PA

email : kla@xxxxxxxxxx
Phone:  443-538-4555 (Cell)
        301-540-4396 (Home)



                                                Linux is like a Teepee
                                                No Windows, No Gates,
                                                Only APACHE Inside!
////////////////////////////////////////////////////////////////////////////




// BarGraph.java

// Java
import java.rmi.RemoteException;
import javax.swing.JFrame;

// VisAD
import visad.ConstantMap;
import visad.DataReferenceImpl;
import visad.DelaunayCustom;
import visad.Display;
import visad.Field;
import visad.FlatField;
import visad.FunctionType;
import visad.GraphicsModeControl;
import visad.Linear3DSet;
import visad.Real;
import visad.RealTupleType;
import visad.RealType;
import visad.ScalarMap;
import visad.Set;
import visad.UnionSet;
import visad.VisADException;
import visad.java3d.DisplayImplJ3D;


/**
 * Demo program to show a 3D bar graph.
 *
 * MathType: (flow1, flow2)-->(utility)
 *
 * The height of the bars is controlled by "utility".
 * The colour of the the bars is also controlled by "utility".
 *
 * You'll need to rotate the display so that you can see
 * the volume rendering.
 */
public final class BarGraph
{

        private final RealType flow1Type;
        private final RealType flow2Type;
        private final RealType utilityType;
        private final RealType dummyType;

        /**
         * Constructor.
         */
        public BarGraph() throws VisADException, RemoteException
        {

                final DisplayImplJ3D display = new DisplayImplJ3D("display");
                display.getDisplayRenderer().setBoxOn(false);

                final GraphicsModeControl gmc
                        display.getGraphicsModeControl();
                gmc.setScaleEnable(true);
                gmc.setProjectionPolicy(DisplayImplJ3D.PARALLEL_PROJECTION);

                flow1Type = RealType.getRealType("flow1");
                flow2Type = RealType.getRealType("flow2");
                utilityType = RealType.getRealType("utility");
                dummyType = RealType.getRealType("dummy"); 

                final ScalarMap xMap
                        new ScalarMap(flow1Type, Display.XAxis);
                final ScalarMap yMap
                        new ScalarMap(flow2Type, Display.YAxis);
                final ScalarMap zMap
                        new ScalarMap(utilityType, Display.ZAxis);
                final ScalarMap colourMap
                        new ScalarMap(dummyType, Display.RGB);

                display.addMap(xMap);
                display.addMap(yMap);
                display.addMap(zMap);
                display.addMap(colourMap);

                // flow1 values are in the range (5, 40)
                xMap.setRange(5, 40);
                // flow2 values are in the range (5, 40)
                yMap.setRange(5, 40);
                // intensity values are in the range (0, 1)
                zMap.setRange(0, 1);

                final int numSamples = 10;

                for (int i = 0; i < numSamples; ++i) {

                        // Pick a flow1 value in the range (5, 35)
                        final float flow1 = (float) ((Math.random() * 35) + 5);

                        // Pick a flow2 value in the range (5, 35)
                        final float flow2 = (float) ((Math.random() * 35) + 5);

                        // Pick a utility value in the range (0, 1)
                        final float utility = (float) Math.random();

                        final Field field = create3DBar(flow1, flow2, utility);
        
                        final DataReferenceImpl dataRef = 
                                new DataReferenceImpl("data_ref");
                        dataRef.setData(field);
                        display.addReference(dataRef);

                } // for (i<numSamples)

                final JFrame frame = new JFrame("Bar Graph");
                frame.getContentPane().add(display.getComponent());
                frame.setSize(640, 480);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);

        } // BarGraph.BarGraph()


        /**
         * Creates a 3D bar at the given position ("flow1", "flow2")
         * with a height of "utility".  Note that the depth and
         * width of the bar is 1.
         */
        private Field create3DBar(final float flow1, final float flow2,
                final float utility)
                        throws VisADException, RemoteException
        {

                final RealTupleType domainType = new RealTupleType(
                        new RealType[] {flow1Type, flow2Type, utilityType});

                final int numValues = 2;
                final float width = 1.0f;
                final float depth = width;

                final Set domainSet = new Linear3DSet(domainType,
                        flow1, flow1 + width, numValues,
                        flow2, flow2 + depth, numValues,
                        0, utility, numValues);

                final FunctionType functionType = new FunctionType(
                        domainType, dummyType);

                final Field field = new FlatField(functionType, domainSet);

                final int numPoints = numValues * numValues * numValues;
                final int numRangeComponents = 1;
                final float[][] values
                        new float[numRangeComponents][numPoints];

                for (int i = 0; i < numPoints; ++i) {
                        values[0][i] = utility;
                }

                field.setSamples(values);
                return field;

        } // BarGraph.create3DBar()


        /**
         * Runs the program.
         *
         * @param args no arguments are expected.
         */
        public static final void main(String[] args)
                throws VisADException, RemoteException
        {

                new BarGraph();

        } // BarGraph.main()

} // class BarGraph