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 = 5.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
71 private static final double MODEL_SCALE_FACTOR = 20.0;
76 * @param inFrame parent frame
78 public Java3DWindow(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 exaggeration to use
100 Object factorString = JOptionPane.showInputDialog(_parentFrame,
101 I18nManager.getText("dialog.3d.altitudefactor"),
102 I18nManager.getText("dialog.3d.title"),
103 JOptionPane.QUESTION_MESSAGE, null, null, _altFactor);
104 if (factorString == null) return;
106 _altFactor = Double.parseDouble(factorString.toString());
108 catch (Exception e) {} // Ignore parse errors
109 if (_altFactor < 1.0) {_altFactor = 1.0;}
111 // Set up the graphics config
112 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
115 // Config shouldn't be null, but we can try to create a new one as a workaround
116 GraphicsConfigTemplate3D gc = new GraphicsConfigTemplate3D();
118 config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gc);
123 // Second attempt also failed, going to have to give up here.
124 throw new ThreeDException("Couldn't create graphics config");
127 // Check number of points in model isn't too big, and suggest compression
128 Object[] buttonTexts = {I18nManager.getText("button.continue"), I18nManager.getText("button.cancel")};
129 if (_track.getNumPoints() > MAX_TRACK_SIZE && !TRACK_SIZE_WARNING_GIVEN)
131 if (JOptionPane.showOptionDialog(_parentFrame,
132 I18nManager.getText("dialog.3d.warningtracksize"),
133 I18nManager.getText("function.show3d"), JOptionPane.OK_CANCEL_OPTION,
134 JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
135 == JOptionPane.OK_OPTION)
137 // opted to continue, don't show warning again
138 TRACK_SIZE_WARNING_GIVEN = true;
141 // opted to cancel - show warning again next time
146 Canvas3D canvas = new Canvas3D(config);
147 canvas.setSize(400, 300);
149 // Create the scene and attach it to the virtual universe
150 BranchGroup scene = createSceneGraph();
151 SimpleUniverse u = new SimpleUniverse(canvas);
153 // This will move the ViewPlatform back a bit so the
154 // objects in the scene can be viewed.
155 u.getViewingPlatform().setNominalViewingTransform();
157 // Add behaviour to rotate using mouse
158 _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
159 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
160 _orbit.setSchedulingBounds(bounds);
161 u.getViewingPlatform().setViewPlatformBehavior(_orbit);
162 u.addBranchGraph(scene);
164 // Don't reuse _frame object from last time, because data and/or scale might be different
165 // Need to regenerate everything
166 _frame = new JFrame(I18nManager.getText("dialog.3d.title"));
167 _frame.getContentPane().setLayout(new BorderLayout());
168 _frame.getContentPane().add(canvas, BorderLayout.CENTER);
169 _frame.setIconImage(_parentFrame.getIconImage());
170 // Make panel for render, close buttons
171 JPanel panel = new JPanel();
172 panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
173 // Add button for exporting pov
174 JButton povButton = new JButton(I18nManager.getText("function.exportpov"));
175 povButton.addActionListener(new ActionListener() {
176 /** Export pov button pressed */
177 public void actionPerformed(ActionEvent e)
179 if (_orbit != null) {
180 callbackRender(FunctionLibrary.FUNCTION_POVEXPORT);
183 panel.add(povButton);
184 // Add button for exporting svg
185 JButton svgButton = new JButton(I18nManager.getText("function.exportsvg"));
186 svgButton.addActionListener(new ActionListener() {
187 public void actionPerformed(ActionEvent e)
189 if (_orbit != null) {
190 callbackRender(FunctionLibrary.FUNCTION_SVGEXPORT);
193 panel.add(svgButton);
196 JButton closeButton = new JButton(I18nManager.getText("button.close"));
197 closeButton.addActionListener(new ActionListener()
199 /** Close button pressed - clean up */
200 public void actionPerformed(ActionEvent e) {
205 panel.add(closeButton);
206 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
207 _frame.setSize(500, 350);
209 // Add a listener to clean up when window closed
210 _frame.addWindowListener(new WindowAdapter() {
211 public void windowClosing(WindowEvent e) {
217 _frame.setVisible(true);
218 if (_frame.getState() == JFrame.ICONIFIED) {
219 _frame.setState(JFrame.NORMAL);
224 * Dispose of the frame and its resources
226 public void dispose()
228 if (_frame != null) {
235 * Create the whole scenery from the given track
236 * @return all objects in the scene
238 private BranchGroup createSceneGraph()
240 // Create the root of the branch graph
241 BranchGroup objRoot = new BranchGroup();
243 // Create the transform group node and initialize it.
244 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
245 TransformGroup objTrans = new TransformGroup();
246 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
248 // Create a translation
249 Transform3D shiftz = new Transform3D();
250 shiftz.setScale(0.055);
251 TransformGroup shiftTrans = new TransformGroup(shiftz);
253 objRoot.addChild(shiftTrans);
254 Transform3D rotTrans = new Transform3D();
255 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
256 Transform3D rot2 = new Transform3D();
257 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
258 TransformGroup tg2 = new TransformGroup(rot2);
259 objTrans.setTransform(rotTrans);
260 shiftTrans.addChild(tg2);
261 tg2.addChild(objTrans);
264 Appearance planeAppearance = null;
266 planeAppearance = new Appearance();
267 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
268 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
269 new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
270 plane = new Box(10f, 0.04f, 10f, planeAppearance);
271 objTrans.addChild(plane);
274 GeneralPath bevelPath = new GeneralPath();
275 bevelPath.moveTo(0.0f, 0.0f);
276 for (int i=0; i<91; i+= 5) {
277 bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
278 (float) (0.1 * Math.sin(Math.toRadians(i))));
280 for (int i=90; i>0; i-=5) {
281 bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
282 (float) (0.1 * Math.sin(Math.toRadians(i))));
284 Font3D compassFont = new Font3D(
285 new Font(CARDINALS_FONT, Font.PLAIN, 1),
286 new FontExtrusion(bevelPath));
287 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -10f), compassFont));
288 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 10f), compassFont));
289 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
290 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
292 // create and scale model
293 _model = new ThreeDModel(_track);
294 _model.setAltitudeFactor(_altFactor);
297 // Add points to model
298 objTrans.addChild(createDataPoints(_model));
301 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
302 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
303 aLgt.setInfluencingBounds(bounds);
304 objTrans.addChild(aLgt);
306 PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
307 new Point3f(0f, 0f, 2f), new Point3f(0.25f, 0.05f, 0.0f) );
308 pLgt.setInfluencingBounds(bounds);
309 objTrans.addChild(pLgt);
311 PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
312 new Point3f(6f, 1f, 6f), new Point3f(0.2f, 0.1f, 0.05f) );
313 pl2.setInfluencingBounds(bounds);
314 objTrans.addChild(pl2);
316 PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
317 new Point3f(0.0f, 12f, -2f), new Point3f(0.1f, 0.1f, 0.0f) );
318 pl3.setInfluencingBounds(bounds);
319 objTrans.addChild(pl3);
321 // Have Java 3D perform optimizations on this scene graph.
329 * Create a text object for compass point, N S E or W
330 * @param text text to display
331 * @param locn position at which to display
332 * @param font 3d font to use
333 * @return Shape3D object
335 private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
337 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
338 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
339 new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
340 new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
341 mat.setLightingEnable(true);
342 Appearance app = new Appearance();
343 app.setMaterial(mat);
344 Shape3D shape = new Shape3D(txt, app);
350 * Make a Group of the data points to be added
351 * @param inModel model containing data
352 * @return Group object containing spheres, rods etc
354 private static Group createDataPoints(ThreeDModel inModel)
356 // Add points to model
357 Group group = new Group();
358 int numPoints = inModel.getNumPoints();
359 for (int i=0; i<numPoints; i++)
361 byte pointType = inModel.getPointType(i);
362 if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
365 // Note that x, y and z are horiz, altitude, -vert
366 group.addChild(createWaypoint(new Point3d(
367 inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
368 inModel.getScaledAltValue(i) * MODEL_SCALE_FACTOR,
369 -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR)));
373 // Add colour-coded track point
374 // Note that x, y and z are horiz, altitude, -vert
375 group.addChild(createTrackpoint(new Point3d(
376 inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
377 inModel.getScaledAltValue(i) * MODEL_SCALE_FACTOR,
378 -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR), inModel.getPointHeightCode(i)));
386 * Create a waypoint sphere
387 * @param inPointPos position of point
388 * @return Group object containing sphere
390 private static Group createWaypoint(Point3d inPointPos)
392 Material mat = getWaypointMaterial();
393 // MAYBE: sort symbol scaling
394 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
395 return createBall(inPointPos, dot, mat);
400 * @return a new Material object to define waypoint colour / shine etc
402 private static Material getWaypointMaterial()
404 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
405 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
406 new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
410 /** @return track point object */
411 private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
413 Material mat = getTrackpointMaterial(inHeightCode);
414 // MAYBE: sort symbol scaling
415 Sphere dot = new Sphere(0.2f);
416 return createBall(inPointPos, dot, mat);
420 /** @return Material object for track points with the appropriate colour for the height */
421 private static Material getTrackpointMaterial(byte inHeightCode)
423 // create default material
424 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
425 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
426 new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
427 // change colour according to height code
428 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
429 else if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
430 else if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.3f, 0.6f, 0.4f));
431 else if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
432 else if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
439 * Create a ball at the given point
440 * @param inPosition scaled position of point
441 * @param inSphere sphere object
442 * @param inMaterial material object
443 * @return Group containing sphere
445 private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
447 Group group = new Group();
448 // Create ball and add to group
449 Transform3D ballShift = new Transform3D();
450 ballShift.setTranslation(new Vector3d(inPosition));
451 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
452 inMaterial.setLightingEnable(true);
453 Appearance ballApp = new Appearance();
454 ballApp.setMaterial(inMaterial);
455 inSphere.setAppearance(ballApp);
456 ballShiftTrans.addChild(inSphere);
457 group.addChild(ballShiftTrans);
458 // Also create rod for ball to sit on
459 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
460 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
461 new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
462 new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
463 rodMat.setLightingEnable(true);
464 Appearance rodApp = new Appearance();
465 rodApp.setMaterial(rodMat);
466 rod.setAppearance(rodApp);
467 Transform3D rodShift = new Transform3D();
468 rodShift.setTranslation(new Vector3d(inPosition.x, inPosition.y/2.0, inPosition.z));
469 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
470 rodShiftTrans.addChild(rod);
471 group.addChild(rodShiftTrans);
478 * Calculate the angles and call them back to the app
479 * @param inFunction function to call (either pov or svg)
481 private void callbackRender(Export3dFunction inFunction)
483 Transform3D trans3d = new Transform3D();
484 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
485 Matrix3d matrix = new Matrix3d();
487 Point3d point = new Point3d(0.0, 0.0, 1.0);
488 matrix.transform(point);
489 // Set up initial rotations
490 Transform3D firstTran = new Transform3D();
491 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
492 Transform3D secondTran = new Transform3D();
493 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
494 // Apply inverse rotations in reverse order to the test point
495 Point3d result = new Point3d();
496 secondTran.transform(point, result);
497 firstTran.transform(result);
498 // Callback settings to pov export function
499 inFunction.setCameraCoordinates(result.x, result.y, result.z);
500 inFunction.setAltitudeExaggeration(_altFactor);