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
24 import com.sun.j3d.utils.behaviors.keyboard.*;
25 import com.sun.j3d.utils.behaviors.mouse.*;
26 import com.sun.j3d.utils.picking.behaviors.*;
27 import javax.media.j3d.*;
28 import javax.vecmath.*;
29 import java.io.*;
30 import java.awt.*;
31 import javax.swing.ImageIcon;
32 import javax.swing.Icon;
33 import camera3d.event.*;
34
35 /***
36 * Represents a camera in the virtual world. This class encapsulates the javax.media.j3d.View
37 * and the javax.media.j3d.ViewPlatform objects.<br>
38 * The userData field of the ViewPlatform is set to point to this object, making it possible
39 * to retrieve an reference to the VcView object from the javax.media.j3d.View object it
40 * encapsulates, through this call sequence:<br>
41 * View.getViewPlatform.getUserData()
42 *
43 * @author Fábio Roberto de Miranda, Carlos da Silva dos Santos
44 */
45 public class VcView extends VcObject {
46 // overrides VcObject.instanceCounter
47 // allows instance counting of different subclasses of VcObject
48 private static int instanceCounter = 0;
49
50 /*** Specifies that Java3D should generate a parallel projectioin matrix for this view. */
51 public static final int PARALLEL_PROJECTION = javax.media.j3d.View.PARALLEL_PROJECTION;
52 /*** Specifies that Java3D should generate a perspective projectioin matrix for this view. */
53 public static final int PERSPECTIVE_PROJECTION = javax.media.j3d.View.PERSPECTIVE_PROJECTION;
54 /*** Policy for resizing and moving windows. */
55 public static final int VIRTUAL_WORLD = javax.media.j3d.View.VIRTUAL_WORLD;
56 /*** Policy for resizing and moving windows. */
57 public static final int PHYSICAL_WORLD = javax.media.j3d.View.PHYSICAL_WORLD;
58
59 private Icon cameraIcon = new ImageIcon(this.getClass().getResource("icons/camera_item.gif"));
60
61 private View view;
62 private ViewPlatform viewPlatform;
63 private VcKeyNavigatorBehavior keyNav;
64
65 private BoundingSphere behaviorBounds;
66 private BoundingBox tempBB = new BoundingBox();
67 private Point3d lowerP3d = new Point3d();
68 private Point3d upperP3d = new Point3d();
69 private Matrix3f tempM3f = new Matrix3f();
70 private Vector3f tempVec3f = new Vector3f();
71
72 private double lastBBlength;
73
74 /***
75 * Constructs a VcView with given parameters.
76 * @param body PhysicalBody to be used by this VcView
77 * @param environment PhysicalEnvironment to be used by this VcView
78 */
79 public VcView(PhysicalBody body, PhysicalEnvironment environment) {
80 this();
81 view.setPhysicalBody(body);
82 view.setPhysicalEnvironment(environment);
83 //view.setBackClipDistance(0.01);
84 //view.setFrontClipDistance(90.0);
85 }
86
87 /***
88 * Constructs a VcView with default parameters
89 */
90 public VcView(){
91 super();
92 view = new View();
93 viewPlatform = new ViewPlatform();
94 viewPlatform.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
95
96 // this line was added in order to allow retrieving of VcView object from a
97 // given Canvas3D, using canvas3d.getView().getViewPlatform().getUserData();
98 viewPlatform.setUserData(this);
99
100 pivotTG.addChild(viewPlatform);
101 view.attachViewPlatform(viewPlatform);
102 PhysicalBody body = null;
103 PhysicalEnvironment environment = null;
104 if (body==null){
105 body = new PhysicalBody();
106 }
107 if (environment==null){
108 environment = new PhysicalEnvironment();
109 }
110 view.setPhysicalBody(body);
111 view.setPhysicalEnvironment(environment);
112
113 // added 22/04/2003
114 view.setBackClipPolicy(View.VIRTUAL_EYE);
115 view.setFrontClipPolicy(View.VIRTUAL_EYE);
116 view.setBackClipDistance(100.0);
117 view.setFrontClipDistance(1.0);
118
119 this.setLabel("View"+VcView.instanceCounter++);
120 Shape3D avatar = AvatarManager.getAvatarManager().getCameraAvatarShape();
121 pivotTG.addChild(avatar);
122
123 behaviorBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
124
125 //keyNav = new VcKeyNavigatorBehavior(this.childTG);
126 //keyNav = new VcKeyNavigatorBehavior(this);
127 /* creates key navigator as a listener to events in canvas3d;
128 solves some problem I can't remember of now.
129 Carlos */
130 keyNav = new VcKeyNavigatorBehavior(null,this);
131 keyNav.setSchedulingBounds(behaviorBounds);
132 bg.addChild(keyNav);
133
134 /* disables execution of this behavior by default;
135 as the processStimulus() method is executed on a
136 once per frame basis, it might get slow if we have
137 a large number of cameras; execution of behavior is only
138 turned on when VcView is attached to a Canvas3D
139 */
140 keyNav.setEnable(false);
141
142 }
143
144
145
146 /***
147 * Adds a given Canvas3D to the list of Canvas3D objects to which this VcView is rendered.
148 * @param canvas
149 */
150 public void addCanvas3D(Canvas3D canvas){
151 if(canvas==null) debugln("VcView.addViewport: canvas3D ==null");
152 else debugln("VcView.addViewport: canvas3D == "+canvas.toString());
153 this.view.addCanvas3D(canvas);
154 keyNav.addCanvas3D(canvas);
155 try{
156 keyNav.addListener(canvas);
157 }catch(Exception e){
158 e.printStackTrace();
159 }
160 // enables execution of navigation behavior
161 //DANGER
162 // disabling navigation in order to debug memory problems
163 // 28/02/2003
164 keyNav.setEnable(true);
165 //viewport.setView(this);
166 }
167
168 /***
169 * Remove all Canvas3Ds to which this view is attached.
170 */
171 public void removeAllCanvas3Ds(){
172 view.removeAllCanvas3Ds();
173 keyNav.removeAllCanvas3Ds();
174 // disables execution of navigation behavior
175 keyNav.setEnable(false);
176 }
177
178 /***
179 * Removes a Canvas3D.
180 * @param canvas Canvas3D to be removed.
181 */
182 public void removeCanvas3D(Canvas3D canvas){
183 view.removeCanvas3D(canvas);
184 keyNav.removeCanvas3D(canvas);
185 try{
186 keyNav.removeListener(canvas);
187 }catch(Exception e){
188 e.printStackTrace();
189 }
190 if(view.numCanvas3Ds()==0) keyNav.setEnable(false);
191 }
192
193 /***
194 * Method to be called from inside VcKeyNavigator. All the internal properties
195 * are updated accordingly, but notifyChangeListeners is not called.
196 * @param rotQ4d quaternion holding new rotation
197 * @param transVec Vector3d holding new translation
198 * @param scale new scale
199 */
200 public void setTransform(Quat4d rotQ4d, Vector3d transVec, double scale){
201 this.invalidData = true;
202
203 this.transVec.x = transVec.x;
204 this.transVec.y = transVec.y;
205 this.transVec.z = transVec.z;
206
207 // sets translation
208 tempT3D.setIdentity();
209 tempT3D.setTranslation(transVec);
210 parentTransTG.setTransform(tempT3D);
211 // sets rotation and scale
212 tempT3D.setIdentity();
213 tempT3D.set(rotQ4d);
214 tempT3D.setScale(scale);
215 parentRotScaleTG.setTransform(tempT3D);
216 scaleVec.x = scaleVec.y = scaleVec.z = scale;
217 rotAngles = this.getRotAngle(tempT3D);
218 this.invalidData= false;
219 //debugln("__VcView: Calling notify");
220 currentEvent = transformEvent;
221 //DANGER
222 notifyChangeListeners();
223 //debugln("____VcView: End Notify");
224 }
225
226
227 public String toString(){
228 return this.label;
229 }
230
231 public int getProjectionPolicy(){
232 return this.view.getProjectionPolicy();
233 }
234
235 /***
236 * Sets projection policy; notifyChangeListeners is called after execution.
237 * @param policy the new projection policy
238 */
239 public void setProjectionPolicy(int policy){
240 this.view.setProjectionPolicy(policy);
241 genericEvent.setType(VcObjectEvent.VCVIEW_CHANGED);
242 currentEvent = genericEvent;
243 notifyChangeListeners();
244 }
245
246 /***
247 *
248 */
249 public int getWindowMovementPolicy(){
250 return this.view.getWindowMovementPolicy();
251 }
252
253 public void setWindowMovementPolicy(int policy){
254 this.view.setWindowMovementPolicy(policy);
255 }
256
257 public int getWindowResizePolicy(){
258 return this.view.getWindowResizePolicy();
259 }
260
261 public void setWindowResizePolicy(int i){
262 this.view.setWindowResizePolicy(i);
263 }
264
265 public void setNavigateEnable(boolean navigating){
266 this.keyNav.setEnable(navigating);
267 }
268
269 public boolean getNavigateEnable(){
270 return this.keyNav.getEnable();
271 }
272
273 /***
274 * Sets the field of view used to compute the projection transform.
275 * @param fov value of field of view in degrees.
276 */
277 public void setFieldOfView(double fov){
278 this.view.setFieldOfView(Math.toRadians(fov));
279 }
280
281 /***
282 * Returns the field of view used to compute the projection transform.
283 * @return value of field of view in degrees.
284 */
285 public double getFieldOfView(){
286 return Math.toDegrees(view.getFieldOfView());
287 }
288
289 public double getFrontClipDistance(){
290 return view.getFrontClipDistance();
291 }
292
293 public void setFrontClipDistance(double dist){
294 this.view.setFrontClipDistance(dist);
295 }
296
297 public double getBackClipDistance(){
298 return view.getBackClipDistance();
299 }
300
301 public void setBackClipDistance(double dist){
302 this.view.setBackClipDistance(dist);
303 }
304
305 /***
306 * Translates the camera so a given VcObject is made completely visible.
307 */
308 public void frameObjectInView(VcObject vcObject){
309 vcObject.getBoundingBoxInVWorld(tempBB);
310 tempBB.getUpper(upperP3d);
311 tempBB.getLower(lowerP3d);
312 frameBoundingBox(upperP3d, lowerP3d);
313 }
314
315 void frameBoundingBox(Point3d lowerP3d, Point3d upperP3d){
316 debugln("J3DBase:Coordinates of lower corner ("+lowerP3d.x+","+lowerP3d.y+","+lowerP3d.z+")");
317 debugln("J3DBase:Coordinates of upper corner ("+upperP3d.x+","+upperP3d.y+","+upperP3d.z+")");
318 // calculates center of bounding box
319 double xCenter = (upperP3d.x + lowerP3d.x)/2;
320 double yCenter = (upperP3d.y + lowerP3d.y)/2;
321 double zCenter = (upperP3d.z + lowerP3d.z)/2;
322
323 // Position view at the center of the BoundingBox
324 // We'll keep only rotation and scale values, resetting
325 // all translation
326 pivotTG.getTransform(auxT3D);
327 pivotTG.getLocalToVworld(tempT3D);
328 tempT3D.mul(auxT3D);// calculates total transformation of this view
329 this.setTransformsToIdentity();// resets this view's transformation
330 tempT3D.getRotationScale(tempM3f);
331 tempT3D.setIdentity();
332 tempT3D.setRotationScale(tempM3f);// now tempT3D only contains the rotational
333 // and scaling compoenents of the initial transformation
334 tempVec3f.x = (float)xCenter;
335 tempVec3f.y = (float)yCenter;
336 tempVec3f.z = (float)zCenter;
337 auxT3D.setIdentity();
338 auxT3D.set(tempVec3f);// sets auxT3D so it stores a translation
339 // to the center of the bounding box
340 auxT3D.mul(tempT3D);
341
342 tempVec3f.x = (float)(upperP3d.x - lowerP3d.x);
343 tempVec3f.y = (float)(upperP3d.y - lowerP3d.y);
344 tempVec3f.z = (float)(upperP3d.z - lowerP3d.z);
345
346 double length = tempVec3f.length();
347 float distanceFactor = 1.0f;
348
349 this.lastBBlength = length;
350
351 tempVec3f.z = distanceFactor*(float)length;
352 tempVec3f.x = tempVec3f.y = 0.0f;
353
354
355 tempT3D.setIdentity();
356
357 tempT3D.set(tempVec3f);
358
359 auxT3D.mul(tempT3D);
360 /* Set front and back clipping planes
361 */
362 //childTG.setTransform(auxT3D);
363 this.setTransform(auxT3D);
364
365 /*
366 * frontClipQuocient, backClipFactor and boundingFactor are all
367 * guessed, they can be changed at will if needed
368 */
369 /*
370 this.setFrontClipDistance(length/frontClipQuocient);
371 this.setBackClipDistance(backClipFactor*length);
372 */
373 this.setFrontClipDistance(0.25);
374 this.setBackClipDistance(400);
375
376 }
377
378 /***
379 * Returns the number of VcView objects created so far.
380 */
381 public static int getInstanceCounter(){
382 return instanceCounter;
383 }
384
385 /*
386 double getLastFramedBBLength(){
387 return this.lastBBlength;
388 }*/
389
390 /***
391 * Returns the Icon associated with cameras.
392 */
393 public Icon getIcon(){
394 return this.cameraIcon;
395 }
396
397 /***
398 * Sets the Icon associated with cameras.
399 */
400 public void setIcon(Icon cameraIcon){
401 this.cameraIcon=cameraIcon;
402 }
403
404
405 }
This page was automatically generated by Maven