]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/threedee/Java3DWindow.java
Version 3, August 2007
[GpsPrune.git] / tim / prune / threedee / Java3DWindow.java
1 package tim.prune.threedee;
2
3 import java.awt.FlowLayout;
4 import java.awt.BorderLayout;
5 import java.awt.Font;
6 import java.awt.GraphicsConfiguration;
7 import java.awt.GraphicsEnvironment;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.geom.GeneralPath;
11
12 import javax.media.j3d.AmbientLight;
13 import javax.media.j3d.Appearance;
14 import javax.media.j3d.BoundingSphere;
15 import javax.media.j3d.BranchGroup;
16 import javax.media.j3d.Canvas3D;
17 import javax.media.j3d.Font3D;
18 import javax.media.j3d.FontExtrusion;
19 import javax.media.j3d.GraphicsConfigTemplate3D;
20 import javax.media.j3d.Group;
21 import javax.media.j3d.Material;
22 import javax.media.j3d.PointLight;
23 import javax.media.j3d.Shape3D;
24 import javax.media.j3d.Text3D;
25 import javax.media.j3d.Transform3D;
26 import javax.media.j3d.TransformGroup;
27 import javax.swing.JButton;
28 import javax.swing.JFrame;
29 import javax.swing.JOptionPane;
30 import javax.swing.JPanel;
31 import javax.vecmath.Color3f;
32 import javax.vecmath.Matrix3d;
33 import javax.vecmath.Point3d;
34 import javax.vecmath.Point3f;
35 import javax.vecmath.Vector3d;
36
37 import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
38 import com.sun.j3d.utils.geometry.Box;
39 import com.sun.j3d.utils.geometry.Cylinder;
40 import com.sun.j3d.utils.geometry.Sphere;
41 import com.sun.j3d.utils.universe.SimpleUniverse;
42
43 import tim.prune.App;
44 import tim.prune.I18nManager;
45 import tim.prune.data.Altitude;
46 import tim.prune.data.Track;
47
48
49 /**
50  * Class to hold main window for java3d view of data
51  */
52 public class Java3DWindow implements ThreeDWindow
53 {
54         private App _app = null;
55         private Track _track = null;
56         private JFrame _parentFrame = null;
57         private JFrame _frame = null;
58         private ThreeDModel _model = null;
59         private OrbitBehavior _orbit = null;
60         private int _altitudeCap = ThreeDModel.MINIMUM_ALTITUDE_CAP;
61
62         /** only prompt about big track size once */
63         private static boolean TRACK_SIZE_WARNING_GIVEN = false;
64
65         // Constants
66         private static final double INITIAL_Y_ROTATION = -25.0;
67         private static final double INITIAL_X_ROTATION = 15.0;
68         private static final String CARDINALS_FONT = "Arial";
69         private static final int MAX_TRACK_SIZE = 2500; // threshold for warning
70
71
72         /**
73          * Constructor
74          * @param inApp App object to use for callbacks
75          * @param inFrame parent frame
76          */
77         public Java3DWindow(App inApp, JFrame inFrame)
78         {
79                 _app = inApp;
80                 _parentFrame = inFrame;
81         }
82
83
84         /**
85          * Set the track object
86          * @param inTrack Track object
87          */
88         public void setTrack(Track inTrack)
89         {
90                 _track = inTrack;
91         }
92
93
94         /**
95          * Show the window
96          */
97         public void show() throws ThreeDException
98         {
99                 // Get the altitude cap to use
100                 String altitudeUnits = getAltitudeUnitsLabel(_track);
101                 Object altCapString = JOptionPane.showInputDialog(_parentFrame,
102                         I18nManager.getText("dialog.3d.altitudecap") + " (" + altitudeUnits + ")",
103                         I18nManager.getText("dialog.3d.title"),
104                         JOptionPane.QUESTION_MESSAGE, null, null, "" + _altitudeCap);
105                 if (altCapString == null) return;
106                 try
107                 {
108                         _altitudeCap = Integer.parseInt(altCapString.toString());
109                 }
110                 catch (Exception e) {} // Ignore parse errors
111
112                 // Set up the graphics config
113                 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
114                 if (config == null)
115                 {
116                         // Config shouldn't be null, but we can try to create a new one as a workaround
117                         GraphicsConfigTemplate3D gc = new GraphicsConfigTemplate3D();
118                         gc.setDepthSize(0);
119                         config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gc);
120                 }
121
122                 if (config == null)
123                 {
124                         // Second attempt also failed, going to have to give up here.
125                         throw new ThreeDException("Couldn't create graphics config");
126                 }
127
128                 // Check number of points in model isn't too big, and suggest compression
129                 Object[] buttonTexts = {I18nManager.getText("button.continue"), I18nManager.getText("button.cancel")};
130                 if (_track.getNumPoints() > MAX_TRACK_SIZE && !TRACK_SIZE_WARNING_GIVEN)
131                 {
132                         if (JOptionPane.showOptionDialog(_frame,
133                                         I18nManager.getText("dialog.exportpov.warningtracksize"),
134                                         I18nManager.getText("dialog.exportpov.title"), JOptionPane.OK_CANCEL_OPTION,
135                                         JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
136                                 == JOptionPane.OK_OPTION)
137                         {
138                                 // opted to continue, don't show warning again
139                                 TRACK_SIZE_WARNING_GIVEN = true;
140                         }
141                         else
142                         {
143                                 // opted to cancel - show warning again next time
144                                 return;
145                         }
146                 }
147
148                 Canvas3D canvas = new Canvas3D(config);
149                 canvas.setSize(400, 300);
150
151                 // Create the scene and attach it to the virtual universe
152                 BranchGroup scene = createSceneGraph();
153                 SimpleUniverse u = new SimpleUniverse(canvas);
154
155                 // This will move the ViewPlatform back a bit so the
156                 // objects in the scene can be viewed.
157                 u.getViewingPlatform().setNominalViewingTransform();
158
159                 // Add behaviour to rotate using mouse
160                 _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL |
161                                                                   OrbitBehavior.STOP_ZOOM);
162                 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
163                 _orbit.setSchedulingBounds(bounds);
164                 u.getViewingPlatform().setViewPlatformBehavior(_orbit);
165                 u.addBranchGraph(scene);
166
167                 // Don't reuse _frame object from last time, because data and/or scale might be different
168                 // Need to regenerate everything
169                 _frame = new JFrame(I18nManager.getText("dialog.3d.title"));
170                 _frame.getContentPane().setLayout(new BorderLayout());
171                 _frame.getContentPane().add(canvas, BorderLayout.CENTER);
172                 // Make panel for render, close buttons
173                 JPanel panel = new JPanel();
174                 panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
175                 // Add callback button for render
176                 JButton renderButton = new JButton(I18nManager.getText("menu.file.exportpov"));
177                 renderButton.addActionListener(new ActionListener()
178                 {
179                         /** Render button pressed */
180                         public void actionPerformed(ActionEvent e)
181                         {
182                                 if (_orbit != null)
183                                 {
184                                         callbackRender();
185                                 }
186                         }});
187                 panel.add(renderButton);
188                 // Display coordinates of lat/long lines of 3d graph in separate dialog
189                 JButton showLinesButton = new JButton(I18nManager.getText("button.showlines"));
190                 showLinesButton.addActionListener(new ActionListener() {
191                         public void actionPerformed(ActionEvent e)
192                         {
193                                 double[] latLines = _model.getLatitudeLines();
194                                 double[] lonLines = _model.getLongitudeLines();
195                                 LineDialog dialog = new LineDialog(_frame, latLines, lonLines);
196                                 dialog.showDialog();
197                         }
198                 });
199                 panel.add(showLinesButton);
200                 // Close button
201                 JButton closeButton = new JButton(I18nManager.getText("button.close"));
202                 closeButton.addActionListener(new ActionListener()
203                 {
204                         /** Close button pressed - clean up */
205                         public void actionPerformed(ActionEvent e)
206                         {
207                                 _frame.dispose();
208                                 _frame = null;
209                                 _orbit = null;
210                         }});
211                 panel.add(closeButton);
212                 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
213                 _frame.setSize(500, 350);
214                 _frame.pack();
215
216                 // show frame
217                 _frame.show();
218                 if (_frame.getState() == JFrame.ICONIFIED)
219                 {
220                         _frame.setState(JFrame.NORMAL);
221                 }
222         }
223
224
225         /**
226          * Create the whole scenery from the given track
227          * @return all objects in the scene
228          */
229         private BranchGroup createSceneGraph()
230         {
231                 // Create the root of the branch graph
232                 BranchGroup objRoot = new BranchGroup();
233
234                 // Create the transform group node and initialize it.
235                 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
236                 TransformGroup objTrans = new TransformGroup();
237                 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
238
239                 // Create a translation
240                 Transform3D shiftz = new Transform3D();
241                 shiftz.setScale(0.055);
242                 TransformGroup shiftTrans = new TransformGroup(shiftz);
243
244                 objRoot.addChild(shiftTrans);
245                 Transform3D rotTrans = new Transform3D();
246                 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
247                 Transform3D rot2 = new Transform3D();
248                 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
249                 TransformGroup tg2 = new TransformGroup(rot2);
250                 objTrans.setTransform(rotTrans);
251                 shiftTrans.addChild(tg2);
252                 tg2.addChild(objTrans);
253
254                 // Base plane
255                 Appearance planeAppearance = null;
256                 Box plane = null;
257                 planeAppearance = new Appearance();
258                 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
259                  new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
260                  new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
261                 plane = new Box(10f, 0.04f, 10f, planeAppearance);
262                 objTrans.addChild(plane);
263
264                 // N, S, E, W
265                 GeneralPath bevelPath = new GeneralPath();
266                 bevelPath.moveTo(0.0f, 0.0f);
267                 for (int i=0; i<91; i+= 5)
268                         bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
269                           (float) (0.1 * Math.sin(Math.toRadians(i))));
270                 for (int i=90; i>0; i-=5)
271                         bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
272                           (float) (0.1 * Math.sin(Math.toRadians(i))));
273                 Font3D compassFont = new Font3D(
274                         new Font(CARDINALS_FONT, Font.PLAIN, 1),
275                         new FontExtrusion(bevelPath));
276                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -10f), compassFont));
277                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 10f), compassFont));
278                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
279                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
280
281                 // create and scale model
282                 _model = new ThreeDModel(_track);
283                 _model.setAltitudeCap(_altitudeCap);
284                 _model.scale();
285
286                 // Lat/Long lines
287                 objTrans.addChild(createLatLongs(_model));
288
289                 // Add points to model
290                 objTrans.addChild(createDataPoints(_model));
291
292                 // Create lights
293                 BoundingSphere bounds =
294                   new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
295                 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
296                 aLgt.setInfluencingBounds(bounds);
297                 objTrans.addChild(aLgt);
298
299                 PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
300                  new Point3f(0f, 0f, 2f),
301                  new Point3f(0.25f, 0.05f, 0.0f) );
302                 pLgt.setInfluencingBounds(bounds);
303                 objTrans.addChild(pLgt);
304
305                 PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
306                  new Point3f(6f, 1f, 6f),
307                  new Point3f(0.2f, 0.1f, 0.05f) );
308                 pl2.setInfluencingBounds(bounds);
309                 objTrans.addChild(pl2);
310
311                 PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
312                  new Point3f(0.0f, 12f, -2f),
313                  new Point3f(0.1f, 0.1f, 0.0f) );
314                 pl3.setInfluencingBounds(bounds);
315                 objTrans.addChild(pl3);
316
317                 // Have Java 3D perform optimizations on this scene graph.
318                 objRoot.compile();
319
320                 return objRoot;
321         }
322
323
324         /**
325          * Create a text object for compass point, N S E or W
326          * @param text text to display
327          * @param locn position at which to display
328          * @param font 3d font to use
329          * @return Shape3D object
330          */
331         private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
332         {
333                 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
334                 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
335                  new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
336                  new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
337                 mat.setLightingEnable(true);
338                 Appearance app = new Appearance();
339                 app.setMaterial(mat);
340                 Shape3D shape = new Shape3D(txt, app);
341                 return shape;
342         }
343
344
345         /**
346          * Create all the latitude and longitude lines on the base plane
347          * @param inModel model containing data
348          * @return Group object containing cylinders for lat and long lines
349          */
350         private static Group createLatLongs(ThreeDModel inModel)
351         {
352                 Group group = new Group();
353                 int numlines = inModel.getLatitudeLines().length;
354                 for (int i=0; i<numlines; i++)
355                 {
356                         group.addChild(createLatLine(inModel.getScaledLatitudeLine(i), inModel.getModelSize()));
357                 }
358                 numlines = inModel.getLongitudeLines().length;
359                 for (int i=0; i<numlines; i++)
360                 {
361                         group.addChild(createLonLine(inModel.getScaledLongitudeLine(i), inModel.getModelSize()));
362                 }
363                 return group;
364         }
365
366
367         /**
368          * Make a single latitude line for the specified latitude
369          * @param inLatitude latitude in scaled units
370          * @param inSize size of model, for length of line
371          * @return Group object containing cylinder for latitude line
372          */
373         private static Group createLatLine(double inLatitude, double inSize)
374         {
375                 Cylinder latline = new Cylinder(0.1f, (float) (inSize*2));
376                 Transform3D horizShift = new Transform3D();
377                 horizShift.setTranslation(new Vector3d(0.0, 0.0, inLatitude));
378                 TransformGroup horizTrans = new TransformGroup(horizShift);
379                 Transform3D zRot = new Transform3D();
380                 zRot.rotZ(Math.toRadians(90.0));
381                 TransformGroup zTrans = new TransformGroup(zRot);
382                 horizTrans.addChild(zTrans);
383                 zTrans.addChild(latline);
384                 return horizTrans;
385         }
386
387
388         /**
389          * Make a single longitude line for the specified longitude
390          * @param inLongitude longitude in scaled units
391          * @param inSize size of model, for length of line
392          * @return Group object containing cylinder for longitude line
393          */
394         private static Group createLonLine(double inLongitude, double inSize)
395         {
396                 Cylinder lonline = new Cylinder(0.1f, (float) (inSize*2));
397                 Transform3D horizShift = new Transform3D();
398                 horizShift.setTranslation(new Vector3d(inLongitude, 0.0, 0.0));
399                 TransformGroup horizTrans = new TransformGroup(horizShift);
400                 Transform3D xRot = new Transform3D();
401                 xRot.rotX(Math.toRadians(90.0));
402                 TransformGroup xTrans = new TransformGroup(xRot);
403                 horizTrans.addChild(xTrans);
404                 xTrans.addChild(lonline);
405                 return horizTrans;
406         }
407
408
409         /**
410          * Make a Group of the data points to be added
411          * @param inModel model containing data
412          * @return Group object containing spheres, rods etc
413          */
414         private static Group createDataPoints(ThreeDModel inModel)
415         {
416                 // Add points to model
417                 Group group = new Group();
418                 int numPoints = inModel.getNumPoints();
419                 for (int i=0; i<numPoints; i++)
420                 {
421                         byte pointType = inModel.getPointType(i);
422                         if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
423                         {
424                                 // Add waypoint
425                                 // Note that x, y and z are horiz, altitude, -vert
426                                 group.addChild(createWaypoint(new Point3d(
427                                         inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i))));
428                         }
429                         else
430                         {
431                                 // Add colour-coded track point
432                                 // Note that x, y and z are horiz, altitude, -vert
433                                 group.addChild(createTrackpoint(new Point3d(
434                                         inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i)),
435                                         inModel.getPointHeightCode(i)));
436                         }
437                 }
438                 return group;
439         }
440
441
442         /**
443          * Create a waypoint sphere
444          * @param inPointPos position of point
445          * @return Group object containing sphere
446          */
447         private static Group createWaypoint(Point3d inPointPos)
448         {
449                 Material mat = getWaypointMaterial();
450                 // TODO: sort symbol scaling
451                 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
452                 return createBall(inPointPos, dot, mat);
453         }
454
455
456         /**
457          * @return a new Material object to define waypoint colour / shine etc
458          */
459         private static Material getWaypointMaterial()
460         {
461                 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
462                          new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
463                          new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
464         }
465
466
467         private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
468         {
469                 Material mat = getTrackpointMaterial(inHeightCode);
470                 // TODO: sort symbol scaling
471                 Sphere dot = new Sphere(0.2f); // * symbolScaling / 100f);
472                 return createBall(inPointPos, dot, mat);
473         }
474
475
476         private static Material getTrackpointMaterial(byte inHeightCode)
477         {
478                 // create default material
479                 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
480                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
481                         new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
482                 // change colour according to height code
483                 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
484                 if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
485                 if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.5f, 0.85f, 0.95f));
486                 if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
487                 if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
488                 // return object
489                 return mat;
490         }
491
492
493         /**
494          * Create a ball at the given point
495          * @param inPosition scaled position of point
496          * @param inSphere sphere object
497          * @param inMaterial material object
498          * @return Group containing sphere
499          */
500         private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
501         {
502                 Group group = new Group();
503                 // Create ball and add to group
504                 Transform3D ballShift = new Transform3D();
505                 ballShift.setTranslation(new Vector3d(inPosition));
506                 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
507                 inMaterial.setLightingEnable(true);
508                 Appearance ballApp = new Appearance();
509                 ballApp.setMaterial(inMaterial);
510                 inSphere.setAppearance(ballApp);
511                 ballShiftTrans.addChild(inSphere);
512                 group.addChild(ballShiftTrans);
513                 // Also create rod for ball to sit on
514                 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
515                 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
516                  new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
517                  new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
518                 rodMat.setLightingEnable(true);
519                 Appearance rodApp = new Appearance();
520                 rodApp.setMaterial(rodMat);
521                 rod.setAppearance(rodApp);
522                 Transform3D rodShift = new Transform3D();
523                 rodShift.setTranslation(new Vector3d(inPosition.x,
524                  inPosition.y/2.0, inPosition.z));
525                 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
526                 rodShiftTrans.addChild(rod);
527                 group.addChild(rodShiftTrans);
528                 // return the pair
529                 return group;
530         }
531
532
533         /**
534          * Calculate the angles and call them back to the app
535          */
536         private void callbackRender()
537         {
538                 Transform3D trans3d = new Transform3D();
539                 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
540                 Matrix3d matrix = new Matrix3d();
541                 trans3d.get(matrix);
542                 Point3d point = new Point3d(0.0, 0.0, 1.0);
543                 matrix.transform(point);
544                 // Set up initial rotations
545                 Transform3D firstTran = new Transform3D();
546                 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
547                 Transform3D secondTran = new Transform3D();
548                 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
549                 // Apply inverse rotations in reverse order to test point
550                 Point3d result = new Point3d();
551                 secondTran.transform(point, result);
552                 firstTran.transform(result);
553                 // Callback settings to App
554                 _app.exportPov(result.x, result.y, result.z, _altitudeCap);
555         }
556
557
558         /**
559          * Get a units label for the altitudes in the given Track
560          * @param inTrack Track object
561          * @return units label for altitude used in Track
562          */
563         private static String getAltitudeUnitsLabel(Track inTrack)
564         {
565                 int altitudeFormat = inTrack.getAltitudeRange().getFormat();
566                 if (altitudeFormat == Altitude.FORMAT_METRES)
567                         return I18nManager.getText("units.metres.short");
568                 return I18nManager.getText("units.feet.short");
569         }
570
571 }