1 package tim.prune.threedee;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
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 OrbitBehavior _orbit = null;
59 private int _altitudeCap = ThreeDModel.MINIMUM_ALTITUDE_CAP;
61 /** only prompt about big track size once */
62 private static boolean TRACK_SIZE_WARNING_GIVEN = false;
65 private static final double INITIAL_Y_ROTATION = -25.0;
66 private static final double INITIAL_X_ROTATION = 15.0;
67 private static final int INITIAL_ALTITUDE_CAP = 500;
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 JButton closeButton = new JButton(I18nManager.getText("button.close"));
189 closeButton.addActionListener(new ActionListener()
191 /** Close button pressed - clean up */
192 public void actionPerformed(ActionEvent e)
198 panel.add(closeButton);
199 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
200 _frame.setSize(500, 350);
205 if (_frame.getState() == JFrame.ICONIFIED)
207 _frame.setState(JFrame.NORMAL);
213 * Create the whole scenery from the given track
214 * @return all objects in the scene
216 private BranchGroup createSceneGraph()
218 // Create the root of the branch graph
219 BranchGroup objRoot = new BranchGroup();
221 // Create the transform group node and initialize it.
222 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
223 TransformGroup objTrans = new TransformGroup();
224 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
226 // Create a translation
227 Transform3D shiftz = new Transform3D();
228 shiftz.setScale(0.055);
229 TransformGroup shiftTrans = new TransformGroup(shiftz);
231 objRoot.addChild(shiftTrans);
232 Transform3D rotTrans = new Transform3D();
233 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
234 Transform3D rot2 = new Transform3D();
235 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
236 TransformGroup tg2 = new TransformGroup(rot2);
237 objTrans.setTransform(rotTrans);
238 shiftTrans.addChild(tg2);
239 tg2.addChild(objTrans);
242 Appearance planeAppearance = null;
244 Transform3D planeShift = null;
245 TransformGroup planeTrans = null;
246 planeAppearance = new Appearance();
247 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
248 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
249 new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
250 plane = new Box(10f, 0.04f, 10f, planeAppearance);
251 objTrans.addChild(plane);
254 GeneralPath bevelPath = new GeneralPath();
255 bevelPath.moveTo(0.0f, 0.0f);
256 for (int i=0; i<91; i+= 5)
257 bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
258 (float) (0.1 * Math.sin(Math.toRadians(i))));
259 for (int i=90; i>0; i-=5)
260 bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
261 (float) (0.1 * Math.sin(Math.toRadians(i))));
262 Font3D compassFont = new Font3D(
263 new Font(CARDINALS_FONT, Font.PLAIN, 1),
264 new FontExtrusion(bevelPath));
265 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -10f), compassFont));
266 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 10f), compassFont));
267 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
268 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
270 // create and scale model
271 ThreeDModel model = new ThreeDModel(_track);
272 model.setAltitudeCap(_altitudeCap);
276 objTrans.addChild(createLatLongs(model));
278 // Add points to model
279 objTrans.addChild(createDataPoints(model));
282 BoundingSphere bounds =
283 new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
284 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
285 aLgt.setInfluencingBounds(bounds);
286 objTrans.addChild(aLgt);
288 PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
289 new Point3f(0f, 0f, 2f),
290 new Point3f(0.25f, 0.05f, 0.0f) );
291 pLgt.setInfluencingBounds(bounds);
292 objTrans.addChild(pLgt);
294 PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
295 new Point3f(6f, 1f, 6f),
296 new Point3f(0.2f, 0.1f, 0.05f) );
297 pl2.setInfluencingBounds(bounds);
298 objTrans.addChild(pl2);
300 PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
301 new Point3f(0.0f, 12f, -2f),
302 new Point3f(0.1f, 0.1f, 0.0f) );
303 pl3.setInfluencingBounds(bounds);
304 objTrans.addChild(pl3);
306 // Have Java 3D perform optimizations on this scene graph.
314 * Create a text object for compass point, N S E or W
315 * @param text text to display
316 * @param locn position at which to display
317 * @param font 3d font to use
318 * @return Shape3D object
320 private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
322 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
323 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
324 new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
325 new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
326 mat.setLightingEnable(true);
327 Appearance app = new Appearance();
328 app.setMaterial(mat);
329 Shape3D shape = new Shape3D(txt, app);
335 * Create all the latitude and longitude lines on the base plane
336 * @param inModel model containing data
337 * @return Group object containing cylinders for lat and long lines
339 private static Group createLatLongs(ThreeDModel inModel)
341 Group group = new Group();
342 int numlines = inModel.getNumLatitudeLines();
343 for (int i=0; i<numlines; i++)
345 group.addChild(createLatLine(inModel.getScaledLatitudeLine(i), inModel.getModelSize()));
347 numlines = inModel.getNumLongitudeLines();
348 for (int i=0; i<numlines; i++)
350 group.addChild(createLonLine(inModel.getScaledLongitudeLine(i), inModel.getModelSize()));
357 * Make a single latitude line for the specified latitude
358 * @param inLatitude latitude in scaled units
359 * @param inSize size of model, for length of line
360 * @return Group object containing cylinder for latitude line
362 private static Group createLatLine(double inLatitude, double inSize)
364 Cylinder latline = new Cylinder(0.1f, (float) (inSize*2));
365 Transform3D horizShift = new Transform3D();
366 horizShift.setTranslation(new Vector3d(0.0, 0.0, inLatitude));
367 TransformGroup horizTrans = new TransformGroup(horizShift);
368 Transform3D zRot = new Transform3D();
369 zRot.rotZ(Math.toRadians(90.0));
370 TransformGroup zTrans = new TransformGroup(zRot);
371 horizTrans.addChild(zTrans);
372 zTrans.addChild(latline);
378 * Make a single longitude line for the specified longitude
379 * @param inLongitude longitude in scaled units
380 * @param inSize size of model, for length of line
381 * @return Group object containing cylinder for longitude line
383 private static Group createLonLine(double inLongitude, double inSize)
385 Cylinder lonline = new Cylinder(0.1f, (float) (inSize*2));
386 Transform3D horizShift = new Transform3D();
387 horizShift.setTranslation(new Vector3d(inLongitude, 0.0, 0.0));
388 TransformGroup horizTrans = new TransformGroup(horizShift);
389 Transform3D xRot = new Transform3D();
390 xRot.rotX(Math.toRadians(90.0));
391 TransformGroup xTrans = new TransformGroup(xRot);
392 horizTrans.addChild(xTrans);
393 xTrans.addChild(lonline);
399 * Make a Group of the data points to be added
400 * @param inModel model containing data
401 * @return Group object containing spheres, rods etc
403 private static Group createDataPoints(ThreeDModel inModel)
405 // Add points to model
406 Group group = new Group();
407 int numPoints = inModel.getNumPoints();
408 for (int i=0; i<numPoints; i++)
410 byte pointType = inModel.getPointType(i);
411 if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
414 // Note that x, y and z are horiz, altitude, -vert
415 group.addChild(createWaypoint(new Point3d(
416 inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i))));
420 // Add colour-coded track point
421 // Note that x, y and z are horiz, altitude, -vert
422 group.addChild(createTrackpoint(new Point3d(
423 inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i)),
424 inModel.getPointHeightCode(i)));
432 * Create a waypoint sphere
433 * @param inPointPos position of point
434 * @return Group object containing sphere
436 private static Group createWaypoint(Point3d inPointPos)
438 Material mat = getWaypointMaterial();
439 // TODO: sort symbol scaling
440 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
441 return createBall(inPointPos, dot, mat);
446 * @return a new Material object to define waypoint colour / shine etc
448 private static Material getWaypointMaterial()
450 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
451 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
452 new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
456 private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
458 Material mat = getTrackpointMaterial(inHeightCode);
459 // TODO: sort symbol scaling
460 Sphere dot = new Sphere(0.2f); // * symbolScaling / 100f);
461 return createBall(inPointPos, dot, mat);
465 private static Material getTrackpointMaterial(byte inHeightCode)
467 // create default material
468 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
469 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
470 new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
471 // change colour according to height code
472 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
473 if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
474 if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.5f, 0.85f, 0.95f));
475 if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
476 if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
483 * Create a ball at the given point
484 * @param inPosition scaled position of point
485 * @param inSphere sphere object
486 * @param inMaterial material object
487 * @return Group containing sphere
489 private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
491 Group group = new Group();
492 // Create ball and add to group
493 Transform3D ballShift = new Transform3D();
494 ballShift.setTranslation(new Vector3d(inPosition));
495 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
496 inMaterial.setLightingEnable(true);
497 Appearance ballApp = new Appearance();
498 ballApp.setMaterial(inMaterial);
499 inSphere.setAppearance(ballApp);
500 ballShiftTrans.addChild(inSphere);
501 group.addChild(ballShiftTrans);
502 // Also create rod for ball to sit on
503 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
504 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
505 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
506 new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
507 rodMat.setLightingEnable(true);
508 Appearance rodApp = new Appearance();
509 rodApp.setMaterial(rodMat);
510 rod.setAppearance(rodApp);
511 Transform3D rodShift = new Transform3D();
512 rodShift.setTranslation(new Vector3d(inPosition.x,
513 inPosition.y/2.0, inPosition.z));
514 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
515 rodShiftTrans.addChild(rod);
516 group.addChild(rodShiftTrans);
523 * Calculate the angles and call them back to the app
525 private void callbackRender()
527 Transform3D trans3d = new Transform3D();
528 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
529 Matrix3d matrix = new Matrix3d();
531 Point3d point = new Point3d(0.0, 0.0, 1.0);
532 matrix.transform(point);
533 // Set up initial rotations
534 Transform3D firstTran = new Transform3D();
535 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
536 Transform3D secondTran = new Transform3D();
537 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
538 // Apply inverse rotations in reverse order to test point
539 Point3d result = new Point3d();
540 secondTran.transform(point, result);
541 firstTran.transform(result);
542 // Callback settings to App
543 _app.exportPov(result.x, result.y, result.z, _altitudeCap);
548 * Get a units label for the altitudes in the given Track
549 * @param inTrack Track object
550 * @return units label for altitude used in Track
552 private static String getAltitudeUnitsLabel(Track inTrack)
554 int altitudeFormat = inTrack.getAltitudeRange().getFormat();
555 if (altitudeFormat == Altitude.FORMAT_METRES)
556 return I18nManager.getText("units.metres.short");
557 return I18nManager.getText("units.feet.short");