View Javadoc

1   /*
2    * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    */
8   package edu.uci.ics.jung.visualization3d.control;
9   
10  import java.beans.PropertyChangeListener;
11  import java.beans.PropertyChangeSupport;
12  
13  import javax.media.j3d.Bounds;
14  import javax.media.j3d.BranchGroup;
15  import javax.media.j3d.Canvas3D;
16  import javax.media.j3d.Node;
17  import javax.media.j3d.Transform3D;
18  import javax.media.j3d.TransformGroup;
19  import javax.vecmath.Point3d;
20  import javax.vecmath.Vector3d;
21  
22  import com.sun.j3d.utils.geometry.Primitive;
23  import com.sun.j3d.utils.picking.PickResult;
24  import com.sun.j3d.utils.picking.PickTool;
25  
26  /**
27   * A mouse behavior that allows user to pick and translate scene 
28   * graph objects.
29   * Common usage: 
30   *   1. Create your scene graph. 
31   *   2. Create this behavior with the root and canvas. 
32   * See PickRotateBehavior for more details.
33   */
34  
35  public class PickSphereBehavior extends PickTranslateBehavior {
36      
37      Node oldShape;
38      PropertyChangeSupport support;
39      TransformGroup currGrp;
40      double x_factor = .005;
41      double y_factor = .005;
42      double dx, dy;
43      Vector3d translation = new Vector3d();
44      Canvas3D canvas;
45      
46      public PickSphereBehavior(BranchGroup root, Canvas3D canvas, 
47              Bounds bounds){
48          super(root, canvas, bounds);
49          setCanvas(canvas);
50          this.setSchedulingBounds(bounds);
51          root.addChild(this);
52          pickCanvas.setMode(PickTool.GEOMETRY);
53          pickCanvas.setTolerance(0.f);
54      }
55      
56      public void setTransformGroup(TransformGroup t) {
57          currGrp = t;
58      }
59      
60      public void setCanvas(Canvas3D canvas) {
61          this.canvas = canvas;
62      }
63      
64      public void updateScene(int xpos, int ypos) {
65      	System.err.println("update scene ");
66          PickResult pickResult = null;
67          Primitive shape = null;
68  
69          pickCanvas.setShapeLocation(xpos, ypos);
70          pickResult = pickCanvas.pickClosest();
71          
72          if (pickResult != null) {
73              
74              shape = (Primitive) pickResult.getNode(PickResult.PRIMITIVE);
75              if(shape != null) {
76                  
77                  firePropertyChange("PeakSelected",
78                          oldShape,
79                          shape);
80                  oldShape = shape;
81                  setTransformGroup((TransformGroup)shape.getParent());
82                  changeTranslation(xpos,ypos);
83              }
84          }
85      }   
86      
87      private void changeTranslation(int x, int y) {
88          Transform3D currXform = new Transform3D();
89          Transform3D transformX = new Transform3D();
90          Transform3D transformY = new Transform3D();
91          
92          // fill currXform with the current values
93          currGrp.getTransform(currXform);
94          
95          Point3d position = new Point3d();
96          canvas.getPixelLocationInImagePlate(x, y, position);
97          
98          Transform3D imagePlateToVworldTransform = new Transform3D();
99          canvas.getImagePlateToVworld(imagePlateToVworldTransform);
100         
101         translation.x = position.x;
102         translation.y = position.y;
103         
104         transformX.set(translation);
105         
106         transformY.mul(imagePlateToVworldTransform, transformX);
107         
108         // now i just want the translational part of transformY
109         Vector3d trans = new Vector3d();
110         transformY.get(trans);
111         
112         trans.x = -trans.x;
113         trans.y = -trans.y;
114         
115         transformX.set(trans);
116         
117         currXform.mul(transformX, currXform);
118         
119         currGrp.setTransform(currXform);
120     }
121     
122     public void addPropertyChangeListener(PropertyChangeListener l) {
123         if(support == null) {
124             support = new PropertyChangeSupport(this);
125         }
126         support.addPropertyChangeListener(l);
127     }
128     
129     public void removePropertyChangeListener(PropertyChangeListener l) {
130         if(support != null) {
131             support.removePropertyChangeListener(l);
132         }
133     }
134     
135     public void firePropertyChange(String name, Object oldValue, Object newValue) {
136         if(support != null) {
137             support.firePropertyChange(name, oldValue, newValue);
138         }
139     }
140 }