]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/threedee/Java3DWindow.java
Version 15, March 2013
[GpsPrune.git] / tim / prune / threedee / Java3DWindow.java
index e3531f08d6acb3f4914e632c6236a0ef2121fd9d..cfac9afabf1798d87e33f199371ed98ba62fb743 100644 (file)
@@ -44,8 +44,8 @@ import com.sun.j3d.utils.universe.SimpleUniverse;
 
 import tim.prune.FunctionLibrary;
 import tim.prune.I18nManager;
-import tim.prune.data.Altitude;
 import tim.prune.data.Track;
+import tim.prune.function.Export3dFunction;
 
 
 /**
@@ -58,7 +58,7 @@ public class Java3DWindow implements ThreeDWindow
        private JFrame _frame = null;
        private ThreeDModel _model = null;
        private OrbitBehavior _orbit = null;
-       private int _altitudeCap = ThreeDModel.MINIMUM_ALTITUDE_CAP;
+       private double _altFactor = 5.0;
 
        /** only prompt about big track size once */
        private static boolean TRACK_SIZE_WARNING_GIVEN = false;
@@ -68,6 +68,7 @@ public class Java3DWindow implements ThreeDWindow
        private static final double INITIAL_X_ROTATION = 15.0;
        private static final String CARDINALS_FONT = "Arial";
        private static final int MAX_TRACK_SIZE = 2500; // threshold for warning
+       private static final double MODEL_SCALE_FACTOR = 20.0;
 
 
        /**
@@ -95,18 +96,17 @@ public class Java3DWindow implements ThreeDWindow
         */
        public void show() throws ThreeDException
        {
-               // Get the altitude cap to use
-               String altitudeUnits = getAltitudeUnitsLabel(_track);
-               Object altCapString = JOptionPane.showInputDialog(_parentFrame,
-                       I18nManager.getText("dialog.3d.altitudecap") + " (" + altitudeUnits + ")",
+               // Get the altitude exaggeration to use
+               Object factorString = JOptionPane.showInputDialog(_parentFrame,
+                       I18nManager.getText("dialog.3d.altitudefactor"),
                        I18nManager.getText("dialog.3d.title"),
-                       JOptionPane.QUESTION_MESSAGE, null, null, "" + _altitudeCap);
-               if (altCapString == null) return;
-               try
-               {
-                       _altitudeCap = Integer.parseInt(altCapString.toString());
+                       JOptionPane.QUESTION_MESSAGE, null, null, _altFactor);
+               if (factorString == null) return;
+               try {
+                       _altFactor = Double.parseDouble(factorString.toString());
                }
                catch (Exception e) {} // Ignore parse errors
+               if (_altFactor < 1.0) {_altFactor = 1.0;}
 
                // Set up the graphics config
                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
@@ -128,17 +128,16 @@ public class Java3DWindow implements ThreeDWindow
                Object[] buttonTexts = {I18nManager.getText("button.continue"), I18nManager.getText("button.cancel")};
                if (_track.getNumPoints() > MAX_TRACK_SIZE && !TRACK_SIZE_WARNING_GIVEN)
                {
-                       if (JOptionPane.showOptionDialog(_frame,
-                                       I18nManager.getText("dialog.exportpov.warningtracksize"),
-                                       I18nManager.getText("function.exportpov"), JOptionPane.OK_CANCEL_OPTION,
+                       if (JOptionPane.showOptionDialog(_parentFrame,
+                                       I18nManager.getText("dialog.3d.warningtracksize"),
+                                       I18nManager.getText("function.show3d"), JOptionPane.OK_CANCEL_OPTION,
                                        JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
                                == JOptionPane.OK_OPTION)
                        {
                                // opted to continue, don't show warning again
                                TRACK_SIZE_WARNING_GIVEN = true;
                        }
-                       else
-                       {
+                       else {
                                // opted to cancel - show warning again next time
                                return;
                        }
@@ -156,8 +155,7 @@ public class Java3DWindow implements ThreeDWindow
                u.getViewingPlatform().setNominalViewingTransform();
 
                // Add behaviour to rotate using mouse
-               _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL |
-                                                                 OrbitBehavior.STOP_ZOOM);
+               _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
                BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
                _orbit.setSchedulingBounds(bounds);
                u.getViewingPlatform().setViewPlatformBehavior(_orbit);
@@ -172,40 +170,35 @@ public class Java3DWindow implements ThreeDWindow
                // Make panel for render, close buttons
                JPanel panel = new JPanel();
                panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
-               // Add callback button for render
-               JButton renderButton = new JButton(I18nManager.getText("function.exportpov"));
-               renderButton.addActionListener(new ActionListener()
-               {
-                       /** Render button pressed */
+               // Add button for exporting pov
+               JButton povButton = new JButton(I18nManager.getText("function.exportpov"));
+               povButton.addActionListener(new ActionListener() {
+                       /** Export pov button pressed */
                        public void actionPerformed(ActionEvent e)
                        {
-                               if (_orbit != null)
-                               {
-                                       callbackRender();
+                               if (_orbit != null) {
+                                       callbackRender(FunctionLibrary.FUNCTION_POVEXPORT);
                                }
                        }});
-               panel.add(renderButton);
-               // Display coordinates of lat/long lines of 3d graph in separate dialog
-               JButton showLinesButton = new JButton(I18nManager.getText("button.showlines"));
-               showLinesButton.addActionListener(new ActionListener() {
+               panel.add(povButton);
+               // Add button for exporting svg
+               JButton svgButton = new JButton(I18nManager.getText("function.exportsvg"));
+               svgButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)
                        {
-                               double[] latLines = _model.getLatitudeLines();
-                               double[] lonLines = _model.getLongitudeLines();
-                               LineDialog dialog = new LineDialog(_frame, latLines, lonLines);
-                               dialog.showDialog();
-                       }
-               });
-               panel.add(showLinesButton);
+                               if (_orbit != null) {
+                                       callbackRender(FunctionLibrary.FUNCTION_SVGEXPORT);
+                               }
+                       }});
+               panel.add(svgButton);
+
                // Close button
                JButton closeButton = new JButton(I18nManager.getText("button.close"));
                closeButton.addActionListener(new ActionListener()
                {
                        /** Close button pressed - clean up */
-                       public void actionPerformed(ActionEvent e)
-                       {
-                               _frame.dispose();
-                               _frame = null;
+                       public void actionPerformed(ActionEvent e) {
+                               dispose();
                                _orbit = null;
                        }
                });
@@ -215,16 +208,14 @@ public class Java3DWindow implements ThreeDWindow
                _frame.pack();
                // Add a listener to clean up when window closed
                _frame.addWindowListener(new WindowAdapter() {
-                       public void windowClosing(WindowEvent e)
-                       {
+                       public void windowClosing(WindowEvent e) {
                                dispose();
                        }
                });
 
                // show frame
                _frame.setVisible(true);
-               if (_frame.getState() == JFrame.ICONIFIED)
-               {
+               if (_frame.getState() == JFrame.ICONIFIED) {
                        _frame.setState(JFrame.NORMAL);
                }
        }
@@ -274,20 +265,22 @@ public class Java3DWindow implements ThreeDWindow
                Box plane = null;
                planeAppearance = new Appearance();
                planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
-                new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
-                new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
+                       new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
+                       new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
                plane = new Box(10f, 0.04f, 10f, planeAppearance);
                objTrans.addChild(plane);
 
                // N, S, E, W
                GeneralPath bevelPath = new GeneralPath();
                bevelPath.moveTo(0.0f, 0.0f);
-               for (int i=0; i<91; i+= 5)
+               for (int i=0; i<91; i+= 5) {
                        bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
                          (float) (0.1 * Math.sin(Math.toRadians(i))));
-               for (int i=90; i>0; i-=5)
+               }
+               for (int i=90; i>0; i-=5) {
                        bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
                          (float) (0.1 * Math.sin(Math.toRadians(i))));
+               }
                Font3D compassFont = new Font3D(
                        new Font(CARDINALS_FONT, Font.PLAIN, 1),
                        new FontExtrusion(bevelPath));
@@ -298,37 +291,30 @@ public class Java3DWindow implements ThreeDWindow
 
                // create and scale model
                _model = new ThreeDModel(_track);
-               _model.setAltitudeCap(_altitudeCap);
+               _model.setAltitudeFactor(_altFactor);
                _model.scale();
 
-               // Lat/Long lines
-               objTrans.addChild(createLatLongs(_model));
-
                // Add points to model
                objTrans.addChild(createDataPoints(_model));
 
                // Create lights
-               BoundingSphere bounds =
-                 new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
+               BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
                AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
                aLgt.setInfluencingBounds(bounds);
                objTrans.addChild(aLgt);
 
                PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
-                new Point3f(0f, 0f, 2f),
-                new Point3f(0.25f, 0.05f, 0.0f) );
+                       new Point3f(0f, 0f, 2f), new Point3f(0.25f, 0.05f, 0.0f) );
                pLgt.setInfluencingBounds(bounds);
                objTrans.addChild(pLgt);
 
                PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
-                new Point3f(6f, 1f, 6f),
-                new Point3f(0.2f, 0.1f, 0.05f) );
+                       new Point3f(6f, 1f, 6f), new Point3f(0.2f, 0.1f, 0.05f) );
                pl2.setInfluencingBounds(bounds);
                objTrans.addChild(pl2);
 
                PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
-                new Point3f(0.0f, 12f, -2f),
-                new Point3f(0.1f, 0.1f, 0.0f) );
+                       new Point3f(0.0f, 12f, -2f), new Point3f(0.1f, 0.1f, 0.0f) );
                pl3.setInfluencingBounds(bounds);
                objTrans.addChild(pl3);
 
@@ -350,8 +336,8 @@ public class Java3DWindow implements ThreeDWindow
        {
                Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
                Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
-                new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
-                new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
+                       new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
+                       new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
                mat.setLightingEnable(true);
                Appearance app = new Appearance();
                app.setMaterial(mat);
@@ -360,70 +346,6 @@ public class Java3DWindow implements ThreeDWindow
        }
 
 
-       /**
-        * Create all the latitude and longitude lines on the base plane
-        * @param inModel model containing data
-        * @return Group object containing cylinders for lat and long lines
-        */
-       private static Group createLatLongs(ThreeDModel inModel)
-       {
-               Group group = new Group();
-               int numlines = inModel.getLatitudeLines().length;
-               for (int i=0; i<numlines; i++)
-               {
-                       group.addChild(createLatLine(inModel.getScaledLatitudeLine(i), inModel.getModelSize()));
-               }
-               numlines = inModel.getLongitudeLines().length;
-               for (int i=0; i<numlines; i++)
-               {
-                       group.addChild(createLonLine(inModel.getScaledLongitudeLine(i), inModel.getModelSize()));
-               }
-               return group;
-       }
-
-
-       /**
-        * Make a single latitude line for the specified latitude
-        * @param inLatitude latitude in scaled units
-        * @param inSize size of model, for length of line
-        * @return Group object containing cylinder for latitude line
-        */
-       private static Group createLatLine(double inLatitude, double inSize)
-       {
-               Cylinder latline = new Cylinder(0.1f, (float) (inSize*2));
-               Transform3D horizShift = new Transform3D();
-               horizShift.setTranslation(new Vector3d(0.0, 0.0, inLatitude));
-               TransformGroup horizTrans = new TransformGroup(horizShift);
-               Transform3D zRot = new Transform3D();
-               zRot.rotZ(Math.toRadians(90.0));
-               TransformGroup zTrans = new TransformGroup(zRot);
-               horizTrans.addChild(zTrans);
-               zTrans.addChild(latline);
-               return horizTrans;
-       }
-
-
-       /**
-        * Make a single longitude line for the specified longitude
-        * @param inLongitude longitude in scaled units
-        * @param inSize size of model, for length of line
-        * @return Group object containing cylinder for longitude line
-        */
-       private static Group createLonLine(double inLongitude, double inSize)
-       {
-               Cylinder lonline = new Cylinder(0.1f, (float) (inSize*2));
-               Transform3D horizShift = new Transform3D();
-               horizShift.setTranslation(new Vector3d(inLongitude, 0.0, 0.0));
-               TransformGroup horizTrans = new TransformGroup(horizShift);
-               Transform3D xRot = new Transform3D();
-               xRot.rotX(Math.toRadians(90.0));
-               TransformGroup xTrans = new TransformGroup(xRot);
-               horizTrans.addChild(xTrans);
-               xTrans.addChild(lonline);
-               return horizTrans;
-       }
-
-
        /**
         * Make a Group of the data points to be added
         * @param inModel model containing data
@@ -442,15 +364,18 @@ public class Java3DWindow implements ThreeDWindow
                                // Add waypoint
                                // Note that x, y and z are horiz, altitude, -vert
                                group.addChild(createWaypoint(new Point3d(
-                                       inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i))));
+                                       inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
+                                       inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
+                                       -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR)));
                        }
                        else
                        {
                                // Add colour-coded track point
                                // Note that x, y and z are horiz, altitude, -vert
                                group.addChild(createTrackpoint(new Point3d(
-                                       inModel.getScaledHorizValue(i), inModel.getScaledAltValue(i), -inModel.getScaledVertValue(i)),
-                                       inModel.getPointHeightCode(i)));
+                                       inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
+                                       inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
+                                       -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR), inModel.getPointHeightCode(i)));
                        }
                }
                return group;
@@ -465,7 +390,7 @@ public class Java3DWindow implements ThreeDWindow
        private static Group createWaypoint(Point3d inPointPos)
        {
                Material mat = getWaypointMaterial();
-               // TODO: sort symbol scaling
+               // MAYBE: sort symbol scaling
                Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
                return createBall(inPointPos, dot, mat);
        }
@@ -482,15 +407,17 @@ public class Java3DWindow implements ThreeDWindow
        }
 
 
+       /** @return track point object */
        private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
        {
                Material mat = getTrackpointMaterial(inHeightCode);
-               // TODO: sort symbol scaling
-               Sphere dot = new Sphere(0.2f); // * symbolScaling / 100f);
+               // MAYBE: sort symbol scaling
+               Sphere dot = new Sphere(0.2f);
                return createBall(inPointPos, dot, mat);
        }
 
 
+       /** @return Material object for track points with the appropriate colour for the height */
        private static Material getTrackpointMaterial(byte inHeightCode)
        {
                // create default material
@@ -499,10 +426,10 @@ public class Java3DWindow implements ThreeDWindow
                        new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
                // change colour according to height code
                if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
-               if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
-               if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.5f, 0.85f, 0.95f));
-               if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
-               if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
+               else if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
+               else if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.3f, 0.6f, 0.4f));
+               else if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
+               else if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
                // return object
                return mat;
        }
@@ -531,15 +458,14 @@ public class Java3DWindow implements ThreeDWindow
                // Also create rod for ball to sit on
                Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
                Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
-                new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
-                new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
+                       new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
+                       new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
                rodMat.setLightingEnable(true);
                Appearance rodApp = new Appearance();
                rodApp.setMaterial(rodMat);
                rod.setAppearance(rodApp);
                Transform3D rodShift = new Transform3D();
-               rodShift.setTranslation(new Vector3d(inPosition.x,
-                inPosition.y/2.0, inPosition.z));
+               rodShift.setTranslation(new Vector3d(inPosition.x, inPosition.y/2.0, inPosition.z));
                TransformGroup rodShiftTrans = new TransformGroup(rodShift);
                rodShiftTrans.addChild(rod);
                group.addChild(rodShiftTrans);
@@ -550,8 +476,9 @@ public class Java3DWindow implements ThreeDWindow
 
        /**
         * Calculate the angles and call them back to the app
+        * @param inFunction function to call (either pov or svg)
         */
-       private void callbackRender()
+       private void callbackRender(Export3dFunction inFunction)
        {
                Transform3D trans3d = new Transform3D();
                _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
@@ -564,28 +491,13 @@ public class Java3DWindow implements ThreeDWindow
                firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
                Transform3D secondTran = new Transform3D();
                secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
-               // Apply inverse rotations in reverse order to test point
+               // Apply inverse rotations in reverse order to the test point
                Point3d result = new Point3d();
                secondTran.transform(point, result);
                firstTran.transform(result);
                // Callback settings to pov export function
-               FunctionLibrary.FUNCTION_POVEXPORT.setCameraCoordinates(result.x, result.y, result.z);
-               FunctionLibrary.FUNCTION_POVEXPORT.setAltitudeCap(_altitudeCap);
-               FunctionLibrary.FUNCTION_POVEXPORT.begin();
-       }
-
-
-       /**
-        * Get a units label for the altitudes in the given Track
-        * @param inTrack Track object
-        * @return units label for altitude used in Track
-        */
-       private static String getAltitudeUnitsLabel(Track inTrack)
-       {
-               Altitude.Format altitudeFormat = inTrack.getAltitudeRange().getFormat();
-               if (altitudeFormat == Altitude.Format.METRES)
-                       return I18nManager.getText("units.metres.short");
-               return I18nManager.getText("units.feet.short");
+               inFunction.setCameraCoordinates(result.x, result.y, result.z);
+               inFunction.setAltitudeExaggeration(_altFactor);
+               inFunction.begin();
        }
-
 }