From 633208f3bde9ab3c0e2da258601d4a8bd996c80b Mon Sep 17 00:00:00 2001
From: till busch <buti@bux.at>
Date: Tue, 1 Apr 2008 14:17:38 +0200
Subject: [PATCH] add an elevation-util v0.0.3

---
 configure.ac                  |    1 +
 src/Main/Makefile.am          |    2 +-
 utils/Makefile.am             |    4 +-
 utils/elevation/Makefile.am   |   81 +++++++++++++++++++
 utils/elevation/README        |   22 +++++
 utils/elevation/elevation.cxx |  171 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 278 insertions(+), 3 deletions(-)
 create mode 100644 utils/elevation/Makefile.am
 create mode 100644 utils/elevation/README
 create mode 100644 utils/elevation/elevation.cxx

diff --git a/configure.ac b/configure.ac
index 14514d8..fec8483 100644
--- a/configure.ac
+++ b/configure.ac
@@ -690,6 +690,7 @@ AC_CONFIG_FILES([ \
 	utils/GPSsmooth/Makefile \
 	utils/fgadmin/Makefile
 	utils/fgadmin/src/Makefile \
+	utils/elevation/Makefile \
 	utils/js_server/Makefile \
 	utils/Modeller/Makefile \
 	utils/TerraSync/Makefile \
diff --git a/src/Main/Makefile.am b/src/Main/Makefile.am
index 3991083..661c8c8 100644
--- a/src/Main/Makefile.am
+++ b/src/Main/Makefile.am
@@ -95,7 +95,7 @@ fgfs_LDADD = \
 	$(top_builddir)/src/Sound/libSound.a \
 	$(top_builddir)/src/Airports/libAirports.a \
 	$(MPLAYER_LIBS) \
-        $(top_builddir)/src/AIModel/libAIModel.a \
+	$(top_builddir)/src/AIModel/libAIModel.a \
 	$(top_builddir)/src/Systems/libSystems.a \
 	$(top_builddir)/src/Time/libTime.a \
 	$(top_builddir)/src/Traffic/libTraffic.a \
diff --git a/utils/Makefile.am b/utils/Makefile.am
index e73dcc1..ba9918a 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -1,4 +1,4 @@
-DIST_SUBDIRS = GPSsmooth TerraSync Modeller js_server fgadmin xmlgrep
+DIST_SUBDIRS = GPSsmooth TerraSync Modeller js_server fgadmin xmlgrep elevation
 
-SUBDIRS = GPSsmooth TerraSync Modeller js_server
+SUBDIRS = GPSsmooth TerraSync Modeller js_server elevation
 
diff --git a/utils/elevation/Makefile.am b/utils/elevation/Makefile.am
new file mode 100644
index 0000000..90ec958
--- /dev/null
+++ b/utils/elevation/Makefile.am
@@ -0,0 +1,81 @@
+EXTRA_DIST = README
+
+bin_PROGRAMS = elevation
+
+elevation_SOURCES = elevation.cxx
+
+#elevation_LDADD = -lplibnet -lplibul $(network_LIBS)
+
+MPLAYER_LIBS = $(top_builddir)/src/MultiPlayer/libMultiPlayer.a
+
+if ENABLE_SP_FDM
+SP_FDM_LIBS = $(top_builddir)/src/FDM/SP/libSPFDM.a
+else
+SP_FDM_LIBS = 
+endif
+
+if WITH_THREADS
+THREAD_LIBS = -lsgthreads $(thread_LIBS)
+else
+THREAD_LIBS =
+endif
+
+INCLUDES = -I$(top_srcdir)/src/Scenery/ -I$(top_srcdir)/src/ -I$(top_srcdir)/
+
+JSBSIM_LIBS = \
+	$(top_builddir)/src/FDM/JSBSim/libJSBSim.a \
+        $(top_builddir)/src/FDM/JSBSim/initialization/libInit.a \
+        $(top_builddir)/src/FDM/JSBSim/models/libModels.a \
+        $(top_builddir)/src/FDM/JSBSim/models/flight_control/libFlightControl.a\
+        $(top_builddir)/src/FDM/JSBSim/models/atmosphere/libAtmosphere.a \
+        $(top_builddir)/src/FDM/JSBSim/models/propulsion/libPropulsion.a \
+        $(top_builddir)/src/FDM/JSBSim/input_output/libInputOutput.a \
+        $(top_builddir)/src/FDM/JSBSim/math/libMath.a
+
+elevation_LDADD = \
+	$(top_builddir)/src/Main/libMain.a \
+	$(top_builddir)/src/Aircraft/libAircraft.a \
+	$(top_builddir)/src/ATC/libATC.a \
+	$(top_builddir)/src/Cockpit/libCockpit.a \
+	$(top_builddir)/src/Cockpit/built_in/libBuilt_in.a \
+	$(top_builddir)/src/FDM/libFlight.a \
+	$(top_builddir)/src/FDM/Balloon/libBalloon.a \
+	$(top_builddir)/src/FDM/ExternalNet/libExternalNet.a \
+	$(top_builddir)/src/FDM/ExternalPipe/libExternalPipe.a \
+	$(JSBSIM_LIBS) \
+	$(top_builddir)/src/FDM/YASim/libYASim.a \
+	$(top_builddir)/src/FDM/LaRCsim/libLaRCsim.a \
+	$(top_builddir)/src/FDM/UIUCModel/libUIUCModel.a \
+	$(SP_FDM_LIBS) \
+	$(top_builddir)/src/GUI/libGUI.a \
+	$(top_builddir)/src/Autopilot/libAutopilot.a \
+	$(top_builddir)/src/Input/libInput.a \
+	$(top_builddir)/src/Instrumentation/KLN89/libKLN89.a \
+	$(top_builddir)/src/Instrumentation/libInstrumentation.a \
+	$(top_builddir)/src/Instrumentation/HUD/libHUD.a \
+	$(top_builddir)/src/Model/libModel.a \
+	$(top_builddir)/src/Network/libNetwork.a \
+	$(top_builddir)/src/Navaids/libNavaids.a \
+	$(top_builddir)/src/Scenery/libScenery.a \
+	$(top_builddir)/src/Scripting/libScripting.a \
+	$(top_builddir)/src/Sound/libSound.a \
+	$(top_builddir)/src/Airports/libAirports.a \
+	$(MPLAYER_LIBS) \
+	$(top_builddir)/src/AIModel/libAIModel.a \
+	$(top_builddir)/src/Systems/libSystems.a \
+	$(top_builddir)/src/Time/libTime.a \
+	$(top_builddir)/src/Traffic/libTraffic.a \
+	$(top_builddir)/src/Environment/libEnvironment.a \
+	-lsgroute -lsgsky -lsgsound -lsgephem -lsgmaterial -lsgtgdb -lsgmodel \
+	-lsgutil -lsgtiming -lsgio -lsgscreen -lsgmath -lsgbucket -lsgprops \
+	-lsgdebug -lsgmagvar -lsgmisc -lsgnasal -lsgxml -lsgsound -lsgserial \
+	-lsgstructure -lsgenvironment \
+	-lplibpuaux -lplibpu -lplibfnt -lplibjs -lplibnet \
+	-lplibsg -lplibul \
+	$(OSG_LIBS) \
+	$(THREAD_LIBS) \
+	$(network_LIBS) \
+	-lz \
+	$(opengl_LIBS) \
+	$(openal_LIBS)
+
diff --git a/utils/elevation/README b/utils/elevation/README
new file mode 100644
index 0000000..9a14a91
--- /dev/null
+++ b/utils/elevation/README
@@ -0,0 +1,22 @@
+# GPL, (l) 2008 by till busch <buti@bux.at>
+
+WARNING: This is a quick hack. Do not expect more than that. There are no
+configuration options, all paths are hard-coded.
+
+The elevation-tool does intersection-testing on Scenery (including models!).
+It reads lines from stdin and writes results to stdout.
+
+INPUT FORMAT:
+id lat lon tile
+
+OUTPUT FORMAT:
+id lat lon tile elevation
+
+id and tile are not used internally (there is no need to provide meaningful
+numbers).
+
+NOTE: This tool is a lot slower with vsync enabled. Turn off vsync with
+nvidia-settings -a SyncToVBlank=0
+
+strip the binary for maximum performance
+
diff --git a/utils/elevation/elevation.cxx b/utils/elevation/elevation.cxx
new file mode 100644
index 0000000..5a9844a
--- /dev/null
+++ b/utils/elevation/elevation.cxx
@@ -0,0 +1,171 @@
+// elevation.cxx - calculate terrain elevation
+// GPL, (l) 2008 by till busch <buti@bux.at>
+
+#include <config.h>
+#include <simgear/scene/util/SGSceneFeatures.hxx>
+#include <simgear/scene/material/matlib.hxx>
+#include <simgear/scene/model/modellib.hxx>
+#include <simgear/scene/material/mat.hxx>
+#include <simgear/scene/tgdb/userdata.hxx>
+#include <Main/globals.hxx>
+#include <Scenery/tilemgr.hxx>
+#include <Scenery/scenery.hxx>
+#include <AIModel/AIMultiplayer.hxx>
+#include <Main/renderer.hxx>
+
+#define FG_ROOT      "/usr/local/share/FlightGear"
+#define SCENERY_DIRS "/usr/local/share/FlightGear/Scenery:/usr/local/share/FlightGear/Scenery-Terrasync"
+
+#define WINDOW_SIZE 400
+
+#define visibility 20000.0
+#define SEASON "summer"
+
+extern FGGlobals *globals;
+
+int _bootstrap_OSInit=0;
+char *homedir = ::getenv( "HOME" );
+char *hostname = ::getenv( "HOSTNAME" );
+bool free_hostname = false;
+SGLocation loc;
+
+enum State {
+    Read,
+    Update
+};
+
+static void update( void ) {
+    static State state=Read;
+    static int id=0;
+    static int tile=0;
+    static double lon=0.0,lat=0.0;
+    static int cnt=0;
+
+    if(state==Update)
+    {
+        cnt++;
+
+        globals->get_tile_mgr()->update(&loc, 1000.0);
+        globals->get_tile_mgr()->prep_ssg_nodes(visibility);
+        // if(globals->get_scenery()->scenery_available(lat, lon, 100.0))
+        if(globals->get_tile_mgr()->scenery_available(lat, lon, 100.0))
+        {
+            double alt=0;
+            const SGMaterial *mat;
+            globals->get_scenery()->get_elevation_m(lat, lon, SG_MAX_ELEVATION_M, alt, &mat);
+            //if(mat)
+            //    cout << lon << " " << lat << " " << alt << " " << mat->get_names().front() << endl;
+            //else
+                cout << id << " " << lat << " " << lon << " " << tile << " " << alt << endl;
+            cnt=0;
+            state=Read;
+        }
+        else
+        {
+            if(cnt > 10000)
+            {
+                cerr << "No scenery for 10000 frames. bailing out!\n";
+                cerr << id << " " << lat << " " << lon << " " << tile << endl;
+                state=Read;
+                cnt=0;
+                globals->get_renderer()->getViewer()->setDone(true);
+            }
+        }
+    }
+
+    if(state==Read)
+    {
+        state=Update;
+        cin >> id >> ws >> lat >> ws >> lon >> ws >> tile;
+        if(cin.eof())
+            globals->get_renderer()->getViewer()->setDone(true);
+
+        loc.setPosition(lon, lat, SG_MAX_ELEVATION_M*SG_METER_TO_FEET);
+        SGGeod pos(SGGeod::fromDegM(lon, lat, SG_MAX_ELEVATION_M));
+        SGVec3f eye = SGVec3f::fromGeod(pos);
+        //g=SGGeod::fromDegM(lon, lat, 0);
+        //SGVec3f center = SGVec3f::fromGeod(g);
+
+        osg::Vec3d up, center;
+        float *south=loc.get_surface_south();
+        up=osg::Vec3(south[0],south[1],south[2])*-1.0;
+        up.normalize();
+
+        osg::Matrix mm;
+        mm.makeLookAt(eye.osg(), center, up);
+        globals->get_renderer()->getManipulator()->setByInverseMatrix(mm);
+        return;
+    }
+
+}
+
+int main(int argc, char *argv[])
+{
+    // setting this higher may cause garbage on stdout
+    osg::setNotifyLevel(osg::WARN);
+
+    logbuf::set_log_classes(SG_ALL);
+    logbuf::set_log_priority(SG_WARN);
+
+    globals = new FGGlobals;
+    globals->set_fg_root(FG_ROOT);
+    globals->set_fg_scenery(SCENERY_DIRS);
+
+    //SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
+    //                              distance_attenuation );
+
+    SGSceneFeatures::instance()->setEnablePointSpriteLights(false);
+    SGSceneFeatures::instance()->setEnableDistanceAttenuationLights(false);
+
+    fgSetDouble("/environment/visibility-m", visibility);
+    fgSetString("/sim/startup/season", SEASON);
+    fgSetBool("/sim/rendering/random-objects", false);
+    fgSetBool("/sim/rendering/random-vegetation", false);
+
+    sgUserDataInit( globals->get_props() );
+    globals->set_matlib( new SGMaterialLib );
+    SGPath mpath( globals->get_fg_root() );
+    mpath.append( "materials.xml" );
+    if(!globals->get_matlib()->load(globals->get_fg_root(), mpath.str(), SEASON, globals->get_props()))
+        SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
+
+    globals->set_scenery( new FGScenery );
+    simgear::SGModelLib::init(globals->get_fg_root());
+
+    globals->set_tile_mgr(new FGTileMgr);
+    globals->get_scenery()->init();
+    globals->get_scenery()->bind();
+    osgViewer::Viewer viewer;
+
+    osg::Matrix projection;
+    projection.makePerspective(40.0, 1.0, 1.0, visibility);
+
+    viewer.getCamera()->setProjectionMatrix(projection);
+    viewer.getCamera()->setClearColor(osg::Vec4(0.5, 0.6, 1.0, 1.0));
+
+    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
+
+    viewer.setDatabasePager(globals->get_scenery()->getPagerSingleton());
+    viewer.setSceneData(globals->get_scenery()->get_scene_graph());
+    viewer.setCameraManipulator(globals->get_renderer()->getManipulator());
+
+    globals->get_renderer()->setViewer(&viewer);
+    globals->get_renderer()->splashinit();
+
+    globals->get_tile_mgr()->init();
+
+    fgRegisterIdleHandler(update);
+
+    viewer.setUpViewInWindow(0, 0, WINDOW_SIZE, WINDOW_SIZE);
+    viewer.run();
+
+    //delete globals->get_renderer();
+    delete globals->get_matlib();
+    delete globals->get_tile_mgr();
+    delete globals->get_scenery();
+
+//    delete globals;
+
+    return 0;
+}
+
-- 
1.5.2.5


