]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/threedee/Java3DWindow.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / threedee / Java3DWindow.java
index cfac9afabf1798d87e33f199371ed98ba62fb743..579245db62eb2d07a08d005529f234ea13fa4bb4 100644 (file)
@@ -1,7 +1,7 @@
 package tim.prune.threedee;
 
-import java.awt.FlowLayout;
 import java.awt.BorderLayout;
+import java.awt.FlowLayout;
 import java.awt.Font;
 import java.awt.GraphicsConfiguration;
 import java.awt.GraphicsEnvironment;
@@ -18,12 +18,16 @@ import javax.media.j3d.BranchGroup;
 import javax.media.j3d.Canvas3D;
 import javax.media.j3d.Font3D;
 import javax.media.j3d.FontExtrusion;
+import javax.media.j3d.GeometryArray;
 import javax.media.j3d.GraphicsConfigTemplate3D;
 import javax.media.j3d.Group;
 import javax.media.j3d.Material;
 import javax.media.j3d.PointLight;
+import javax.media.j3d.QuadArray;
 import javax.media.j3d.Shape3D;
 import javax.media.j3d.Text3D;
+import javax.media.j3d.Texture;
+import javax.media.j3d.TextureAttributes;
 import javax.media.j3d.Transform3D;
 import javax.media.j3d.TransformGroup;
 import javax.swing.JButton;
@@ -34,19 +38,28 @@ import javax.vecmath.Color3f;
 import javax.vecmath.Matrix3d;
 import javax.vecmath.Point3d;
 import javax.vecmath.Point3f;
+import javax.vecmath.TexCoord2f;
 import javax.vecmath.Vector3d;
 
+import tim.prune.DataStatus;
+import tim.prune.FunctionLibrary;
+import tim.prune.I18nManager;
+import tim.prune.data.Track;
+import tim.prune.function.Export3dFunction;
+import tim.prune.function.srtm.LookupSrtmFunction;
+import tim.prune.gui.map.MapSourceLibrary;
+import tim.prune.save.GroutedImage;
+import tim.prune.save.MapGrouter;
+
 import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
 import com.sun.j3d.utils.geometry.Box;
 import com.sun.j3d.utils.geometry.Cylinder;
+import com.sun.j3d.utils.geometry.GeometryInfo;
+import com.sun.j3d.utils.geometry.NormalGenerator;
 import com.sun.j3d.utils.geometry.Sphere;
+import com.sun.j3d.utils.image.TextureLoader;
 import com.sun.j3d.utils.universe.SimpleUniverse;
 
-import tim.prune.FunctionLibrary;
-import tim.prune.I18nManager;
-import tim.prune.data.Track;
-import tim.prune.function.Export3dFunction;
-
 
 /**
  * Class to hold main window for java3d view of data
@@ -58,7 +71,11 @@ public class Java3DWindow implements ThreeDWindow
        private JFrame _frame = null;
        private ThreeDModel _model = null;
        private OrbitBehavior _orbit = null;
-       private double _altFactor = 5.0;
+       private double _altFactor = -1.0;
+       private ImageDefinition _imageDefinition = null;
+       private GroutedImage _baseImage = null;
+       private TerrainDefinition _terrainDefinition = null;
+       private DataStatus _dataStatus = null;
 
        /** only prompt about big track size once */
        private static boolean TRACK_SIZE_WARNING_GIVEN = false;
@@ -90,23 +107,52 @@ public class Java3DWindow implements ThreeDWindow
                _track = inTrack;
        }
 
+       /**
+        * @param inFactor altitude factor to use
+        */
+       public void setAltitudeFactor(double inFactor)
+       {
+               _altFactor = inFactor;
+       }
+
+       /**
+        * Set the parameters for the base image and do the grouting already
+        * (setTrack should already be called by now)
+        */
+       public void setBaseImageParameters(ImageDefinition inDefinition)
+       {
+               _imageDefinition = inDefinition;
+               if (inDefinition != null && inDefinition.getUseImage())
+               {
+                       _baseImage = new MapGrouter().createMapImage(_track, MapSourceLibrary.getSource(inDefinition.getSourceIndex()),
+                               inDefinition.getZoom());
+               }
+               else _baseImage = null;
+       }
+
+       /**
+        * Set the terrain parameters
+        */
+       public void setTerrainParameters(TerrainDefinition inDefinition)
+       {
+               _terrainDefinition = inDefinition;
+       }
+
+       /**
+        * Set the current data status
+        */
+       public void setDataStatus(DataStatus inStatus)
+       {
+               _dataStatus = inStatus;
+       }
 
        /**
         * Show the window
         */
        public void show() throws ThreeDException
        {
-               // 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, _altFactor);
-               if (factorString == null) return;
-               try {
-                       _altFactor = Double.parseDouble(factorString.toString());
-               }
-               catch (Exception e) {} // Ignore parse errors
-               if (_altFactor < 1.0) {_altFactor = 1.0;}
+               // Make sure altitude exaggeration is positive
+               if (_altFactor < 0.0) {_altFactor = 1.0;}
 
                // Set up the graphics config
                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
@@ -181,17 +227,6 @@ public class Java3DWindow implements ThreeDWindow
                                }
                        }});
                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)
-                       {
-                               if (_orbit != null) {
-                                       callbackRender(FunctionLibrary.FUNCTION_SVGEXPORT);
-                               }
-                       }});
-               panel.add(svgButton);
-
                // Close button
                JButton closeButton = new JButton(I18nManager.getText("button.close"));
                closeButton.addActionListener(new ActionListener()
@@ -270,6 +305,71 @@ public class Java3DWindow implements ThreeDWindow
                plane = new Box(10f, 0.04f, 10f, planeAppearance);
                objTrans.addChild(plane);
 
+               // Image on top of base plane, if specified
+               final boolean showTerrain = _terrainDefinition != null && _terrainDefinition.getUseTerrain();
+               if (_baseImage != null && !showTerrain)
+               {
+                       QuadArray baseSquare = new QuadArray (4, QuadArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
+                       baseSquare.setCoordinate(0, new Point3f(-10f, 0.05f, -10f));
+                       baseSquare.setCoordinate(1, new Point3f(-10f, 0.05f, 10f));
+                       baseSquare.setCoordinate(2, new Point3f( 10f, 0.05f, 10f));
+                       baseSquare.setCoordinate(3, new Point3f( 10f, 0.05f, -10f));
+                       // and set anchor points for the texture
+                       baseSquare.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 1.0f));
+                       baseSquare.setTextureCoordinate(0, 1, new TexCoord2f(0.0f, 0.0f));
+                       baseSquare.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 0.0f));
+                       baseSquare.setTextureCoordinate(0, 3, new TexCoord2f(1.0f, 1.0f));
+                       // Set appearance including image
+                       Appearance baseAppearance = new Appearance();
+                       Texture mapImage = new TextureLoader(_baseImage.getImage(), _frame).getTexture();
+                       baseAppearance.setTexture(mapImage);
+                       objTrans.addChild(new Shape3D(baseSquare, baseAppearance));
+               }
+
+               // Create model containing track information
+               _model = new ThreeDModel(_track);
+               _model.setAltitudeFactor(_altFactor);
+
+               if (showTerrain)
+               {
+                       TerrainHelper terrainHelper = new TerrainHelper(_terrainDefinition.getGridSize());
+                       // See if there's a previously saved terrain track we can reuse
+                       Track terrainTrack = TerrainCache.getTerrainTrack(_dataStatus, _terrainDefinition);
+                       if (terrainTrack == null)
+                       {
+                               // Construct the terrain track according to these extents and the grid size
+                               terrainTrack = terrainHelper.createGridTrack(_track);
+                               // Get the altitudes from SRTM for all the points in the track
+                               LookupSrtmFunction srtmLookup = (LookupSrtmFunction) FunctionLibrary.FUNCTION_LOOKUP_SRTM;
+                               srtmLookup.begin(terrainTrack);
+                               while (srtmLookup.isRunning())
+                               {
+                                       try {
+                                               Thread.sleep(750);  // just polling in a wait loop isn't ideal but simple
+                                       }
+                                       catch (InterruptedException e) {}
+                               }
+
+                               // Fix the voids
+                               terrainHelper.fixVoids(terrainTrack);
+
+                               // Store this back in the cache, maybe we'll need it again
+                               TerrainCache.storeTerrainTrack(terrainTrack, _dataStatus, _terrainDefinition);
+                       }
+                       // else System.out.println("Yay - reusing the cached track!");
+
+                       // Give the terrain definition to the _model as well
+                       _model.setTerrain(terrainTrack);
+                       _model.scale();
+
+                       objTrans.addChild(createTerrain(_model, terrainHelper, _baseImage));
+               }
+               else
+               {
+                       // No terrain, so just scale the model as it is
+                       _model.scale();
+               }
+
                // N, S, E, W
                GeneralPath bevelPath = new GeneralPath();
                bevelPath.moveTo(0.0f, 0.0f);
@@ -289,11 +389,6 @@ public class Java3DWindow implements ThreeDWindow
                objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
                objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
 
-               // create and scale model
-               _model = new ThreeDModel(_track);
-               _model.setAltitudeFactor(_altFactor);
-               _model.scale();
-
                // Add points to model
                objTrans.addChild(createDataPoints(_model));
 
@@ -407,7 +502,9 @@ public class Java3DWindow implements ThreeDWindow
        }
 
 
-       /** @return track point object */
+       /**
+        * @return track point object
+        */
        private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
        {
                Material mat = getTrackpointMaterial(inHeightCode);
@@ -417,7 +514,9 @@ public class Java3DWindow implements ThreeDWindow
        }
 
 
-       /** @return Material object for track points with the appropriate colour for the height */
+       /**
+        * @return Material object for track points with the appropriate colour for the height
+        */
        private static Material getTrackpointMaterial(byte inHeightCode)
        {
                // create default material
@@ -473,10 +572,65 @@ public class Java3DWindow implements ThreeDWindow
                return group;
        }
 
+       /**
+        * Create a java3d Shape for the terrain
+        * @param inModel threedModel
+        * @param inHelper terrain helper
+        * @param inBaseImage base image for shape, or null for no image
+        * @return Shape3D object
+        */
+       private static Shape3D createTerrain(ThreeDModel inModel, TerrainHelper inHelper, GroutedImage inBaseImage)
+       {
+               final int numNodes = inHelper.getGridSize();
+               final int RESULT_SIZE = numNodes * (numNodes * 2 - 2);
+               int[] stripData = inHelper.getStripLengths();
+
+               // Get the scaled terrainTrack coordinates (or just heights) from the model
+               final int nSquared = numNodes * numNodes;
+               Point3d[] rawPoints = new Point3d[nSquared];
+               for (int i=0; i<nSquared; i++)
+               {
+                       double height = inModel.getScaledTerrainValue(i) * MODEL_SCALE_FACTOR;
+                       rawPoints[i] = new Point3d(inModel.getScaledTerrainHorizValue(i) * MODEL_SCALE_FACTOR,
+                               Math.max(height, 0.05), // make sure it's above the box
+                               -inModel.getScaledTerrainVertValue(i) * MODEL_SCALE_FACTOR);
+               }
+
+               GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
+               gi.setCoordinates(inHelper.getTerrainCoordinates(rawPoints));
+               gi.setStripCounts(stripData);
+
+               Appearance tAppearance = new Appearance();
+               if (inBaseImage != null)
+               {
+                       gi.setTextureCoordinateParams(1, 2); // one coord set of two dimensions
+                       gi.setTextureCoordinates(0, inHelper.getTextureCoordinates());
+                       Texture mapImage = new TextureLoader(inBaseImage.getImage()).getTexture();
+                       tAppearance.setTexture(mapImage);
+                       TextureAttributes texAttr = new TextureAttributes();
+                       texAttr.setTextureMode(TextureAttributes.MODULATE);
+                       tAppearance.setTextureAttributes(texAttr);
+               }
+               else
+               {
+                       Color3f[] colours = new Color3f[RESULT_SIZE];
+                       Color3f terrainColour = new Color3f(0.1f, 0.2f, 0.2f);
+                       for (int i=0; i<RESULT_SIZE; i++) {colours[i] = terrainColour;}
+                       gi.setColors(colours);
+               }
+               new NormalGenerator().generateNormals(gi);
+               Material terrnMat = new Material(new Color3f(0.4f, 0.4f, 0.4f), // ambient colour
+                       new Color3f(0f, 0f, 0f), // emissive (none)
+                       new Color3f(0.8f, 0.8f, 0.8f), // diffuse
+                       new Color3f(0.2f, 0.2f, 0.2f), //specular
+                       30f); // shinyness
+               tAppearance.setMaterial(terrnMat);
+               return new Shape3D(gi.getGeometryArray(), tAppearance);
+       }
 
        /**
         * Calculate the angles and call them back to the app
-        * @param inFunction function to call (either pov or svg)
+        * @param inFunction function to call for export
         */
        private void callbackRender(Export3dFunction inFunction)
        {
@@ -495,9 +649,13 @@ public class Java3DWindow implements ThreeDWindow
                Point3d result = new Point3d();
                secondTran.transform(point, result);
                firstTran.transform(result);
-               // Callback settings to pov export function
+
+               // Give the settings to the rendering function
                inFunction.setCameraCoordinates(result.x, result.y, result.z);
                inFunction.setAltitudeExaggeration(_altFactor);
+               inFunction.setTerrainDefinition(_terrainDefinition);
+               inFunction.setImageDefinition(_imageDefinition);
+
                inFunction.begin();
        }
 }