]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/save/PovExporter.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / save / PovExporter.java
index 1f29d4a70bb075420db66a471ca7a9d7e730c28d..d4a6beb87a0482219c5ecedb5620bd2f72992f1c 100644 (file)
@@ -43,6 +43,8 @@ import tim.prune.gui.map.MapSource;
 import tim.prune.gui.map.MapSourceLibrary;
 import tim.prune.load.GenericFileFilter;
 import tim.prune.threedee.ImageDefinition;
+import tim.prune.threedee.TerrainCache;
+import tim.prune.threedee.TerrainDefinition;
 import tim.prune.threedee.TerrainHelper;
 import tim.prune.threedee.ThreeDModel;
 
@@ -319,8 +321,11 @@ public class PovExporter extends Export3dFunction
                                        {
                                                // file saved - store directory in config for later
                                                Config.setConfigString(Config.KEY_TRACK_DIR, povFile.getParentFile().getAbsolutePath());
-                                               // also store exaggeration
+                                               // also store exaggeration and grid size
                                                Config.setConfigInt(Config.KEY_HEIGHT_EXAGGERATION, (int) (_altFactor * 100));
+                                               if (_terrainPanel.getUseTerrain() && _terrainPanel.getGridSize() > 20) {
+                                                       Config.setConfigInt(Config.KEY_TERRAIN_GRID_SIZE, _terrainPanel.getGridSize());
+                                               }
                                        }
                                        else
                                        {
@@ -392,19 +397,29 @@ public class PovExporter extends Export3dFunction
                        if (useTerrain)
                        {
                                TerrainHelper terrainHelper = new TerrainHelper(_terrainPanel.getGridSize());
-                               Track 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())
+                               // See if there's a previously saved terrain track we can reuse
+                               TerrainDefinition terrainDef = new TerrainDefinition(_terrainPanel.getUseTerrain(), _terrainPanel.getGridSize());
+                               Track terrainTrack = TerrainCache.getTerrainTrack(_app.getCurrentDataStatus(), terrainDef);
+                               if (terrainTrack == null)
                                {
-                                       try {
-                                               Thread.sleep(750);  // just polling in a wait loop isn't ideal but simple
+                                       // 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) {}
                                        }
-                                       catch (InterruptedException e) {}
+                                       // Fix the voids
+                                       terrainHelper.fixVoids(terrainTrack);
+
+                                       // Store this back in the cache, maybe we'll need it again
+                                       TerrainCache.storeTerrainTrack(terrainTrack, _app.getCurrentDataStatus(), terrainDef);
                                }
-                               // Fix the voids
-                               terrainHelper.fixVoids(terrainTrack);
 
                                model.setTerrain(terrainTrack);
                                model.scale();
@@ -465,7 +480,7 @@ public class PovExporter extends Export3dFunction
        private void writeStartOfFile(FileWriter inWriter, String inLineSeparator, File inImageFile, File inTerrainFile)
        throws IOException
        {
-               inWriter.write("// Pov file produced by GpsPrune - see http://gpsprune.activityworkshop.net/");
+               inWriter.write("// Pov file produced by GpsPrune - see https://gpsprune.activityworkshop.net/");
                inWriter.write(inLineSeparator);
                inWriter.write("#version 3.6;");
                inWriter.write(inLineSeparator);
@@ -496,6 +511,18 @@ public class PovExporter extends Export3dFunction
                // Definition of terrain shape if any
                final String terrainDefinition = makeTerrainString(inTerrainFile, inImageFile, inLineSeparator);
 
+               final String[] pointLights = {
+                       "// lights",
+                       "light_source { <-1, 9, -4> color rgb <0.5 0.5 0.5>}",
+                       "light_source { <1, 6, -14> color rgb <0.6 0.6 0.6>}",
+                       "light_source { <11, 12, 8> color rgb <0.3 0.3 0.3>}"
+               };
+               final String[] northwestLight = {
+                       "// lights from NW",
+                       "light_source { <-10, 10, 10> color rgb <1.5 1.5 1.5> parallel }",
+               };
+               final String[] lightsLines = (inTerrainFile == null ? pointLights : northwestLight);
+
                // Set up output
                String[] outputLines = {
                  "global_settings { ambient_light rgb <4, 4, 4> }", "",
@@ -611,21 +638,30 @@ public class PovExporter extends Export3dFunction
                  "  ttf \"" + fontPath + "\" \"" + I18nManager.getText("cardinal.w") + "\" 0.3, 0",
                  "  pigment { color rgb <1 1 1> }",
                  "  translate <-10.3, 0.2, 0>",
-                 "}", "",
-                 // MAYBE: Light positions should relate to model size
-                 "// lights",
-                 "light_source { <-1, 9, -4> color rgb <0.5 0.5 0.5>}",
-                 "light_source { <1, 6, -14> color rgb <0.6 0.6 0.6>}",
-                 "light_source { <11, 12, 8> color rgb <0.3 0.3 0.3>}",
-                 "",
+                 "}"
                };
+
                // write strings to file
-               int numLines = outputLines.length;
-               for (int i=0; i<numLines; i++)
+               writeLinesToFile(inWriter, inLineSeparator, outputLines);
+               writeLinesToFile(inWriter, inLineSeparator, lightsLines);
+       }
+
+       /**
+        * Write the given lines to the file
+        * @param inWriter writer object
+        * @param inLineSeparator line separator string
+        * @param lines array of lines to write
+        * @throws IOException
+        */
+       private void writeLinesToFile(FileWriter inWriter, String inLineSeparator, String[] lines)
+               throws IOException
+       {
+               for (int i=0; i<lines.length; i++)
                {
-                       inWriter.write(outputLines[i]);
+                       inWriter.write(lines[i]);
                        inWriter.write(inLineSeparator);
                }
+               inWriter.write(inLineSeparator);
        }
 
        /**
@@ -849,7 +885,7 @@ public class PovExporter extends Export3dFunction
 
        /**
         * @param inCode height code to check
-        * @return validated height code within range 0 to max
+        * @return validated height code within range 0 to maxHeightCode
         */
        private static byte checkHeightCode(byte inCode)
        {