View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, Joćo E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.test; 21 22 import javax.media.j3d.Shape3D; 23 import javax.media.j3d.*; 24 import com.sun.j3d.*; 25 import com.sun.j3d.utils.universe.*; 26 import com.sun.j3d.utils.behaviors.keyboard.*; 27 import javax.swing.*; 28 import javax.vecmath.*; 29 import java.awt.*; 30 import java.awt.event.KeyAdapter; 31 import java.awt.event.KeyEvent; 32 import camera3d.HelpingGrid; 33 34 35 /*** 36 * An utility class to help with debugging of scene graphs. It encapsulates 37 * the basic Java3D and GUI structures, providing an access point for plugging 38 * in new nodes. 39 * 40 * @author Fabio Roberto de Miranda, Carlos da Silva dos Santos 41 */ 42 public class NodeTester { 43 /* 44 TODO: 45 - methods for customization. 46 - analyze current access/control of the scene graph. Either provide new methods 47 for access or document current ones in order to make clear to user how to do it. 48 For instance, in the current implementation of Sun's pick behavior, the scene 49 graph must be passed to the behavior before it is set live, we must find out 50 if it is feasible to use NodeTester with pick behavior, as NodeTester sets 51 the scene graph live at object creation. 52 */ 53 private TransformGroup tGroup; 54 private Canvas3D canvas3d; 55 private BranchGroup bg; 56 private JFrame frame; 57 private Background back; 58 private SimpleUniverse su; 59 private KeyNavigatorBehavior keynav; 60 private boolean isStandalone = true; 61 62 /*** 63 * Creates a new NodeTester that runs in its own window. 64 */ 65 public NodeTester() { 66 this(true); 67 } 68 69 /*** 70 * Creates a new NodeTester that runs in its own window. 71 * @param isStandalone if true, the NodeTester will be created inside its own 72 * frame, otherwise no frame will be created now and the 73 * NodeTester's Canvas3D can later be added to another window. 74 */ 75 public NodeTester(boolean isStandalone){ 76 this.isStandalone = isStandalone; 77 assembleGraph(); 78 canvas3d.addKeyListener(new KeyAdapter(){ 79 public void keyPressed(KeyEvent k){ 80 //put code to dump camera coordinates here 81 } 82 }); 83 if(isStandalone){ 84 frame = new JFrame(); 85 frame.getContentPane().add(canvas3d); 86 frame.setSize(300,300); 87 frame.setVisible(true); 88 frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 89 } 90 } 91 92 /*** 93 * Returns the TransformGroup under which are attached the new Nodes. 94 */ 95 public TransformGroup getNodeTG(){ 96 return tGroup; 97 } 98 99 /*** 100 * Returns the root BranchGroup of this object's scenegraph. 101 */ 102 public BranchGroup getRootBG(){ 103 return this.bg; 104 } 105 106 /*** 107 * Returns the Canvas3D into which the scene will be rendered. 108 */ 109 public Canvas3D getCanvas3D(){ 110 return this.canvas3d; 111 } 112 113 /*** 114 * Sets the transformation of the View objects which is associated with 115 * this NodeTester. 116 * @param viewT3D the new value of the view transform. 117 */ 118 public void setViewTransform(Transform3D viewT3D){ 119 keynav.setEnable(false); 120 TransformGroup viewTG = su.getViewingPlatform().getViewPlatformTransform(); 121 viewTG.setTransform(viewT3D); 122 keynav.setEnable(true); 123 } 124 125 public void cleanup(){ 126 su.cleanup(); 127 } 128 129 /*** 130 * Returns the frame that contains this object, if it has been created as 131 * standalone. 132 * @throws IllegalStateException if the object was not created as standalone. 133 */ 134 public JFrame getFrame(){ 135 if(this.isStandalone) 136 return this.frame; 137 else throw new java.lang.IllegalStateException("NodeTester is not standalone"); 138 } 139 140 /*** 141 * Adds a node to the scene graph 142 */ 143 public void add(Node n){ 144 BranchGroup bG = new BranchGroup(); 145 bG.addChild(n); 146 tGroup.addChild(bG); 147 } 148 149 /*** 150 * Sets the background color for the virtual scene. 151 */ 152 public void setBackgroundColor(Color3f color){ 153 back.setColor(color); 154 } 155 156 /*** 157 * Creates the java 3D basic structure. 158 */ 159 void assembleGraph() { 160 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D(); 161 canvas3d = new Canvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template )); 162 su = new SimpleUniverse(canvas3d); 163 164 su.getViewingPlatform().setNominalViewingTransform(); 165 166 tGroup = new TransformGroup(); 167 tGroup.setCapability(Group.ALLOW_CHILDREN_WRITE); 168 tGroup.setCapability(Group.ALLOW_CHILDREN_READ); 169 tGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND); 170 tGroup.setCapability(Node.ENABLE_PICK_REPORTING); 171 tGroup.setCapability(Node.ALLOW_BOUNDS_READ); 172 tGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 173 tGroup.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); 174 tGroup.setPickable(true); 175 int xGrids = 100; 176 int zGrids = 100; 177 //System.out.println("grids: ("+xGrids+","+zGrids+")"); 178 Transform3D tempT3D = new Transform3D(); 179 tempT3D.set(new Vector3d(-10.0 ,0.0 , 10.0)); 180 HelpingGrid grid = new HelpingGrid(tempT3D, xGrids, zGrids); 181 tGroup.addChild(grid); 182 bg = new BranchGroup(); 183 bg.setCapability(Group.ALLOW_CHILDREN_READ); 184 bg.setCapability(Group.ALLOW_CHILDREN_WRITE); 185 bg.setCapability(Group.ALLOW_CHILDREN_EXTEND); 186 bg.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); 187 bg.setCapability(Group.ENABLE_PICK_REPORTING); 188 bg.setCapability(Node.ALLOW_BOUNDS_READ); 189 bg.setCapability(BranchGroup.ALLOW_DETACH); 190 bg.setPickable(true); 191 TransformGroup vpTrans = su.getViewingPlatform().getViewPlatformTransform(); 192 keynav = new KeyNavigatorBehavior(vpTrans); 193 su.getViewingPlatform().setNominalViewingTransform(); 194 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000); 195 back = new Background(new Color3f(0.5f, 0.5f, 0.5f)); 196 back.setCapability(Background.ALLOW_COLOR_READ); 197 back.setCapability(Background.ALLOW_COLOR_WRITE); 198 back.setApplicationBounds(bounds); 199 bg.addChild(back); 200 keynav.setSchedulingBounds(bounds); 201 tGroup.addChild(keynav); 202 Color3f white = new Color3f(1.0f,1.0f,1.0f); 203 Point3f center = new Point3f(0.0f, 0.0f, 0.0f); 204 Point3f attenuation = new Point3f(0.1f,0.1f,0.1f); 205 PointLight light = new PointLight(true, white, center, attenuation); 206 tGroup.addChild(light); 207 bg.addChild(tGroup); 208 su.getLocale().addBranchGraph(bg); 209 } 210 211 }

This page was automatically generated by Maven