osgEarth 2.1.1

/home/cube/sources/osgearth/src/applications/osgearth_measure/osgearth_measure.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #include <osg/Notify>
00021 #include <osgGA/StateSetManipulator>
00022 #include <osgGA/GUIEventHandler>
00023 #include <osgViewer/Viewer>
00024 #include <osgViewer/ViewerEventHandlers>
00025 #include <osgEarth/MapNode>
00026 #include <osgEarth/XmlUtils>
00027 #include <osgEarthUtil/EarthManipulator>
00028 #include <osgEarthUtil/AutoClipPlaneHandler>
00029 #include <osgEarthUtil/Controls>
00030 #include <osgEarthUtil/Graticule>
00031 #include <osgEarthUtil/SkyNode>
00032 #include <osgEarthUtil/Viewpoint>
00033 #include <osgEarthSymbology/Color>
00034 
00035 #include <osgEarthUtil/MeasureTool>
00036 
00037 using namespace osgEarth::Util;
00038 using namespace osgEarth::Util::Controls;
00039 using namespace osgEarth::Symbology;
00040 
00041 class MyMeasureToolCallback : public MeasureToolHandler::MeasureToolEventHandler
00042 {
00043 public:
00044     MyMeasureToolCallback(LabelControl* label):
00045       _label(label)
00046     {
00047     }
00048 
00049     virtual void onDistanceChanged(MeasureToolHandler* sender, double distance)
00050     {
00051         std::stringstream ss;
00052         ss << "Distance = " << std::setprecision(10) << distance << "m" << std::endl;        
00053         _label->setText( ss.str() );
00054     }
00055     LabelControl* _label;
00056 };
00057 
00058 struct TogglePathHandler : public ControlEventHandler
00059 {
00060     TogglePathHandler( MeasureToolHandler* tool) :
00061     _tool( tool )
00062     { }
00063 
00064     virtual void onValueChanged(Control* control, bool value) {
00065         _tool->setIsPath( value );
00066     }
00067 
00068     osg::ref_ptr<MeasureToolHandler> _tool;
00069 };
00070 
00071 struct ToggleModeHandler : public ControlEventHandler
00072 {
00073     ToggleModeHandler( MeasureToolHandler* tool) :
00074     _tool( tool )
00075     { }
00076 
00077     virtual void onValueChanged(Control* control, bool value) {
00078         if (_tool->getGeoInterpolation() == GEOINTERP_GREAT_CIRCLE)
00079         {
00080             _tool->setGeoInterpolation( GEOINTERP_RHUMB_LINE);
00081         }
00082         else
00083         {
00084             _tool->setGeoInterpolation( GEOINTERP_GREAT_CIRCLE);
00085         }        
00086     }
00087 
00088     osg::ref_ptr<MeasureToolHandler> _tool;
00089 };
00090 
00091 
00092 
00093 int
00094 main(int argc, char** argv)
00095 {
00096     osg::ArgumentParser arguments(&argc,argv);
00097     osg::DisplaySettings::instance()->setMinimumNumStencilBits( 8 );
00098 
00099     // load the .earth file from the command line.
00100     osg::Node* earthNode = osgDB::readNodeFiles( arguments );
00101     if (!earthNode)
00102     {
00103         OE_NOTICE << "Unable to load earth model." << std::endl;
00104         return 1;
00105     }
00106 
00107     MapNode* mapNode = MapNode::findMapNode( earthNode );
00108     if ( !mapNode )
00109     {
00110         OE_NOTICE << "Input file was not a .earth file" << std::endl;
00111         return 1;
00112     }
00113 
00114     earthNode->setNodeMask( 0x1 );
00115 
00116 
00117     osgViewer::Viewer viewer(arguments);
00118     
00119     osgEarth::Util::EarthManipulator* earthManip = new EarthManipulator();
00120     viewer.setCameraManipulator( earthManip );
00121 
00122     osg::Group* root = new osg::Group();
00123     root->addChild( earthNode );
00124 
00125     //Create the MeasureToolHandler
00126     MeasureToolHandler* measureTool = new MeasureToolHandler(root, mapNode);    
00127     measureTool->setIntersectionMask( 0x1 );
00128     viewer.addEventHandler( measureTool );
00129 
00130     //Create some controls to interact with the measuretool
00131     ControlCanvas* canvas = new ControlCanvas( &viewer );
00132     root->addChild( canvas );
00133     canvas->setNodeMask( 0x1 << 1 );
00134 
00135     Grid* grid = new Grid();
00136     grid->setBackColor(0,0,0,0.5);
00137     grid->setMargin( 10 );
00138     grid->setPadding( 10 );
00139     grid->setChildSpacing( 10 );
00140     grid->setChildVertAlign( Control::ALIGN_CENTER );
00141     grid->setAbsorbEvents( true );
00142     grid->setVertAlign( Control::ALIGN_BOTTOM );
00143     grid->setFrame(new RoundedFrame());
00144 
00145     canvas->addControl( grid );
00146 
00147     //Add a label to display the distance
00148     // Add a text label:
00149     LabelControl* label = new LabelControl();
00150     label->setFont( osgText::readFontFile( "arialbd.ttf" ) );
00151     label->setFontSize( 24.0f );
00152     label->setHorizAlign( Control::ALIGN_LEFT );    
00153     label->setText("Distance");
00154     grid->setControl( 0, 0, label);
00155 
00156     //Add a callback to update the label when the distance changes
00157     measureTool->addEventHandler( new MyMeasureToolCallback(label) );
00158     
00159     Style style;
00160     style.getOrCreate<LineSymbol>()->stroke()->color() = Color::Yellow;
00161     measureTool->setLineStyle(style);
00162 
00163     //Add a checkbox to control if we are doing path based measurement or just point to point
00164     grid->setControl( 0, 1, new LabelControl("Path"));
00165     CheckBoxControl* checkBox = new CheckBoxControl(false);
00166     checkBox->setHorizAlign(Control::ALIGN_LEFT);
00167     checkBox->addEventHandler( new TogglePathHandler(measureTool));
00168     grid->setControl( 1, 1, checkBox);
00169 
00170     //Add a toggle to set the mode of the measuring tool
00171     grid->setControl( 0, 2, new LabelControl("Great Circle"));
00172     CheckBoxControl* mode = new CheckBoxControl(true);
00173     mode->setHorizAlign(Control::ALIGN_LEFT);
00174     mode->addEventHandler( new ToggleModeHandler(measureTool));
00175     grid->setControl( 1, 2, mode);
00176 
00177 
00178     
00179 
00180    
00181     viewer.setSceneData( root );
00182 
00183     // add some stock OSG handlers:
00184     viewer.addEventHandler(new osgViewer::StatsHandler());
00185     viewer.addEventHandler(new osgViewer::WindowSizeHandler());
00186     viewer.addEventHandler(new osgViewer::ThreadingHandler());
00187     viewer.addEventHandler(new osgViewer::LODScaleHandler());
00188     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
00189     viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
00190 
00191     return viewer.run();
00192 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines