View Javadoc
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; 15 import com.sun.j3d.loaders.*; 16 import com.sun.j3d.loaders.vrml97.*; 17 import com.sun.j3d.utils.behaviors.keyboard.*; 18 import com.sun.j3d.utils.geometry.*; 19 import com.sun.j3d.utils.universe.*; 20 import java.io.*; 21 import javax.media.j3d.Background; 22 import javax.media.j3d.BoundingSphere; 23 import javax.media.j3d.BranchGroup; 24 import javax.media.j3d.Locale; 25 import javax.media.j3d.Node; 26 import javax.media.j3d.Transform3D; 27 import javax.media.j3d.TransformGroup; 28 import javax.media.j3d.VirtualUniverse; 29 30 import javax.vecmath.Matrix3d; 31 import javax.vecmath.Point3d; 32 import javax.vecmath.Vector3d; 33 import javax.vecmath.Vector3f; 34 35 36 /*** 37 * This class provides some basic Java 3D infrastructure. It encapsulates the 38 * VirtualUniverse and Locale objects and provides some connection points 39 * (BranchGroups) so objects like lights, cameras and geometries can be 40 * attached to the scene graph. 41 * 42 *@author Fábio Roberto de Miranda 43 *@created October 22, 2003 44 *@version 1.0 45 */ 46 class CoreScene { 47 48 private VirtualUniverse virtualUniverse; 49 private Locale locale; 50 51 private BranchGroup rootBG; 52 53 /*** 54 * Branch group to which content objects are attached 55 */ 56 private BranchGroup contentBG; 57 /*** 58 * Branch group to which light objects are attached 59 */ 60 private BranchGroup lightingBG; 61 /*** 62 * Branch group to which view objects are attached 63 */ 64 private BranchGroup viewBG; 65 /*** 66 * Branch group to which the Cursor3D object is attached 67 */ 68 private BranchGroup cursorBG; 69 /*** 70 * Branch group to which the HelpingGrid object is attached 71 */ 72 private BranchGroup gridBG; 73 /*** 74 * Branch group to which helper objects are attached 75 */ 76 private BranchGroup helperBG; 77 /*** 78 * Branch group to which the Background object is attached 79 */ 80 private BranchGroup bgTG; 81 82 /*** 83 * Shows a grid to help with orientation 84 */ 85 private HelpingGrid helpingGrid; 86 /*** 87 * Controls exhibition of the helping grid 88 */ 89 private boolean helpingGridVisible = true; 90 91 private TransformGroup cursorTG; 92 private Transform3D helpingGridT3D = new Transform3D(); 93 94 /* 95 * group of objects that will hold temporary values; to avoid frequent 96 * instantiation 97 */ 98 private Transform3D tempT3D = new Transform3D(); 99 private Transform3D tempRotT3D = new Transform3D(); 100 private Transform3D tempTransT3D = new Transform3D(); 101 private Transform3D tempScaleT3D = new Transform3D(); 102 private Transform3D tempRotT3D2 = new Transform3D(); 103 private Matrix3d tempRot = new Matrix3d(); 104 private Vector3d tempTrans = new Vector3d(); 105 private Vector3d tempVec3d = new Vector3d(); 106 private Point3d tempP3d = new Point3d(); 107 108 /*** 109 * Object which serves as background to the scene 110 */ 111 private Background background; 112 113 private BoundingSphere globalBoundingSphere; 114 115 116 /*** 117 * Class constructor. 118 */ 119 public CoreScene() { 120 121 virtualUniverse = new VirtualUniverse(); 122 locale = new Locale(virtualUniverse); 123 124 viewBG = new BranchGroup(); 125 lightingBG = new BranchGroup(); 126 contentBG = new BranchGroup(); 127 helperBG = new BranchGroup(); 128 rootBG = new BranchGroup(); 129 130 tempT3D.setIdentity(); 131 tempVec3d.x = 0.0; 132 tempVec3d.y = 0.0; 133 tempVec3d.z = 0.0; 134 135 rootBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); 136 rootBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 137 rootBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); 138 rootBG.setCapability(BranchGroup.ALLOW_LOCAL_TO_VWORLD_READ); 139 rootBG.setCapability(Node.ENABLE_PICK_REPORTING); 140 rootBG.setPickable(true); 141 142 lightingBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); 143 lightingBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 144 lightingBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); 145 146 contentBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); 147 contentBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 148 contentBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); 149 contentBG.setCapability(BranchGroup.ALLOW_LOCAL_TO_VWORLD_READ); 150 contentBG.setCapability(Node.ENABLE_PICK_REPORTING); 151 152 helperBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); 153 helperBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 154 helperBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); 155 helperBG.setCapability(BranchGroup.ALLOW_LOCAL_TO_VWORLD_READ); 156 helperBG.setCapability(Node.ENABLE_PICK_REPORTING); 157 158 viewBG.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); 159 viewBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); 160 viewBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 161 viewBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); 162 163 cursorBG = new BranchGroup(); 164 cursorTG = new TransformGroup(); 165 166 helpingGridT3D.setIdentity(); 167 tempVec3d.set(-50, 0, 50); 168 helpingGridT3D.setTranslation(tempVec3d); 169 helpingGrid = new HelpingGrid(helpingGridT3D); 170 gridBG = new BranchGroup(); 171 gridBG.setCapability(BranchGroup.ALLOW_DETACH); 172 gridBG.addChild(helpingGrid); 173 174 cursorTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ); 175 cursorTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); 176 cursorTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 177 cursorTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 178 cursorBG.addChild(cursorTG); 179 cursorBG.setCapability(BranchGroup.ALLOW_DETACH); 180 181 bgTG = new BranchGroup(); 182 background = new Background(0.6f, 0.6f, 0.6f); 183 background.setCapability(Background.ALLOW_COLOR_READ); 184 background.setCapability(Background.ALLOW_COLOR_WRITE); 185 background.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000)); 186 187 bgTG.addChild(background); 188 189 /* 190 * BranchGroups that will have pickable objects will 191 * go under rootBG, the others will go directly under the Locale locale 192 */ 193 rootBG.addChild(lightingBG); 194 rootBG.addChild(contentBG); 195 rootBG.addChild(viewBG); 196 rootBG.addChild(helperBG); 197 198 /* 199 * Setting pickable Groups 200 */ 201 lightingBG.setPickable(true); 202 contentBG.setPickable(true); 203 viewBG.setPickable(true); 204 rootBG.setPickable(true); 205 206 /* 207 * Setting unpickable Groups 208 */ 209 cursorBG.setPickable(false); 210 gridBG.setPickable(false); 211 bgTG.setPickable(false); 212 213 locale.addBranchGraph(rootBG); 214 locale.addBranchGraph(cursorBG); 215 locale.addBranchGraph(gridBG); 216 locale.addBranchGraph(bgTG); 217 218 // nFormat.setMaximumFractionDigits(2); 219 globalBoundingSphere = new BoundingSphere(new Point3d(), 3000.0); 220 tempT3D.set(new Vector3f(0.0f, 0.0f, 0.0f)); 221 cursorTG.setTransform(tempT3D); 222 tempT3D.setIdentity(); 223 } 224 225 226 /*** 227 * Changes color of the background. 228 * 229 *@param r red value 230 *@param g greeen value 231 *@param b blue value 232 */ 233 final void setBackgroundColor(float r, float g, float b) { 234 background.setColor(r, g, b); 235 } 236 237 238 /*** 239 * Returns the BranchGroup to which lights are attached. 240 * 241 *@return the light BG 242 */ 243 final BranchGroup getLightBG() { 244 return this.lightingBG; 245 } 246 247 248 /*** 249 * Returns the BranchGroup to which content objects are attached. 250 * 251 *@return the content BG 252 */ 253 final BranchGroup getContentBG() { 254 return this.contentBG; 255 } 256 257 258 /*** 259 * Returns the BranchGroup to which view objects are attached. 260 * 261 *@return the view BG 262 */ 263 final BranchGroup getViewBG() { 264 return this.viewBG; 265 } 266 267 268 /*** 269 * Returns the BranchGroup to which helper objects are attached. 270 * 271 *@return the helper BG 272 */ 273 final BranchGroup getHelperBG() { 274 return this.helperBG; 275 } 276 277 278 /*** 279 * Returns the BranchGroup that serves as root to all editable objects. 280 * 281 *@return the root BG 282 */ 283 final BranchGroup getRootBranchGroup() { 284 return this.rootBG; 285 } 286 287 288 289 /*** 290 * Prints a debug message. 291 * 292 *@param s Description of the Parameter 293 */ 294 final void debug(String s) { 295 System.out.println(s); 296 } 297 298 299 /*** 300 * String-printing for runtime, not only debugging. 301 * 302 *@param s a message to be displayed. 303 */ 304 final void msg(String s) { 305 System.out.println(s); 306 } 307 308 309 /*** 310 * Toggles exhibition of the helping grid. 311 */ 312 final void toggleHelpingGrid() { 313 if(helpingGridVisible) { 314 locale.removeBranchGraph(gridBG); 315 helpingGridVisible = false; 316 } else { 317 locale.addBranchGraph(gridBG); 318 helpingGridVisible = true; 319 } 320 } 321 322 323 /*** 324 * Terminates program execution. 325 */ 326 final void exit() { 327 System.exit(0); 328 } 329 330 331 /*** 332 * Sets the globalBoundsRadius attribute of the CoreScene object 333 * 334 *@param bounds The new globalBoundsRadius value 335 */ 336 void setGlobalBoundsRadius(double bounds) { 337 this.globalBoundingSphere.setRadius(bounds); 338 } 339 340 341 /*** 342 * Sets the globalBoundsCenter attribute of the CoreScene object 343 * 344 *@param x The new globalBoundsCenter value 345 *@param y The new globalBoundsCenter value 346 *@param z The new globalBoundsCenter value 347 */ 348 void setGlobalBoundsCenter(double x, double y, double z) { 349 tempP3d.x = x; 350 tempP3d.y = y; 351 tempP3d.z = z; 352 this.globalBoundingSphere.setCenter(tempP3d); 353 } 354 355 356 /*** 357 * Gets the globalBoundsRadius attribute of the CoreScene object 358 * 359 *@return The globalBoundsRadius value 360 */ 361 double getGlobalBoundsRadius() { 362 return this.globalBoundingSphere.getRadius(); 363 } 364 365 366 /* 367 * Copies into point, passed as an argument, the position of the center of the global bounds 368 */ 369 /*** 370 * Gets the globalBoundsCenter attribute of the CoreScene object 371 * 372 *@param point a point into which is copied the globalBoundsCenter 373 */ 374 void getGlobalBoundsCenter(Point3d point) { 375 this.globalBoundingSphere.getCenter(point); 376 } 377 378 }

This page was automatically generated by Maven