a class to make VisAD shapes

/*
 * $Id$
 *
 * Copyright (c) 23 Maohai Huang / OAT
 *
 * This is a prototype based on and/or partly adapted from VisAD
 * ( http://www.ssec.wisc.edu/~billh/visad.html
 * Copyright (C) 1996 - 2003 Bill Hibbard, Curtis Rueden, Tom
 * Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
 * Tommy Jasmin. )
 */


package herschel.spire.qla.plot;

import visad.*;
import java.rmi.RemoteException;

/**
 * Provides a shape class using VisAD package. So far this class
 * is written to faciliate adding shapes to VisAD displays.
 * <p>
 * So far defined shpes are: DOT (a pixel), STAR3 (three-point skeletal
 * star), STAR4 or CROSS, STAR5 (five-point), OPEN_TRIANGLE, SOLID_TRIANGLE,
 * OPEN_SQUARE, SOLID_SQUARE, OPEN_CIRCLE (actually a decagon).
 * <p>
 * GNU GPL applies. see copyright notice in source.
 * @version May 25, 2003 prototype. At this point only 2D shapes are defined.
 * @author   M. Huang       OAT/SPIRE mh@xxxxxxxxxxx
 */
public class ShapesVisAD {

    public static final int DOT = 0;
    public static final int STAR3 = 1;
    public static final int STAR4 = 2;
    public static final int CROSS = STAR4;
    public static final int STAR5 = 3;
    public static final int OPEN_TRIANGLE = 4;
    public static final int SOLID_TRIANGLE = 5;
    public static final int OPEN_SQUARE = 6;
    public static final int SOLID_SQUARE = 7;
    public static final int OPEN_CIRCLE = 8;
//  public static final int SOLID_CIRCLE = 9;

    public final VisADGeometryArray[] shapes;

    float[][] indices;


    /**
     * Creates the shape object. Initialize shape array etc.
     */
    public ShapesVisAD() {

        indices = new float[][] {{
            (float) DOT,
            (float) STAR3,
            (float) CROSS,
            (float) STAR5,
            (float) OPEN_TRIANGLE,
            (float) SOLID_TRIANGLE,
            (float) OPEN_SQUARE,
            (float) SOLID_SQUARE,
            (float) OPEN_CIRCLE,
//          (float) SOLID_CIRCLE
        }};

        shapes = new VisADGeometryArray[] {
            getShape(DOT),
            getShape(STAR3),
            getShape(CROSS),
            getShape(STAR5),
            getShape(OPEN_TRIANGLE),
            getShape(SOLID_TRIANGLE),
            getShape(OPEN_SQUARE),
            getShape(SOLID_SQUARE),
            getShape(OPEN_CIRCLE),
//          getShape(SOLID_CIRCLE)
        };
    }

    /**
     * Returns the VisAD equivalence of shape.
     *
     * @param idx the shape index such as {@link #DOT}.
     * @return VisADGeometryArray the shape.
     */
    public static VisADGeometryArray getShape(int idx) {
        switch(idx) {
        case STAR3: {
            VisADLineArray shape = new VisADLineArray();
            shape.coordinates = new float[]
            { 0f,      1f,   0.0f,    0f,  0f, 0.0f,
             -0.866f, -0.5f, 0.0f,    0f,  0f, 0.0f,
              0.666f, -0.5f, 0.0f,    0f,  0f, 0.0f,
            };
            shape.vertexCount = 6;
            return shape;
        }
        
        case CROSS: {
            VisADLineArray shape = new VisADLineArray();
            shape.coordinates = new float[]
            {0.707f,  0.707f, 0.0f,    -0.707f, -0.707f, 0.0f,
             0.707f, -0.707f, 0.0f,    -0.707f,  0.707f, 0.0f};
            shape.vertexCount = 4;
            return shape;
        }
            
        case STAR5: {
            VisADLineArray shape = new VisADLineArray();
            shape.coordinates = new float[]
            {0f,      1f,     0.0f,    0f,  0f, 0.0f,
             0.951f,  0.309f, 0.0f,    0f,  0f, 0.0f,
            -0.951f,  0.309f, 0.0f,    0f,  0f, 0.0f,
             0.588f, -0.809f, 0.0f,    0f,  0f, 0.0f,
            -0.588f, -0.809f, 0.0f,    0f,  0f, 0.0f,
            };
            shape.vertexCount = 10;
            return shape;
        }
            
        case OPEN_TRIANGLE: {
            VisADLineArray shape = new VisADLineArray();
            shape.coordinates = new float[]
            {   0f,    1f,   0.0f,  -0.866f, -0.5f, 0.0f,
             -0.866f, -0.5f, 0.0f,   0.866f, -0.5f, 0.0f,
              0.866f, -0.5f, 0.0f,   0f,      1f,   0.0f,
            };
            shape.vertexCount = 6;
            return shape;
        }
            
        case SOLID_TRIANGLE: {
            VisADTriangleArray shape = new VisADTriangleArray();
            shape.coordinates = new float[]
            {  0f,      1f,   0.0f,
              -0.866f, -0.5f, 0.0f,
               0.866f, -0.5f, 0.0f,
            };
            shape.vertexCount = 3;
            return shape;
        }
        
        case OPEN_SQUARE: {
            VisADLineArray shape = new VisADLineArray();
            shape.coordinates = new float[]
            {-0.707f,  0.707f, 0.0f,   0.707f,  0.707f, 0.0f,
              0.707f,  0.707f, 0.0f,   0.707f, -0.707f, 0.0f,
              0.707f, -0.707f, 0.0f,  -0.707f, -0.707f, 0.0f,
             -0.707f, -0.707f, 0.0f,  -0.707f,  0.707f, 0.0f,
            };
            shape.vertexCount = 8;
            return shape;
        }
        
        case SOLID_SQUARE: {
            VisADQuadArray shape = new VisADQuadArray();
            shape.coordinates = new float[]
            {-0.707f,  0.707f, 0.0f,
              0.707f,  0.707f, 0.0f,
              0.707f, -0.707f, 0.0f,
             -0.707f, -0.707f, 0.0f,
            };
            shape.vertexCount = 4;
            return shape;
        }
        
        case OPEN_CIRCLE: {
            VisADLineArray shape = new VisADLineArray();
             /* use a decagon for circle. jython script for this:
                from java.lang import Math
                i=0
                while i<10:
                  an=i*36/57.29578
                  an1=(i-1)*36/57.29578
                  print "%6.3ff, %6.3ff, 0.0f,  %6.3ff, %6.3ff, 0.0f," %\
                   (Math.cos(an),Math.sin(an),Math.cos(an1),Math.sin(an1))
                  i=i+1
             */
            shape.coordinates = new float[]
             { 1.000f,  0.000f, 0.0f,   0.809f, -0.588f, 0.0f,
               0.809f,  0.588f, 0.0f,   1.000f,  0.000f, 0.0f,
               0.309f,  0.951f, 0.0f,   0.809f,  0.588f, 0.0f,
              -0.309f,  0.951f, 0.0f,   0.309f,  0.951f, 0.0f,
              -0.809f,  0.588f, 0.0f,  -0.309f,  0.951f, 0.0f,
              -1.000f,  0.000f, 0.0f,  -0.809f,  0.588f, 0.0f,
              -0.809f, -0.588f, 0.0f,  -1.000f,  0.000f, 0.0f,
              -0.309f, -0.951f, 0.0f,  -0.809f, -0.588f, 0.0f,
               0.309f, -0.951f, 0.0f,  -0.309f, -0.951f, 0.0f,
               0.809f, -0.588f, 0.0f,   0.309f, -0.951f, 0.0f,
             };
            shape.vertexCount = 20;
            return shape;
        }
        // the VisAD10Array class won't work with VisAD renderers currently
//      case SOLID_CIRCLE: { 
//          return null;
//          VisAD10Array shape = new VisAD10Array();
//          shape.coordinates = new float[]
//          { 1.000f,  0.000f, 0.0f,
//            0.809f,  0.588f, 0.0f,
//            0.309f,  0.951f, 0.0f,
//           -0.309f,  0.951f, 0.0f,
//           -0.809f,  0.588f, 0.0f,
//           -1.000f,  0.000f, 0.0f,
//           -0.809f, -0.588f, 0.0f,
//           -0.309f, -0.951f, 0.0f,
//            0.309f, -0.951f, 0.0f,
//            0.809f, -0.588f, 0.0f,
//          };
//          shape.vertexCount = 20;
//          return shape;
//      }
        
        case DOT:
        default: {
            VisADPointArray shape = new VisADPointArray();
            shape.coordinates = new float[]
            { 0f,  0f, 0.0f,
            };
            shape.vertexCount = 1;
            return shape;
        }
        }
    }
        
    /** Returns an index array in float[][] format.
     * i.e. float[][] = {{(float) {@link #DOT}, ...}}.
     *
     * @return float[][] the index array
     */
    public float [][] getIndices() {
        return indices;
    }

    /** for testing
     */
    public static void main(String av[]) {
        ShapesVisAD shapes = new ShapesVisAD();
    }
}
/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2002 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

import java.awt.Component;

import java.rmi.RemoteException;

import java.util.Vector;

import visad.*;

import visad.java2d.DisplayImplJ2D;

import herschel.spire.qla.plot.*;

/**
 * Tests ShapesVisAD class. GNU GPL applies. see copyright notice in source.
 * Maohai Huang 2003
 */

public class TestP
  extends UISkeleton
{
  public TestP() { }

  public TestP(String[] args)
    throws RemoteException, VisADException
  {
    super(args);
  }

  DisplayImpl[] setupServerDisplays()
    throws RemoteException, VisADException
  {
    DisplayImpl[] dpys = new DisplayImpl[1];
    dpys[0] = new DisplayImplJ2D("display");
    return dpys;
  }

  void setupServerData(LocalDisplay[] dpys)
    throws RemoteException, VisADException
  {
    RealType ir_radiance = RealType.getRealType("ir_radiance");
    RealType count = RealType.getRealType("count");
    FunctionType ir_histogram = new FunctionType(ir_radiance, count);

//      float[][] values = {{0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
//                       6.0f, 7.0f, 8.0f, 9.0f   }};
    float[][] values = (new ShapesVisAD()).getIndices();
    int size = values[0].length;
    Integer1DSet ir_set = new Integer1DSet(size);
    FlatField histogram1 = new FlatField(ir_histogram, ir_set);
    histogram1.setSamples(values);

    dpys[0].addMap(new ScalarMap(ir_radiance, Display.XAxis));
    dpys[0].addMap(new ScalarMap(ir_radiance, Display.YAxis));
    dpys[0].addMap(new ScalarMap(count, Display.Green));
    dpys[0].addMap(new ConstantMap(1.0, Display.Blue));
    dpys[0].addMap(new ConstantMap(1.0, Display.Red));
    ScalarMap shape_map = new ScalarMap(count, Display.Shape);
    dpys[0].addMap(shape_map);

//      ScalarMap shape_map2 = new ScalarMap(count, Display.Shape);
//      dpys[0].addMap(shape_map2);

    DataReferenceImpl ref_histogram1;
    ref_histogram1 = new DataReferenceImpl("ref_histogram1");
    ref_histogram1.setData(histogram1);
    dpys[0].addReference(ref_histogram1, null);
  }

  Component getSpecialComponent(LocalDisplay[] dpys)
    throws RemoteException, VisADException
  {
    Vector v = dpys[0].getMapVector();
    ScalarMap shape_map = (ScalarMap )v.elementAt(3);
//      ScalarMap shape_map2 = (ScalarMap )v.elementAt(4);

    RealType count = (RealType )shape_map.getScalar();

    ShapesVisAD sv = new ShapesVisAD();
    float[][] counts = sv.getIndices();

    Gridded1DSet count_set
      new Gridded1DSet(count, counts, counts[0].length);

    VisADGeometryArray[] shapes = sv.shapes;

    ShapeControl shape_control = (ShapeControl) shape_map.getControl();
    shape_control.setShapeSet(count_set);
    shape_control.setShapes(shapes);
    shape_control.setScale(0.1f);

//      VisADGeometryArray[] shapes2 = shapes;
//      ShapeControl shape_control2 = (ShapeControl) shape_map2.getControl();
//      shape_control2.setShapeSet(count_set);
//      shape_control2.setShapes(shapes2);

    return null;
  }

  String getFrameTitle() { return "shape in Java2D"; }

  public String toString() { return ": shape in Java2D"; }

  public static void main(String[] args)
    throws RemoteException, VisADException
  {
    new TestP(args);
  }
}
/*
 * $Id$
 *
 * Copyright (c) 2003 Maohai Huang / OAT
 *
 * This is based on and/or partly adapted from VisAD
 * ( http://www.ssec.wisc.edu/~billh/visad.html
 * Copyright (C) 1996 - 2003 Bill Hibbard, Curtis Rueden, Tom
 * Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
 * Tommy Jasmin. )
 */


package herschel.spire.qla.plot;

import visad.VisADGeometryArray;

/**
 * Provides a 10-sided polygone shape.
 * GNU GPL applies. see copyright notice in source.
 * @version May 26, 2003 prototype based on VisADQuadArray.java
 * @author   M. Huang       OAT/SPIRE mh@xxxxxxxxxxx
 */
public class VisAD10Array extends VisADGeometryArray {
    public Object clone() {
        VisAD10Array array = new VisAD10Array();
        copy(array);
        return array;
    }
}