mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/terrain/SelectionShape.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: SelectionShape.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.3 $
2 26 Feb 07 jari 8  * $Date: 2005/03/10 20:33:20 $
2 26 Feb 07 jari 9  * $Author: braistedj $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev.cluster.gui.impl.terrain;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import javax.media.j3d.Appearance;
2 26 Feb 07 jari 15 import javax.media.j3d.Geometry;
2 26 Feb 07 jari 16 import javax.media.j3d.GeometryArray;
2 26 Feb 07 jari 17 import javax.media.j3d.Material;
2 26 Feb 07 jari 18 import javax.media.j3d.PolygonAttributes;
2 26 Feb 07 jari 19 import javax.media.j3d.QuadArray;
2 26 Feb 07 jari 20 import javax.media.j3d.Shape3D;
2 26 Feb 07 jari 21 import javax.media.j3d.TransparencyAttributes;
2 26 Feb 07 jari 22 import javax.vecmath.Point2f;
2 26 Feb 07 jari 23 import javax.vecmath.Point3d;
2 26 Feb 07 jari 24 import javax.vecmath.Point3f;
2 26 Feb 07 jari 25
2 26 Feb 07 jari 26 public class SelectionShape extends Shape3D {
2 26 Feb 07 jari 27
2 26 Feb 07 jari 28     private Point3d from = new Point3d();
2 26 Feb 07 jari 29     private Point3d to = new Point3d();
2 26 Feb 07 jari 30
2 26 Feb 07 jari 31     public SelectionShape() {
2 26 Feb 07 jari 32         setCapability(ALLOW_GEOMETRY_READ);
2 26 Feb 07 jari 33         setGeometry(createGeometry());
2 26 Feb 07 jari 34         setAppearance(createAppearance());
2 26 Feb 07 jari 35     }
2 26 Feb 07 jari 36
2 26 Feb 07 jari 37     public void startSelection(Point3d from) {
2 26 Feb 07 jari 38         if (from == null)
2 26 Feb 07 jari 39             return;
2 26 Feb 07 jari 40         this.from = from;
2 26 Feb 07 jari 41     }
2 26 Feb 07 jari 42
2 26 Feb 07 jari 43     public void dragSelection(Point3d to) {
2 26 Feb 07 jari 44         if (to == null)
2 26 Feb 07 jari 45             return;
2 26 Feb 07 jari 46         this.to = to;
2 26 Feb 07 jari 47         updateCoords((GeometryArray)getGeometry(), this.from, this.to);
2 26 Feb 07 jari 48     }
2 26 Feb 07 jari 49
2 26 Feb 07 jari 50     public boolean hasSelection() {
2 26 Feb 07 jari 51         return !from.equals(to);
2 26 Feb 07 jari 52     }
2 26 Feb 07 jari 53
2 26 Feb 07 jari 54     public Point2f getStartCoords() {
2 26 Feb 07 jari 55         float x1 = (float)Math.min(this.from.x, this.to.x);
2 26 Feb 07 jari 56         float z1 = (float)Math.min(this.from.z, this.to.z);
2 26 Feb 07 jari 57         return new Point2f(x1, z1);
2 26 Feb 07 jari 58     }
2 26 Feb 07 jari 59
2 26 Feb 07 jari 60     public Point2f getEndCoords() {
2 26 Feb 07 jari 61         float x2 = (float)Math.max(this.from.x, this.to.x);
2 26 Feb 07 jari 62         float z2 = (float)Math.max(this.from.z, this.to.z);
2 26 Feb 07 jari 63         return new Point2f(x2, z2);
2 26 Feb 07 jari 64     }
2 26 Feb 07 jari 65
2 26 Feb 07 jari 66     public void clearSelection() {
2 26 Feb 07 jari 67         this.from.set(0, 0, 0);
2 26 Feb 07 jari 68         this.to.set(0, 0, 0);
2 26 Feb 07 jari 69         updateCoords((GeometryArray)getGeometry(), this.from, this.to);
2 26 Feb 07 jari 70     }
2 26 Feb 07 jari 71
2 26 Feb 07 jari 72     protected Appearance createAppearance() {
2 26 Feb 07 jari 73         PolygonAttributes pa = new PolygonAttributes();
2 26 Feb 07 jari 74         pa.setCullFace(PolygonAttributes.CULL_NONE);
2 26 Feb 07 jari 75
2 26 Feb 07 jari 76         TransparencyAttributes ta = new TransparencyAttributes();
2 26 Feb 07 jari 77         ta.setTransparency(0.5f);
2 26 Feb 07 jari 78         ta.setTransparencyMode(TransparencyAttributes.BLENDED);
2 26 Feb 07 jari 79
2 26 Feb 07 jari 80         Material material = new Material();
2 26 Feb 07 jari 81         material.setDiffuseColor(0.5f, 0.5f, 0.5f);
2 26 Feb 07 jari 82
2 26 Feb 07 jari 83         Appearance appearance = new Appearance();
2 26 Feb 07 jari 84         appearance.setTransparencyAttributes(ta);
2 26 Feb 07 jari 85         appearance.setPolygonAttributes(pa);
2 26 Feb 07 jari 86         appearance.setMaterial(material);
2 26 Feb 07 jari 87         return appearance;
2 26 Feb 07 jari 88     }
2 26 Feb 07 jari 89
2 26 Feb 07 jari 90     protected Geometry createGeometry() {
2 26 Feb 07 jari 91         QuadArray geometry = new QuadArray(24, GeometryArray.COORDINATES | GeometryArray.NORMALS);
2 26 Feb 07 jari 92         geometry.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
2 26 Feb 07 jari 93         geometry.setNormals(0, normals);
2 26 Feb 07 jari 94         geometry.setCapability(Geometry.ALLOW_INTERSECT);           
2 26 Feb 07 jari 95         geometry.setCapability(GeometryArray.ALLOW_COUNT_READ);     
2 26 Feb 07 jari 96         geometry.setCapability(GeometryArray.ALLOW_FORMAT_READ);    
2 26 Feb 07 jari 97         geometry.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
2 26 Feb 07 jari 98         return geometry;
2 26 Feb 07 jari 99     }
2 26 Feb 07 jari 100
2 26 Feb 07 jari 101     public void updateData(Geometry geometry) {
2 26 Feb 07 jari 102         updateCoords((GeometryArray)geometry, this.from, this.to);
2 26 Feb 07 jari 103     }
2 26 Feb 07 jari 104
2 26 Feb 07 jari 105     protected void updateCoords(GeometryArray geometry, Point3d from, Point3d to) {
2 26 Feb 07 jari 106         float x1 = (float)Math.min(from.x, to.x);
2 26 Feb 07 jari 107         float z1 = (float)Math.min(from.z, to.z);
2 26 Feb 07 jari 108         float x2 = (float)Math.max(from.x, to.x);
2 26 Feb 07 jari 109         float z2 = (float)Math.max(from.z, to.z);
2 26 Feb 07 jari 110         float y1 = 0f;
2 26 Feb 07 jari 111         float y2 = from.equals(to) ? 0f : 0.25f;
2 26 Feb 07 jari 112
2 26 Feb 07 jari 113         Point3f p3f = new Point3f();
2 26 Feb 07 jari 114         //front
2 26 Feb 07 jari 115         p3f.set(x1, y1, z2); geometry.setCoordinate(0 , p3f);
2 26 Feb 07 jari 116         p3f.set(x2, y1, z2); geometry.setCoordinate(1 , p3f);
2 26 Feb 07 jari 117         p3f.set(x2, y2, z2); geometry.setCoordinate(2 , p3f);
2 26 Feb 07 jari 118         p3f.set(x1, y2, z2); geometry.setCoordinate(3 , p3f);
2 26 Feb 07 jari 119         //back
2 26 Feb 07 jari 120         p3f.set(x1, y1, z1); geometry.setCoordinate(4 , p3f);
2 26 Feb 07 jari 121         p3f.set(x1, y2, z1); geometry.setCoordinate(5 , p3f);
2 26 Feb 07 jari 122         p3f.set(x2, y2, z1); geometry.setCoordinate(6 , p3f);
2 26 Feb 07 jari 123         p3f.set(x2, y1, z1); geometry.setCoordinate(7 , p3f);
2 26 Feb 07 jari 124         //left
2 26 Feb 07 jari 125         p3f.set(x1, y1, z1); geometry.setCoordinate(8 , p3f);
2 26 Feb 07 jari 126         p3f.set(x1, y1, z2); geometry.setCoordinate(9 , p3f);
2 26 Feb 07 jari 127         p3f.set(x1, y2, z2); geometry.setCoordinate(10, p3f);
2 26 Feb 07 jari 128         p3f.set(x1, y2, z1); geometry.setCoordinate(11, p3f);
2 26 Feb 07 jari 129         //right
2 26 Feb 07 jari 130         p3f.set(x2, y1, z2); geometry.setCoordinate(12, p3f);
2 26 Feb 07 jari 131         p3f.set(x2, y1, z1); geometry.setCoordinate(13, p3f);
2 26 Feb 07 jari 132         p3f.set(x2, y2, z1); geometry.setCoordinate(14, p3f);
2 26 Feb 07 jari 133         p3f.set(x2, y2, z2); geometry.setCoordinate(15, p3f);
2 26 Feb 07 jari 134         //upper
2 26 Feb 07 jari 135         p3f.set(x1, y2, z1); geometry.setCoordinate(16, p3f);
2 26 Feb 07 jari 136         p3f.set(x1, y2, z2); geometry.setCoordinate(17, p3f);
2 26 Feb 07 jari 137         p3f.set(x2, y2, z2); geometry.setCoordinate(18, p3f);
2 26 Feb 07 jari 138         p3f.set(x2, y2, z1); geometry.setCoordinate(19, p3f);
2 26 Feb 07 jari 139         //bottom                                     
2 26 Feb 07 jari 140         p3f.set(x1, y1, z1); geometry.setCoordinate(20, p3f);
2 26 Feb 07 jari 141         p3f.set(x2, y1, z1); geometry.setCoordinate(21, p3f);
2 26 Feb 07 jari 142         p3f.set(x2, y1, z2); geometry.setCoordinate(22, p3f);
2 26 Feb 07 jari 143         p3f.set(x1, y1, z2); geometry.setCoordinate(23, p3f);
2 26 Feb 07 jari 144     }
2 26 Feb 07 jari 145
2 26 Feb 07 jari 146     private static final float normals[] = {
2 26 Feb 07 jari 147            0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1, // front 
2 26 Feb 07 jari 148            0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1, // back  
2 26 Feb 07 jari 149           -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, // left  
2 26 Feb 07 jari 150            1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0, // right 
2 26 Feb 07 jari 151            0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0, // upper 
2 26 Feb 07 jari 152            0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0  // bottom
2 26 Feb 07 jari 153     };
2 26 Feb 07 jari 154 }