Skip to content

Instantly share code, notes, and snippets.

@gwaldron
Last active June 14, 2018 15:12
Show Gist options
  • Select an option

  • Save gwaldron/6df90cedc1c1d0011e1ee9616d8b1154 to your computer and use it in GitHub Desktop.

Select an option

Save gwaldron/6df90cedc1c1d0011e1ee9616d8b1154 to your computer and use it in GitHub Desktop.
Async Elevation Query Example
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2016 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/ThreadingUtils>
#include <osgEarth/Metrics>
#include <osgEarth/Registry>
#include <osgEarthUtil/Controls>
#define LC "[async] "
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Util::Controls;
using namespace osgEarth::Threading;
namespace ui = osgEarth::Util::Controls;
/**
* Demonstrates how to use the asynchronous ElevationPool::getElevation
* method, which uses the Promise/Future pattern.
*/
int
usage(const char* name)
{
OE_NOTICE
<< "\nUsage: " << name << " file.earth" << std::endl
<< MapNodeHelper().usage() << std::endl;
return 0;
}
struct App
{
osg::ref_ptr<MapNode> _mapNode;
osg::ref_ptr<ui::LabelControl> _debugLabel;
osg::ref_ptr<ui::LabelControl> _resultLabel;
App()
{
_debugLabel = new LabelControl("Debug");
_resultLabel = new LabelControl("Result");
}
};
struct QueryElevationUnderMouse : public osgGA::GUIEventHandler
{
App& _app;
QueryElevationUnderMouse(App& app) : _app(app) { }
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if (ea.getEventType() == ea.MOVE)
{
osg::Vec3d world;
_app._mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world);
GeoPoint point;
point.fromWorld(_app._mapNode->getMapSRS(), world);
_result = _app._mapNode->getMap()->getElevationPool()->getElevation(point);
return true;
}
else if (ea.getEventType() == ea.FRAME)
{
if (_result.isAvailable())
{
osg::ref_ptr<ElevationSample> sample = _result.release();
if (sample.valid())
{
_app._resultLabel->setText(Stringify() <<"Elevation=" << sample->elevation << ", Resolution=" << sample->resolution);
}
}
}
return false;
}
Future<ElevationSample> _result;
};
int
main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
// help?
if ( arguments.read("--help") )
return usage(argv[0]);
// create a viewer:
osgViewer::Viewer viewer(arguments);
viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( true, false );
viewer.setCameraManipulator( new EarthManipulator(arguments) );
// load an earth file, and support all or our example command-line options
// and earth file <external> tags
osg::Node* node = MapNodeHelper().load(arguments, &viewer);
if ( node )
{
App app;
app._mapNode = MapNode::get(node);
ui::VBox* box = new ui::VBox();
box->setVertAlign(ui::Control::ALIGN_BOTTOM);
box->addControl(app._debugLabel.get());
box->addControl(app._resultLabel.get());
ui::ControlCanvas::get(&viewer)->addControl(box);
viewer.setSceneData( node );
viewer.addEventHandler(new QueryElevationUnderMouse(app));
Metrics::run(viewer);
}
else
{
return usage(argv[0]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment