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; 21 22 23 import java.util.*; 24 import javax.media.j3d.*; 25 import javax.vecmath.*; 26 import camera3d.gizmo.*; 27 import camera3d.test.NodeTester; 28 import com.sun.j3d.utils.geometry.*; 29 import javax.swing.Icon; 30 import javax.swing.ImageIcon; 31 32 /*** 33 * This class allows the easy creation of many kinds of geometry. Its main intended use 34 * is helping with the debugging of programs and fast prototyping of applications. The 35 * types of geometry currently supported include cube, line, plane and sphere. 36 * @author Fábio de Miranda, Carlos da Silva dos Santos 37 * @version 1.0 38 */ 39 public class GeometryBag extends VcObject { 40 private static int instanceCounter; 41 private double planeWidth = 2.0; 42 private double pointWidth = 0.2; 43 private double lineLength = 8.0; 44 45 private Stack colorStack = new Stack(); 46 private Color3b currentColor = new Color3b(); 47 48 private ArrayList points = new ArrayList(); 49 private ArrayList lines = new ArrayList(); 50 private ArrayList planes = new ArrayList(); 51 private ArrayList cubes = new ArrayList(); 52 /* 53 private Vector points = new Vector(); 54 private Vector lines = new Vector(); 55 private Vector planes = new Vector(); 56 private Vector cubes = new Vector(); 57 */ 58 59 private Color3b[] colorCycle = new Color3b[6]; 60 private int colorCounter; 61 62 private Icon bagIcon = new ImageIcon(this.getClass().getResource("icons/bagIcon.GIF")); 63 64 private boolean rotatingColor = true; 65 private boolean useDefaultColor = true; 66 67 private LineArrayGizmo lineArray; 68 69 public GeometryBag() { 70 this.setLabel("GeometryBag"+instanceCounter++); 71 this.tooltipText = "Geometry Bag Object"; 72 debugln("New GeometryBag created"); 73 /* 74 colorCycle[0] = new Color3b((byte) 127,(byte) 0,(byte)0); 75 colorCycle[1] = new Color3b((byte) 0,(byte) 127,(byte)116); 76 colorCycle[2] = new Color3b((byte) 112,(byte) 0,(byte)127); 77 colorCycle[3] = new Color3b((byte) 0,(byte) 0,(byte)127); 78 colorCycle[4] = new Color3b((byte) 127,(byte)95,(byte)0); 79 colorCycle[5] = new Color3b((byte) 0,(byte) 127,(byte)0); 80 */ 81 colorCycle[0] = new Color3b((byte) 255,(byte) 0,(byte)0); 82 colorCycle[1] = new Color3b((byte) 0,(byte) 255,(byte)246); 83 colorCycle[2] = new Color3b((byte) 240,(byte) 0,(byte)255); 84 colorCycle[3] = new Color3b((byte) 30,(byte) 0,(byte)255); 85 colorCycle[4] = new Color3b((byte) 255,(byte)198,(byte)0); 86 colorCycle[5] = new Color3b((byte) 0,(byte) 255,(byte)90); 87 88 89 } 90 91 /*** 92 * Returns the number of objects instatiated so far. 93 */ 94 public static int getInstanceCounter(){ 95 return instanceCounter; 96 } 97 98 99 public void addLine(Point3d p1, Point3d p2){ 100 LineGizmo line = new LineGizmo(getCurrentColor(), p1, p2); 101 //childTG.addChild(line); 102 parentRotScaleTG.addChild(line); 103 lines.add(line); 104 } 105 106 public void startLineArray(){ 107 lineArray = new LineArrayGizmo(); 108 } 109 110 public void addLineSegment(Point3d p1, Point3d p2){ 111 lineArray.addSegment(p1, p2); 112 } 113 114 public void finishLineArray(){ 115 lineArray.buildGeometry(); 116 this.bg.addChild(lineArray); 117 } 118 119 public void addLine(Point3d p1, Vector3d direction){ 120 LineGizmo line = new LineGizmo(p1, direction, lineLength, getCurrentColor()); 121 //childTG.addChild(line); 122 parentRotScaleTG.addChild(line); 123 lines.add(line); 124 } 125 126 public void addPoint(Point3d point){ 127 double wid = this.pointWidth/2.0; 128 double width = pointWidth; 129 PointGizmo pt = new PointGizmo(getCurrentColor(), point); 130 //childTG.addChild(pt); 131 parentRotScaleTG.addChild(pt); 132 points.add(pt); 133 } 134 135 public void addPlane(Point3d p1, Point3d p2, Point3d p3){ 136 /*** 137 * 138 */ 139 PlaneGizmo plane = new PlaneGizmo(getCurrentColor(), p1, p2, p3); 140 planes.add(plane); 141 //childTG.addChild(plane); 142 parentRotScaleTG.addChild(plane); 143 } 144 145 public void addPlane(Point3d point, Vector3d normal){ 146 PlaneGizmo plane = new PlaneGizmo(getCurrentColor(), point, normal, this.planeWidth); 147 planes.add(plane); 148 //childTG.addChild(plane); 149 parentRotScaleTG.addChild(plane); 150 } 151 152 public void addPlane(Vector4d plane4d){ 153 PlaneGizmo plane = new PlaneGizmo(plane4d, getCurrentColor()); 154 planes.add(plane); 155 //childTG.addChild(plane); 156 parentRotScaleTG.addChild(plane); 157 } 158 159 public void addPlane(Vector4d plane4d, Color3b color){ 160 PlaneGizmo plane = new PlaneGizmo(plane4d, color); 161 planes.add(plane); 162 //childTG.addChild(plane); 163 parentRotScaleTG.addChild(plane); 164 } 165 166 167 public void addSphere(Point3d center, double radius){ 168 System.out.println("addSphere still has to be implemented."); 169 } 170 171 public void addCube(Point3d lowCorner, Point3d upCorner){ 172 System.out.println("addCube still has to be implemented."); 173 } 174 175 public void setColor(byte r, byte g, byte b){ 176 if (currentColor!=null){ 177 colorStack.push(currentColor); 178 } 179 currentColor = new Color3b(r,g,b); 180 } 181 182 /*** 183 * Pushes a color onto a stack. This way, the color that was previously beeing used 184 * can be later restored with popColor() 185 */ 186 public void pushColor(byte r, byte g, byte b){ 187 colorStack.push(currentColor); 188 currentColor = new Color3b(r,g,b); 189 } 190 191 /*** 192 * Pops a color from aforementioned stack 193 */ 194 public void popColor(){ 195 currentColor = (Color3b)colorStack.pop(); 196 } 197 198 Color3b getCurrentColor(){ 199 if (rotatingColor){ 200 // Rotates color, duh... 201 currentColor = colorCycle[(colorCounter++)%colorCycle.length]; 202 } 203 return currentColor; 204 } 205 206 207 /*** 208 * Erases all geometry contained in this object 209 */ 210 public void clearAll(){ 211 clearLines(); 212 clearPlanes(); 213 clearPoints(); 214 clearCubes(); 215 /* 216 217 iter = points.iterator(); 218 while (iter.hasNext()){ 219 SimpleGizmo giz = (SimpleGizmo)iter.next(); 220 giz.detach(); 221 } 222 points.removeAll(); 223 224 iter = planes.iterator(); 225 while (iter.hasNext()){ 226 SimpleGizmo giz = (SimpleGizmo)iter.next(); 227 giz.detach(); 228 } 229 planes.removeAll(); 230 */ 231 } 232 233 /*** 234 * Erases all lines contained in this object 235 */ 236 public void clearLines(){ 237 Iterator iter = lines.iterator(); 238 while (iter.hasNext()){ 239 SimpleGizmo giz = (SimpleGizmo)iter.next(); 240 giz.detach(); 241 } 242 lines.clear(); 243 244 } 245 246 /*** 247 * Erases all points contained in this object 248 */ 249 public void clearPoints(){ 250 Iterator iter = points.iterator(); 251 while (iter.hasNext()){ 252 SimpleGizmo giz = (SimpleGizmo)iter.next(); 253 giz.detach(); 254 } 255 points.clear(); 256 } 257 258 /*** 259 * Erases all cubes contained in this object 260 */ 261 public void clearCubes(){ 262 Iterator iter = cubes.iterator(); 263 while (iter.hasNext()){ 264 SimpleGizmo giz = (SimpleGizmo)iter.next(); 265 giz.detach(); 266 } 267 cubes.clear(); 268 } 269 270 public void clearPlanes(){ 271 Iterator iter = planes.iterator(); 272 while (iter.hasNext()){ 273 SimpleGizmo giz = (SimpleGizmo)iter.next(); 274 giz.detach(); 275 } 276 planes.clear(); 277 } 278 279 public void setPlaneWidth(double planeWidth){ 280 this.planeWidth = planeWidth; 281 } 282 283 /*** 284 * I know, I know, points are not supposed to have a dimension. But the gizmo used to 285 * display a point does have a dimension, and its width is controlled by this method. 286 */ 287 public void setPointWidth(double width){ 288 this.pointWidth = width; 289 } 290 291 /*** 292 * Lines, points and planes usually have a default color associated with them. 293 * That color will be used, instead of rotating or user-supplied colors if this flag is set 294 */ 295 public void setUsingDefaultColors(boolean b){ 296 this.useDefaultColor = b; 297 } 298 299 300 /*** 301 * Lines, points and planes usually have a default color associated with them. 302 * That color will be used, instead of rotating or user-supplied colors if this flag is set 303 */ 304 public boolean isUsingDefaultColors(){ 305 return this.useDefaultColor; 306 } 307 308 /*** 309 * Returns the Icon associated with GeometryBag. 310 */ 311 public Icon getIcon(){ 312 return this.bagIcon; 313 } 314 315 /*** 316 * Sets the Icon associated with GeometryBag. 317 */ 318 public void setIcon(Icon bagIcon){ 319 this.bagIcon=bagIcon; 320 } 321 322 323 324 public void setColorRotatingEnable(boolean enable){ 325 this.rotatingColor = enable; 326 } 327 328 public boolean isColorRotating(){ 329 return this.rotatingColor; 330 } 331 332 public static void main(String[] args) { 333 Point3d origin = new Point3d(); 334 Point3d p1 = new Point3d(1.0, 1.0, 1.0); 335 Point3d p2 = new Point3d(0.0, 1.0, 5.0); 336 GeometryBag bag = new GeometryBag(); 337 NodeTester tester = new NodeTester(); 338 tester.add(bag.getBranchGroup()); 339 bag.addLine(origin, p1); 340 bag.addLine(p1, p2); 341 bag.addLine(p2, origin); 342 bag.addPlane(origin, p1, p2); 343 bag.addPoint(origin); 344 bag.addPoint(p1); 345 bag.addPoint(p2); 346 Point3d offset = new Point3d(0.0, 1.0, 0.0); 347 Point3d a = new Point3d(0.0, 1.0, 1.0); 348 Point3d b = new Point3d(0.0, 0.0, 0.0); 349 Point3d c = new Point3d(1.0, 0.0, 0.0); 350 System.out.println("Cycle"); 351 for (int i = 0; i < bag.colorCycle.length; i++) { 352 System.out.println(bag.colorCycle[i]); 353 } 354 355 for (int i=0; i<100; i++) { 356 a.add(offset); 357 b.add(offset); 358 c.add(offset); 359 bag.addPlane(a,b,c); 360 bag.addLine(a,b); 361 bag.addLine(b,c); 362 bag.addPoint(a); 363 bag.addPoint(b); 364 bag.addPoint(c); 365 } 366 } 367 368 369 } 370

This page was automatically generated by Maven