1 package tim.prune.threedee;
3 import java.awt.FlowLayout;
4 import java.awt.BorderLayout;
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;
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;
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;
44 import tim.prune.I18nManager;
45 import tim.prune.data.Altitude;
46 import tim.prune.data.Track;
50 * Class to hold main window for java3d view of data
52 public class Java3DWindow implements ThreeDWindow
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;
62 /** only prompt about big track size once */
63 private static boolean TRACK_SIZE_WARNING_GIVEN = false;
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
74 * @param inApp App object to use for callbacks
75 * @param inFrame parent frame
77 public Java3DWindow(App inApp, JFrame inFrame)
80 _parentFrame = inFrame;
85 * Set the track object
86 * @param inTrack Track object
88 public void setTrack(Track inTrack)
97 public void show() throws ThreeDException
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;
108 _altitudeCap = Integer.parseInt(altCapString.toString());
110 catch (Exception e) {} // Ignore parse errors
112 // Set up the graphics config
113 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
116 // Config shouldn't be null, but we can try to create a new one as a workaround
117 GraphicsConfigTemplate3D gc = new GraphicsConfigTemplate3D();
119 config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gc);
124 // Second attempt also failed, going to have to give up here.
125 throw new ThreeDException("Couldn't create graphics config");
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)
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)
138 // opted to continue, don't show warning again
139 TRACK_SIZE_WARNING_GIVEN = true;
143 // opted to cancel - show warning again next time
148 Canvas3D canvas = new Canvas3D(config);
149 canvas.setSize(400, 300);
151 // Create the scene and attach it to the virtual universe
152 BranchGroup scene = createSceneGraph();
153 SimpleUniverse u = new SimpleUniverse(canvas);
155 // This will move the ViewPlatform back a bit so the
156 // objects in the scene can be viewed.
157 u.getViewingPlatform().setNominalViewingTransform();
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);
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()
179 /** Render button pressed */
180 public void actionPerformed(ActionEvent e)
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)
193 double[] latLines = _model.getLatitudeLines();
194 double[] lonLines = _model.getLongitudeLines();
195 LineDialog dialog = new LineDialog(_frame, latLines, lonLines);
199 panel.add(showLinesButton);
201 JButton closeButton = new JButton(I18nManager.getText("button.close"));
202 closeButton.addActionListener(new ActionListener()
204 /** Close button pressed - clean up */
205 public void actionPerformed(ActionEvent e)
211 panel.add(closeButton);
212 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
213 _frame.setSize(500, 350);
218 if (_frame.getState() == JFrame.ICONIFIED)
220 _frame.setState(JFrame.NORMAL);
226 * Create the whole scenery from the given track
227 * @return all objects in the scene
229 private BranchGroup createSceneGraph()
231 // Create the root of the branch graph
232 BranchGroup objRoot = new BranchGroup();
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);
239 // Create a translation
240 Transform3D shiftz = new Transform3D();
241 shiftz.setScale(0.055);
242 TransformGroup shiftTrans = new TransformGroup(shiftz);
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);
255 Appearance planeAppearance = 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);
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));
281 // create and scale model
282 _model = new ThreeDModel(_track);
283 _model.setAltitudeCap(_altitudeCap);
287 objTrans.addChild(createLatLongs(_model));
289 // Add points to model
290 objTrans.addChild(createDataPoints(_model));
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);
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);
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);
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);
317 // Have Java 3D perform optimizations on this scene graph.
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
331 private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
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);
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
350 private static Group createLatLongs(ThreeDModel inModel)
352 Group group = new Group();
353 int numlines = inModel.getLatitudeLines().length;
354 for (int i=0; i<numlines; i++)
356 group.addChild(createLatLine(inModel.getScaledLatitudeLine(i), inModel.getModelSize()));
358 numlines = inModel.getLongitudeLines().length;
359 for (int i=0; i<numlines; i++)
361 group.addChild(createLonLine(inModel.getScaledLongitudeLine(i), inModel.getModelSize()));
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
373 private static Group createLatLine(double inLatitude, double inSize)
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);
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
394 private static Group createLonLine(double inLongitude, double inSize)
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);
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
414 private static Group createDataPoints(ThreeDModel inModel)
416 // Add points to model
417 Group group = new Group();
418 int numPoints = inModel.getNumPoints();
419 for (int i=0; i<numPoints; i++)
421 byte pointType = inModel.getPointType(i);
422 if (pointType == ThreeDModel.POINT_TYPE_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))));
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)));
443 * Create a waypoint sphere
444 * @param inPointPos position of point
445 * @return Group object containing sphere
447 private static Group createWaypoint(Point3d inPointPos)
449 Material mat = getWaypointMaterial();
450 // TODO: sort symbol scaling
451 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
452 return createBall(inPointPos, dot, mat);
457 * @return a new Material object to define waypoint colour / shine etc
459 private static Material getWaypointMaterial()
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);
467 private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
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);
476 private static Material getTrackpointMaterial(byte inHeightCode)
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));
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
500 private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
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);
534 * Calculate the angles and call them back to the app
536 private void callbackRender()
538 Transform3D trans3d = new Transform3D();
539 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
540 Matrix3d matrix = new Matrix3d();
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);
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
563 private static String getAltitudeUnitsLabel(Track inTrack)
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");