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 import javax.media.j3d.*;
23 import com.sun.j3d.utils.picking.*;
24 import javax.vecmath.*;
25 import org.j3d.geom.Sphere;
26 import javax.swing.Icon;
27 import javax.swing.ImageIcon;
28
29
30 /***
31 * Emulates a Laser Beam in the virtual world. The beam is simply a sphere which
32 * is placed so to rest on the surface of the object that is hit by the laser.
33 * @author Carlos da Silva dos Santos, Fabio Roberto de Miranda
34 * @version
35 */
36 class VcLaserBeam extends VcLaser {
37
38 private static int instanceCounter;
39 private boolean debugflag = true;
40 private VcLaserTracer tracer;
41 private Sphere sphere = null;
42 private ColoringAttributes color;
43 private Appearance app;
44 private Icon icon;
45
46 /* Classe mais importante na atividade de picking */
47 private PickTool pickTool;
48
49 /* Geometria com a qual interseção determina se um objeto foi "picked" ou não*/
50 private PickRay pickRay;
51
52 /* Para fins de resultado - informação sobre onde ocorreu o picking */
53 private PickIntersection pickIntersection;
54
55 /* Resultados da operação de picking */;
56 private PickResult[] pickResult;
57
58 /* Direção (e sentido) do raio de picking */
59 private Vector3d direction = new Vector3d();
60
61 /* De onde parte o raio de picking */
62 private Point3d origin = new Point3d();
63
64 /* Ponto pegado/pego/"pickado" */
65 private Point3d pickedPoint;
66
67 /***
68 * contentBG é um BranchGroup que serve para determinar o escopo de objetos
69 * que podem ser "picked".
70 * O nome da variável se deve ao propósito original do distanceMeasurer
71 * ser apenas medir a distância em relação a objetos do tipo VcContent,que ficam
72 * dentro de um branchGroup específico de coreScene
73 * */
74 private BranchGroup contentBG;
75
76 /***
77 * O único motivo pelo qual esta classe precisa de um J3DBase é para obter a referência para o BranchGroup
78 * que vai definir o escopo da operação de picking deste measurer
79 */
80 private J3DBase base;
81
82 /* Objetos "globais" de uso temporário para evitar alocações frequentes*/
83 private Point3d tempP3d = new Point3d();
84 private Vector3d tempVec3d = new Vector3d();
85 //Transform3D auxT3D = new Transform3D();
86
87 /* Um gizmo para se mostrado na posição correspondente à origem do raio de picking */
88 private Node originNode;
89 private VcHelperPoint target = new VcHelperPoint();
90 /* Outro gizmo, que é mostrado na posição onde o raio bate */
91 Node targetNode;
92
93 /***
94 * Default constructor
95 *
96 */
97 VcLaserBeam(){
98 this(null,null,null);
99 }
100
101 VcLaserBeam(VcLaserTracer tracer){
102 this(null,null,tracer);
103 }
104
105 VcLaserBeam(Node targetGizmo){
106 this(null,targetGizmo,null);
107 }
108
109 VcLaserBeam(Node targetGizmo, VcLaserTracer tracer){
110 this(null,targetGizmo,tracer);
111 }
112
113 /***
114 * Constructor
115 * @param originGizmo Gizmo correspondent to origin
116 * @param targetGizmo Gizmo correspondent to target
117 * @param tracer Object responsible for updating the target's position
118 */
119 VcLaserBeam(Node originGizmo, Node targetGizmo, VcLaserTracer tracer) {
120 super();
121 pickRay = new PickRay();
122 this.setLabel("LaserBeam"+(VcLaserBeam.instanceCounter++));
123 this.bg.setPickable(false);
124 this.bg.clearCapability(Node.ENABLE_PICK_REPORTING);
125 // if an originGizmo was provided it will be added to LaserBeam,
126 // otherwise we will create an cursor3D and use it as originGizmo
127 if(originGizmo!=null){
128 this.addNode(originGizmo);
129 this.originNode = originGizmo;
130 }
131 else{
132 Cursor3D cursor = new Cursor3D();
133 cursor.setLineWidth(1.0f);
134 this.addNode(cursor);
135 this.originNode=(Node)cursor;
136 }
137
138 // if one targetGizmo was provided to constructor we will use it,
139 // otherwise we will create a new sphere to be the targetGizmo
140 if(targetGizmo!=null){
141 target.addNode(targetGizmo);
142 this.targetNode = targetGizmo;
143 }
144 else{
145 color = new ColoringAttributes(1.0f,0.2f,0.2f,ColoringAttributes.FASTEST);
146 color.setCapability(ColoringAttributes.ALLOW_COLOR_READ);
147 color.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
148 app = new Appearance();
149 app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
150 app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
151 app.setColoringAttributes(color);
152
153 sphere = new Sphere(0.005f,app);
154 targetNode = (Node) sphere;
155 target.addNode(targetNode);
156 }
157
158 this.addNode(target.bg);
159
160 // if no tracer was provided we will create a new one
161 if(tracer==null){
162 // debugln("VcLaserBeam: creating trace behavior");
163 VcLaserTraceBehavior traceBeh = new VcLaserTraceBehavior(this);
164 traceBeh.setSchedulingBounds(new BoundingSphere());
165 this.tracer = traceBeh;
166 this.bg.addChild((Node)this.tracer);
167 } else this.tracer = tracer;
168
169 }
170
171 public Icon getIcon(){
172 return this.icon;
173 }
174
175 public void setIcon(Icon icon){
176 this.icon = icon;
177 }
178
179
180 /***
181 * Returns the number of objects instatiated so far.
182 */
183 public static int getInstanceCounter(){
184 return instanceCounter;
185 }
186
187
188 public static void main(String[] args) {
189 VcLaserBeam laserBeam1 = new VcLaserBeam();
190 }
191
192 /* Indicates the scope for picking operations: bg and its children. */
193 void setBranchGroup(BranchGroup bg){
194 this.contentBG = bg;
195 pickTool = new PickTool(bg);
196 }
197
198 /***
199 * Replaces the current target Gizmo with a new one
200 * @param gizmo the new gizmo to be used as target
201 */
202 void setTargetGizmo(Node gizmo){
203 target.removeAllNodes();
204 target.addNode(gizmo);
205 this.targetNode=gizmo;
206 }
207
208 /***
209 * Replaces the current origin Gizmo with a new one
210 * @param gizmo the new gizmo to be used as origin
211 */
212 void setOriginGizmo(Node gizmo){
213 this.removeNode(originNode);
214 this.addNode(gizmo);
215 this.originNode = gizmo;
216 }
217
218
219 /***
220 * Method from VcLaser interface
221 */
222 public void trace(){
223 // Faz o picking
224 // sets origin Point
225 setOrigin();
226 // sets direction vector
227 setDirection();
228 pickRay.set(origin, direction);
229 pickTool.setShape(pickRay, origin);
230 // uses geometry for better precision; any other mode
231 // does not work well
232 pickTool.setMode(PickTool.GEOMETRY);
233 /* O critério de sorting do pickAllSorted é distância */
234 pickResult = pickTool.pickAllSorted();
235 if (pickResult!=null){
236 //debugln(pickResult.length+" objects picked");
237 // Descobre o objeto
238 if (pickResult.length > 0){
239 // Descobre qual é o ponto mais próximo da origem deste pickRay
240 pickIntersection = pickResult[0].getClosestIntersection(origin);
241 pickedPoint = pickIntersection.getPointCoordinatesVW();
242 // places target Gizmo at pickedPoint coordinates
243 showPicked(pickedPoint);
244 } else {
245 //debugln("Length of pickResults vector was zero");
246 }
247
248 } else {
249 // Hides the target as picking operation hit no object;
250 // otherwise it would be shown floating in the void
251 target.detach();
252 //debugln("Null returned as result of picking.");
253 }
254
255 }
256
257 /***
258 * Method from VcLaser interface
259 *
260 */
261 public void initialize(){
262
263 }
264
265 VcHelperPoint getTarget() {
266 return this.target;
267 }
268
269 /***
270 * Returns a Point3d object containing the target's translation
271 *
272 */
273 Point3d getTargetTranslation() {
274 Point3d trans = new Point3d();
275 trans.x = target.getXTranslation();
276 trans.y = target.getYTranslation();
277 trans.z = target.getZTranslation();
278 return trans;
279 }
280
281 /***
282 * Sets the color of the target gizmo
283 */
284 void setColor(float red, float green, float blue){
285 if(sphere!=null){
286 color.setColor(red,green,blue);
287 app.setColoringAttributes(color);
288 notifyChangeListeners();
289 }
290 }
291
292 void setColor(Color3f c){
293 if(sphere!=null){
294 color.setColor(c);
295 app.setColoringAttributes(color);
296 notifyChangeListeners();
297 }
298 }
299
300
301 /***
302 * Sets the radius of the target gizmo
303 */
304 void setRadius(float radius){
305 if(sphere!=null){
306 sphere.setDimensions(radius);
307 notifyChangeListeners();
308 }
309 }
310
311 void setTracerEnable(boolean trace){
312 tracer.setEnable(trace);
313 }
314
315
316 // Talvez no futuro implementar um método que o posiciona perto de um objeto a
317 // uma certa distancia de seus Bounding Points, etc
318
319
320 public void debugln(String s){
321 if (GUIControl.debugflag){
322 System.out.println(s);
323 }
324 }
325
326 void setJ3DBase(J3DBase base){
327 this.base = base;
328 this.setBranchGroup(base.getJ3DRootBranchGroup());
329 }
330
331 /*** O ponto de origem (em coordenadas de VWorld) é obtido
332 * Simplesmente convertendo as coordenadas do ponto (0,0,0), que está no mesmo
333 * referencial que o gizmo que indica a origem do medidor para
334 * coordenadas de VWorld
335 * */
336 protected void setOrigin(){
337 tempP3d.x = 0.0;
338 tempP3d.y = 0.0;
339 tempP3d.z = 0.0;
340 //this.childTG.getLocalToVworld(tempT3D);
341 this.parentRotScaleTG.getLocalToVworld(tempT3D);
342 tempT3D.transform(tempP3d);
343 origin.set(tempP3d);
344 //debugln("Origin point is "+origin.x +","+ origin.y +","+ origin.z);
345 }
346
347 protected void setDirection(){
348 tempP3d.x = 0.0;
349 tempP3d.y = 0.0;
350 tempP3d.z = 1.0;
351 // Atenção para o método getTransformS
352 // ^
353 //this.childTG.getLocalToVworld(tempT3D);
354 this.parentRotScaleTG.getLocalToVworld(tempT3D);
355 tempT3D.transform(tempP3d);
356 direction.set(tempP3d);
357 direction.sub(origin);
358 //debugln("Direction point is "+direction.x +","+ direction.y +","+ direction.z);
359
360 }
361
362 /***
363 * Coloca um gizmo no ponto especificado (que deve ser onde o raio de picking bateu)
364 */
365 protected void showPicked(Point3d point){
366 tempT3D.setIdentity();
367 /* Coloca as coordenadas do ponto num vetor, que passará a apontar
368 * da origem para o ponto */
369 tempVec3d.set(point);
370 /* Ajusta a translação da transformada para um valor igual ao do vetor*/
371 tempT3D.set(tempVec3d);
372 //cursor3d.getLocalToVworld(auxT3D);
373 this.pivotTG.getLocalToVworld(auxT3D);
374 // inverting the local to vworld transform we get a transform which takes
375 // a point in VWorld coordinates and gives back a point in local coordinates
376 auxT3D.invert();
377 // after the multiplication auxT3D holds the transform that goes from the pivot
378 // point to the picked point
379 auxT3D.mul(tempT3D);
380
381 target.setTransform(auxT3D);
382 // shows the target in case it was hidden before
383 if(!target.bg.isLive()) this.addNode(target.bg);
384 target.notifyChangeListeners();
385 //debugln("showPicked: notifyChangeListeners");
386 }
387
388 protected void setTargetPosition(Point3d point){
389
390 }
391
392 protected void setTargetPositionRelative(Point3d point){
393 Point3d pointOut = new Point3d();
394 tempVec3d.set(point);
395 tempT3D.set(tempVec3d);
396 target.setTransform(tempT3D);
397 // debugln("setTargetPositionRelative: notifyChangeListeners");
398 target.notifyChangeListeners();
399 }
400
401 }
This page was automatically generated by Maven