1   /***
2    *  Virtual Mockup for Machine Vision Copyright (C) 2001-2003 Fabio R. de
3    *  Miranda, João E. Kogler Jr., Carlos S. Santos. Virtual Mockup for Machine
4    *  Vision Project funded by SENAC-SP Permission is granted to redistribute
5    *  and/or modify this software under the terms of the GNU Lesser General Public
6    *  License as published by the Free Software Foundation; either version 2.1 of
7    *  the License, or (at your option) any later version. This software is
8    *  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9    *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10   *  PARTICULAR PURPOSE. See the GNU Lesser General Public License
11   *  (http://www.gnu.org/copyleft/lesser.html) for more details.
12   */
13  
14  package camera3d.manipulation;
15  import camera3d.Axis;
16  import camera3d.TransformMode;
17  import camera3d.TransformScope;
18  
19  import camera3d.test.NodeTester;
20  import com.sun.j3d.utils.geometry.ColorCube;
21  import java.awt.event.*;
22  import javax.media.j3d.*;
23  import javax.vecmath.*;
24  
25  /***
26   *@author     Fábio Roberto de Miranda, Carlos da Silva dos Santos
27   *@created    November 13, 2003
28   *@version    21/08/2003
29   */
30  public class TransAdapterTester {
31  
32      /***
33       *  Indicates whether the app should run in standalone mode (in its own
34       *  window).
35       */
36      private final boolean isStandalone;
37      /***
38       *  Creates basic Java 3D structure.
39       */
40      private NodeTester tester;
41      /***
42       *  Cube that will be manipulated.
43       */
44      private ColorCube cube;
45      /***
46       *  cube TransformGroup.
47       */
48      private TransformGroup cubeTG;
49  
50      /***
51       *  Object which calculates the future coordinates of the manipulated object
52       *  based on the position of the mouse cursor.
53       */
54      private TranslationManipulator manip;
55  
56      /***
57       *  Object which receives the coordinates calculated by the manipulator and
58       *  then sets a TransformGroup's transformation matrix to reflect the new
59       *  coordinates.
60       */
61      private TransTGAdapter adapter;
62  
63      /***
64       *  Visual representation of the cube local coordinate system.
65       */
66      private Axis cubeAxis;
67      /***
68       *  Visual representation of the virtual world coordinate system.
69       */
70      private Axis worldAxis;
71  
72      // used to displace cube from origin
73      private TransformGroup dummyTG;
74      private Transform3D dummyT3D;
75  
76  
77      /***
78       *  Creates a Translation Adapter tester that runs as a standalone
79       *  application.
80       */
81      public TransAdapterTester() {
82          this(true);
83      }
84  
85  
86      /***
87       *  Creates Translation Adapter tester.
88       *
89       *@param  isStandalone  if true, the tester will run inside its own frame.
90       */
91      public TransAdapterTester(boolean isStandalone) {
92          this.isStandalone = isStandalone;
93          tester = new NodeTester(isStandalone);
94  
95          cube = new ColorCube();
96          cubeAxis = new Axis();
97          cubeAxis.setVisible(true);
98  
99          dummyT3D = new Transform3D();
100         dummyT3D.rotZ(Math.PI / 6);
101         dummyT3D.setTranslation(new Vector3d(-1.0, -1.0, -1.0));
102 
103         cubeTG = new TransformGroup();
104         cubeTG.setTransform(dummyT3D);
105 
106         // necessary capabilities so we can read and set the object's
107         // transform at runtime
108         cubeTG.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
109         cubeTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
110         cubeTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
111 
112         cubeTG.addChild(cube);
113         cubeAxis.setSize(3.0);
114         // attachs axis to cube
115         cubeTG.addChild(cubeAxis.getRootGroup());
116 
117         // the adapter will act upon cubeTG
118         adapter = new TransTGAdapter(cubeTG);
119 
120         dummyTG = new TransformGroup();
121         dummyT3D.rotX(Math.PI / 4);
122         dummyT3D.setTranslation(new Vector3d(0, 1, -5.0));
123 
124         dummyTG.setTransform(dummyT3D);
125         dummyTG.addChild(cubeTG);
126 
127         worldAxis = new Axis();
128         worldAxis.setVisible(true);
129         // adds axis to universe at the origin of coordinates
130         tester.add(worldAxis.getRootGroup());
131         tester.add(dummyTG);
132 
133         // manipulator initialization
134         manip = new TranslationManipulator();
135         // manip will forward the coordinates to adapter
136         manip.setTranslatableObject(adapter);
137         // initially, manipulation will be performed in absolute mode
138         manip.setTransformationMode(TransformMode.ABSOLUTE);
139         // translation will happen in the X axis
140         manip.setTransformationScope(TransformScope.X);
141         manip.setCanvas3D(tester.getCanvas3D());
142 
143         //repositioning the camera
144         dummyT3D.setIdentity();
145         dummyT3D.setTranslation(new Vector3d(0.0, 1.0, 4.0));
146         tester.setViewTransform(dummyT3D);
147 
148         InternalListener listener = new InternalListener();
149         // add listeners to canvas so mouse events get forwarded to
150         // the manipulator
151         tester.getCanvas3D().addMouseListener(listener);
152         tester.getCanvas3D().addMouseMotionListener(listener);
153         // keyboard listener to change manipulation parameters at execution
154         tester.getCanvas3D().addKeyListener(new KeybListener());
155 
156         if(isStandalone) {
157             tester.getFrame().setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
158         }
159     }
160 
161 
162     /***
163      *  Runs tester application.
164      *
165      *@param  args  The command line arguments
166      */
167     public static void main(String[] args) {
168         TransAdapterTester taTester = new TransAdapterTester();
169     }
170 
171 
172     /***
173      *  Gets the canvas3D attribute of the TransAdapterTester object
174      *
175      *@return    The canvas3D value
176      */
177     public Canvas3D getCanvas3D() {
178         return this.tester.getCanvas3D();
179     }
180 
181 
182     /***
183      *  Returns flag indicating whether this object is running inside its own
184      *  window.
185      *
186      *@return    The standalone value
187      */
188     public boolean isStandalone() {
189         return this.isStandalone;
190     }
191 
192 
193     /***
194      *  Description of the Method
195      */
196     public void cleanup() {
197         tester.cleanup();
198     }
199 
200 
201     /***
202      *  Description of the Class
203      *
204      *@author     carlos.ssantos
205      *@created    November 13, 2003
206      */
207     class InternalListener extends MouseAdapter implements MouseMotionListener {
208         /***
209          *  Description of the Method
210          *
211          *@param  e  Description of the Parameter
212          */
213         public void mouseClicked(MouseEvent e) {
214             //System.out.println("click");
215         }
216 
217 
218         /***
219          *  Description of the Method
220          *
221          *@param  e  Description of the Parameter
222          */
223         public void mousePressed(MouseEvent e) {
224             // System.out.println("pressed");
225             // forwards the initial location of the
226             // mouse dragging to the manipulator
227             int mod = e.getModifiers();
228             if((mod & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
229                 manip.setBeginPoint(e.getX(), e.getY());
230             }
231         }
232 
233 
234         /***
235          *  Description of the Method
236          *
237          *@param  e  Description of the Parameter
238          */
239         public void mouseDragged(MouseEvent e) {
240             //System.out.println("drag");
241             int mod = e.getModifiers();
242             if((mod & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
243                 manip.setCurrentPoint(e.getX(), e.getY());
244             }
245         }
246 
247 
248         /***
249          *  Description of the Method
250          *
251          *@param  e  Description of the Parameter
252          */
253         public void mouseMoved(MouseEvent e) {
254             //System.out.println("move");
255             //int mod = e.getModifiers();
256             //if ((mod&MouseEvent.BUTTON1_MASK)==MouseEvent.BUTTON1_MASK){}
257         }
258     }
259 
260 
261     /***
262      *  Description of the Class
263      *
264      *@author     carlos.ssantos
265      *@created    November 13, 2003
266      */
267     class KeybListener extends KeyAdapter {
268         /***
269          *  Description of the Method
270          *
271          *@param  k  Description of the Parameter
272          */
273         public void keyPressed(KeyEvent k) {
274             if(k.getKeyCode() == KeyEvent.VK_X) {
275                 manip.setTransformationScope(TransformScope.X);
276                 System.out.println("__X AXIS__");
277             }
278             if(k.getKeyCode() == KeyEvent.VK_Y) {
279                 manip.setTransformationScope(TransformScope.Y);
280                 System.out.println("__Y AXIS__");
281             }
282             if(k.getKeyCode() == KeyEvent.VK_Z) {
283                 manip.setTransformationScope(TransformScope.Z);
284                 System.out.println("__Z AXIS__");
285             }
286             if(k.getKeyCode() == KeyEvent.VK_A) {
287                 manip.setTransformationMode(TransformMode.ABSOLUTE);
288                 System.out.println("__ABSOLUTE MODE__");
289             }
290             if(k.getKeyCode() == KeyEvent.VK_R) {
291                 manip.setTransformationMode(TransformMode.RELATIVE);
292                 System.out.println("__RELATIVE MODE__");
293             }
294         }
295 
296 
297         /***
298          *  Description of the Method
299          *
300          *@param  k  Description of the Parameter
301          */
302         public void keyReleased(KeyEvent k) { }
303     }
304 
305 }
This page was automatically generated by Maven