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.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.awt.geom.GeneralPath;
14 import javax.media.j3d.AmbientLight;
15 import javax.media.j3d.Appearance;
16 import javax.media.j3d.BoundingSphere;
17 import javax.media.j3d.BranchGroup;
18 import javax.media.j3d.Canvas3D;
19 import javax.media.j3d.Font3D;
20 import javax.media.j3d.FontExtrusion;
21 import javax.media.j3d.GraphicsConfigTemplate3D;
22 import javax.media.j3d.Group;
23 import javax.media.j3d.Material;
24 import javax.media.j3d.PointLight;
25 import javax.media.j3d.Shape3D;
26 import javax.media.j3d.Text3D;
27 import javax.media.j3d.Transform3D;
28 import javax.media.j3d.TransformGroup;
29 import javax.swing.JButton;
30 import javax.swing.JFrame;
31 import javax.swing.JOptionPane;
32 import javax.swing.JPanel;
33 import javax.vecmath.Color3f;
34 import javax.vecmath.Matrix3d;
35 import javax.vecmath.Point3d;
36 import javax.vecmath.Point3f;
37 import javax.vecmath.Vector3d;
39 import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
40 import com.sun.j3d.utils.geometry.Box;
41 import com.sun.j3d.utils.geometry.Cylinder;
42 import com.sun.j3d.utils.geometry.Sphere;
43 import com.sun.j3d.utils.universe.SimpleUniverse;
45 import tim.prune.FunctionLibrary;
46 import tim.prune.I18nManager;
47 import tim.prune.data.Track;
48 import tim.prune.function.Export3dFunction;
52 * Class to hold main window for java3d view of data
54 public class Java3DWindow implements ThreeDWindow
56 private Track _track = null;
57 private JFrame _parentFrame = null;
58 private JFrame _frame = null;
59 private ThreeDModel _model = null;
60 private OrbitBehavior _orbit = null;
61 private double _altFactor = 50.0;
63 /** only prompt about big track size once */
64 private static boolean TRACK_SIZE_WARNING_GIVEN = false;
67 private static final double INITIAL_Y_ROTATION = -25.0;
68 private static final double INITIAL_X_ROTATION = 15.0;
69 private static final String CARDINALS_FONT = "Arial";
70 private static final int MAX_TRACK_SIZE = 2500; // threshold for warning
75 * @param inFrame parent frame
77 public Java3DWindow(JFrame inFrame)
79 _parentFrame = inFrame;
84 * Set the track object
85 * @param inTrack Track object
87 public void setTrack(Track inTrack)
96 public void show() throws ThreeDException
98 // Get the altitude exaggeration to use
99 Object factorString = JOptionPane.showInputDialog(_parentFrame,
100 I18nManager.getText("dialog.3d.altitudefactor"),
101 I18nManager.getText("dialog.3d.title"),
102 JOptionPane.QUESTION_MESSAGE, null, null, _altFactor);
103 if (factorString == null) return;
105 _altFactor = Double.parseDouble(factorString.toString());
107 catch (Exception e) {} // Ignore parse errors
108 if (_altFactor < 1.0) {_altFactor = 1.0;}
110 // Set up the graphics config
111 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
114 // Config shouldn't be null, but we can try to create a new one as a workaround
115 GraphicsConfigTemplate3D gc = new GraphicsConfigTemplate3D();
117 config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gc);
122 // Second attempt also failed, going to have to give up here.
123 throw new ThreeDException("Couldn't create graphics config");
126 // Check number of points in model isn't too big, and suggest compression
127 Object[] buttonTexts = {I18nManager.getText("button.continue"), I18nManager.getText("button.cancel")};
128 if (_track.getNumPoints() > MAX_TRACK_SIZE && !TRACK_SIZE_WARNING_GIVEN)
130 if (JOptionPane.showOptionDialog(_frame,
131 I18nManager.getText("dialog.exportpov.warningtracksize"),
132 I18nManager.getText("function.exportpov"), JOptionPane.OK_CANCEL_OPTION,
133 JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
134 == JOptionPane.OK_OPTION)
136 // opted to continue, don't show warning again
137 TRACK_SIZE_WARNING_GIVEN = true;
140 // opted to cancel - show warning again next time
145 Canvas3D canvas = new Canvas3D(config);
146 canvas.setSize(400, 300);
148 // Create the scene and attach it to the virtual universe
149 BranchGroup scene = createSceneGraph();
150 SimpleUniverse u = new SimpleUniverse(canvas);
152 // This will move the ViewPlatform back a bit so the
153 // objects in the scene can be viewed.
154 u.getViewingPlatform().setNominalViewingTransform();
156 // Add behaviour to rotate using mouse
157 _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
158 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
159 _orbit.setSchedulingBounds(bounds);
160 u.getViewingPlatform().setViewPlatformBehavior(_orbit);
161 u.addBranchGraph(scene);
163 // Don't reuse _frame object from last time, because data and/or scale might be different
164 // Need to regenerate everything
165 _frame = new JFrame(I18nManager.getText("dialog.3d.title"));
166 _frame.getContentPane().setLayout(new BorderLayout());
167 _frame.getContentPane().add(canvas, BorderLayout.CENTER);
168 _frame.setIconImage(_parentFrame.getIconImage());
169 // Make panel for render, close buttons
170 JPanel panel = new JPanel();
171 panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
172 // Add button for exporting pov
173 JButton povButton = new JButton(I18nManager.getText("function.exportpov"));
174 povButton.addActionListener(new ActionListener() {
175 /** Export pov button pressed */
176 public void actionPerformed(ActionEvent e)
178 if (_orbit != null) {
179 callbackRender(FunctionLibrary.FUNCTION_POVEXPORT);
182 panel.add(povButton);
183 // Add button for exporting svg
184 JButton svgButton = new JButton(I18nManager.getText("function.exportsvg"));
185 svgButton.addActionListener(new ActionListener() {
186 public void actionPerformed(ActionEvent e)
188 if (_orbit != null) {
189 callbackRender(FunctionLibrary.FUNCTION_SVGEXPORT);
192 panel.add(svgButton);
193 // Display coordinates of lat/long lines of 3d graph in separate dialog
194 JButton showLinesButton = new JButton(I18nManager.getText("button.showlines"));
195 showLinesButton.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent e)
198 double[] latLines = _model.getLatitudeLines();
199 double[] lonLines = _model.getLongitudeLines();
200 LineDialog dialog = new LineDialog(_frame, latLines, lonLines);
204 panel.add(showLinesButton);
206 JButton closeButton = new JButton(I18nManager.getText("button.close"));
207 closeButton.addActionListener(new ActionListener()
209 /** Close button pressed - clean up */
210 public void actionPerformed(ActionEvent e) {
215 panel.add(closeButton);
216 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
217 _frame.setSize(500, 350);
219 // Add a listener to clean up when window closed
220 _frame.addWindowListener(new WindowAdapter() {
221 public void windowClosing(WindowEvent e) {
227 _frame.setVisible(true);
228 if (_frame.getState() == JFrame.ICONIFIED) {
229 _frame.setState(JFrame.NORMAL);
234 * Dispose of the frame and its resources
236 public void dispose()
238 if (_frame != null) {
245 * Create the whole scenery from the given track
246 * @return all objects in the scene
248 private BranchGroup createSceneGraph()
250 // Create the root of the branch graph
251 BranchGroup objRoot = new BranchGroup();
253 // Create the transform group node and initialize it.
254 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
255 TransformGroup objTrans = new TransformGroup();
256 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
258 // Create a translation
259 Transform3D shiftz = new Transform3D();
260 shiftz.setScale(0.055);
261 TransformGroup shiftTrans = new TransformGroup(shiftz);
263 objRoot.addChild(shiftTrans);
264 Transform3D rotTrans = new Transform3D();
265 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
266 Transform3D rot2 = new Transform3D();
267 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
268 TransformGroup tg2 = new TransformGroup(rot2);
269 objTrans.setTransform(rotTrans);
270 shiftTrans.addChild(tg2);
271 tg2.addChild(objTrans);
274 Appearance planeAppearance = null;
276 planeAppearance = new Appearance();
277 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
278 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
279 new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
280 plane = new Box(10f, 0.04f, 10f, planeAppearance);
281 objTrans.addChild(plane);
284 GeneralPath bevelPath = new GeneralPath();
285 bevelPath.moveTo(0.0f, 0.0f);
286 for (int i=0; i<91; i+= 5) {
287 bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
288 (float) (0.1 * Math.sin(Math.toRadians(i))));
290 for (int i=90; i>0; i-=5) {
291 bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
292 (float) (0.1 * Math.sin(Math.toRadians(i))));
294 Font3D compassFont = new Font3D(
295 new Font(CARDINALS_FONT, Font.PLAIN, 1),
296 new FontExtrusion(bevelPath));
297 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -10f), compassFont));
298 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 10f), compassFont));
299 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
300 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
302 // create and scale model
303 _model = new ThreeDModel(_track);
304 _model.setAltitudeFactor(_altFactor);
308 objTrans.addChild(createLatLongs(_model));
310 // Add points to model
311 objTrans.addChild(createDataPoints(_model));
314 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
315 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
316 aLgt.setInfluencingBounds(bounds);
317 objTrans.addChild(aLgt);
319 PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
320 new Point3f(0f, 0f, 2f), new Point3f(0.25f, 0.05f, 0.0f) );
321 pLgt.setInfluencingBounds(bounds);
322 objTrans.addChild(pLgt);
324 PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
325 new Point3f(6f, 1f, 6f), new Point3f(0.2f, 0.1f, 0.05f) );
326 pl2.setInfluencingBounds(bounds);
327 objTrans.addChild(pl2);
329 PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
330 new Point3f(0.0f, 12f, -2f), new Point3f(0.1f, 0.1f, 0.0f) );
331 pl3.setInfluencingBounds(bounds);
332 objTrans.addChild(pl3);
334 // Have Java 3D perform optimizations on this scene graph.
342 * Create a text object for compass point, N S E or W
343 * @param text text to display
344 * @param locn position at which to display
345 * @param font 3d font to use
346 * @return Shape3D object
348 private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
350 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
351 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
352 new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
353 new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
354 mat.setLightingEnable(true);
355 Appearance app = new Appearance();
356 app.setMaterial(mat);
357 Shape3D shape = new Shape3D(txt, app);
363 * Create all the latitude and longitude lines on the base plane
364 * @param inModel model containing data
365 * @return Group object containing cylinders for lat and long lines
367 private static Group createLatLongs(ThreeDModel inModel)
369 Group group = new Group();
370 int numlines = inModel.getLatitudeLines().length;
371 for (int i=0; i<numlines; i++)
373 group.addChild(createLatLine(inModel.getScaledLatitudeLine(i), inModel.getModelSize()));
375 numlines = inModel.getLongitudeLines().length;
376 for (int i=0; i<numlines; i++)
378 group.addChild(createLonLine(inModel.getScaledLongitudeLine(i), inModel.getModelSize()));
385 * Make a single latitude line for the specified latitude
386 * @param inLatitude latitude in scaled units
387 * @param inSize size of model, for length of line
388 * @return Group object containing cylinder for latitude line
390 private static Group createLatLine(double inLatitude, double inSize)
392 Cylinder latline = new Cylinder(0.1f, (float) (inSize*2));
393 Transform3D horizShift = new Transform3D();
394 horizShift.setTranslation(new Vector3d(0.0, 0.0, -inLatitude));
395 TransformGroup horizTrans = new TransformGroup(horizShift);
396 Transform3D zRot = new Transform3D();
397 zRot.rotZ(Math.toRadians(90.0));
398 TransformGroup zTrans = new TransformGroup(zRot);
399 horizTrans.addChild(zTrans);
400 zTrans.addChild(latline);
406 * Make a single longitude line for the specified longitude
407 * @param inLongitude longitude in scaled units
408 * @param inSize size of model, for length of line
409 * @return Group object containing cylinder for longitude line
411 private static Group createLonLine(double inLongitude, double inSize)
413 Cylinder lonline = new Cylinder(0.1f, (float) (inSize*2));
414 Transform3D horizShift = new Transform3D();
415 horizShift.setTranslation(new Vector3d(inLongitude, 0.0, 0.0));
416 TransformGroup horizTrans = new TransformGroup(horizShift);
417 Transform3D xRot = new Transform3D();
418 xRot.rotX(Math.toRadians(90.0));
419 TransformGroup xTrans = new TransformGroup(xRot);
420 horizTrans.addChild(xTrans);
421 xTrans.addChild(lonline);
427 * Make a Group of the data points to be added
428 * @param inModel model containing data
429 * @return Group object containing spheres, rods etc
431 private static Group createDataPoints(ThreeDModel inModel)
433 // Add points to model
434 Group group = new Group();
435 int numPoints = inModel.getNumPoints();
436 for (int i=0; i<numPoints; i++)
438 byte pointType = inModel.getPointType(i);
439 if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
442 // Note that x, y and z are horiz, altitude, -vert
443 group.addChild(createWaypoint(new Point3d(
444 inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i))));
448 // Add colour-coded track point
449 // Note that x, y and z are horiz, altitude, -vert
450 group.addChild(createTrackpoint(new Point3d(
451 inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i)),
452 inModel.getPointHeightCode(i)));
460 * Create a waypoint sphere
461 * @param inPointPos position of point
462 * @return Group object containing sphere
464 private static Group createWaypoint(Point3d inPointPos)
466 Material mat = getWaypointMaterial();
467 // MAYBE: sort symbol scaling
468 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
469 return createBall(inPointPos, dot, mat);
474 * @return a new Material object to define waypoint colour / shine etc
476 private static Material getWaypointMaterial()
478 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
479 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
480 new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
484 /** @return track point object */
485 private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
487 Material mat = getTrackpointMaterial(inHeightCode);
488 // MAYBE: sort symbol scaling
489 Sphere dot = new Sphere(0.2f);
490 return createBall(inPointPos, dot, mat);
494 /** @return Material object for track points with the appropriate colour for the height */
495 private static Material getTrackpointMaterial(byte inHeightCode)
497 // create default material
498 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
499 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
500 new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
501 // change colour according to height code
502 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
503 else if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
504 else if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.3f, 0.6f, 0.4f));
505 else if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
506 else if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
513 * Create a ball at the given point
514 * @param inPosition scaled position of point
515 * @param inSphere sphere object
516 * @param inMaterial material object
517 * @return Group containing sphere
519 private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
521 Group group = new Group();
522 // Create ball and add to group
523 Transform3D ballShift = new Transform3D();
524 ballShift.setTranslation(new Vector3d(inPosition));
525 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
526 inMaterial.setLightingEnable(true);
527 Appearance ballApp = new Appearance();
528 ballApp.setMaterial(inMaterial);
529 inSphere.setAppearance(ballApp);
530 ballShiftTrans.addChild(inSphere);
531 group.addChild(ballShiftTrans);
532 // Also create rod for ball to sit on
533 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
534 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
535 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
536 new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
537 rodMat.setLightingEnable(true);
538 Appearance rodApp = new Appearance();
539 rodApp.setMaterial(rodMat);
540 rod.setAppearance(rodApp);
541 Transform3D rodShift = new Transform3D();
542 rodShift.setTranslation(new Vector3d(inPosition.x, inPosition.y/2.0, inPosition.z));
543 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
544 rodShiftTrans.addChild(rod);
545 group.addChild(rodShiftTrans);
552 * Calculate the angles and call them back to the app
553 * @param inFunction function to call (either pov or svg)
555 private void callbackRender(Export3dFunction inFunction)
557 Transform3D trans3d = new Transform3D();
558 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
559 Matrix3d matrix = new Matrix3d();
561 Point3d point = new Point3d(0.0, 0.0, 1.0);
562 matrix.transform(point);
563 // Set up initial rotations
564 Transform3D firstTran = new Transform3D();
565 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
566 Transform3D secondTran = new Transform3D();
567 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
568 // Apply inverse rotations in reverse order to the test point
569 Point3d result = new Point3d();
570 secondTran.transform(point, result);
571 firstTran.transform(result);
572 // Callback settings to pov export function
573 inFunction.setCameraCoordinates(result.x, result.y, result.z);
574 inFunction.setAltitudeExaggeration(_altFactor);