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.Iterator;
24 import org.j3d.geom.Sphere;
25 import org.j3d.geom.Cylinder;
26 import javax.media.j3d.*;
27 import javax.vecmath.*;
28 import javax.swing.Icon;
29 import javax.swing.ImageIcon;
30
31 /***
32 * A laser array for structured lighting simulation.
33 * @author Carlos da Silva dos Santos
34 * @version 1.0
35 */
36 public class VcLaserArray extends VcLaser implements VcLaserTracer{
37
38 private double minimumDistance = 0.001;
39 private double minimumScale = 0.001;
40
41 private static int instanceCounter=0;
42 private int xBeams;
43 private int yBeams;
44 private int numBeams;
45 private double xDelta;
46 private double yDelta;
47 private double x,y;
48 private Iterator iter;
49 private Vector3d scale = new Vector3d();
50 /* signals if target geometry is editable */
51 private boolean editable = true;
52 /* holds targetGizmo so it can be used by multiple beams*/
53 private SharedGroup gizmoSG = new SharedGroup();
54 /* used for compensating scale */
55 private TransformGroup gizmoTG = new TransformGroup();
56 private VcLaserBeam beam =null;
57 private VcLaserTracer tracer;
58 private BranchGroup contentBG;
59 private Shape3D targetGizmo =null;
60 // to be shown at the origin of each beam
61 private Sphere origin;
62 private SharedGroup originSG = new SharedGroup();
63
64 /* actual geometry of the target */
65 private Sphere sphere;
66 private ColoringAttributes color;
67 private Appearance app;
68 private float red,green,blue;
69
70 private Icon arrayIcon = new ImageIcon(this.getClass().getResource("icons/array_item.gif"));
71
72 /***
73 * Creates a LaserArray with the following configuration:
74 * 2 beams in X direction
75 * 2 beams in Y direction
76 * 1.0 meter between beams in X direction
77 * 1.0 meter between beams in Y direction
78 */
79 public VcLaserArray() {
80 this(2,2,1.0d,1.0d,null,null);
81 }
82
83 /***
84 * Constructor
85 * @param xBeams Number of beams in X direction
86 * @param yBeams Number of beams in Y direction
87 * @param xDelta Distance between beams in X direction
88 * @param yDelta Distance between beams in Y direction
89 */
90 public VcLaserArray(int xBeams, int yBeams, double xDelta, double yDelta) {
91 this(xBeams,yBeams,xDelta,yDelta,null,null);
92 }
93
94 /***
95 * Constructor
96 * @param xBeams Number of beams in X direction
97 * @param yBeams Number of beams in Y direction
98 * @param xDelta Distance between beams in X direction
99 * @param yDelta Distance between beams in Y direction
100 * @param tracer Object responsible for updating the position of the beams
101 * @param targetGizmo Gizmo to be shown where the beam hits an object
102 */
103 public VcLaserArray(int xBeams, int yBeams, double xDelta, double yDelta, VcLaserTracer tracer, Shape3D targetGizmo) {
104 this.xBeams=xBeams;
105 this.yBeams=yBeams;
106 this.xDelta=xDelta;
107 this.yDelta=yDelta;
108 this.numBeams=xBeams*yBeams;
109 // DANGER!!
110 this.bg.setPickable(false);
111 debugln("VcLaserArray constructor");
112 debugln("beams: X:"+this.xBeams+" y:"+this.yBeams+" delta: X:"+this.xDelta+" Y:"+this.yDelta);
113 if(targetGizmo!=null){
114 debugln("targetGizmo != null");
115 this.targetGizmo = targetGizmo;
116 editable = false;
117 }
118 else{
119 debugln("targetGizmo == null; creating new one");
120 /* color is set to red */
121 this.red=1.0f;
122 this.green=0.0f;
123 this.blue=0.0f;
124 color = new ColoringAttributes(this.red,this.green,this.blue,ColoringAttributes.FASTEST);
125 color.setCapability(ColoringAttributes.ALLOW_COLOR_READ);
126 color.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
127 app = new Appearance();
128 app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
129 app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
130 app.setColoringAttributes(color);
131 sphere = new Sphere(0.005f,app);
132 sphere.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
133 sphere.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
134 debugln("Sphere: "+sphere);
135 this.targetGizmo =(Shape3D)sphere;
136 debugln("target gizmo: "+this.targetGizmo);
137 }
138 gizmoTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
139 gizmoTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
140 gizmoTG.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ);
141 gizmoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
142 gizmoTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
143 gizmoTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
144 gizmoTG.addChild(this.targetGizmo);
145
146 gizmoSG.setCapability(SharedGroup.ALLOW_CHILDREN_EXTEND);
147 gizmoSG.setCapability(SharedGroup.ALLOW_CHILDREN_READ);
148 gizmoSG.setCapability(SharedGroup.ALLOW_CHILDREN_WRITE);
149 gizmoSG.setCapability(SharedGroup.ALLOW_LINK_READ);
150 gizmoSG.addChild(gizmoTG);
151 //gizmoSG.addChild((Leaf)this.targetGizmo);
152 debugln("gizmoSG child(0): "+ gizmoSG.getChild(0));
153
154 // just for test
155 //Link link = new Link(gizmoSG);
156 //this.addNode(link);
157 //this.addNode(gizmoTG);
158
159
160 ColoringAttributes color2 = new ColoringAttributes(0,0,0,ColoringAttributes.FASTEST);
161 Appearance app2 = new Appearance();
162 app2.setColoringAttributes(color2);
163 origin = new Sphere(0.02f,app2);
164 originSG.addChild(origin);
165
166 ColoringAttributes color3 = new ColoringAttributes(0.4f,0.4f,0.0f,ColoringAttributes.FASTEST);
167 Appearance app3 = new Appearance();
168 app3.setColoringAttributes(color3);
169 Cylinder cylinder = new Cylinder(0.5f,0.02f,app3);
170 TransformGroup cylTG = new TransformGroup();
171 Transform3D cylT3D = new Transform3D();
172 cylTG.getTransform(cylT3D);
173 cylT3D.rotX(-Math.PI/2);
174 cylT3D.setTranslation(new Vector3f(0.0f,0.0f,0.25f));
175 cylTG.setTransform(cylT3D);
176 cylTG.addChild(cylinder);
177 this.addNode(cylTG);
178
179
180 if(tracer!=null) this.tracer = tracer;
181 else{
182 debugln("Array: creating traceBehavior");
183 VcLaserTraceBehavior traceBeh = new VcLaserTraceBehavior(this);
184 traceBeh.setSchedulingBounds(new BoundingSphere());
185 this.bg.addChild(traceBeh);
186 this.tracer = traceBeh;
187 }
188
189 calcBeams();
190 this.setLabel("LaserArray"+instanceCounter++);
191 }
192
193 /* Indica o escopo da operação de picking: bg e seus filhos */
194 public void setBranchGroup(BranchGroup bg){
195 this.contentBG = bg;
196 iter = childrenList.iterator();
197 while(iter.hasNext()){
198 beam = (VcLaserBeam) iter.next();
199 beam.setBranchGroup(bg);
200 }
201 }
202
203 private void calcBeams(){
204 debugln("Array: running calcBeams");
205 int children = this.childrenList.size();
206 debugln("number of children: "+children);
207
208 if(children < numBeams){
209 for(int i=0;i<numBeams-children;i++){
210 //beam = new VcLaserBeam(new Link(gizmoSG),this);
211 beam = new VcLaserBeam(new Link(originSG),new Link(gizmoSG),this);
212 beam.setBranchGroup(this.contentBG);
213 //this.gizmoTG.addChild(beam.bg);
214 //childrenList.add(beam.bg);
215 this.addVcChild(beam);
216 }
217 }
218 if(children > numBeams){
219 // Is this right?
220 for(int i=children-1;i>=numBeams;i--){
221 //gizmoTG.removeChild(i);
222 debugln("removing: "+i);
223 this.removeVcChild(i);
224 }
225 }
226
227 debugln("children:"+ childrenList.size()+"; numBeams: "+numBeams);
228 for(int i=0;i<xBeams;i++){
229 for(int j=0;j<yBeams;j++){
230 beam = (VcLaserBeam) childrenList.get(i+j*(xBeams));
231 tempT3D.setIdentity();
232 this.x= (i-0.5*(xBeams-1))*xDelta;
233 this.y= (j-0.5*(yBeams-1))*yDelta;
234 tempTrans.set(x,y,0.0d);
235 // sets this transform translation
236 tempT3D.set(tempTrans);
237 //tempT3D.setScale(0.4d);
238 beam.setTransform(tempT3D);
239 }
240 }
241 //notifyChangeListeners();
242 }
243
244
245 /***
246 * Method from VcLaser interface
247 */
248 public void trace(){
249 //debugln("Array: Tracing");
250
251 iter = childrenList.iterator();
252 while(iter.hasNext()){
253 try{
254 beam = (VcLaserBeam) iter.next();
255 beam.trace();
256 }catch(java.util.ConcurrentModificationException cme){
257 cme.printStackTrace();
258 }
259 }
260 }
261
262 /***
263 * Sets the color of the target gizmo
264 */
265 public void setColor(float red, float green, float blue){
266 if(red > 1.0) red = 1.0f;
267 if(green > 1.0) green = 1.0f;
268 if(blue > 1.0) blue = 1.0f;
269 if(red < 0.0) red = 0.0f;
270 if(green < 0.0) green = 0.0f;
271 if(blue < 0.0) blue = 0.0f;
272 color.setColor(red,green,blue);
273 this.red = red;
274 this.green=green;
275 this.blue=blue;
276 app.setColoringAttributes(color);
277 notifyChangeListeners();
278 }
279
280 /***
281 * Sets the color of the target gizmo.
282 * It calls notifyChangeListeners() at the end of execution.
283 * @param red value of Red component (between 0.0 and 1.0).
284 * @param green value of Green component (between 0.0 and 1.0).
285 * @param blue value of Blue component (between 0.0 and 1.0).
286 */
287 public void setColor(double red, double green, double blue){
288 setColor((float)red, (float)green, (float)blue);
289 }
290
291 /***
292 * Sets the color of the target gizmo.
293 * It calls notifyChangeListeners() at the end of execution.
294 * @param color new color to be used
295 */
296 public void setColor(Color3f color){
297 this.red=color.x;
298 this.green=color.y;
299 this.blue=color.z;
300 this.color.setColor(color);
301 app.setColoringAttributes(this.color);
302 notifyChangeListeners();
303 }
304
305
306 /***
307 * Sets the radius of the target gizmo.
308 * It calls notifyChangeListeners() at the end of execution.
309 */
310 public void setRadius(float radius){
311 sphere.setDimensions(radius);
312 //notifyChangeListeners();
313 }
314
315
316 public void setTargetScale(double scale){
317 if(scale < minimumScale) scale = minimumScale;
318 gizmoTG.getTransform(tempT3D);
319 tempT3D.setScale(scale);
320 gizmoTG.setTransform(tempT3D);
321 notifyChangeListeners();
322 }
323
324 public double getTargetScale(){
325 gizmoTG.getTransform(tempT3D);
326 return tempT3D.getScale();
327 }
328
329 public double getRed(){
330 return (double)this.red;
331 }
332
333 public double getGreen(){
334 return (double)this.green;
335 }
336
337 public double getBlue(){
338 return (double)this.blue;
339 }
340 /*
341 public float getRed(){
342 return this.red;
343 }
344
345 public float getGreen(){
346 return this.green;
347 }
348
349 public float getBlue(){
350 return this.blue;
351 }
352 */
353
354 /***
355 * Sets minimum distance allowed between laser beams (both in X and Y directions).
356 * It does not call notifyChangeListeners().
357 * @param minimumDistance new value of parameter
358 */
359 public void setMinimumDistance(double minimumDistance){
360 this.minimumDistance = minimumDistance;
361 }
362
363 /***
364 * Returns minimum distance allowed between laser beams (both in X and Y directions).
365 * It does not call notifyChangeListeners().
366 * @return current value of parameter.
367 */
368 public double getMinimumDistance(){
369 return this.minimumDistance;
370 }
371
372
373 /***
374 * Sets minimum scale that can be applied to laser beams.
375 * It does not call notifyChangeListeners().
376 * @param minimumScale new value of parameter.
377 */
378 public void setMinimumScale(double minimumScale){
379 this.minimumScale = minimumScale;
380 }
381
382 /***
383 * Returns minimum scale applied to laser beams.
384 * It does not call notifyChangeListeners().
385 * @return current value of parameter.
386 */
387 public double getMinimumScale(double minimumSize){
388 return this.minimumScale;
389 }
390
391 /***
392 * Changes the current grid for this laser array. It calls notifyChangeListeners().
393 * @param xBeams number of beams in X direction.
394 * @param yBeams number of beams in Y direction.
395 * @param xDelta distance between beams in X direction
396 * @param yDelta distance between beams in Y direction
397 */
398 public void setGrid(int xBeams,int yBeams, double xDelta, double yDelta){
399 this.xBeams = xBeams;
400 this.yBeams = yBeams;
401 this.xDelta = xDelta;
402 this.yDelta = yDelta;
403 if(xBeams<1) xBeams=1;
404 if(yBeams<1) yBeams=1;
405 if(xDelta<minimumDistance) xDelta = minimumDistance;
406 if(yDelta<minimumDistance) yDelta = minimumDistance;
407 this.numBeams = xBeams*yBeams;
408 calcBeams();
409 notifyChangeListeners();
410 }
411
412 public int getXBeams(){
413 return this.xBeams;
414 }
415
416 public int getYBeams(){
417 return this.yBeams;
418 }
419
420 public double getXDelta(){
421 return this.xDelta;
422 }
423
424 public double getYDelta(){
425 return this.yDelta;
426 }
427
428 /***
429 * Returns the number of objects instatiated so far.
430 */
431 public static int getInstanceCounter(){
432 return instanceCounter;
433 }
434
435
436 public String getInfo(){
437 String info = "Number of Beams: X: "+ getXBeams() + " Y: "+getYBeams()+
438 "\nDistance: X: "+getXDelta()+" Y: "+getYDelta() +"\nBeams:\n";
439 Point3d trans = new Point3d();
440 for(int i=0;i<xBeams;i++){
441 for(int j=0;j<yBeams;j++){
442 beam = (VcLaserBeam) childrenList.get(i+j*(xBeams));
443 trans = beam.getTargetTranslation();
444 info = info + i+","+j+": X: "+trans.x+": Y: "+trans.y+": Z: "+trans.z+"\n";
445 }
446 }
447 return super.getInfo() + info;
448 }
449
450 /***
451 * Method from VcLaser interface.
452 *
453 */
454 public void initialize(){
455 debugln("Array: initialize running");
456 this.calcBeams();
457 }
458
459
460 /***
461 * Method from VcLaserTracer interface.
462 * Enables updating of target position. It calls notifyChangeListeners() at end of execution.
463 * @param enable true if position should be updated, false otherwise.
464 */
465 public void setEnable(boolean enable){
466 this.tracer.setEnable(enable);
467 //notifyChangeListeners();
468 }
469
470 /***
471 * Returns a flag indicating whether target position is being updated.
472 * @return true if position is being updated, false otherwise.
473 */
474 public boolean getEnable(){
475 return this.tracer.getEnable();
476 }
477
478 /***
479 * Returns the Icon associated with the laser array.
480 */
481 public Icon getIcon(){
482 return this.arrayIcon;
483 }
484
485 /***
486 * Sets the Icon associated with the laser array.
487 */
488 public void setIcon(Icon arrayIcon){
489 this.arrayIcon = arrayIcon;
490 }
491
492 }
This page was automatically generated by Maven