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 javax.swing.JPanel;
24 import javax.swing.JLabel;
25 import javax.media.j3d.*;
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.util.*;
29 import javax.vecmath.*;
30 import com.sun.j3d.utils.picking.behaviors.*;
31 import camera3d.event.*;
32
33 /***
34 * Viewport is a Panel that contains a Canvas3D object.<br> Each viewport has its
35 * own VcPickMouseBehavior object.
36 * @author Fábio Roberto de Miranda
37 * @version 1.0
38 */
39 public class Viewport extends Panel {
40
41 boolean debugflag = true;
42 boolean displayingAxis = true;
43
44 /*** Holds most recent event fired by this Viewport. */
45 private ViewportEvent currentEvent;
46 /*** Event fired when setView is called. */
47 private ViewChangedEvent viewChangedEvent;
48
49 /*** view which is shown by this Viewport. */
50 private VcView vcView;
51 private GUIControl guiControl;
52
53 /*** Canvas3D that is rendered to this Viewport. */
54 private SaverCanvas3D canvas3d;
55
56 private BorderLayout borderLayout1 = new BorderLayout();
57 private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
58
59 /* Vector of objects interested in changes made to this Viewport. These objects
60 must implement the camera3d.ViewportChangeListener interface. */
61 private Vector changeListeners = new Vector(20, 5);
62
63 /*** Objects under this BranchGroup can be picked (if the proper capabilities are set). */
64 private BranchGroup rootBGForPicking;
65 private BranchGroup behaviorBG = new BranchGroup();
66 /*** Behavior responsible for picking objects in response to mouse events. */
67 VcPickMouseBehavior pickMouse;
68 /*** Object that handles selection of objects in the scene graph. When selected,
69 * objects can be target of actions, as manipulation. */
70 private SelectionList selectionList;
71
72 private int axisLength = 5;
73
74 public Viewport() {
75 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
76 canvas3d = new SaverCanvas3D(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration( template ));
77 pickMouse = new VcPickMouseBehavior(canvas3d, bounds);
78 viewChangedEvent = new ViewChangedEvent(this);
79 //canvas3d.enableMessage("Overlay testing");
80 try {
81 jbInit();
82 }
83 catch(Exception e) {
84 e.printStackTrace();
85 }
86 behaviorBG.setCapability(BranchGroup.ALLOW_DETACH);
87 behaviorBG.addChild(pickMouse);
88 }
89
90 /***
91 * Creates a new Viewport to show a specific VcView.
92 *
93 * @param vcView Object whose view will be shown by the new Viewport.
94 */
95 public Viewport(VcView vcView){
96 this();
97 if (vcView == null){
98 debugln("Viewport:VcView passed as argument to Viewport constructor is null");
99 }
100 this.setView(vcView);
101 }
102
103 /***
104 * Creates a new Viewport to show a specific VcView.
105 *
106 * @param vcView Object whose view will be shown by the new Viewport.
107 * @param base J3DBase object whose root BG will be used for picking purposes.
108 */
109 public Viewport(VcView vcView, J3DBase base){
110 this();
111 if (base != null){
112 this.rootBGForPicking = base.getJ3DRootBranchGroup();
113 }
114 this.setView(vcView);
115 }
116
117
118 /***
119 * Sets the VcView which will be shown by this Viewport. At the end of execution
120 * method notifyChangeListeners is called.
121 * Note: before calling setView on this viewport, setJ3DRootBGForPicking must be called,
122 * if it is not called, picking won't work.
123 *
124 * @param view Object whose view will be shown by the new Viewport.
125 */
126 public void setView(VcView view){
127 // this could be optimized, as not all the times we change the camera it is also
128 // necessary to change rootBGForPicking, but for now performance is not compromised
129 // by that and we'll leave as it is.
130 if (rootBGForPicking != null){
131 //pickMouse = new VcPickMouseBehavior(canvas3d, rootBGForPicking, bounds);
132 pickMouse.setRootBG(rootBGForPicking);
133 pickMouse.setTolerance(3.f);
134 behaviorBG.detach();
135 rootBGForPicking.addChild(behaviorBG);
136 debugln("Picking Behavior added");
137 }
138 pickMouse.setSchedulingBounds(bounds);
139
140 pickMouse.setView(view);
141 debugln("Viewport.setView(): Changing view");
142 VcView previousView = this.getView();
143 if (this.vcView!=null){
144 this.vcView.removeCanvas3D(this.canvas3d);
145 }
146 this.vcView = view;
147 if (vcView==null){
148 System.out.println("VcView is null");
149 }
150 vcView.addCanvas3D(this.canvas3d);
151
152 viewChangedEvent.setView(vcView);
153 currentEvent = viewChangedEvent;
154 notifyChangeListeners();
155 }
156
157 public VcView getView(){
158 return this.vcView;
159 }
160
161
162 private void jbInit() throws Exception {
163 this.setLayout(borderLayout1);
164 this.add(canvas3d, BorderLayout.CENTER);
165 }
166
167 /***
168 * Returns the Canvas3D object attached to this Viewport.
169 */
170 public Canvas3D getCanvas3D(){
171 return this.canvas3d;
172 }
173
174 /***
175 * Controls execution of the picking behavior in this Viewport. This control
176 * is necessary for object manipulation, when picking must be disabled so it
177 * doesn't interfere with objects movements.
178 *
179 * @param b enables picking if true; disables picking otherwise.
180 */
181 public void setPickingEnable(boolean b){
182 pickMouse.setEnable(b);
183 }
184
185 public void setGUIControl(GUIControl guiControl){
186 this.guiControl = guiControl;
187 }
188
189 public void debugln(String s){
190 if ((debugflag)&&(GUIControl.debugflag)){
191 System.out.println(s);
192 }
193 }
194
195 public void addChangeListener(ViewportChangeListener listener){
196 this.changeListeners.addElement(listener);
197 }
198
199 public void notifyChangeListeners(){
200 debugln("Viewport: notifying Viewport change listeners");
201 if (changeListeners.isEmpty()){
202 debugln("Viewport.notifyChangeListeners: no listeners registered");
203 }
204 Iterator iter = changeListeners.iterator();
205
206 while (iter.hasNext()){
207 //((ViewportChangeListener)iter.next()).viewportChanged(this);
208 ((ViewportChangeListener)iter.next()).viewportChanged(currentEvent);
209 }
210 }
211
212 public void saveJPGFile(String filename){
213 /*
214 * Feature que o Luciano pediu: imprimir a distancia entre a camera e
215 * o plano de projecao
216 */
217 System.out.println("JPG file: "+filename);
218 Point3d p3d = new Point3d();
219 getCanvas3D().getCenterEyeInImagePlate(p3d);
220 System.out.println("X: "+p3d.x+" Y: "+p3d.y+" Z: "+p3d.z);
221 System.out.println("Distance from camera to Image Plate: "+p3d.z);
222 canvas3d.saveFrame(filename);
223 }
224
225 public void saveJPGFile(String filename, int w, int h){
226 /*
227 * Feature que o Luciano pediu: imprimir a distancia entre a camera e
228 * o plano de projecao
229 */
230 System.out.println("JPG file: "+filename);
231 Point3d p3d = new Point3d();
232 getCanvas3D().getCenterEyeInImagePlate(p3d);
233 System.out.println("X: "+p3d.x+" Y: "+p3d.y+" Z: "+p3d.z);
234 System.out.println("Distance from camera to Image Plate: "+p3d.z);
235
236 int width = w;
237 int height = h;
238 /*
239 View view = canvas3d.getView();
240 OffScreenCanvas3D offCanvas = canvas3d.getOffScreenCanvas3D();
241 view.addCanvas3D(offCanvas);
242 offCanvas.setSize(width, height);
243 offCanvas.getScreen3D().setSize(width, height);
244 offCanvas.getScreen3D().setPhysicalScreenHeight(canvas3d.getScreen3D().getPhysicalScreenHeight());
245 offCanvas.getScreen3D().setPhysicalScreenWidth(canvas3d.getScreen3D().getPhysicalScreenWidth());
246 offCanvas.repaint();
247 System.out.println("Pixel Height: "+offCanvas.getScreen3D().getPhysicalScreenHeight()/height);
248 System.out.println("Pixel Width: "+offCanvas.getScreen3D().getPhysicalScreenWidth()/width);
249
250 /* Originally copied from a demo that came with Java3D */
251 //view.stopView();
252 //canvas3d.setImageReady();
253 //view.startView();
254
255 canvas3d.saveFrame(filename, SaverCanvas3D.JPEG, width, height);
256
257 // removal of offscreen canvas3D is done inside SaverCanvas3D
258 // view.removeCanvas3D(offCanvas);
259 }
260
261
262
263 public void enableMessage(String s){
264 canvas3d.enableMessage(s);
265 }
266
267 public void disableMessage(){
268 canvas3d.disableMessage();
269 }
270
271 /*
272 * Tells this viewport of the coordinates of center and endpoints this canvas3d will
273 * use to display helper axis
274 */
275 public void setAxisPoints(Point3d center, Point3d x, Point3d y, Point3d z){
276 // find the coordinates of given points in the image plate
277 canvas3d.setAxisPoints(center, x, y, z);
278
279 }
280
281
282 /*
283 * Allows the user to specify the length of axes drawn on screen
284 */
285 public void setDrawnAxisLength(int length){
286 this.axisLength = length;
287 }
288
289 /***
290 * Enables/disables the displaying of xyz axis on Viewports
291 */
292 public void setAxisVisible(boolean visible){
293 this.displayingAxis = visible;
294 }
295
296 public void setJ3DRootBGForPicking(BranchGroup bg){
297 this.rootBGForPicking = bg;
298 }
299
300 public void setSelectionList(SelectionList list){
301 this.selectionList = list;
302 //pickMouse.setSelectionList(list);
303 pickMouse.removeAllPickListeners();
304 pickMouse.addPickListener(list);
305 }
306
307 }
This page was automatically generated by Maven